Skip to content

Firmware

See the firmware analysis guide for an introduction.

firmware

Firmware helpers: entropy, signature scanning, carving and unpacking.

Example

from hwntools import firmware
result = firmware.analyze("flash.bin")     # unpack + fwsift + bootlint
print(result.report())

for m in firmware.find_signatures(open("flash.bin", "rb").read()):
    print(hex(m.offset), m.description)

AnalysisResult dataclass

AnalysisResult(unpack: UnpackResult, fwsift: list[FwsiftReport] = list(), bootlint: list[BootlintReport] = list(), findings: list[Finding] = list(), warnings: list[str] = list(), notes: list[str] = list())

Result of analyze: the unpack result, the tool reports and the merged findings.

counts

counts() -> dict[str, int]

Number of findings per severity, most severe first (all five keys present).

to_dict

to_dict() -> dict[str, Any]

JSON-ready summary: unpack info, severity counts, findings, components, warnings.

report

report(limit: int = 40) -> str

Concise human-readable summary.

UnpackResult dataclass

UnpackResult(image: Path, outdir: Path, backend: str, files: list[Path] = list(), rootfs: list[Path] = list(), kernels: list[Path] = list(), dtbs: list[Path] = list(), env_candidates: list[Path] = list(), kconfigs: list[Path] = list(), warnings: list[str] = list(), notes: list[str] = list(), report: Any = None)

What unpack produced.

tree

tree(max_entries: int = 200) -> str

Indented listing of the extracted tree: each entry sits under its parent directory, sub-directories before files, both sorted by name.

to_dict

to_dict() -> dict[str, Any]

JSON-ready summary with paths relative to outdir.

EntropyBlock dataclass

EntropyBlock(offset: int, size: int, entropy: float)

Shannon entropy (0.0 to 8.0 bits per byte) of the size bytes at offset.

Signature dataclass

Signature(name: str, magic: bytes, parse: Parser | None = None, description: str = '')

A magic value to search for, with an optional header parser that validates a hit.

SignatureMatch dataclass

SignatureMatch(offset: int, name: str, description: str, size: int | None = None, info: dict[str, object] = dict())

A validated signature hit. size is the object length when the header tells it.

analyze

analyze(image: str | PathLike[str], outdir: str | PathLike[str] | None = None, backend: str = 'auto', osv: bool = False, timeout: float | None = None, force: bool = False) -> AnalysisResult

Unpack image, run fwsift on every root filesystem and bootlint on DTB/FIT, U-Boot env and kernel config candidates; merge the findings (most severe first).

Missing tools are reported as warnings, so the unpack result is always returned. force is passed to unpack (redo an earlier extraction).

classify_tree

classify_tree(outdir: Path) -> dict[str, list[Path]]

Find root filesystems, kernels, DTBs, U-Boot env candidates and kernel configs.

docker_available

docker_available(image: str = DOCKER_IMAGE) -> bool

True if docker works and image is present locally (never pulls).

unpack

unpack(image: str | PathLike[str], outdir: str | PathLike[str] | None = None, backend: str = 'auto', limits: Limits | None = None, timeout: float | None = None, docker_image: str = DOCKER_IMAGE, force: bool = False) -> UnpackResult

Extract image into outdir (default <image>.extracted next to it).

backend is auto, unblob, docker, binwalk or python. The output directory must be new or empty; extractors never write outside it.

With force, a non-empty outdir left by an earlier unpack is emptied and the extraction redone. Only directories carrying hwntools' marker (.hwntools/unpack.json, written by every unpack) are ever cleared: any other non-empty directory is refused even with force, so a mistyped -o cannot wipe unrelated files.

entropy

entropy(data: bytes, block_size: int = 4096) -> list[float]

Just the entropy values of entropy_map.

entropy_map

entropy_map(data: bytes, block_size: int = 4096) -> list[EntropyBlock]

Entropy per block_size block. High (>7.5) usually means compressed or encrypted.

find_signatures

find_signatures(data: bytes, signatures: Iterable[Signature] = SIGNATURES, names: Iterable[str] | None = None, alignment: int = 1) -> list[SignatureMatch]

Scan data for known magic values and return validated matches sorted by offset.

Signatures with a header parser are only reported if the header looks sane, which keeps false positives for short magics (lzma, jffs2) down. alignment limits matches to offsets that are multiples of it (e.g. 4 or the flash erase size).

shannon_entropy

shannon_entropy(data: bytes) -> float

Shannon entropy of data in bits per byte (0.0 .. 8.0).

entropy_plot

entropy_plot(data: bytes, block_size: int = 4096, width: int = 64) -> str

Return a compact text sparkline of the entropy map (one char per bucket).

carve

carve(data: bytes, offset: int, size: int | None = None, out: str | PathLike[str] | None = None) -> bytes

Return data[offset:offset+size] (to the end if size is None); optionally save it.

scan

scan(target: str | PathLike[str], osv: bool = False) -> FwsiftReport

Run fwsift on an extracted rootfs or a raw image (see hwntools.tools).