-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsonicrack.py
executable file
·465 lines (408 loc) · 15.5 KB
/
sonicrack.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python3
import argparse
import hashlib
import io
import gzip
import os
import shutil
import struct
import subprocess
import sys
import tarfile
from Crypto.Cipher import AES
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
# Load previously extracted keys from a directory
def load_keys(dir_path):
if (
os.path.isdir(dir_path)
and os.path.isfile(f"{dir_path}/FW-crypt-release.key")
and os.path.isfile(f"{dir_path}/SCX-crypt-release.key")
):
try:
keys = {}
with open(f"{dir_path}/FW-crypt-release.key") as keyfile:
keys["fw_crypt_key"] = RSA.importKey(keyfile.read())
with open(f"{dir_path}/SCX-crypt-release.key") as keyfile:
keys["scx_crypt_key"] = RSA.importKey(keyfile.read())
return keys
except:
return {}
# Extract SIG image and bootloader from OVA image
def extract_ova_files(image_path):
# Extract VMDK file
try:
vmdk_file = None
with tarfile.open(image_path, errorlevel=0) as tf:
for file_name in tf.getnames():
if file_name.endswith("disk1.vmdk"):
tf.extract(file_name, "/tmp/sonicrack", filter="data")
vmdk_file = f"/tmp/sonicrack/{os.path.basename(file_name)}"
break
except Exception as err:
raise ValueError(f"Failed to extract VMDK file from OVA image: {err}")
if not vmdk_file or not os.path.isfile(vmdk_file):
raise ValueError("Failed to extract VMDK file from OVA image: not found")
# Extract volume images from VMDK file with 7zip
try:
subprocess.run(
["7z", "e", "-o/tmp/sonicrack", vmdk_file, "BOOT.img", "INSTALL-CACHE.img", "ICACHE.img"],
check=True,
capture_output=True,
)
except Exception as err:
raise ValueError(f"Failed to extract volume images from VMDK file: {err}")
if not os.path.isfile("/tmp/sonicrack/BOOT.img"):
raise ValueError("Failed to extract BOOT.img from VMDK file: not found")
if os.path.isfile("/tmp/sonicrack/INSTALL-CACHE.img"):
installer = "INSTALL-CACHE"
elif os.path.isfile("/tmp/sonicrack/ICACHE.img"):
installer = "ICACHE"
else:
raise ValueError(
"Failed to extract INSTALL-CACHE.img from VMDK file: not found"
)
# Extract bootloader from BOOT volume
mount_image("/tmp/sonicrack/BOOT.img", "/mnt/BOOT")
shutil.copy2("/mnt/BOOT/EFI/BOOT/bootx64.efi", "/tmp/sonicrack")
unmount_image("/mnt/BOOT")
# Extract firmware image from INSTALL-CACHE volume
mount_image(f"/tmp/sonicrack/{installer}.img", f"/mnt/{installer}")
shutil.copy2(
f"/mnt/{installer}/currentFirmware/currentFirmware.bin.sig", "/tmp/sonicrack"
)
unmount_image(f"/mnt/{installer}")
# Clean up
os.remove(vmdk_file)
os.remove("/tmp/sonicrack/BOOT.img")
os.remove(f"/tmp/sonicrack/{installer}.img")
# Mount volume image
def mount_image(image_path, mount_path):
if not os.path.isfile(image_path):
raise ValueError("Failed to load image data: file not found")
try:
if not os.path.isdir(mount_path):
os.makedirs(mount_path)
subprocess.run(
["mount", "-o", "ro", image_path, mount_path],
check=True,
capture_output=True,
)
except subprocess.CalledProcessError as err:
raise ValueError(f"Failed to mount image: {err.stderr.decode().strip()}")
except Exception as err:
raise ValueError(f"Failed to mount image: {err}")
# Unmount volume
def unmount_image(mount_path):
subprocess.run(["umount", mount_path], capture_output=True)
if os.path.isdir(mount_path):
shutil.rmtree(mount_path, ignore_errors=True)
subprocess.run(
["cryptsetup", "close", os.path.basename(mount_path)],
check=False,
capture_output=True,
)
# Extract keys from bootloader
def extract_keys(bootloader_path, output_path):
# Extract initramfs
extract_initramfs(bootloader_path)
if not os.path.isfile("/tmp/sonicrack/initramfs.cpio"):
raise ValueError("Failed to extract initramfs from bootloader")
os.remove(bootloader_path)
# Extract encrypted sunup installer and its key from initramfs
subprocess.run(
[
"cpio",
"-idum",
"--no-absolute-filenames",
"--quiet",
"-F",
"/tmp/sonicrack/initramfs.cpio",
"onetime.key",
"sunup.cpio.gz.enc",
],
cwd="/tmp/sonicrack",
check=True,
capture_output=True,
)
if not os.path.isfile("/tmp/sonicrack/onetime.key") or not os.path.isfile(
"/tmp/sonicrack/sunup.cpio.gz.enc"
):
raise ValueError("Failed to extract sunup installer files from initramfs")
os.remove("/tmp/sonicrack/initramfs.cpio")
# Decrypt sunup installer
decrypt_sunup_package(
"/tmp/sonicrack/sunup.cpio.gz.enc",
"/tmp/sonicrack/onetime.key",
"/tmp/sonicrack",
)
os.remove("/tmp/sonicrack/onetime.key")
os.remove("/tmp/sonicrack/sunup.cpio.gz.enc")
# Decompress sunup installer
with gzip.open("/tmp/sonicrack/sunup.cpio.gz", "rb") as infile:
with open("/tmp/sonicrack/sunup.cpio", "wb") as outfile:
shutil.copyfileobj(infile, outfile)
if not os.path.isfile("/tmp/sonicrack/sunup.cpio"):
raise ValueError("Failed to decompress sunup installer package")
os.remove("/tmp/sonicrack/sunup.cpio.gz")
# Extract keys from sunup installer
subprocess.run(
[
"cpio",
"-idum",
"--no-absolute-filenames",
"--quiet",
"-F",
"/tmp/sonicrack/sunup.cpio",
"usr/share/installer/FW-crypt-release.key",
"usr/share/installer/SCX-crypt-release.key",
],
cwd="/tmp/sonicrack",
check=True,
capture_output=True,
)
if not os.path.isfile(
"/tmp/sonicrack/usr/share/installer/FW-crypt-release.key"
) or not os.path.isfile("/tmp/sonicrack/usr/share/installer/SCX-crypt-release.key"):
raise ValueError("Failed to extract keys from sunup installer package")
os.remove("/tmp/sonicrack/sunup.cpio")
# Copy keys to output directory
os.makedirs(output_path, exist_ok=True)
shutil.copy2(
"/tmp/sonicrack/usr/share/installer/FW-crypt-release.key",
f"{output_path}/FW-crypt-release.key",
)
shutil.copy2(
"/tmp/sonicrack/usr/share/installer/SCX-crypt-release.key",
f"{output_path}/SCX-crypt-release.key",
)
shutil.rmtree("/tmp/sonicrack/usr")
# Carve initramfs from bootloader
def extract_initramfs(file_path):
with open(file_path, "rb") as infile:
bootx = infile.read()
start = bootx.find(b"\x1f\x8b\x08\x00")
if start == -1:
raise ValueError("No initramfs found within bootloader")
# NOTE: There's no good way to identify the end of the gzip member.
# We're relying on the presence of 32 null bytes in a row to indicate
# the end of the stream, but this may not always work.
end = bootx.find(b"\x00" * 32, start)
with open("/tmp/sonicrack/initramfs.cpio", "wb") as outfile:
outfile.write(gzip.decompress(bootx[start:end]))
# Decrypt sunup installer package
def decrypt_sunup_package(file_path, key_path, output_path):
try:
subprocess.run(
[
"openssl",
"enc",
"-a",
"-d",
"-aes-256-cbc",
"-pbkdf2",
"-in",
file_path,
"-out",
f"{output_path}/sunup.cpio.gz",
"-pass",
f"file:{key_path}",
],
check=True,
capture_output=True,
)
except subprocess.CalledProcessError as err:
raise ValueError(
f"Failed to decrypt {file_path}: {err.stderr.decode().strip()}"
)
except Exception as err:
raise ValueError(f"Failed to decrypt {file_path}: {err}")
def read_until(fd, c):
result = bytearray()
while 1:
char = fd.read(1)
result.extend(char)
if char == c:
return bytes(result)
# Extract FW decryption key from SSDH package header
def extract_encrypted_fw_key(fw_image):
# fwEncryptExtract() from usr/bin/fwDecrypt
fw_image.seek(1004) # skip file header
if fw_image.read(4) != b"\x8f\xc4\xe9\xa7":
raise ValueError(
"Failed to extract FW key from image file: invalid magic bytes"
)
headerLen, headerRev, headerVer, _, algoKeyId, sskbIndex, rsaKeyLen = struct.unpack(
">IBBHIHH", fw_image.read(16)
)
if headerLen - rsaKeyLen != 36:
raise ValueError(
"Failed to extract FW key from image file: invalid key length value in header"
)
iv = fw_image.read(16)
encrypted_aeskey = fw_image.read(rsaKeyLen)
return iv, encrypted_aeskey
# Decrypt SW package from SSDH package
def decrypt_sw_package(encrypted_sw_package, scx_crypt_cipher):
# swpackage_decrypt() from usr/bin/installer
# Parse SCX key and payload from SW package
package_file = io.BytesIO(encrypted_sw_package)
read_until(package_file, b"\n") # skip over package header
encrypted_key = package_file.read(256)
sentinel = get_random_bytes(16)
key = scx_crypt_cipher.decrypt(encrypted_key, sentinel=sentinel)
if key == sentinel:
raise ValueError("Failed to decrypt SCX key")
password = key.decode().strip() + ":sonicos-release:"
ciphertext = b"Salted__" + package_file.read()
# Decrypt payload
try:
result = subprocess.run(
[
"openssl",
"enc",
"-d",
"-pbkdf2",
"-aes-256-cbc",
"-pass",
f"pass:{password}",
],
input=ciphertext,
check=True,
capture_output=True,
text=False,
)
except subprocess.CalledProcessError as err:
raise ValueError(f"Failed to decrypt SW package: {err.stderr.decode().strip()}")
except Exception as err:
raise ValueError(f"Failed to decrypt SW package: {err}")
return result.stdout
# Load SIG key decryption key from file
def get_crypt_key(file_path):
try:
with open(file_path) as keyfile:
key = RSA.importKey(keyfile.read())
return key.exportKey("DER").hex()
except:
return None
# Helper function for key decryption
def EVP_BytesToKey(password, salt, key_len, iv_len):
"""
https://stackoverflow.com/questions/13907841/implement-openssl-aes-encryption-in-python
"""
dtot = hashlib.md5(password + salt).digest()
d = [dtot]
while len(dtot) < (iv_len + key_len):
d.append(hashlib.md5(d[-1] + password + salt).digest())
dtot += d[-1]
return dtot[:key_len], dtot[key_len : key_len + iv_len]
# Decrypt SIG-formatted firmware image and extract filesystem image
def decrypt_sig_image(file_path, keys, output_path):
# Prepare decryption ciphers
if "fw_crypt_key" not in keys or "scx_crypt_key" not in keys:
return None
fw_crypt_cipher = PKCS1_v1_5.new(keys["fw_crypt_key"])
scx_crypt_cipher = PKCS1_v1_5.new(keys["scx_crypt_key"])
# Parse FW key and SSDH package from image file
with open(file_path, "rb") as image_file:
iv, encrypted_fw_key = extract_encrypted_fw_key(image_file)
sentinel = get_random_bytes(16)
fw_key = fw_crypt_cipher.decrypt(
encrypted_fw_key, sentinel=sentinel, expected_pt_len=16
)
if fw_key == sentinel:
raise ValueError("Failed to decrypt FW key")
aes_fw_key = AES.new(key=fw_key, mode=AES.MODE_CBC, iv=iv)
ciphertext = image_file.read()
if len(ciphertext) & 0xF != 0:
raise ValueError("Failed to decrypt image file: unaligned ciphertext")
# Decrypt SSDH package
plaintext = aes_fw_key.decrypt(ciphertext)
footer = plaintext[-16:]
magic, version, datalen, dummy = struct.unpack(">IIII", footer)
if magic != 0x777DEADB or version != 1 or dummy != 0:
raise ValueError(
"Failed to decrypt image file: invalid footer after decryption"
)
encrypted_sw_package = plaintext[:datalen]
# Decrypt and decompress nested SW package
sw_package = gzip.decompress(
decrypt_sw_package(encrypted_sw_package, scx_crypt_cipher)
)
# Get image file name
start = sw_package.find(b"FILE=") + 5
end = sw_package.find(b"\n", start)
filename = sw_package[start:end].decode()
# Extract image file (filesystem follows two newlines)
os.makedirs(output_path, exist_ok=True)
start = sw_package.find(b"\n\n") + 2
with open(f"{output_path}/{filename}", "wb") as outfile:
outfile.write(sw_package[start:])
return filename
def main():
# Parse command line arguments
desc = "A tool to decrypt SonicOSX NSv firmware. Run it against an OVA image first to extract keys, then it can also decrypt SIG images."
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
"-k",
"--keys",
metavar="KEYS_DIR",
help="Directory path where keys are stored (defaults to ./keys)",
)
parser.add_argument(
"-o",
"--output",
metavar="OUTPUT_DIR",
help="Directory path where decrypted firmware is saved (defaults to ./)",
)
parser.add_argument(
"IMAGE_PATH",
help="Path to encrypted firmware image (supported file extensions: .ova, .sig)",
)
args = parser.parse_args()
# Validate firmware image file extension
if not (args.IMAGE_PATH.endswith(".ova") or args.IMAGE_PATH.endswith(".sig")):
print("[-] Firmware image format not supported (must be .ova or .sig)")
sys.exit(1)
# Determine output directories
key_path = args.keys if args.keys else f"{os.path.dirname(args.IMAGE_PATH)}/keys"
output_path = args.output if args.output else os.path.dirname(args.IMAGE_PATH)
# Load keys from disk if available
keys = load_keys(key_path)
if not keys and args.IMAGE_PATH.endswith(".sig"):
print(
"[-] Keys must be extracted from an OVA image before a SIG image can be decrypted"
)
print(
"[-] If already extracted, use --keys to indicate the directory where they are saved"
)
sys.exit(1)
# Process firmware
try:
# Create temporary directory
os.makedirs("/tmp/sonicrack", exist_ok=True)
# Unpack OVA image
if args.IMAGE_PATH.endswith(".ova"):
# Extract bootloader and firmware
print("[*] Extracting files from OVA archive")
extract_ova_files(args.IMAGE_PATH)
image_path = "/tmp/sonicrack/currentFirmware.bin.sig"
# Extract keys from bootloader if needed
if not keys:
print("[*] Extracting keys from bootloader")
extract_keys("/tmp/sonicrack/bootx64.efi", key_path)
keys = load_keys(key_path)
else:
image_path = args.IMAGE_PATH
# Decrypt SIG image
print("[*] Decrypting firmware image")
fw_image = decrypt_sig_image(image_path, keys, output_path)
print(f"[+] Successfully decrypted {fw_image}")
except Exception as err:
print(f"[-] {err}")
finally:
# Clean up
shutil.rmtree("/tmp/sonicrack", ignore_errors=True)
if __name__ == "__main__":
main()