Bitcoin Forum
July 07, 2024, 11:11:40 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Need Help with a Python Code that I put together, Wallet.dat Extractor  (Read 147 times)
onemd (OP)
Full Member
***
Offline Offline

Activity: 309
Merit: 118


View Profile
November 01, 2018, 06:37:27 AM
 #1

I have coded a wallet.dat private key extractor, that allows input of a folder directory, and all (Anyname).dat files in the entire directionary to be outputted to  a file called privatekey.txt it does as expected, but it doesn't give me addresses and private keys that the bitcoin core client finds with the dat file.
Did I do something wrong, which results in incorrect privatekeys?

Tip of 0.0025 BTC for help, thanks


Code:
Import re
import hashlib
import base58
import binascii
import argparse
import glob
import fileinput
import argparse

from pycoin.ecdsa import generator_secp256k1, public_pair_for_secret_exponent

location = input('Location of Wat File: ')

def from_long(v, prefix, base, charset):
l = bytearray()
while v > 0:
try:
v, mod = divmod(v, base)
l.append(charset(mod))
except Exception:
raise EncodingError("can't convert to character corresponding to %d" % mod)
l.extend([charset(0)] * prefix)
l.reverse()
return bytes(l)

def to_bytes_32(v):
v = from_long(v, 0, 256, lambda x: x)
if len(v) > 32:
raise ValueError("input to to_bytes_32 is too large")
return ((b'\0' * 32) + v)[-32:]

def bytetohex(byteStr):
return ''.join( [ "%02X" % x for x in byteStr ] ).strip()

#litecoin = [b"\x30", b"\xb0"]
bitcoin = [b"\x00", b"\x80"]
#darkcoin = [b"\x4c", b"\xcc"]

cointype = bitcoin

walletHandle = []
for file in glob.glob(str(location)+"/*.dat"):
if file.endswith('.dat'):
print(file)
walletHandle = open(file, "rb")
wallet = walletHandle.read()
privKeys_re_c=re.compile(b'\x30\x81\xD3\x02\x01\x01\x04\x20(.{32})', re.DOTALL)
privKeys=set(privKeys_re_c.findall(wallet))
print("Found %d privKeys" % len(privKeys))


for key in privKeys:
public_x, public_y = public_pair_for_secret_exponent(generator_secp256k1, int(bytetohex(key), 16))
public_key = b'\4' + to_bytes_32(public_x) + to_bytes_32(public_y)
compressed_public_key = bytes.fromhex("%02x%064x" % (2 + (public_y & 1), public_x))

m = hashlib.new('ripemd160')
m.update(hashlib.sha256(public_key).digest())
ripe = m.digest()

m = hashlib.new('ripemd160')
m.update(hashlib.sha256(compressed_public_key).digest())
ripe_c = m.digest()

extRipe = cointype[0] + ripe
extRipe_c = cointype[0] + ripe_c

chksum = hashlib.sha256(hashlib.sha256(extRipe).digest()).digest()[:4] # Step 5-7
chksum_c = hashlib.sha256(hashlib.sha256(extRipe_c).digest()).digest()[:4] # Step 5-7

addr = extRipe + chksum
addr_c = extRipe_c + chksum_c

keyWIF = cointype[1] + key
keyWIF_c = cointype[1] + key + b"\x01"

chksum = hashlib.sha256(hashlib.sha256(keyWIF).digest()).digest()[:4]
chksum_c = hashlib.sha256(hashlib.sha256(keyWIF_c).digest()).digest()[:4]

addr = keyWIF + chksum
addr_c = keyWIF_c + chksum_c

str(bytetohex(key))
private_key_static = str(bytetohex(key))
extended_key = "80"+private_key_static
first_sha256 = hashlib.sha256(binascii.unhexlify(extended_key)).hexdigest()
second_sha256 = hashlib.sha256(binascii.unhexlify(first_sha256)).hexdigest()
final_key = extended_key+second_sha256[:8]
WIF = base58.b58encode(binascii.unhexlify(final_key))
str(WIF)
f = open(str(location)+"\privatekey.txt", "a")
f.write(str(WIF)[2:99][:-1]+"\n")
continue
else:
continue
walletHandle.close()

onemd (OP)
Full Member
***
Offline Offline

Activity: 309
Merit: 118


View Profile
November 01, 2018, 05:10:39 PM
Merited by bob123 (3)
 #2

Nvm solved it myself, needs compressed keys.

Code:
CWIF = base58.b58encode(addr_c)
str(CWIF)

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!