Firmware analysis¶
Once you have a flash image, hwntools.firmware takes it from bytes to findings: entropy and
signature scans, carving, a one-call multi-backend unpack(), and analyze(), which runs the
Reflex audit tools on everything it unpacked. The same is available as
hwn fw analyze.
$ hwn fw analyze firmware.bin
[*] Unpacking firmware.bin with the built-in extractor
[+] Unpacked with python: 13 files, 1 rootfs, 1 kernel(s), 1 dtb(s), 1 env candidate(s)
[+] Analysis done: critical 2, high 1, medium 3, low 3
Image: /work/firmware.bin
Unpacked: /work/firmware.bin.extracted (backend python, 13 files)
Found: 1 rootfs, 1 kernel(s), 1 DTB/FIT, 1 U-Boot env, 1 kernel config(s)
rootfs: 00001348.uimage-ramdisk/tar-root
Findings: critical 2, high 1, medium 3, low 3
CRITICAL BL-CMD-002 init replaced with a shell 00000040.uboot-env.bin [bootargs]
CRITICAL CRED-001 Account without password 00001348.uimage-ramdisk/tar-root/etc/shadow:1
HIGH SVC-006 Serial console shell without authentication 00001348.uimage-ramdisk/tar-root/etc/inittab:1
MEDIUM BL-CMD-001 Kernel console on a serial UART 00000040.uboot-env.bin [bootargs]
MEDIUM BL-ENV-001 Autoboot can be interrupted without a stop key 00000040.uboot-env.bin [bootdelay]
MEDIUM COMP-001 Component older than baseline 00001348.uimage-ramdisk/tar-root/bin/busybox
LOW BL-ENV-003 U-Boot console not silenced 00000040.uboot-env.bin [silent]
LOW BL-ENV-005 No runtime environment access control (.flags) 00000040.uboot-env.bin [.flags]
LOW BL-ENV-007 Kernel booted without signature verification 00000040.uboot-env.bin [bootcmd]
info: U-Boot env (single, 0x1000 bytes) found at 0x40
warning: cpio: skipped unsafe member: path traversal in member name '../cpio-escape'
This is a small synthetic image from the test suite (tests/fwimages.py), unpacked by the
built-in extractor and audited with fwsift and bootlint installed. The [*]/[+] progress
lines go to stderr, the report to stdout.
Look before you unpack¶
from hwntools import firmware
data = open("firmware.bin", "rb").read()
for m in firmware.find_signatures(data):
print(hex(m.offset), m.name, m.description)
print(firmware.entropy_plot(data)) # one-line sparkline
for block in firmware.entropy_map(data, 0x10000):
print(hex(block.offset), round(block.entropy, 2)) # 0.0 .. 8.0 bits per byte
kernel = firmware.carve(data, 0x40000, 0x200000, out="kernel.uimage")
find_signatures() knows uImage, FIT/DTB, SquashFS (both byte orders), gzip, xz, LZMA, ELF,
Zstandard, bzip2, LZ4, cpio (newc), CramFS, JFFS2, UBI, UBIFS, ZIP, Android boot images and PEM
blocks. Signatures with a header parser (uImage, FDT, SquashFS, ELF, gzip, LZMA) are only
reported when the header looks sane, which keeps false positives for short magic values down.
Use alignment= to only accept offsets that are multiples of, say, the flash erase size.
High entropy (close to 8) means compressed or encrypted data; long runs near 0 are usually
erased flash (0xff) or padding.
The CLI equivalents are hwn sigs, hwn entropy and hwn hexdump:
$ hwn sigs firmware.bin --align 4
$ hwn entropy firmware.bin --plot
$ hwn hexdump firmware.bin -s 0x40 -n 0x100
Unpack¶
res = firmware.unpack("firmware.bin") # into firmware.bin.extracted/
print(res.backend) # unblob | unblob-docker | binwalk | python
print(res.rootfs, res.kernels, res.dtbs, res.env_candidates, res.kconfigs)
print(res.tree())
The output directory (default <image>.extracted next to the image) must be new or empty.
To redo an extraction, pass force=True (--force on the command line): hwntools then empties
the directory first, but only if an earlier unpack() created it (it leaves a marker,
.hwntools/unpack.json). Any other non-empty directory is refused even with force=True, so a
mistyped -o cannot wipe unrelated files.
After extraction the tree is classified: root filesystems (directories with etc/ and bin/ or
sbin/), kernels, device trees and FIT images, U-Boot environment candidates and kernel
configs. res.warnings lists problems (what could not be extracted, which tools to install);
res.notes lists plain information such as where a U-Boot environment was found. The CLI
prints them as warning: and info: lines.
Backends¶
With backend="auto" (the default) hwntools tries, in order:
- native
unblob, if it is onPATH. If it reports missing extractors or failed chunks for filesystem formats (for example SquashFS withoutsasquatchon macOS) and the Docker image is available, the Docker image is tried next. unblobin Docker (ghcr.io/onekey-sec/unblob:latest, which hwntools never pulls on its own; rundocker pull ghcr.io/onekey-sec/unblob:latestonce). The container runs with--network none, the input's directory mounted read-only and only the output directory writable.binwalk(the v2 and v3 command lines are both detected).- the built-in pure-Python extractor: uImage (with header and data CRC checks),
gzip/xz/lzma/bzip2, FIT/DTB carving, kernel
IKCFGconfigs, CRC-verified U-Boot environments, tar and cpio, and SquashFS/UBI/JFFS2 throughunsquashfs,ubireader_extract_filesandjeffersonwhen they are installed.
Pick one explicitly with backend="unblob", "docker", "binwalk" or "python" (an explicit
backend that is missing raises ToolNotFoundError), or set HWNTOOLS_UNBLOB_DOCKER=1 to try
Docker first. After an external extractor, hwntools post-scans the files it left unidentified
for U-Boot environments, uImage kernels, DTBs and embedded kernel configs.
Safe by default¶
Firmware images are untrusted input. The built-in extractor never writes outside the output
directory (no .., no absolute paths, no writing through symlinks) and bounds every
decompression against decompression bombs: 512 MiB per stream, a 1000:1 ratio, 2 GiB and
200,000 files in total, and a nesting depth of 6. Unsafe archive members are skipped with a
warning:
Findings with fwsift and bootlint¶
analyze() unpacks the image and then runs:
- fwsift on every root filesystem: credentials, keys, services started at boot, components (SBOM) and ELF hardening;
- bootlint on device trees and FIT images, U-Boot environments and kernel configs.
report = firmware.analyze("firmware.bin")
print(report.report()) # the text summary shown at the top of this page
print(report.counts()) # {'critical': 2, 'high': 1, 'medium': 3, 'low': 3, 'info': 0}
for f in report.findings: # most severe first
print(f.severity, f.rule_id, f.title, f.path, f.line)
data = report.to_dict() # JSON-ready: unpack info, summary, findings, components
Every finding is a normalised Finding (tool, rule_id, severity, title, message,
path, line, offset, location, evidence, plus raw with the tool's original JSON).
Severities are info < low < medium < high < critical.
Both tools are separate programs (see
Installation). When one is missing,
analyze() still returns the unpack result and adds a warning:
Findings: none
warning: fwsift is not installed. Install it with `cargo install --git https://github.com/reflex-hs/fwsift` (needs Rust: https://rustup.rs) or set HWNTOOLS_FWSIFT to its path.
osv=True (--osv on the command line) lets fwsift query OSV for known
vulnerabilities in the components it found. This is the only option that uses the network.
In CI¶
$ hwn fw analyze firmware.bin --json > triage.json
$ hwn fw analyze firmware.bin --fail-on high # exit 1 if any high or critical finding
The tools on their own¶
hwntools.tools wraps the four Reflex Rust CLIs directly. Each wrapper
runs the tool with --json and parses the result; "findings above threshold" exit codes are not
errors, real failures raise ToolError, and timeouts come from context.tool_timeout (10
minutes by default).
from hwntools.tools import fwsift, bootlint, baudhound, mcpwarden
rep = fwsift.scan("rootfs/") # or a raw image for signature triage
for f in rep.at_least("high"):
print(f.severity, f.rule_id, f.path, f.line, f.message)
env = bootlint.audit("uboot-env.bin", type="env-bin")
uart = baudhound.analyze("boot.sr").best # most likely target TX channel
mcp = mcpwarden.scan(paths=["device-rootfs/etc/mcp.json"])
mcpwarden.scan() audits MCP server configurations and tool metadata on edge-AI devices. It
refuses to run without explicit inputs, so it never scans your own workstation's MCP configs by
accident; pass discover=True if that is what you want.
A complete script is in
examples/firmware_triage.py.