-
Notifications
You must be signed in to change notification settings - Fork 97
/
keyhunter.py
executable file
·91 lines (71 loc) · 2.28 KB
/
keyhunter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
import binascii
import os
import hashlib
import sys
# bytes to read at a time from file (10meg)
readlength=10*1024*1024
magic = '\x01\x30\x82\x01\x13\x02\x01\x01\x04\x20'
magiclen = len(magic)
##### start code from pywallet.py #############
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__b58base = len(__b58chars)
def b58encode(v):
""" encode v, which is a string of bytes, to base58.
"""
long_value = 0L
for (i, c) in enumerate(v[::-1]):
long_value += (256**i) * ord(c)
result = ''
while long_value >= __b58base:
div, mod = divmod(long_value, __b58base)
result = __b58chars[mod] + result
long_value = div
result = __b58chars[long_value] + result
# Bitcoin does a little leading-zero-compression:
# leading 0-bytes in the input become leading-1s
nPad = 0
for c in v:
if c != '\0':
break
nPad += 1
return (__b58chars[0]*nPad) + result
def Hash(data):
return hashlib.sha256(hashlib.sha256(data).digest()).digest()
def EncodeBase58Check(secret):
hash = Hash(secret)
return b58encode(secret + hash[0:4])
########## end code from pywallet.py ############
def find_keys(filename):
keys = set()
with open(filename, "rb") as f:
# read through target file one block at a time
while True:
data = f.read(readlength)
if not data:
break
# look in this block for keys
pos = 0
while True:
# find the magic number
pos = data.find(magic, pos)
if pos == -1:
break
key_offset = pos + magiclen
key_data = "\x80" + data[key_offset:key_offset + 32]
keys.add(EncodeBase58Check(key_data))
pos += 1
# are we at the end of the file?
if len(data) == readlength:
# make sure we didn't miss any keys at the end of the block
f.seek(f.tell() - (32 + magiclen))
return keys
def main():
if len(sys.argv) != 2:
print "./{0} <filename>".format(sys.argv[0])
exit()
keys = find_keys(sys.argv[1])
for key in keys:
print key
if __name__ == "__main__":
main()