From d9f6b141ba95f0d407f39189e902380d5dad11c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Fri, 1 Dec 2023 16:42:55 +0100 Subject: [PATCH] Do not crash after KVS unpack failure --- bk7231tools/__main__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/bk7231tools/__main__.py b/bk7231tools/__main__.py index 78ce946..3202acc 100644 --- a/bk7231tools/__main__.py +++ b/bk7231tools/__main__.py @@ -303,7 +303,12 @@ def dissect_dump_file(args): break pos, storage_data = result aes = AES.new(key=KEY_MASTER, mode=AES.MODE_ECB) - kvs = KVStorage.unpack(storage_data, aes=aes) + try: + kvs = KVStorage.unpack(storage_data, aes=aes) + except Exception: + print("!!! Couldn't unpack KVStorage, perhaps dump file is invalid?") + traceback.print_exc() + break keys = list(kvs.indexes.keys()) print(f"\t{pos:#06x}: {kvs.length // 1024:d} KiB - {len(keys)} keys") print("\n".join(f"\t- '{key}'" for key in keys)) @@ -311,7 +316,12 @@ def dissect_dump_file(args): dumpfile_name = Path(dumpfile).stem out_name = os.path.join(output_directory, f"{dumpfile_name}_storage.json") - kvs_data = kvs.read_all_values_parsed() + try: + kvs_data = kvs.read_all_values_parsed() + except Exception: + print("!!! Couldn't read parsed KVS values") + traceback.print_exc() + break with open(out_name, "w") as f: print(f"\t\textracted all keys to {out_name}") json.dump(kvs_data, f, indent="\t")