-
Notifications
You must be signed in to change notification settings - Fork 55
/
ethereum2john.py
executable file
·118 lines (102 loc) · 3.69 KB
/
ethereum2john.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# This software is Copyright (c) 2017, Dhiru Kholia <dhiru.kholia at gmail.com>
# and it is hereby released to the general public under the following terms:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted.
#
# Special thanks goes to @Chick3nman for coming up with the output hash format.
#
# References,
#
# https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
#
# https://github.com/ethereum/go-ethereum/wiki/Passphrase-protected-key-store-spec,
# v1 wallets are not supported (yet)
import os
import sys
import traceback
try:
import json
assert json
except ImportError:
try:
sys.path.append(".")
import simplejson as json
except ImportError:
sys.stderr.write("Please install json / simplejson module which is currently not installed.\n")
sys.exit(-1)
def process_presale_wallet(filename, data):
try:
bkp = data["bkp"]
except KeyError:
sys.stdout.write("%s: presale wallet is missing 'bkp' field, this is unsupported!\n" % filename)
return
try:
encseed = data["encseed"]
ethaddr = data["ethaddr"]
except KeyError:
sys.stdout.write("%s: presale wallet is missing necessary fields!\n" % filename)
return
# 16 bytes of bkp should be enough
sys.stdout.write("%s:$ethereum$w*%s*%s*%s\n" %
(os.path.basename(filename), encseed, ethaddr, bkp[:32]))
def process_file(filename):
try:
f = open(filename, "rb")
except IOError:
e = sys.exc_info()[1]
sys.stderr.write("%s\n" % str(e))
return
data = f.read().decode("utf-8")
sys.stderr.write("WARNING: Upon successful password recovery, this hash format may expose your PRIVATE KEY. Do not share extracted hashes with any untrusted parties!\n")
try:
data = json.loads(data)
try:
crypto = data["crypto"]
except KeyError:
try:
crypto = data["Crypto"]
except: # hack for presale wallet
process_presale_wallet(filename, data)
return
cipher = crypto["cipher"]
if cipher != "aes-128-ctr":
sys.stdout.write("%s: unexpected cipher '%s' found\n" % (filename, cipher))
return -2
kdf = crypto["kdf"]
ciphertext = crypto["ciphertext"]
mac = crypto["mac"]
if kdf == "scrypt":
kdfparams = crypto["kdfparams"]
n = kdfparams["n"]
r = kdfparams["r"]
p = kdfparams["p"]
salt = kdfparams["salt"]
sys.stdout.write("%s:$ethereum$s*%s*%s*%s*%s*%s*%s\n" %
(os.path.basename(filename), n, r, p, salt,
ciphertext, mac))
elif kdf == "pbkdf2":
kdfparams = crypto["kdfparams"]
n = kdfparams["c"]
prf = kdfparams["prf"]
if prf != 'hmac-sha256':
sys.stdout.write("%s: unexpected prf '%s' found\n" % (filename, prf))
return
salt = kdfparams["salt"]
sys.stdout.write("%s:$ethereum$p*%s*%s*%s*%s\n" %
(os.path.basename(filename), n, salt,
ciphertext, mac))
else:
assert 0
except:
sys.stdout.write("%s: json parsing failed\n" % filename)
traceback.print_exc()
return -1
f.close()
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.stderr.write("Usage: %s [Ethereum Wallet files (Geth/Mist/MyEtherWallet)]\n" % sys.argv[0])
sys.exit(-1)
for j in range(1, len(sys.argv)):
process_file(sys.argv[j])