Skip to content

5. Analyze the firmware

Goal: turn flash.bin into a list of concrete weaknesses.

You will learn

  • a first look with hwn sigs and hwn entropy
  • firmware.unpack() and what it finds
  • hwn fw analyze with the fwsift and bootlint audit tools

In your terminal: hwn learn 5 (how)

Look before you unpack

Firmware is a few known formats glued together. hwn sigs finds their headers, and hwn entropy shows where the data is compressed (high), empty (low) or erased flash (0xff, also low):

$ hwn sigs flash.bin
0x00003000  uimage     uImage "Linux-4.4.60", data size 91, load 0x80008000, compression none
0x00003400  uimage     uImage "rootfs", data size 655, load 0x80008000, compression gzip
0x00003440  gzip       gzip compressed data
$ hwn entropy flash.bin --block 0x400 --plot
██  ▂   ▂   ▁▆

A kernel image at 0x3000 (the address bootm jumped to in lesson 3) and a gzip'd root filesystem at 0x3400. The entropy bar has one character per 1 KiB block: the boot stub at the start, the two small bumps at 4 and 8 KiB (U-Boot environments, as you will see), the kernel and the compressed rootfs at 12 to 13 KiB, and erased flash after that.

Unpack and read the environment copies

firmware.unpack() extracts everything it recognises into flash.bin.extracted/ and sorts the results: root filesystems, kernels, device trees, U-Boot environments and kernel configs. It uses unblob or binwalk when they are installed; backend="python" forces the built-in extractor, which needs nothing else and gives the same result on every machine.

U-Boot often keeps two copies of its environment in flash. Let's read bootargs from each:

05_env_copies.py
from hwntools import firmware

res = firmware.unpack("flash.bin", backend="python")
for env_file in res.env_candidates:
    raw = env_file.read_bytes()[4:]  # skip the CRC32
    env = dict(v.split("=", 1) for v in raw.decode().split("\0") if "=" in v)
    print(env_file.name, "->", env["bootargs"])
Output
[*] Unpacking flash.bin with the built-in extractor
[+] Unpacked with python: 13 files, 1 rootfs, 1 kernel(s), 0 dtb(s), 2 env candidate(s)
00001000.uboot-env.bin -> console=ttyS0,115200 root=/dev/ram0 rw
00002000.uboot-env.bin -> console=ttyS0,115200 root=/dev/ram0 rw init=/bin/sh

The copy at 0x1000 is what the board booted with. The one at 0x2000 is a leftover from the factory: it boots straight into a root shell. If U-Boot ever falls back to that copy (for example after the first one is corrupted), anyone on the console is root. You would not have seen this from printenv alone.

Running it again

unpack() refuses to write into a folder that is not empty. Delete flash.bin.extracted/ or pass another output folder to rerun the script.

The full audit: hwn fw analyze

Reading files by hand does not scale. hwn fw analyze unpacks the image and runs two Reflex audit tools on what it found: fwsift on root filesystems (passwords, keys, services, old components) and bootlint on U-Boot environments, device trees and kernel configs. They are separate programs written in Rust; install them with cargo install --git https://github.com/reflex-hs/fwsift and cargo install --git https://github.com/reflex-hs/bootlint (see Installation).

$ hwn fw analyze flash.bin --backend python -o report
[*] Unpacking flash.bin with the built-in extractor
[+] Unpacked with python: 13 files, 1 rootfs, 1 kernel(s), 0 dtb(s), 2 env candidate(s)
[+] Analysis done: critical 3, high 3, medium 4, low 6
Image:    /home/you/hwn-learn/flash.bin
Unpacked: /home/you/hwn-learn/report (backend python, 13 files)
Found:    1 rootfs, 1 kernel(s), 0 DTB/FIT, 2 U-Boot env, 0 kernel config(s)
  rootfs: 00003400.uimage-ramdisk/tar-root
Findings: critical 3, high 3, medium 4, low 6
  CRITICAL BL-CMD-002   init replaced with a shell  00002000.uboot-env.bin [bootargs]
  CRITICAL CRED-001     Account without password  00003400.uimage-ramdisk/tar-root/etc/shadow:1
  CRITICAL SVC-005      Network shell listener (bind shell)  00003400.uimage-ramdisk/tar-root/etc/init.d/rcS:3
  HIGH     BL-ENV-001   Autoboot can be interrupted without a stop key  00001000.uboot-env.bin [bootdelay]
  HIGH     BL-ENV-001   Autoboot can be interrupted without a stop key  00002000.uboot-env.bin [bootdelay]
  HIGH     SVC-006      Serial console shell without authentication  00003400.uimage-ramdisk/tar-root/etc/inittab:2
  MEDIUM   BL-CMD-001   Kernel console on a serial UART  00001000.uboot-env.bin [bootargs]
  MEDIUM   BL-CMD-001   Kernel console on a serial UART  00002000.uboot-env.bin [bootargs]
  MEDIUM   COMP-001     Component older than baseline  00003400.uimage-ramdisk/tar-root/bin/busybox
  MEDIUM   CRED-003     Weak password hash algorithm  00003400.uimage-ramdisk/tar-root/etc/shadow:2
  LOW      BL-ENV-003   U-Boot console not silenced  00001000.uboot-env.bin [silent]
  LOW      BL-ENV-003   U-Boot console not silenced  00002000.uboot-env.bin [silent]
  LOW      BL-ENV-005   No runtime environment access control (.flags)  00001000.uboot-env.bin [.flags]
  LOW      BL-ENV-005   No runtime environment access control (.flags)  00002000.uboot-env.bin [.flags]
  LOW      BL-ENV-007   Kernel booted without signature verification  00001000.uboot-env.bin [bootcmd]
  LOW      BL-ENV-007   Kernel booted without signature verification  00002000.uboot-env.bin [bootcmd]
info: U-Boot env (single, 0x1000 bytes) found at 0x1000
info: U-Boot env (single, 0x1000 bytes) found at 0x2000

The first finding is the init=/bin/sh you just found by hand. The others are the next lesson's way in: root has no password (etc/shadow line 1), the serial console starts a shell without a login (etc/inittab line 2), and a startup script runs telnetd -l /bin/sh, a root shell for anyone on the network.

Without fwsift and bootlint installed, analyze still unpacks the image, prints Findings: none and adds a warning for each missing tool, so it is safe to run in CI.

Try it

Try it

Do the same from Python: call firmware.analyze() on flash.bin (with "analysis" as the output folder) and print only the critical findings with their evidence.

Solution
05_try.py
from hwntools import firmware

report = firmware.analyze("flash.bin", "analysis", backend="python")
for f in report.findings:
    if f.severity == "critical":
        print(f.rule_id, "|", f.title, "|", f.evidence)
Output
[*] Unpacking flash.bin with the built-in extractor
[+] Unpacked with python: 13 files, 1 rootfs, 1 kernel(s), 0 dtb(s), 2 env candidate(s)
[+] Analysis done: critical 3, high 3, medium 4, low 6
BL-CMD-002 | init replaced with a shell | init=/bin/sh
CRED-001 | Account without password | root::...
SVC-005 | Network shell listener (bind shell) | telnetd -l /bin/sh

This needs fwsift and bootlint. report.to_dict() gives you everything as JSON, and hwn fw analyze --fail-on high exits with status 1 when a finding of that severity or worse exists, which is handy in a firmware release pipeline.

Next: Get a shell