forked from tlambertz/seedvault_backup_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
executable file
·399 lines (315 loc) · 12.4 KB
/
parse.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env python3
import re
import os
import sys
import getpass
import glob
import string
import struct
import hashlib
# debian does not ship pycryptodome under the pycrypto namespace. Try both.
# See Issue #8
try:
from Crypto.Cipher import AES
AES.MODE_GCM
except:
try:
from Cryptodome.Cipher import AES
AES.MODE_GCM
except:
print("ERROR: Please install pycryptodome.")
exit(-1)
from base64 import urlsafe_b64decode, urlsafe_b64encode
# separator can't be in urlsafe b64 alphabet. -> no A-Za-Z0-9-_ -> choose .
B64_SEPARATOR = "."
# parses file-path, where file is a base64 encoded key into the decoded filename
def filepath_to_key(filepath):
filename = filepath.split("/")[-1]
# seedvault removes padding =, add them back, else python complains
return urlsafe_b64decode(filename + "=" * ((4 - len(filename) % 4) % 4))
# parses key-value pairs stored in the "kv" subfolder
# see KVBackup.kt
def parse_kv_backup(backupfolder, targetfolder, userkey):
kvs = sorted(glob.glob(f"{backupfolder}/kv/*"))
#print("Found kv folders: ")
#for kv in kvs:
# print(" "+"/".join(kv.split("/")[2:]))
print("Decrypting Key-Value files: ")
kv_parsed = {}
for kv in kvs:
appname = "/".join(kv.split("/")[-1:])
print(" for app "+appname)
pairsb64 = glob.glob(kv+"/*")
if targetfolder:
os.makedirs(f"{targetfolder}/kv/{appname}", exist_ok=True)
# verbose: dump all found paths
if not targetfolder:
for p in sorted([filepath_to_key(p) for p in pairsb64]):
print(f" {p.decode()}")
pairs = {}
for p in pairsb64:
with open(p, "rb") as f:
ct = f.read()
key = filepath_to_key(p)
b64 = urlsafe_b64encode(key)
version = ct[0]
ct = ct[1:]
assert version == 0 # only version 0 supported
versionheader_bytes, ct = decrypt_segment(ct, userkey)
versionheader = parse_versionheader(versionheader_bytes)
# if decrypted versionheader does not match folder/filename, something has gone wrong
#print(versionheader, appname, filepath_to_key(p))
assert versionheader['name'].decode() == appname
assert versionheader['key'] == key
assert versionheader['version'] == version
# parse all remaining segments
data = decrypt_segments(ct, userkey)
if targetfolder:
# we need to save as b64, since some keys contain "/" etc
whitelist = string.ascii_letters + string.digits + '.'
cleanname = re.sub(f'[^{whitelist}]', '', key.decode())
with open(f"{targetfolder}/kv/{appname}/{cleanname}{B64_SEPARATOR}{b64.decode()}", "wb") as f:
f.write(data)
pairs[key] = data
#print(key, data, b64)
kv_parsed[appname] = pairs
return kv_parsed
# just prints all apk names found
def parse_apk_backup(backupfolder):
apks = sorted(glob.glob(f"{backupfolder}/*.apk"))
print("Found apks: ")
for apk in apks:
print(" "+"/".join(apk.split("/")[1:]))
# parses the full app backups, stored in the "full" subfolder
# see FullBackup.kt::performFullBackup()
def parse_full_app_backups(backupfolder, targetfolder, userkey):
if targetfolder:
os.makedirs(f"{targetfolder}/full", exist_ok=True)
fulls = sorted(glob.glob(f"{backupfolder}/full/*"))
print("Decrypting full backup for apps: ")
for full in fulls:
appname = "/".join(full.split("/")[-1:])
print(" "+appname)
with open(full, "rb") as f:
ct = f.read()
#key = filepath_to_key(p)
#b64 = urlsafe_b64encode(key)
version = ct[0]
ct = ct[1:]
assert version == 0 # only version 0 supported
versionheader_bytes, ct = decrypt_segment(ct, userkey)
versionheader = parse_versionheader(versionheader_bytes, False)
# if decrypted versionheader does not match folder/filename, something has gone wrong
#print(versionheader, appname, filepath_to_key(p))
assert versionheader['name'].decode() == appname
assert versionheader['version'] == version
# parse all remaining segments
data = decrypt_segments(ct, userkey)
if targetfolder:
with open(f"{targetfolder}/full/{appname}", "wb") as f:
f.write(data)
else:
print(" Value: ", data)
print("\n\n\n")
def parse_metadata(backupfolder, targetfolder, key):
with open(f"{backupfolder}/.backup.metadata", "rb") as f:
ct = f.read()
version = ct[0]
assert version == 0
pt = decrypt_segments(ct[1:], key)
if targetfolder:
with open(f"{targetfolder}/.backup.metadata", "wb") as f:
f.write(pt)
else:
print("Metadata:")
print(pt)
# parses everything
def parse_backup(backupfolder, targetfolder, key):
if targetfolder:
os.makedirs(targetfolder, exist_ok=True)
parse_metadata(backupfolder, targetfolder, key)
parse_apk_backup(backupfolder)
kv_parsed = parse_kv_backup(backupfolder, targetfolder, key)
if targetfolder == None:
print_kv_pairs(kv_parsed)
print("\n\n")
# only decrypt apps into a folder, never print, since they might be huge
if targetfolder:
parse_full_app_backups(backupfolder, targetfolder, key)
else:
print("Skipping full app backup decryption, since they might be too large to show. Use the DECRYPT option")
return kv_parsed
# "pretty" prints all found key-value pairs
def print_kv_pairs(kv):
for app, pairs in kv.items():
print("------------------------------------------------------\n")
for key, value in pairs.items():
print(f"APP: {app}\t\tKEY: {key.decode()}")
print(value)
print()
# takes a single segment, decrypts it. Returns trailing data (other segments)
# segment consists of
# 2 Bytes - Segment length x
# 12 Bytes - IV used for encryption
# x Bytes - Encrypted Data (of which last 16 bytes are aes-gcm-tag)
def decrypt_segment(ct, key):
# parse segment header to get iv and segment length
length = struct.unpack(">h", ct[:2])[0]
assert len(ct[2:]) >= length
remainder = ct[2+12+length:]
iv = ct[2:2+12]
ct = ct[2+12:2+12+length]
# use iv from segment header to decrypt
pt = aes_decrypt(ct, key, iv)
#print(length, iv, ct)
return pt, remainder
# decrypt multiple consecutive segments
def decrypt_segments(ct, key):
data = b""
while ct:
pt, ct = decrypt_segment(ct, key)
data += pt
return data
# decrypt a ciphertext with aesgcm and verify its tag. Last 16 bytes of ct are tag
def aes_decrypt(ct, key, iv):
TAG_LEN = 128//8
cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
tag = ct[-TAG_LEN:]
try:
pt = cipher.decrypt_and_verify(ct[:-TAG_LEN], tag)
except ValueError as e:
print(e)
print("Could not decrypt data! Is your key correct?")
sys.exit(-1)
return pt
# encrypt a ciphertext with aesgcm. Last 16 bytes of ct are tag
def aes_encrypt(pt, key, iv):
TAG_LEN = 128//8
cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
ct, tag = cipher.encrypt_and_digest(pt)
return ct + tag
# encrypts a segment, creates random iv and correct header
def encrypt_segment(pt, key):
# create segment header
# length of cipher text and following tag (16 bytes) must fit into 15 bits
# because the android code reads it as a signed short.
assert len(pt) + 16 < 2**15, f"Encryption block must be less than {(2**15)-16}, was {len(pt)}"
header = struct.pack(">h", len(pt) + 16)
iv = os.urandom(12) # random IV
header += iv
ct = aes_encrypt(pt, key, iv)
return header + ct
# encrypt multiple consecutive segments
# blocksize defaults to maximum segment data size. Segment length is 2 bytes,
# but read as a signed short, so 15bits, and subtract the 16 byte auth tag.
def encrypt_segments(pt, key, blocksize=(2**15)-(16+1)):
ct = b""
for i in range(0, len(pt), blocksize):
ct += encrypt_segment(pt[i:i+blocksize], key)
return ct
# Version Header is:
# 1 Byte - Version
# 2 Bytes - Packagename length x
# x Bytes - Packagename
# 2 Bytes - Keyname length y
# y Bytes - Keyname
#
# see HeaderWriter.kt
def parse_versionheader(vb, include_key=True):
version = vb[0]
namelen = struct.unpack(">H", vb[1:3])[0]
name = vb[3:3+namelen]
key = None
if include_key:
keylen = struct.unpack(">H", vb[3+namelen:3+namelen+2])[0]
assert len(vb) == namelen + keylen + 2 + 2 + 1
key = vb[3+2+namelen:]
return {
"version": version,
"name": name,
"key": key,
}
def create_versionheader(appname, key=None):
data = b"\0" # version
assert len(appname) < 255
data += struct.pack(">H", len(appname))
data += appname.encode()
if key:
data += struct.pack(">H", len(key))
data += key
return data
# reencrypts key-value pairs from a decrypted backup, so they can be flashed to the device
def encrypt_backup(plainfolder, targetfolder, userkey):
assert targetfolder
os.makedirs(f"{targetfolder}/kv", exist_ok=True)
kvs = sorted(glob.glob(f"{plainfolder}/kv/*"))
print("Encrypting Key-Value files: ")
for kv in kvs:
appname = "/".join(kv.split("/")[2:])
print(" for app "+appname, kv)
pairsb64 = glob.glob(kv+"/*")
os.makedirs(f"{targetfolder}/kv/{appname}", exist_ok=True)
for p in pairsb64:
with open(p, "rb") as f:
pt = f.read()
# file has to have an B64_SEPARATOR followed by the base64 of the key!
keyb64 = p.split(B64_SEPARATOR)[-1]
print(keyb64)
key = urlsafe_b64decode(keyb64)
print(" ", key)
ct = b""
# version is 0
ct += b"\0"
versionheader_bytes = create_versionheader(appname, key)
ct += encrypt_segment(versionheader_bytes, userkey)
# encrypt the plaintext
ct += encrypt_segments(pt, userkey)
with open(f"{targetfolder}/kv/{appname}/{keyb64.replace('=', '')}", "wb") as f:
f.write(ct)
print("Encrypting Metadata file")
with open(f"{plainfolder}/.backup.metadata", "rb") as f:
meta = f.read()
metac = b"\0" + encrypt_segment(meta, userkey)
with open(f"{targetfolder}/.backup.metadata", "wb") as f:
f.write(metac)
print("Done.")
# generate the key from a user-input mnemonic phrase
# uses the same algorithms as seedvault, see
# https://github.com/NovaCrypto/BIP39/blob/master/src/main/java/io/github/novacrypto/bip39/SeedCalculator.java
# https://github.com/NovaCrypto/BIP39/blob/master/src/main/java/io/github/novacrypto/bip39/JavaxPBKDF2WithHmacSHA512.java
def get_key():
salt = b"mnemonic"
rounds = 2048
keysize = 256
vis = input("Should mnemonic be visible while typing? [y/n]: ")
if vis.lower().startswith("y"):
mnemonic = input("Please enter mnemonic: ").encode()
else:
mnemonic = getpass.getpass("Please enter mnemonic: ").encode()
key = hashlib.pbkdf2_hmac("sha512", mnemonic, salt, rounds)
return key[:keysize//8]
def main():
if len(sys.argv) < 3:
print("Usage: %s SHOW [BACKUPFOLDER]" % sys.argv[0])
print(" %s DECRYPT [BACKUPFOLDER] [TARGETFOLDER]" % sys.argv[0])
print(" %s ENCRYPT [PLAINFOLDER] [TARGETFOLDER] (only KV support right now)" % sys.argv[0])
sys.exit(-1)
backupfolder = sys.argv[2]
if sys.argv[1].lower() == "show":
targetfolder = None
print(f"Parsing backup {backupfolder}")
userkey = get_key()
kv_parsed = parse_backup(backupfolder, targetfolder, userkey)
elif sys.argv[1].lower() == "decrypt":
targetfolder = sys.argv[3]
print(f"Decrypting backup from {backupfolder} into {targetfolder}")
userkey = get_key()
kv_parsed = parse_backup(backupfolder, targetfolder, userkey)
elif sys.argv[1].lower() == "encrypt":
plainfolder = sys.argv[2]
targetfolder = sys.argv[3]
print(f"Encrypting backup from {plainfolder} into {targetfolder}")
userkey = get_key()
kv_parsed = encrypt_backup(plainfolder, targetfolder, userkey)
if __name__ == "__main__":
main()