Firmware Emulation
Firmware Reverse Engineering · Embedded Systems & Firmware track
6 min read
In this module
Running Firmware Without Hardware
Firmware emulation lets you boot embedded firmware on your laptop instead of needing the actual gadget in hand. Dynamic analysis, debugging, vulnerability hunting — all of it becomes tractable without hooking up serial cables and JTAG probes every time. Need to fuzz a network daemon, debug a crash, or stage an exploit? The sandbox is right there.
This lesson works through four depths of emulation, from cheapest to most faithful to the real board:
QEMU User-Mode Emulation
QEMU's user-mode emulation runs a single Linux binary built for some other architecture on your machine, translating its instructions to native code on the fly:
# Run an ARM binary on x86qemu-arm -L /path/to/extracted/rootfs ./usr/bin/httpd
# Run a MIPS binaryqemu-mipsel -L /path/to/extracted/rootfs ./usr/sbin/some_daemon
# With strace-like tracingqemu-arm -strace -L /path/to/extracted/rootfs ./usr/bin/targetFast, simple, and limited. User-mode only works for single Linux binaries that don't stray off the standard syscall path. It models nothing of the hardware. Anything reaching for ioctls, custom drivers, or weird pseudo-filesystems will probably fall over.
Firmware binaries link against libraries from the firmware's root filesystem, not your host's libraries. The -L flag tells QEMU to use the extracted rootfs as the library search path. If the binary uses dynamically linked libraries (most do), you need the complete extracted filesystem with all shared libraries intact.
QEMU Full-System Emulation
Full-system emulation goes the whole way: kernel and userland both running inside QEMU, like a tiny VM:
# Create a QEMU disk image from the extracted filesystemdd if=/dev/zero of=rootfs.ext2 bs=1M count=256mkfs.ext2 rootfs.ext2mount rootfs.ext2 /mntcp -a extracted_rootfs/* /mnt/umount /mnt
# Boot the emulated systemqemu-system-arm \ -M vexpress-a9 \ -kernel zImage \ -dtb vexpress-v2p-ca9.dtb \ -drive file=rootfs.ext2,if=sd,format=raw \ -append "root=/dev/mmcblk0 console=ttyAMA0" \ -nographic \ -nic user,hostfwd=tcp::8080-:80Note whose kernel that is: a generic zImage built for QEMU's vexpress-a9 board, not the vendor's. Vendor kernels are built for the real SoC and rarely boot on a QEMU board, so the usual trick (and what Firmadyne and FirmAE automate) is a stock kernel plus the firmware's extracted root filesystem. The firmware's init scripts then fire as if the box were powered on. Network services come up, the web admin panel pops out on localhost:8080 (in the example above), and you can poke at the device exactly like it was sitting on your desk.
Firmadyne for Automated Emulation
Firmadyne tries to take all that setup off your hands for Linux-based firmware. It's the original research tool and is no longer actively maintained; FirmAE builds on it with heuristics that get many more images to boot, and EMBA and Qiling are other current options. The steps are the same idea in each:
- Extracts the firmware image and identifies the architecture
- Builds a custom kernel for the target architecture
- Creates a QEMU disk image with the extracted filesystem
- Configures networking (bridges, IP assignment)
- Boots the emulated firmware and verifies network reachability
# Firmadyne workflow (simplified)./sources/extractor/extractor.py -b vendor -sql 127.0.0.1 -np -nk firmware.bin images/./scripts/getArch.sh ./images/1.tar.gz./scripts/makeImage.sh 1./scripts/inferNetwork.sh 1./scratch/1/run.sh# The firmware's web interface is now accessiblePartial Emulation Strategies
A lot of firmware refuses to come up under full emulation because it expects hardware QEMU doesn't model. The workaround is to emulate just the parts you actually care about, in isolation.
NVRAM simulation: lots of router firmwares pull config from NVRAM, a vendor-specific non-volatile blob. Intercept the reads, hand back plausible values, and the firmware happily boots.
Hardware stub libraries: swap out hardware-dependent shared libraries with stubs that return sensible-looking junk. A library that reads a temperature sensor, say, gets replaced with one that always returns 25°C.
Unicorn Engine: a CPU emulator carved out of QEMU and turned into a library. You get tight control: emulate a single function, hook every memory read or write, fake hardware on demand:
from unicorn import *from unicorn.arm_const import *
# Emulate a single firmware functionmu = Uc(UC_ARCH_ARM, UC_MODE_THUMB)mu.mem_map(0x08000000, 0x100000) # Map flashmu.mem_map(0x20000000, 0x20000) # Map RAMmu.mem_write(0x08000000, firmware_code) # Load firmware code
# Fake the peripheral block. A UC_HOOK_MEM_READ hook can't supply the# value read (Unicorn ignores its return value), so map the range as MMIO:# Unicorn calls these functions instead of touching memory.def mmio_read(uc, offset, size, user_data): if offset == 0x20014: # 0x40020014: a GPIO data register return 0 # Every pin reads low return 0
def mmio_write(uc, offset, size, value, user_data): print(f"MMIO write 0x{0x40000000 + offset:08x} = 0x{value:x}")
mu.mmio_map(0x40000000, 0x10000000, mmio_read, None, mmio_write, None) # Unicorn 2.xmu.emu_start(function_address | 1, function_end) # Thumb modeDealing with Hardware Dependencies
The hardest part of firmware emulation is, unsurprisingly, the hardware. A few approaches that actually work:
- Stub the hardware accesses you care about: scan the firmware for MMIO reads and writes, and feed back plausible responses
- Replay captured responses: hook a logic analyser to the real device, record what the chip says, and play it back during emulation
- Model peripherals: for the few peripherals that really matter (Ethernet MAC, flash controller), it's worth writing a minimal QEMU device model
- Avatar2: a hybrid framework — emulate most of the firmware, but forward selected memory accesses to a real piece of hardware sitting on the side
Perfect emulation requires modelling every hardware peripheral exactly, which is often impractical. The goal is emulating enough to reach the code you want to analyse. For web interface security testing, you need networking and the web server to work - you do not need accurate GPIO emulation. For bootloader analysis, you need flash and UART emulation but not WiFi. Focus your emulation effort on the attack surface you are testing.