Learn hwntools¶
This track teaches the first day of a hardware assessment, one short lesson at a time: read a serial console, find its baud rate, stop U-Boot, dump the flash, find what is wrong in the firmware, get a shell and capture a flag. Everything runs on a simulated router on your own computer, so you need no board, no soldering iron and no USB-UART adapter.
Each lesson has a goal, code you can run, the output it really prints, a small Try it challenge with a hidden solution, and a link to the next lesson. Plan on 10 to 15 minutes per lesson.
Set up once¶
You need Python 3.10 or newer. Make a folder for the track and install hwntools into a virtual environment:
$ mkdir hwn-learn && cd hwn-learn
$ python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
$ pip install hwntools
$ hwn --version
hwntools 0.3.0
The practice device ships with hwntools as hwntools.learn.lab: it builds a small
synthetic router firmware and plugs it into the simulator (hwntools.sim). Every lesson
starts with from hwntools.learn.lab import ...; there is nothing else to download.
Prefer the terminal? hwn learn
The same lessons run as an interactive terminal tutorial with exercise files, automatic
checks, hints and saved progress: pip install hwntools && hwn learn.
See Learn in your terminal.
What is real and what is lab scaffolding?
The consoles, prompts and timing come from hwntools' own simulator
(FakeUBoot, FakeLinuxShell, SimTube), and every UBoot, LinuxShell, autobaud,
firmware, Flashrom and OpenOCD call in the lessons is the real library code you
would run against a board. hwntools.learn.lab
(source)
only adds what the simulator does not have: a firmware image to find things in, an
sf read command, a "loose cable", a console that garbles text at the wrong baud rate,
and stand-ins for the flashrom and OpenOCD programs. The lessons say each time when a
stand-in is used.
Save each code block under the file name shown in its title and run it with
python <name>.py from the same folder. The lessons build on each other's files (lesson 4
writes flash.bin, lesson 5 unpacks it, lesson 6 explores the result), so do them in order.
Lessons¶
-
0. Before you start
What hardware hacking is, the usual attack path, and the rules: authorization and not bricking things.
-
1. Talk to a UART
Open the board's serial console and read its boot log with
recvuntilandrecvline. -
2. Find the baud rate
Garbage on the console? Let
autobaud()try the common rates and score the output. -
3. Stop U-Boot
Interrupt autoboot, read the environment and change the kernel command line in RAM.
-
4. Dump the flash
Copy the SPI flash out over the console with
md.b, survive a loose wire and resume. -
5. Analyze the firmware
Unpack the dump, find a forgotten
init=/bin/sh, and runhwn fw analyze. -
6. Get a shell
Log in with an empty root password and look around a Linux console from a script.
-
7. SPI flash and JTAG
The same work with a flash clip and a debugger:
FlashromandOpenOCD. -
8. Capture the flag
A locked-down board hides a flag. Put everything together and get it out.
When you finish, the guides cover each topic in depth and the API reference lists every function.