mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
working draft of backalyzer
This commit is contained in:
parent
ba7532a4fe
commit
22a85ba022
1 changed files with 165 additions and 74 deletions
|
|
@ -1,12 +1,16 @@
|
|||
#! /usr/bin/env python3
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
from Crypto.Cipher import AES
|
||||
from rfc8452 import AES_GCM_SIV
|
||||
from Crypto.Hash import SHA512
|
||||
import binascii
|
||||
from datetime import datetime
|
||||
import bcrypt
|
||||
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap_with_padding, aes_key_wrap_with_padding
|
||||
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap_with_padding
|
||||
from cryptography.hazmat.primitives import hashes
|
||||
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
||||
|
||||
from pddbcommon import *
|
||||
|
||||
|
|
@ -14,41 +18,6 @@ from bip_utils import (
|
|||
Bip39MnemonicValidator, Bip39MnemonicDecoder
|
||||
)
|
||||
|
||||
def keycommit_decrypt(key, aad, pp_data):
|
||||
# print([hex(d) for d in pp_data[:32]])
|
||||
nonce = pp_data[:12]
|
||||
ct = pp_data[12:12+4004]
|
||||
kcom_nonce = pp_data[12+4004:12+4004+32]
|
||||
kcom = pp_data[12+4004+32:12+4004+32+32]
|
||||
mac = pp_data[-16:]
|
||||
|
||||
#print([hex(d) for d in kcom_nonce])
|
||||
# print([hex(d) for d in kcom])
|
||||
#h_test = SHA512.new(truncate="256") # just something to make sure our hash functions are sane
|
||||
#h_test.update(bytes([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x01]))
|
||||
#print(h_test.hexdigest())
|
||||
|
||||
h_enc = SHA512.new(truncate="256")
|
||||
h_enc.update(key)
|
||||
h_enc.update(bytes([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x01]))
|
||||
h_enc.update(kcom_nonce)
|
||||
k_enc = h_enc.digest()
|
||||
|
||||
h_com = SHA512.new(truncate="256")
|
||||
h_com.update(key)
|
||||
h_com.update(bytes([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x02]))
|
||||
h_com.update(kcom_nonce)
|
||||
k_com_derived = h_com.digest()
|
||||
print('kcom_stored: ' + binascii.hexlify(kcom).decode('utf-8'))
|
||||
print('kcom_derived: ' + binascii.hexlify(k_com_derived).decode('utf-8'))
|
||||
|
||||
cipher = AES_GCM_SIV(k_enc, nonce)
|
||||
pt_data = cipher.decrypt(ct + mac, aad)
|
||||
if k_com_derived != kcom:
|
||||
print("basis failed key commit test")
|
||||
raise Exception(ValueError)
|
||||
return pt_data
|
||||
|
||||
def bytes_to_semverstr(b):
|
||||
maj = int.from_bytes(b[0:2], 'little')
|
||||
min = int.from_bytes(b[2:4], 'little')
|
||||
|
|
@ -69,6 +38,14 @@ def get_key(index, keyrom, length):
|
|||
return ret
|
||||
|
||||
def main():
|
||||
global MBBB_PAGES
|
||||
FSCB_PAGES = 16
|
||||
FSCB_LEN_PAGES = 2
|
||||
KEY_PAGES = 1
|
||||
global PAGE_SIZE
|
||||
global VPAGE_SIZE
|
||||
MAX_DICTS = 16384
|
||||
|
||||
parser = argparse.ArgumentParser(description="Debug Backup Images")
|
||||
parser.add_argument(
|
||||
"-p", "--pin", required=True, help="unlock PIN", type=str,
|
||||
|
|
@ -79,10 +56,24 @@ def main():
|
|||
parser.add_argument(
|
||||
"--basis", required=False, type=str, help="Extra Bases to unlock, as `name:pass`. Each additional basis requires another --basis separator. Note that : is not legal to use in a Basis name.", action="append", nargs="+"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--loglevel", required=False, help="set logging level (INFO/DEBUG/WARNING/ERROR)", type=str, default="INFO",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
numeric_level = getattr(logging, args.loglevel.upper(), None)
|
||||
if not isinstance(numeric_level, int):
|
||||
raise ValueError('Invalid log level: %s' % args.loglevel)
|
||||
logging.basicConfig(level=numeric_level)
|
||||
|
||||
print(args.basis)
|
||||
basis_credentials = {}
|
||||
if args.basis:
|
||||
for pair in args.basis:
|
||||
credpair = pair[0].split(':', 1)
|
||||
if len(credpair) != 2:
|
||||
logging.error("Basis credential pair with name {} has a formatting problem, aborting!".format(credpair[0]))
|
||||
exit(1)
|
||||
basis_credentials[credpair[0]] = credpair[1]
|
||||
|
||||
# insert your mnemonic here. This is the "zero-key" mnemonic.
|
||||
mnemonic = args.backup_key
|
||||
|
|
@ -91,7 +82,7 @@ def main():
|
|||
|
||||
# Like before with automatic language detection
|
||||
key = Bip39MnemonicDecoder().Decode(mnemonic)
|
||||
print("Using backup key: 0x{}".format(key.hex()))
|
||||
logging.debug("Using backup key: 0x{}".format(key.hex()))
|
||||
|
||||
with open("backup.pddb", "rb") as backup_file:
|
||||
SYSTEM_BASIS = b".System"
|
||||
|
|
@ -126,45 +117,46 @@ def main():
|
|||
h_com.update(bytes([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x02]))
|
||||
h_com.update(commit_nonce)
|
||||
k_com_derived = h_com.digest()
|
||||
print('kcom_stored: ' + binascii.hexlify(commit).decode('utf-8'))
|
||||
print('kcom_derived: ' + binascii.hexlify(k_com_derived).decode('utf-8'))
|
||||
logging.debug('kcom_stored: ' + binascii.hexlify(commit).decode('utf-8'))
|
||||
logging.debug('kcom_derived: ' + binascii.hexlify(k_com_derived).decode('utf-8'))
|
||||
|
||||
if k_com_derived != commit:
|
||||
print("Key commitment is incorrect")
|
||||
logging.error("Key commitment is incorrect")
|
||||
exit(1)
|
||||
|
||||
cipher = AES_GCM_SIV(k_enc, nonce)
|
||||
try:
|
||||
pt_data = cipher.decrypt(ct + mac, AAD)
|
||||
except:
|
||||
print("Ciphertext did not pass AES-GCM-SIV validation")
|
||||
logging.error("Ciphertext did not pass AES-GCM-SIV validation")
|
||||
exit(1)
|
||||
|
||||
i = 0
|
||||
print("Backup version: 0x{:08x}".format(int.from_bytes(pt_data[i:i+4], 'little')))
|
||||
logging.info("Backup version: 0x{:08x}".format(int.from_bytes(pt_data[i:i+4], 'little')))
|
||||
i += 4
|
||||
print("Xous version: {}".format(bytes_to_semverstr(pt_data[i:i+16])))
|
||||
logging.info("Xous version: {}".format(bytes_to_semverstr(pt_data[i:i+16])))
|
||||
i += 16
|
||||
print("SOC version: {}".format(bytes_to_semverstr(pt_data[i:i+16])))
|
||||
logging.info("SOC version: {}".format(bytes_to_semverstr(pt_data[i:i+16])))
|
||||
i += 16
|
||||
print("EC version: {}".format(bytes_to_semverstr(pt_data[i:i+16])))
|
||||
logging.info("EC version: {}".format(bytes_to_semverstr(pt_data[i:i+16])))
|
||||
i += 16
|
||||
print("WF200 version: {}".format(bytes_to_semverstr(pt_data[i:i+16])))
|
||||
logging.info("WF200 version: {}".format(bytes_to_semverstr(pt_data[i:i+16])))
|
||||
i += 16
|
||||
i += 4 # padding because align=8
|
||||
ts = int.from_bytes(pt_data[i:i+8], 'little') / 1000
|
||||
print("Timestamp: {} / {}".format(ts, datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')))
|
||||
logging.info("Timestamp: {} / {}".format(ts, datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')))
|
||||
i += 8
|
||||
print("Language code: {}".format(int.from_bytes(pt_data[i:i+4], 'little')))
|
||||
logging.info("Language code: {}".format(int.from_bytes(pt_data[i:i+4], 'little')))
|
||||
i += 4
|
||||
print("Keyboard layout code: {}".format(int.from_bytes(pt_data[i:i+4], 'little')))
|
||||
logging.info("Keyboard layout code: {}".format(int.from_bytes(pt_data[i:i+4], 'little')))
|
||||
i += 4
|
||||
print("DNA: 0x{:x}".format(int.from_bytes(pt_data[i:i+8], 'little')))
|
||||
logging.info("DNA: 0x{:x}".format(int.from_bytes(pt_data[i:i+8], 'little')))
|
||||
dna = pt_data[i:i+8]
|
||||
dna_int = int.from_bytes(dna, 'little')
|
||||
i += 8
|
||||
i += 48 # reserved
|
||||
op = int.from_bytes(pt_data[i:i+4], 'little')
|
||||
print("Opcode: {}".format(op))
|
||||
logging.info("Stored Backup Opcode: {}".format(op))
|
||||
i += 8 # padding because align=8
|
||||
|
||||
keyrom = pt_data[i:i+1024]
|
||||
|
|
@ -179,13 +171,13 @@ def main():
|
|||
for b in bytes(boot_pw.encode('utf-8')):
|
||||
boot_pw_array[pw_len] = b
|
||||
pw_len += 1
|
||||
pw_len += 1
|
||||
pw_len += 1 # null terminate, so even the null password is one character long
|
||||
bcrypter = bcrypt.BCrypt()
|
||||
print("{}".format(boot_pw_array[:pw_len]))
|
||||
print("user_key_enc: {}".format(list(user_key_enc)))
|
||||
print("salt: {}".format(list(pepper)))
|
||||
# logging.debug("{}".format(boot_pw_array[:pw_len]))
|
||||
logging.debug("user_key_enc: {}".format(list(user_key_enc)))
|
||||
logging.debug("salt: {}".format(list(pepper)))
|
||||
hashed_pw = bcrypter.crypt_raw(boot_pw_array[:pw_len], pepper, 7)
|
||||
print("hashed_pw: {}".format(list(hashed_pw)))
|
||||
logging.debug("hashed_pw: {}".format(list(hashed_pw)))
|
||||
hasher = SHA512.new(truncate="256")
|
||||
hasher.update(hashed_pw)
|
||||
user_pw = hasher.digest()
|
||||
|
|
@ -193,47 +185,146 @@ def main():
|
|||
user_key = []
|
||||
for (a, b) in zip(user_key_enc, user_pw):
|
||||
user_key += [a ^ b]
|
||||
print("user_key: {}".format(user_key))
|
||||
logging.debug("user_key: {}".format(user_key))
|
||||
|
||||
rollback_limit = 255 - int.from_bytes(keyrom[254 * 4 : 254 * 4 + 4], 'little')
|
||||
print("rollback limit: {}".format(rollback_limit))
|
||||
logging.info("rollback limit: {}".format(rollback_limit))
|
||||
for i in range(rollback_limit):
|
||||
hasher = SHA512.new(truncate="256")
|
||||
hasher.update(bytes(user_key))
|
||||
user_key = hasher.digest()
|
||||
|
||||
print("hashed_key: {}".format(list(user_key)))
|
||||
logging.debug("hashed_key: {}".format(list(user_key)))
|
||||
|
||||
pddb = backup[4096:]
|
||||
PDDB_A_LEN = 0x620_0000
|
||||
pt_len = (PDDB_A_LEN // 0x1000) * 16
|
||||
pt = pddb[pt_len]
|
||||
static_crypto_data = pddb[pt_len:pt_len + 0x1000]
|
||||
scd_ver = int.from_bytes(static_crypto_data[:4], 'little')
|
||||
if scd_ver != 2:
|
||||
print("Static crypto data has wrong version {}", 2)
|
||||
logging.error("Static crypto data has wrong version {}", 2)
|
||||
exit(1)
|
||||
|
||||
wrapped_key_pt = static_crypto_data[4:4+40]
|
||||
wrapped_key_data = static_crypto_data[4+40:4+40+40]
|
||||
pddb_salt = static_crypto_data[4+40+40:]
|
||||
|
||||
print(len(wrapped_key_pt))
|
||||
print("decap input: {}".format(list(wrapped_key_pt)))
|
||||
|
||||
# inverse test
|
||||
to_wrap = [5, 34, 117, 252, 137, 19, 29, 131, 64, 99, 195, 103, 228, 67, 165, 53, 179, 207, 169, 87, 232, 122, 52, 219, 202, 54, 210, 9, 233, 198, 38, 34]
|
||||
wrap_key = [113, 227, 211, 48, 222, 183, 93, 94, 212, 86, 127, 203, 43, 125, 76, 117, 10, 249, 161, 96, 84, 206, 90, 170, 26, 207, 205, 203, 122, 177, 77, 171]
|
||||
wrapped_key = aes_key_wrap_with_padding(bytes(wrap_key), bytes(to_wrap))
|
||||
print("wrapped_key: {}".format(list(wrapped_key)))
|
||||
|
||||
# extract the .System key
|
||||
key_pt = aes_key_unwrap_with_padding(bytes(user_key), bytes(wrapped_key_pt))
|
||||
key_data = aes_key_unwrap_with_padding(bytes(user_key), bytes(wrapped_key_data))
|
||||
|
||||
print("key_pt {}".format(key_pt))
|
||||
print("key_data {}".format(key_data))
|
||||
logging.debug("key_pt {}".format(key_pt))
|
||||
logging.debug("key_data {}".format(key_data))
|
||||
keys = {}
|
||||
keys['.System'] = [key_pt, key_data]
|
||||
|
||||
|
||||
for name, pw in basis_credentials.items():
|
||||
bname_copy = [0]*64
|
||||
plaintext_pw = [0]*73
|
||||
i = 0
|
||||
for c in list(name.encode('utf-8')):
|
||||
bname_copy[i] = c
|
||||
i += 1
|
||||
pw_len = 0
|
||||
for c in list(pw.encode('utf-8')):
|
||||
plaintext_pw[pw_len] = c
|
||||
pw_len += 1
|
||||
pw_len += 1 # null terminate
|
||||
# print(bname_copy)
|
||||
# print(plaintext_pw)
|
||||
hasher = SHA512.new(truncate="256")
|
||||
hasher.update(pddb_salt[32:])
|
||||
hasher.update(bytes(bname_copy))
|
||||
hasher.update(bytes(plaintext_pw))
|
||||
derived_salt = hasher.digest()
|
||||
|
||||
bcrypter = bcrypt.BCrypt()
|
||||
hashed_pw = bcrypter.crypt_raw(plaintext_pw[:pw_len], derived_salt[:16], 7)
|
||||
hkdf = HKDF(algorithm=hashes.SHA256(), length=32, salt=pddb_salt[:32], info=b"pddb page table key")
|
||||
pt_key = hkdf.derive(hashed_pw)
|
||||
hkdf = HKDF(algorithm=hashes.SHA256(), length=32, salt=pddb_salt[:32], info=b"pddb data key")
|
||||
data_key = hkdf.derive(hashed_pw)
|
||||
keys[name] = [pt_key, data_key]
|
||||
|
||||
# data_aad = SYSTEM_BASIS + PDDB_VERSION + dna
|
||||
|
||||
# now that we have the credentials, extract the baseline image
|
||||
pddb_len = len(pddb)
|
||||
pddb_size_pages = pddb_len // PAGE_SIZE
|
||||
logging.info("Database size: 0x{:x}".format(pddb_len))
|
||||
|
||||
mbbb_offset = pddb_size_pages * Pte.PTE_LEN + PAGE_SIZE * KEY_PAGES
|
||||
if mbbb_offset & (PAGE_SIZE - 1) != 0:
|
||||
mbbb_offset = (mbbb_offset + PAGE_SIZE) & 0xFFFF_F000 # round up to nearest page
|
||||
logging.info("MBBB: 0x{:x}".format(mbbb_offset))
|
||||
|
||||
img_index = 0
|
||||
tables = decode_pagetable(pddb, pddb_size_pages, keys, pddb[mbbb_offset:mbbb_offset + MBBB_PAGES * PAGE_SIZE])
|
||||
img_index += pddb_size_pages * Pte.PTE_LEN
|
||||
if img_index & (PAGE_SIZE - 1) != 0:
|
||||
img_index = (img_index + PAGE_SIZE) & 0xFFFF_F000
|
||||
|
||||
rawkeys = pddb[img_index : img_index + PAGE_SIZE * KEY_PAGES]
|
||||
logging.debug("Keys: 0x{:x}".format(img_index))
|
||||
img_index += PAGE_SIZE * KEY_PAGES
|
||||
|
||||
mbbb = pddb[img_index : img_index + PAGE_SIZE * MBBB_PAGES]
|
||||
logging.debug("MBBB check: 0x{:x}".format(img_index))
|
||||
img_index += PAGE_SIZE * MBBB_PAGES
|
||||
|
||||
fscb = decode_fscb(pddb[img_index: img_index + PAGE_SIZE * FSCB_PAGES], keys, FSCB_LEN_PAGES=FSCB_LEN_PAGES, dna=dna_int)
|
||||
logging.debug("FSCB: 0x{:x}".format(img_index))
|
||||
img_index += PAGE_SIZE * FSCB_PAGES
|
||||
|
||||
logging.debug("Data: 0x{:x}".format(img_index))
|
||||
data = pddb[img_index:]
|
||||
|
||||
for name, key in keys.items():
|
||||
if name in tables:
|
||||
logging.info("Basis '{}', key_pt: {}, key_data: {}".format(name, key[0].hex(), key[1].hex()))
|
||||
v2p_table = tables[name][0]
|
||||
p2v_table = tables[name][1]
|
||||
# v2p_table[0xfe0fe0] = 0x1200* 0x100
|
||||
|
||||
basis_data = bytearray()
|
||||
pp_start = v2p_table[VPAGE_SIZE]
|
||||
# print("pp_start: {:x}".format(pp_start))
|
||||
pp_data = data[pp_start:pp_start + PAGE_SIZE]
|
||||
try:
|
||||
pt_data = keycommit_decrypt(key[1], basis_aad(name, dna=dna_int), pp_data)
|
||||
basis_data.extend(bytearray(pt_data))
|
||||
logging.debug("decrypted vpage @ {:x} ppage @ {:x}".format(VPAGE_SIZE, v2p_table[VPAGE_SIZE]))
|
||||
# print([hex(x) for x in basis_data[:256]])
|
||||
basis = Basis(basis_data)
|
||||
logging.info(basis.as_str())
|
||||
|
||||
basis_dicts = {}
|
||||
dicts_found = 0
|
||||
dict_index = 0
|
||||
while dict_index < MAX_DICTS and dicts_found < basis.num_dicts:
|
||||
bdict = BasisDicts(dict_index, v2p_table, data, key[1], name, dna=dna_int)
|
||||
if bdict.valid:
|
||||
basis_dicts[bdict.name] = bdict
|
||||
dicts_found += 1
|
||||
dict_index += 1
|
||||
if dicts_found != basis.num_dicts:
|
||||
logging.error("Expected {} dictionaries, only found {}; searched {}".format(basis.num_dicts, dicts_found, dict_index))
|
||||
|
||||
logging.info(" Dictionaries: ")
|
||||
for bdict in basis_dicts.values():
|
||||
logging.info(bdict.as_str())
|
||||
|
||||
for bdict in basis_dicts.values():
|
||||
logging.info("==================================================================")
|
||||
logging.info("Dict {}".format(bdict.as_str()))
|
||||
|
||||
except ValueError:
|
||||
logging.error("couldn't decrypt basis root vpage @ {:x} ppage @ {:x}".format(VPAGE_SIZE, v2p_table[VPAGE_SIZE]))
|
||||
|
||||
|
||||
|
||||
|
||||
data_aad = SYSTEM_BASIS + PDDB_VERSION + dna
|
||||
|
||||
# INFO:root_keys::implementation: pw: a, salt: [187, 174, 50, 20, 242, 60, 232, 34, 102, 172, 228, 51, 131, 21, 250, 2] (services\root-keys\src\implementation.rs:698)
|
||||
# INFO:root_keys::implementation: hashed_pw: [115, 198, 244, 53, 109, 213, 137, 82, 182, 170, 10, 46, 17, 117, 175, 42, 179, 60, 9, 238, 104, 66, 199, 143] (services\root-keys\src\implementation.rs:700)
|
||||
|
|
|
|||
Loading…
Reference in a new issue