-
Notifications
You must be signed in to change notification settings - Fork 36
/
unfactor_ecdsa.py
80 lines (66 loc) · 2.89 KB
/
unfactor_ecdsa.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
from __future__ import print_function
import logging
import sys
import ecdsa
from unfactor import CrackException
log = logging.getLogger('unfactor_ecdsa')
def main(file, *primes):
pubkeys = {}
known_file_magics = ['\xde\xad\xbe\xef\x04', '\x00\x00\x00\x00\x04']
prod = 1
for p in primes:
if int(p) >= 1<<256:
raise CrackException("Factor too large: %s" % p)
prod *= int(p)
with open(file, "rb") as f:
header = f.read(414)
if header[:5] not in known_file_magics:
raise CrackException(file + " doesn't appear to be TeslaCrypted")
ecdh = int(header[0x45:0xc5].rstrip('\0'), 16)
cofactor = ecdh/prod
if cofactor*prod != ecdh:
ecdh = int(header[0x108:0x188].rstrip('\0'), 16)
cofactor = ecdh/prod
if prod > ecdh:
raise CrackException("Superfluous factors or incorrect factorization detected!")
if cofactor*prod != ecdh:
raise CrackException("Error: factors don't divide either pubkey")
i = 1
while i < 1<<len(primes):
x = 1
for j in range(len(primes)):
if i & 1<<j:
x *= int(primes[j])
if x < 1<<256 and ecdh/x < 1<<256:
if x not in pubkeys:
pubkeys[x] = ecdsa.SigningKey.from_secret_exponent(x, curve=ecdsa.SECP256k1).verifying_key.to_string()
if header[5:].startswith(pubkeys[x]):
return "Found Bitcoin private key: %064X" % x
elif header[200:].startswith(pubkeys[x]):
return "Found AES private key: b'\\x" + '\\x'.join([('%064x' % x)[i:i+2] for i in range(0, 64, 2)]) + "' (%064X)" % x
i += 1
i = 1
while i < 1<<len(primes):
x = cofactor
for j in range(len(primes)):
if i & 1<<j:
x *= int(primes[j])
if x < 1<<256 and ecdh/x < 1<<256:
if x not in pubkeys:
pubkeys[x] = ecdsa.SigningKey.from_secret_exponent(x, curve=ecdsa.SECP256k1).verifying_key.to_string()
if header[5:].startswith(pubkeys[x]):
return "Found Bitcoin private key: %064X" % x
elif header[200:].startswith(pubkeys[x]):
return "Found AES private key: b'\\x" + '\\x'.join([('%064x' % x)[i:i+2] for i in range(0, 64, 2)]) + "' (%064X)" % x
i += 1
if cofactor != 1:
raise CrackException("No keys found, incomplete factorization!")
raise CrackException("No keys found, check your factors!")
if __name__ == "__main__":
if len(sys.argv) < 3:
exit("usage: unfactor_ecdsa.py <sample file> <space-separated list of factors>")
try:
print(main(*sys.argv[1:]))
except CrackException as ex:
log.error("Reconstruction failed! %s", ex)
exit(-2)