Skip to content

8. Capture the flag

Goal: use everything from lessons 1 to 5 on a board you have not seen before.

You will practise

  • stopping a U-Boot that ignores Ctrl-C
  • reading the boot setup to find the firmware
  • dumping, unpacking and searching for a hidden string

In your terminal: hwn learn 8 (how)

The board

lab.ctf_device() is a different unit of the same router. The vendor tightened it a bit:

  • Autoboot only stops for a vendor string. Ctrl-C does nothing. The usual suspects are in lab.STOP_STRING_CANDIDATES; one of them works.
  • The firmware is not at the same place in flash as before.
  • Somewhere in the firmware is a flag of the form FLAG{...}. It may not be stored in plain text.

Check your answer with lab.check_flag("FLAG{...}"), which returns True for the right flag (it compares hashes, so reading the lab source will not spoil it).

Start here

08_start.py
from hwntools import HwnTimeoutError, UBoot
from hwntools.learn.lab import ctf_device

ub = UBoot(ctf_device())
try:
    ub.interrupt(timeout=3)  # Ctrl-C, the default key
except HwnTimeoutError as exc:
    print("no prompt:", str(exc)[:40], "...")
Output
[x] Interrupting U-Boot autoboot
[-] Interrupting U-Boot autoboot: Failed
no prompt: no U-Boot prompt within 3s; last output: ...

Each failed attempt lets the board boot, so every guess needs a fresh power-on (ctf_device() again), just like on real hardware.

Hints

Hint 1: the stop string

interrupt() takes key=: the bytes it sends repeatedly. Loop over STOP_STRING_CANDIDATES, power on a fresh device for each, and catch HwnTimeoutError.

Hint 2: where is the firmware?

Lesson 3: bootcmd says which flash offset and length sf read copies to RAM.

Hint 3: the flag is not in plain text

Lesson 5 gave you firmware.unpack(). Walk the root filesystem, and for each file try both a plain search for FLAG{ and decoding long base64-looking strings.

Solution

Full solution
08_solution.py
import base64
import contextlib
import re
import shutil

from hwntools import HwnTimeoutError, UBoot, firmware
from hwntools.learn.lab import RAM_ADDR, STOP_STRING_CANDIDATES, check_flag, ctf_device

# 1. Find the stop string: one fresh power-on per guess.
for key in STOP_STRING_CANDIDATES:
    ub = UBoot(ctf_device())
    try:
        ub.interrupt(key=key, timeout=3)
        print("stop string:", repr(key))
        break
    except HwnTimeoutError:
        print("not", repr(key))

# 2. Read the env to learn where the image lives, then dump it.
env = ub.printenv()
print(env["bootcmd"])
m = re.search(r"sf read \S+ (0x[0-9a-f]+) (0x[0-9a-f]+)", env["bootcmd"])
off, size = int(m.group(1), 16), int(m.group(2), 16)
ub.run("sf probe")
ub.run(f"sf read {RAM_ADDR:x} {off:x} {size:x}")
ub.dump(RAM_ADDR, size, out="ctf.bin", resume=False)

# 3. Unpack, then grep every file for a flag, in plain text or base64.
shutil.rmtree("ctf.bin.extracted", ignore_errors=True)
rootfs = firmware.unpack("ctf.bin", backend="python").rootfs[0]
for path in sorted(p for p in rootfs.rglob("*") if p.is_file()):
    data = path.read_bytes()
    candidates = re.findall(rb"FLAG\{[^}]*\}", data)
    for token in re.findall(rb"[A-Za-z0-9+/]{16,}={0,2}", data):
        with contextlib.suppress(ValueError):
            candidates += re.findall(rb"FLAG\{[^}]*\}", base64.b64decode(token))
    for flag in candidates:
        print(path.relative_to(rootfs), "->", flag.decode(), check_flag(flag.decode()))
Output
[x] Interrupting U-Boot autoboot
[-] Interrupting U-Boot autoboot: Failed
not 'stop'
[x] Interrupting U-Boot autoboot
[-] Interrupting U-Boot autoboot: Failed
not 'tpl'
[x] Interrupting U-Boot autoboot
[-] Interrupting U-Boot autoboot: Failed
not 'xyz'
[x] Interrupting U-Boot autoboot
[+] Interrupting U-Boot autoboot: got prompt b'=> '
stop string: 'rxlab'
sf probe; sf read 0x82000000 0x40000 0x8000; bootm 0x82003000
[x] Dumping 0x8000 bytes from 0x82000000
[+] Dumping 0x8000 bytes from 0x82000000: 32768 bytes
[*] Unpacking ctf.bin with the built-in extractor
[+] Unpacked with python: 16 files, 1 rootfs, 1 kernel(s), 0 dtb(s), 2 env candidate(s)
usr/share/rx/support.txt -> FLAG{st0p_dump_unp4ck_gr3p} True

The flag was base64-encoded inside a "support code" in usr/share/rx/support.txt. root/.ash_history (rm /root/flag.txt) was a decoy: deleted files are gone from this image. On real firmware the same search finds API keys, cloud credentials and private keys, which is exactly what fwsift automates.

What next

You have done a whole first day of a hardware assessment, as a script. From here:

  • the guides go deeper into each topic (serial over TCP, U-Boot prompts and stop keys, file uploads, flash writes, firmware backends);
  • the simulator quickstart shows how to run the same scripts over a real pseudo-terminal, and how to write your own simulated device;
  • the CLI reference lists every hwn command.

When you move to real hardware, re-read Safety and legal first.