Advertisement
skip420

dumpWallet

Aug 3rd, 2022 (edited)
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. # Dumps private keys from unencrypted wallet.dat files using the "0201010420" private key marker
  2. # python3 dumpWallet
  3. # have a wallet.dat in same directory
  4.  
  5.  
  6.  
  7. import binascii
  8. import hashlib
  9. import codecs
  10. import mmap
  11. import sys
  12. import ecdsa
  13. import base58
  14.  
  15. # Define base58str
  16. def base58str(address_hex):
  17.     alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  18.     b58_string = ''
  19.     # Get the number of leading zeros
  20.     leading_zeros = len(address_hex) - len(address_hex.lstrip('0'))
  21.     # Convert hex to decimal
  22.     address_int = int(address_hex, 16)
  23.     # Append digits to the start of string
  24.     while address_int > 0:
  25.         digit = address_int % 58
  26.         digit_char = alphabet[digit]
  27.         b58_string = digit_char + b58_string
  28.         address_int //= 58
  29.     # Add ‘1’ for each 2 leading zeros
  30.     ones = leading_zeros // 2
  31.     for one in range(ones):
  32.         b58_string = '1' + b58_string
  33.     return b58_string
  34.  
  35. def priv_to_addr(address_hex,compressed):
  36.     signing_key = ecdsa.SigningKey.from_string(address_hex, curve=ecdsa.SECP256k1)
  37.     verifying_key = signing_key.get_verifying_key()
  38.     if not compressed:
  39.         public_key = bytes.fromhex("04") + verifying_key.to_string()
  40.     else:
  41.         public_key_x = verifying_key.to_string()[0:32]
  42.         public_key_y = verifying_key.to_string()[32:]
  43.        
  44.         if int(binascii.hexlify(public_key_y),16) % 2 == 0:
  45.             public_key = bytes.fromhex("02") + public_key_x
  46.         else:
  47.             public_key = bytes.fromhex("03") + public_key_x
  48.    
  49.     #hash sha256 of pubkey
  50.     sha256_1 = hashlib.sha256(public_key)
  51.    
  52.     #hash ripemd of sha of pubkey
  53.     ripemd160 = hashlib.new("ripemd160")
  54.     ripemd160.update(sha256_1.digest())
  55.    
  56.     #checksum
  57.     hashed_public_key = bytes.fromhex("00") + ripemd160.digest()
  58.     checksum_full = hashlib.sha256(hashlib.sha256(hashed_public_key).digest()).digest()
  59.     checksum = checksum_full[:4]
  60.     bin_addr = hashed_public_key + checksum
  61.    
  62.     #encode address to base58_check and return
  63.     result_addr = base58.b58encode(bin_addr).decode("utf-8")
  64.     return result_addr
  65.  
  66.  
  67. def main():
  68.  
  69.     f = open(sys.argv[1], "rb")
  70.     mm = mmap.mmap(f.fileno(),0,prot=mmap.PROT_READ)
  71.     currindex = mm.find(b'\x02\x01\x01\x04\x20')
  72.     privkeyList = set()
  73.     while(currindex > -1):
  74.         mm.seek(currindex+5)
  75.         #print(binascii.hexlify(mm.read(32)))
  76.         privkeyList.add(mm.read(32))
  77.         currindex = mm.find(b'\x02\x01\x01\x04\x20')
  78.  
  79.     f.close()
  80.     #print(privkeyList)
  81.     print("Found [" + str(len(privkeyList)) + "] possible keys")
  82.  
  83.     for key in privkeyList:
  84.        
  85.         PKuncomp = b'80'+ binascii.hexlify(key)
  86.         uncomp_address = priv_to_addr(key,False)
  87.         PKcomp = PKuncomp + b'01'
  88.         comp_address = priv_to_addr(key,True)
  89.        
  90.         PKuncomp_sha256_1 = hashlib.sha256(codecs.decode(PKuncomp, 'hex'))
  91.         PKuncomp_sha256_2 = hashlib.sha256(PKuncomp_sha256_1.digest())
  92.        
  93.         checksum_uncomp = codecs.encode(PKuncomp_sha256_2.digest(), 'hex')[0:8]
  94.         uncomp_PK = PKuncomp + checksum_uncomp
  95.         #print(uncomp_PK)
  96.        
  97.         PKcomp_sha256_1 = hashlib.sha256(codecs.decode(PKcomp, 'hex'))
  98.         PKcomp_sha256_2 = hashlib.sha256(PKcomp_sha256_1.digest())
  99.        
  100.         checksum_comp = codecs.encode(PKcomp_sha256_2.digest(), 'hex')[0:8]
  101.         comp_PK = PKcomp + checksum_comp
  102.         #print(comp_PK)
  103.        
  104.         WIF_uncomp = base58str(uncomp_PK.decode("utf-8"))
  105.         WIF_comp = base58str(comp_PK.decode("utf-8"))
  106.        
  107.         print(uncomp_address + ":" + WIF_uncomp)
  108.         print(comp_address + ":" + WIF_comp)
  109.  
  110. if __name__ == "__main__":
  111.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement