Skip to main content
Be Bitwise

Debug Interface Exploitation

Hardware Hacking · Embedded Systems & Firmware track

7 min read

In this module

Debug Access as Attack Surface

Debug interfaces (JTAG, SWD, cJTAG) exist for development and manufacturing test. They hand the user direct access to the CPU's registers, memory, and flash — effectively, full ownership of the device. Leave them accessible on production hardware, and they become a gift to attackers.

OpenOCD for JTAG/SWD

OpenOCD (Open On-Chip Debugger) is the standard open-source tool for talking to debug ports. It supports hundreds of target MCUs and a long list of debug adapters:

bash
# Connect to an STM32 via SWD
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg
# In the OpenOCD console:
> halt # Stop CPU execution
> reg # Display all register values
> mdw 0x08000000 100 # Read 100 words from flash
> mdb 0x20000000 256 # Read 256 bytes from RAM
> dump_image flash.bin 0x08000000 0x100000 # Dump entire flash to file
> resume # Resume execution

With debug access, you can:

  • Read all memory: Flash (firmware), RAM (runtime data, keys), peripheral registers
  • Write all memory: Modify firmware, inject code, change configuration
  • Control execution: Set breakpoints, single-step, redirect execution
  • Program flash: Write new firmware, bypassing any update verification

Reading and Writing Flash Through Debug

The most direct use of debug access is dumping the firmware for analysis:

bash
# Using OpenOCD to dump firmware
openocd -f interface/jlink.cfg -f target/nrf52.cfg \
-c "init; halt; dump_image firmware.bin 0x00000000 0x80000; exit"
# Using pyOCD (alternative for ARM Cortex-M)
pyocd cmd -t nrf52840
>>> read32 0x00000000 256 # Read from flash
>>> savemem 0 0x80000 dump.bin # Save flash contents

For writing, you can flash custom firmware, inject backdoors, or modify specific bytes:

bash
# Write a modified firmware
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \
-c "init; halt; flash write_image erase modified_firmware.bin 0x08000000; reset; exit"
Debug Access = Game Over

If an attacker has unrestricted JTAG/SWD access to a device, the device's security is completely compromised. They can read all secrets, modify all code, and control all execution. This is why readout protection and debug port locking exist - and why bypassing them is a primary goal of hardware security research.

Bypassing Readout Protection

Most MCUs ship readout protection (RDP) that blocks flash reads via the debug port. Protection levels vary:

Level 0 (Unprotected): Full debug access. The default on many MCUs.

Level 1 (Read Protection): Debug connections are allowed; flash reads are not. To get at flash you have to mass-erase, destroying its contents. Protects firmware IP, allows debug access after wipe.

Level 2 (Permanent Protection): Debug port is permanently disabled. No debug access at all, and the change is irreversible — even the manufacturer can't undo it.

These three levels are STM32's scheme. Other vendors draw the lines differently: Nordic's nRF52 has a single APPROTECT bit that blocks the debug port's access to the CPU until an ERASEALL, and NXP's LPC parts use Code Read Protection (CRP) levels with their own rules. Check the reference manual for the exact part before assuming any of this. These bits can be glitched too: in 2020 LimitedResults showed a voltage glitch at boot that re-opened the debug port on APPROTECT-locked nRF52 chips (CVE-2020-27211).

Predict: an STM32F4 is at RDP Level 1 and you connect over SWD. What do halt, mdw 0x20000000 4 and mdw 0x08000000 4 do?

halt works: Level 1 still lets the debugger connect and stop the core. The RAM read at 0x20000000 works too and prints four words, because on this family Level 1 locks the flash and backup SRAM, not main SRAM. That's what makes the SRAM tricks below worth trying.

The flash read at 0x08000000 fails. The bus access is refused while a debugger is attached, so OpenOCD reports a memory read error instead of words. dump_image fails the same way. The only way back to Level 0 is to set it in the option bytes, which triggers a mass erase, so you get a working debug port and an empty chip.

Bypass techniques for Level 1 protection:

  • Fault injection: Glitch the MCU mid-RDP-check so the protection enforcement is skipped
  • Cold boot on SRAM: Some MCUs don't protect SRAM reads even with flash RDP enabled. Reading SRAM right after boot can capture keys or sensitive data before they're wiped
  • Exploiting RDP transitions: A handful of MCUs have bugs in the protection-level transition logic, opening a brief window where flash is readable while RDP is being changed
  • UV exposure: Older MCUs store RDP in EPROM cells, which can be reset by UV light — same trick that erases vintage EPROM chips

Put the STM32 levels side by side and the choice of attack mostly falls out of which level you find. Level 2 looks final, but the level is just a value in option bytes that the chip loads at reset, and a glitch during that load can leave it running at Level 1. The wallet.fail team did exactly that to the STM32F205 in the Trezor One hardware wallet, shown at 35C3 in December 2018.

STM32-style readout protection: what each level leaves open

GDB Integration

OpenOCD and pyOCD act as GDB servers, enabling full source-level debugging:

bash
# Start OpenOCD as GDB server
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg &
# Connect GDB
arm-none-eabi-gdb firmware.elf
(gdb) target remote :3333
(gdb) monitor reset halt
(gdb) break main
(gdb) continue
(gdb) info registers
(gdb) x/20i $pc

For security research, GDB integration enables:

  • Setting breakpoints at security check functions
  • Inspecting key material in registers and memory
  • Modifying return values to bypass authentication
  • Tracing execution paths through security-critical code

Extracting Firmware Through Debug

Flash readout protection is no dead end — there are still extraction routes through debug:

RAM extraction: If the firmware loads itself (or parts of itself) into RAM during execution, reading RAM through the debug port can catch decrypted firmware segments.

Peripheral register reading: SPI flash controller registers often hold flash contents that the MCU has just read into them.

Breakpoint-based extraction: Drop a breakpoint in the firmware's own flash-read routine. Each time it fires, read the data from registers or RAM. The firmware ends up exfiltrating itself one chunk at a time.

Physical Security Matters

Debug interfaces are physical attack vectors. If an attacker has physical access to the device, they can probe the PCB, connect a debugger, and potentially extract all secrets. This is why high-security devices use epoxy potting (covering the PCB in opaque resin), tamper-responsive enclosures, and Level 2 readout protection. For devices deployed in physically accessible locations (like IoT sensors), assume that debug extraction is possible and design the security model accordingly.