Simulator quickstart¶
hwntools.sim ships a fake U-Boot console and a fake Linux shell, so you can write and test
an assessment script before the board arrives. The fakes model what matters for scripting
(echo, prompts, autoboot timing, md.b formatting, login prompts); they are not emulators.
There are two ways to connect to a simulated device:
SimTube: in-process, no extra packages, works on every platform.PtyBridge: exposes the device on a real pseudo-terminal, so your script opens it withserial()exactly like a USB-UART adapter. POSIX only, needs theserialextra.
Every example on this page runs as-is.
A fake U-Boot, in-process¶
from hwntools import UBoot
from hwntools.sim import FakeUBoot, SimTube
ub = UBoot(SimTube(FakeUBoot(memory=bytes(range(256)) * 16)))
ub.interrupt()
print(ub.printenv()["bootcmd"], ub.dump(0x80000000, 0x1000)[:16].hex())
FakeUBoot prints a boot banner and an autoboot countdown, stops when it receives a key (or
only a specific stop_string, such as "tpl"), answers printenv, setenv, saveenv,
md.b, version, echo and help, and "boots Linux" when the countdown runs out or on
boot/bootm/run bootcmd. The bytes you pass as memory are visible at mem_base
(default 0x80000000); reads outside it return 0xff.
Useful constructor arguments:
| Argument | Default | Effect |
|---|---|---|
memory |
b"" |
bytes visible to md.b at mem_base |
env |
a small router-like environment | initial environment |
prompt |
"=> " |
the U-Boot prompt |
autoboot_delay |
1.0 |
seconds the countdown waits for a key (0 boots immediately) |
stop_string |
None |
only this string stops autoboot |
corrupt_md_every |
0 |
corrupt every N-th md.b output, to exercise the dump retries |
A fake Linux console, in-process¶
FakeLinuxShell puts an optional login: prompt in front of a real /bin/sh on your machine
(POSIX only). Commands really run, in cwd, so file reads, base64, od and friends behave
like on a device. Pass path= to restrict which tools are available and exercise the
fallbacks.
from hwntools import LinuxShell
from hwntools.sim import FakeLinuxShell, SimTube
device = FakeLinuxShell(credentials={"root": "toor"}, hostname="cam")
sh = LinuxShell(SimTube(device))
print(sh.detect()) # "login"
print(sh.try_default_credentials()) # ('root', 'toor')
res = sh.run("id -u; false")
print(repr(str(res)), res.status) # output of `id -u` on your machine, then status 1
print(sh.read_file("/etc/hosts")[:40]) # copied over the "console" with base64
Note
FakeLinuxShell executes the commands your script sends on your own machine with
sh -c. Point it at a scratch directory with cwd= when experimenting.
On a real pseudo-terminal¶
PtyBridge makes the device look like a serial port. The script below is exactly what you
would run against a real board, except for the with PtyBridge(...) line:
from hwntools import UBoot, serial
from hwntools.sim import FakeUBoot, PtyBridge
image = bytes(range(256)) * 64 # 16 KiB of "flash" at 0x80000000
with PtyBridge(FakeUBoot(memory=image, autoboot_delay=3)) as bridge:
ub = UBoot(serial(bridge.port, 115200))
ub.interrupt(timeout=5)
print(ub.version())
data = ub.dump(0x80000000, len(image), out="sim-flash.bin", chunk=0x800)
assert data == image
The repository has the same script as
examples/dry_run_simulator.py.
You can also drive the hwn command against a bridge. Keep the bridge open in one Python
process (with a long autoboot_delay, say 30, so the countdown is still running when you
connect) and point hwn at its port from another terminal:
$ python -c "import time; from hwntools.sim import FakeUBoot, PtyBridge; \
b = PtyBridge(FakeUBoot(autoboot_delay=30)); print(b.port, flush=True); time.sleep(60)"
/dev/ttys017
$ hwn uboot env --port /dev/ttys017 --interrupt --json # in a second terminal
Write your own device¶
Subclass SimDevice (or reuse the line editing of the fakes):
override on_input(data) to react to bytes from the host, call emit(...) to send output,
and use tick() for time-based behaviour. Wrap it in SimTube or PtyBridge like the
built-in fakes.
Next: the guides walk through each part of a real assessment.