-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
198 lines (163 loc) · 4.31 KB
/
main.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"bufio"
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"sync"
"golang.org/x/crypto/pbkdf2"
)
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: walletRecover [pathToEncryptedWallet] [pathToPasswordDictionary]")
return
}
// read the encrypted file
encodedPayload, err := ioutil.ReadFile(os.Args[1])
if err != nil {
log.Fatal(err)
}
// Decode base64 payload
cipherText, err := base64.StdEncoding.DecodeString(string(encodedPayload))
if err != nil {
log.Fatal("Payload does not appear to be base64", err)
}
if len(cipherText) < aes.BlockSize {
log.Fatal("Ciphertext block size is too short!")
}
// Load the dictionary
f, err := os.Open(os.Args[2])
if err != nil {
log.Fatal(err)
}
defer f.Close()
sc := bufio.NewScanner(f)
worklist := make(chan string)
progress := make(chan string)
errors := make(chan error)
done := make(chan string)
// seed the worklist using the passwords from the dictionary file
go func(sc *bufio.Scanner) {
defer close(worklist)
for sc.Scan() {
worklist <- sc.Text()
}
}(sc)
// array of methods for decrypting different blockchain.info legacy formats
formats := []func(string, []byte) (string, error){decrypt, decryptLegacy1, decryptLegacy2}
// 20 worker goroutines pulling from the worklist
wg := new(sync.WaitGroup)
for x := 0; x < 20; x++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case p := <-worklist:
// Try decrypting in each legacy format
for _, decryptFormat := range formats {
dec, err := decryptFormat(p, cipherText)
if err != nil {
errors <- err
}
// try to marshal into json
if err = attemptJSON(dec); err != nil {
continue
}
// We decrypted successfully
close(done)
fmt.Printf("Wallet decoded successfully with password \"%s\"\n", p)
fmt.Printf("Decoded: %s\n", string(dec))
return
}
progress <- fmt.Sprintf("Not json: %s", p)
case <-done:
return
}
}
}()
}
// Occasionally print intermediate results
go func() {
x := 0
for update := range progress {
if x%10000 == 0 {
fmt.Printf("Health Check: %s | %d passwords tried so far\n", update, x)
}
x++
}
}()
// Gracefully shutdown if an error occurs
go func() {
for err := range errors {
fmt.Println("Error: ", err)
close(done)
close(errors)
}
}()
wg.Wait()
close(progress)
}
// attempt to marshal into json
func attemptJSON(s string) error {
var f interface{}
return json.Unmarshal([]byte(s), &f)
}
// most recent legacy wallet format
func decrypt(plainKey string, cipherText []byte) (string, error) {
// iv and salt are the first 16 bytes
iv := cipherText[:aes.BlockSize]
salt := cipherText[:aes.BlockSize]
cipherText = cipherText[aes.BlockSize:]
cipherKey := pbkdf2.Key([]byte(plainKey), salt, 10, 32, sha1.New)
block, err := aes.NewCipher(cipherKey)
if err != nil {
return "", err
}
ecb := cipher.NewCBCDecrypter(block, iv)
decrypted := make([]byte, len(cipherText))
ecb.CryptBlocks(decrypted, cipherText)
return string(unpad(decrypted)), nil
}
// Only 1 iteration for pbkdf2
func decryptLegacy1(plainKey string, cipherText []byte) (string, error) {
// iv and salt are the first 16 bytes
iv := cipherText[:aes.BlockSize]
salt := cipherText[:aes.BlockSize]
cipherText = cipherText[aes.BlockSize:]
cipherKey := pbkdf2.Key([]byte(plainKey), salt, 1, 32, sha1.New)
block, err := aes.NewCipher(cipherKey)
if err != nil {
return "", err
}
ecb := cipher.NewCBCDecrypter(block, iv)
decrypted := make([]byte, len(cipherText))
ecb.CryptBlocks(decrypted, cipherText)
return string(unpad(decrypted)), nil
}
// No Salt or IV
func decryptLegacy2(plainKey string, cipherText []byte) (string, error) {
iv := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
salt := []byte{}
cipherKey := pbkdf2.Key([]byte(plainKey), salt, 10, 32, sha1.New)
block, err := aes.NewCipher(cipherKey)
if err != nil {
return "", err
}
ecb := cipher.NewCBCDecrypter(block, iv)
decrypted := make([]byte, len(cipherText))
ecb.CryptBlocks(decrypted, cipherText)
return string(unpad(decrypted)), nil
}
// Remove ISO 10126 padding
func unpad(src []byte) []byte {
length := len(src)
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}