diff --git a/README.md b/README.md index ae2ad6d..d694704 100644 --- a/README.md +++ b/README.md @@ -259,7 +259,7 @@ Run samples only in isolated, disposable environments, and follow your organizat - [ripcord](https://github.com/DavidClawson/ripcord) - Ghidra-based firmware-analysis pipeline producing queryable Parquet tables for DuckDB; requires Ghidra 11.2+ with PyGhidra and Python 3.11+, with optional simulation tooling. - [sigrok](https://github.com/sigrokproject) - Signal analysis tools and protocol decoders for logic analyzers and related hardware. - [UEFITool](https://github.com/LongSoft/UEFITool) - Parses UEFI firmware structures and extracts or replaces modules. -- [unblob](https://github.com/onekey-sec/unblob) - Extracts nested archives, compressed data, and filesystem images from firmware and other files. +- [unblob](https://github.com/onekey-sec/unblob) - Extracts nested archives, compressed data, and filesystem images from firmware and other files; a ZIP handler needs the chunk start at the local header, not merely an end-of-central-directory record later in the file. - [usb-rea](https://github.com/emirmrkaljevic/usb-rea) - Infers USB protocol fields, counters, checksums, and multi-packet commands from captures of labelled actions, with frame-number evidence, and generates pyusb driver code and Wireshark Lua dissectors; requires Python 3.10+, with usbmon plus dumpcap/tshark on Linux or USBPcap on Windows for capture, and remains alpha without physical-device validation. ## File formats diff --git a/examples/resource-intake/README.md b/examples/resource-intake/README.md index 713e0e7..d473946 100644 --- a/examples/resource-intake/README.md +++ b/examples/resource-intake/README.md @@ -1,6 +1,6 @@ # Bounded resource examples -These self-authored fixtures demonstrate Construct, Scapy, YARA-X, Vineflower, CFR, Malduck, hermes-dec, MemProcFS, boofuzz, Keystone, esptool, and Z3 in CI, plus a local PyLingual check. They are small selection examples, not upstream test suites or evidence of full platform/version support. +These self-authored fixtures demonstrate Construct, Scapy, YARA-X, Vineflower, CFR, Malduck, hermes-dec, MemProcFS, boofuzz, Keystone, esptool, Z3, and unblob in CI, plus a local PyLingual check. They are small selection examples, not upstream test suites or evidence of full platform/version support. Use Python 3.11 with `venv`/`pip` support, JDK 17, Node.js, and an Internet connection for the pinned packages, JAR, and Hermes compilers. From the repository root: @@ -44,6 +44,7 @@ $resourcePython = Join-Path $resourceExamplesDir "venv\Scripts\python.exe" | Keystone 0.9.2 (with Capstone 5.0.9) | x86-64 `push rbp; mov rbp, rsp; mov eax, dword ptr [rbx + rcx*4 + 8]; xor eax, eax; pop rbp; ret` and ARM64 `mov x0, #1; add x1, x2, x3; ldr x0, [x1, #8]; b #0x1010; ret` at base `0x1000` assemble to bytes encoded by hand from the Intel SDM and Arm ARM (`554889e58b448b0831c05dc3`, `200080d24100038b200440f901000014c0035fd6`); Capstone disassembles them back to the same text, which reassembles to the same bytes; `b #0x1010` at `0x2000` becomes the backward branch `04fcff17` | Two short sequences on two ISAs, Linux x86-64 only; see the limits below | | esptool 5.6.0 | A 128-byte ESP32 application image built by hand from the [5.6.0 documented layout](https://raw.githubusercontent.com/espressif/esptool/e970488de2c8f1906ca9b08de681b3f9dc58b2d5/docs/en/advanced-topics/firmware-image-format.rst): entry `0x40080400`, a 16-byte DRAM segment at `0x3ffb0000` and a 32-byte IRAM segment at `0x40080400`, checksum `0xf3` (`0xEF` XOR the segment data) and an appended SHA-256 `f8d0bbc1…c9cb`; `image-info` reports the header, both segments at file offsets `0x18` and `0x30`, and a valid checksum and hash, with no device connected. A synthetic variant with chip ID 9 is detected as ESP32-S3 (header identification only); flipping one data bit gives checksum `0xf2` and hash `b1f33ee1…4147`, both reported as invalid | Offline `image-info` on one ESP32 application layout; no serial port, flashing, or bootloader images; see the limits below | | Z3 (z3-solver 5.3.1.0) | A synthetic 16-bit serial check written as a decompiler would show it (`((x * 0x9E37) ^ (x >> 5)) + rotl(x, 3)` with unsigned arithmetic) and target `0x002a`; brute-forcing all 65,536 inputs in plain Python finds exactly `0x1bb9`, `0x9cff`, and `0xb62c`, and Z3's bit-vector solver, asked again with each found serial blocked, returns the same three and then `unsat` | Python API, one 16-bit constraint; no SMT-LIB files, tactics, timeouts, or `unknown` results; see the limits below | +| unblob 26.6.4 | A ZIP written by the stdlib, holding `probe.txt` / `UNBLOB-PROBE-1`, placed 40 bytes into a larger blob; `ZIPHandler.calculate_chunk` from that offset returns exactly the ZIP bytes | One stored ZIP; no nested firmware or external extractors; see the limit below | The branch oracle for `Probe.java` is: @@ -87,6 +88,10 @@ The Z3 check also records two limits, both described in the [5.3.1 Python API](h - `>>` on a bit-vector is an arithmetic (signed) shift; C's unsigned `>>` is `LShR`. Written with `>>`, the same check returns only `0x1bb9`, silently missing the two serials whose top bit is set, without an error. The two shifts agree whenever the top bit is clear. - `model.eval(y, model_completion=True)` gives an unconstrained variable the value 0 and adds it to the model, so afterwards `model[y]` is 0 and `model.decls()` lists `y` as if the solver had chosen it. +The unblob check also records one limit: + +- `ZIPHandler.calculate_chunk` rejects the same blob when the start offset is 0, before the local file header, even though the end-of-central-directory record is present. The central-directory offset is relative to the real start of the ZIP. + Without `-dtb`, MemProcFS rejects a raw image that contains no identifiable operating system (`Unable to locate valid DTB`). The Linux `memprocfs` wheel does not include `leechcore.so`; the runner uses the one from `leechcorepyc` by starting the probe with `LD_LIBRARY_PATH` pointing at its directory. The [dated local result](verified.json) records host/tool versions, input hashes, artifact digests, and the tested scope; it was produced in a Linux x86_64 container. CI repeats the examples and uploads current results through the [resource-example workflow](../../.github/workflows/resource-examples.yml). The current job uses Linux x86-64, Python 3.11, Java 17, Node.js 22, and `libusb-1.0`; it makes no Windows, macOS, hardware, or other-version claim. diff --git a/examples/resource-intake/requirements.txt b/examples/resource-intake/requirements.txt index 7c33a88..b537416 100644 --- a/examples/resource-intake/requirements.txt +++ b/examples/resource-intake/requirements.txt @@ -10,3 +10,4 @@ keystone-engine==0.9.2; sys_platform == "linux" and platform_machine == "x86_64" capstone==5.0.9; sys_platform == "linux" and platform_machine == "x86_64" esptool==5.6.0 z3-solver==5.3.1.0 +unblob==26.6.4 diff --git a/examples/resource-intake/verify.py b/examples/resource-intake/verify.py index f5af22f..5b6030d 100644 --- a/examples/resource-intake/verify.py +++ b/examples/resource-intake/verify.py @@ -1,4 +1,4 @@ -"""Reproduce twelve narrow resource examples with independent expected outputs.""" +"""Reproduce thirteen narrow resource examples with independent expected outputs.""" import argparse import ctypes.util from datetime import datetime, timezone @@ -314,7 +314,7 @@ def keystone_host(): def required_packages(): - packages = {"construct": "2.10.70", "scapy": "2.8.0", "yara-x": "1.21.0", "hermes-dec": "0.1.8", "malduck": "4.4.1", "boofuzz": "0.4.2", "esptool": "5.6.0", "z3-solver": "5.3.1.0"} + packages = {"construct": "2.10.70", "scapy": "2.8.0", "yara-x": "1.21.0", "hermes-dec": "0.1.8", "malduck": "4.4.1", "boofuzz": "0.4.2", "esptool": "5.6.0", "z3-solver": "5.3.1.0", "unblob": "26.6.4"} if memprocfs_host(): packages.update(MEMPROCFS_PACKAGES) if keystone_host(): @@ -530,6 +530,48 @@ def verify_z3(): return {"target": hex(SERIAL_TARGET), "serials": [hex(v) for v in serials], "arithmetic_shift_serials": [hex(v) for v in naive]} +def verify_unblob(work): + import io + import zipfile + from pathlib import Path + + from unblob.file_utils import File, InvalidInputFormat + from unblob.handlers.archive.zip import ZIPHandler + + buffer = io.BytesIO() + with zipfile.ZipFile(buffer, "w") as archive: + archive.writestr("probe.txt", "UNBLOB-PROBE-1") + zip_bytes = buffer.getvalue() + blob = b"\x00" * 40 + zip_bytes + b"\xff" * 8 + path = work / "padded.zip.bin" + path.write_bytes(blob) + handler = ZIPHandler() + with File.from_path(Path(path)) as blob_file: + chunk = handler.calculate_chunk(blob_file, 40) + assert chunk.start_offset == 40 + assert blob[chunk.start_offset:chunk.end_offset] == zip_bytes + + empty = work / "no-eocd.bin" + empty.write_bytes(b"\x00" * 64) + try: + with File.from_path(Path(empty)) as blob_file: + handler.calculate_chunk(blob_file, 0) + except InvalidInputFormat: + pass + else: + raise AssertionError("bytes without an EOCD were accepted") + + # EOCD's offset_of_cd is relative to the real start. Starting earlier rejects the same file. + try: + with File.from_path(Path(path)) as blob_file: + handler.calculate_chunk(blob_file, 0) + except InvalidInputFormat: + pass + else: + raise AssertionError("a ZIP was accepted from before its local header") + return {"zip_sha256": hashlib.sha256(zip_bytes).hexdigest(), "start": 40, "end": chunk.end_offset} + + def verify(work, jar, cfr_jar=None): for package, expected in required_packages().items(): try: @@ -617,6 +659,7 @@ def verify(work, jar, cfr_jar=None): boofuzz_summary = verify_boofuzz(work) esp_digest = verify_esptool(work) z3_summary = verify_z3() + unblob_summary = verify_unblob(work) keystone_result = {"name": "Keystone", "version": "0.9.2", "result": "skipped", "scope": "This check runs on Linux x86-64 only"} if keystone_host(): verify_keystone() @@ -643,6 +686,7 @@ def verify(work, jar, cfr_jar=None): keystone_result, {"name": "esptool", "version": "5.6.0", "result": "passed", "scope": "Offline image-info on a synthetic ESP32 image: header, segments, checksum and SHA-256; chip ID 9 is auto-detected as ESP32-S3; checksum/hash mismatches exit 0, a truncated segment exits 2, and --chip overrides the header chip ID without a warning while the checksum and hash stay valid"}, {"name": "Z3", "version": "5.3.1.0", "result": "passed", "scope": "Synthetic 16-bit serial check: blocking-clause enumeration finds exactly the three serials a brute-force search finds; an arithmetic >> in place of LShR finds only the one with the top bit clear; model_completion adds a default value to the model"}, + {"name": "unblob", "version": "26.6.4", "result": "passed", "scope": "ZIPHandler on a stdlib ZIP after 40 bytes of padding: the chunk is exactly the ZIP; no EOCD raises InvalidInputFormat; the same file is rejected when the start offset is before the local header"}, ], "inputs": [{"path": name, "sha256": sha256(SOURCE / name)} for name in ("BranchDemo.java", "Probe.java", "Shapes.java", "SealedProbe.java", "hbc_fixture.js", "malduck_modules/duckcfg/__init__.py", "malduck_modules/duckcfg/duckcfg.yar", "memprocfs_probe.py", "verify.py", "requirements.txt")], "golden_hex": GOLDEN.hex(), @@ -658,6 +702,7 @@ def verify(work, jar, cfr_jar=None): "keystone": {"base": hex(ASM_BASE), "x64": {"source": X64_SOURCE, "bytes": X64_BYTES}, "arm64": {"source": A64_SOURCE, "bytes": A64_BYTES, "backward_branch_at_0x2000": A64_BACKWARD_BRANCH}}, "z3": z3_summary, + "unblob": unblob_summary, "esptool": {"image_sha256": esp_digest, "entry": hex(ESP_ENTRY), "checksum": hex(ESP_CHECKSUM), "validation_hash": ESP_DIGEST, "corrupt_checksum": hex(ESP_CORRUPT_CHECKSUM), "corrupt_validation_hash": ESP_CORRUPT_DIGEST}, }