-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevernote_exb.go
309 lines (226 loc) · 6.08 KB
/
evernote_exb.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"bytes"
"crypto/sha1"
"database/sql"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/schollz/progressbar/v3"
"golang.org/x/crypto/pbkdf2"
)
var tokenLocations = map[string][4]int{
"evernote": {378, 155, 4, 0},
"enscript": {378, 155, 4, 1},
"evernote_china": {471, 220, 4, 0},
"enscript_china": {471, 220, 4, 1},
}
var userLocations = map[string][4]int{
"evernote": {378, 289, 4, 15},
"enscript": {378, 289, 4, 8},
"evernote_china": {471, 372, 4, 15},
"enscript_china": {471, 372, 4, 8},
}
func exbGetUser(dbFile string, password string, bruteForceStart int64) User {
if _, err := os.Stat(dbFile); errors.Is(err, os.ErrNotExist) {
err = errors.New("database file does not exist")
expectedFail(err)
}
db, err := sql.Open("sqlite", dbFile)
panicFail(err)
token, err := exbGetToken(db, password, bruteForceStart)
expectedFail(err)
user, err := exbGetUserData(db)
expectedFail(err)
return User{
path: dbFile,
id: user.ID,
username: user.Username,
email: user.Email,
token: token,
}
}
func exbGetUserData(db *sql.DB) (user UserData, err error) {
userData, err := exbGetKnownData(db, userLocations)
if err != nil {
userData, err = exbBlindSearchUser(db)
if err != nil {
return
}
}
user = exbReadUserData(userData)
return
}
func exbBlindSearchUser(db *sql.DB) (data []byte, err error) {
for _, rowData := range exbBlindDataQuery(db) {
if len(rowData) > 500 && bytes.HasPrefix(rowData, []byte{8, 0, 1}) {
data = rowData
return
}
}
err = errors.New("user data not found on blind search")
return
}
func exbGetToken(db *sql.DB, password string, bruteForceStart int64) (token string, err error) {
tokenData, err := exbGetKnownData(db, tokenLocations)
if err != nil {
tokenData, err = exbBlindSearchToken(db)
if err != nil {
return
}
}
if bruteForceStart != -1 {
password, err = exbBruteForcePass(tokenData, bruteForceStart)
expectedFail(err)
fmt.Println("\nPassword is:", password)
}
token, err = exbDecryptToken(tokenData, password)
return
}
func exbBlindSearchToken(db *sql.DB) (data []byte, err error) {
for _, rowData := range exbBlindDataQuery(db) {
if len(rowData)%16 == 0 && len(rowData) > 200 && len(rowData) < 300 && Shannon(rowData[:200]) >= 1000 {
data = rowData
return
}
}
err = errors.New("token not found on blind search")
return
}
func exbBlindDataQuery(db *sql.DB) (datas [][]byte) {
rows, err := db.Query("SELECT data FROM attrs")
panicFail(err)
defer func(rows *sql.Rows) {
err := rows.Close()
panicFail(err)
}(rows)
for rows.Next() {
var data []byte
err = rows.Scan(&data)
panicFail(err)
datas = append(datas, data)
}
return
}
func exbGetKnownData(db *sql.DB, knownLocations map[string][4]int) (resultData []byte, err error) {
for _, loc := range knownLocations {
data, err := exbQueryData(db, loc)
if err != nil {
continue
}
resultData = data
}
if len(resultData) == 0 {
err = errors.New("not found in known locations")
}
return
}
func exbQueryData(db *sql.DB, location [4]int) (data []byte, err error) {
row := db.QueryRow("SELECT data FROM attrs WHERE uid=? and aid=? and afl=? and csn=?",
location[0], location[1], location[2], location[3])
err = row.Scan(&data)
return
}
func exbReadUserData(data []byte) (user UserData) {
buf := bytes.NewBuffer(data)
buf.Next(3)
user.ID = strconv.Itoa(int(binary.BigEndian.Uint32(buf.Next(4))))
buf.Next(3)
user.Username = exbReadString(buf)
buf.Next(3)
user.Email = exbReadString(buf)
return
}
func exbReadString(buf *bytes.Buffer) string {
strLen := int(binary.BigEndian.Uint32(buf.Next(4)))
return string(buf.Next(strLen))
}
func exbDecryptToken(data []byte, password string) (token string, err error) {
cleanData := exbUnscrambleTokenData(data)
if password == "" {
password = getDiskSerial()
}
key := exbMakeKey(password)
iv := cleanData[:16]
ciphertext := cleanData[16:]
token = string(aesDecrypt(ciphertext, key, iv))
if !strings.HasPrefix(token, "S=s") {
err = errors.New("failed to decrypt token data")
}
return
}
func exbUnscrambleTokenData(obfData []byte) []byte {
var deobfData []byte
for i, char := range obfData {
curSize := byte(len(obfData) - i)
newByte := char ^ ((curSize * (curSize ^ 0xFF)) & 0x7F)
deobfData = append(deobfData, newByte)
}
data, err := hex.DecodeString(string(deobfData))
panicFail(err)
return data
}
func exbMakeKey(password string) []byte {
const passwordBase = "{154BE163-907C-4188-9929-37F9A2D4EFD4}"
const salt = "{91E71FB1-F55F-4fbc-B9FD-DC0A8C01C469}"
passwordSeed := []byte(passwordBase + password)
return pbkdf2.Key(passwordSeed, []byte(salt), 4096, 16, sha1.New)
}
func exbBruteWorker(ciphertext []byte, iv []byte, jobs <-chan int64, results chan<- string) {
tokenPrefix := []byte("S=s")
for j := range jobs {
password := strconv.FormatInt(j, 10)
key := exbMakeKey(password)
tokenBytes := aesDecrypt(ciphertext, key, iv)
if !bytes.HasPrefix(tokenBytes, tokenPrefix) {
results <- ""
} else {
results <- password
}
}
}
func exbBruteForcePass(encData []byte, start int64) (password string, err error) {
const numJobsTotal int64 = 4294967295
jobsBatch := int64(100000)
bar := progressbar.Default(numJobsTotal)
err = bar.Add64(start)
panicFail(err)
cleanData := exbUnscrambleTokenData(encData)
iv := cleanData[:16]
ciphertext := cleanData[16:]
for i := start; i < numJobsTotal; {
// last batch
if numJobsTotal-i < jobsBatch {
jobsBatch = numJobsTotal - i
}
jobs := make(chan int64, jobsBatch)
results := make(chan string, jobsBatch)
for w := 1; w <= 10; w++ {
go exbBruteWorker(ciphertext, iv, jobs, results)
}
for j := i; j <= i+jobsBatch; j++ {
jobs <- j
}
close(jobs)
for a := int64(1); a <= jobsBatch; a++ {
resPassword := <-results
err := bar.Add64(1)
panicFail(err)
if resPassword != "" {
password = resPassword
return password, nil
}
}
if i+jobsBatch > numJobsTotal {
i = numJobsTotal - i
} else {
i += jobsBatch
}
}
err = errors.New("password not found")
return
}