Firmware reverse engineering is a critical technique in IoT security research, embedded system development, and vulnerability discovery. This tutorial will comprehensively and deeply explain all aspects of firmware reverse engineering, including firmware extraction techniques (binwalk, JTAG), embedded system analysis (ARM, MIPS architectures), firmware patching and modification methods, and help readers master this key technology through detailed code examples and step-by-step analysis.
Firmware Extraction Techniques
Detailed Explanation of binwalk Tool
binwalk is a powerful firmware analysis tool that can automatically identify and extract file systems and code segments within firmware. It relies on signature matching and entropy analysis to detect file systems and code in firmware.
binwalk Installation:
# Install binwalk on Linux systems
sudo apt-get update
sudo apt-get install binwalk
Basic Usage:
# Scan firmware file
binwalk firmware.bin
# Extract firmware content
binwalk -e firmware.bin
# Deep scan (more detailed analysis)
binwalk -M firmware.bin
binwalk Working Principle:
- File Header Recognition: Identifies file systems using known file header signatures
- Entropy Analysis: Detects high-entropy regions (likely compressed or encrypted data)
- Pattern Matching: Uses predefined patterns to match firmware structures
- Recursive Extraction: Recursively extracts identified file systems
binwalk Advanced Options:
# Specify extraction directory
binwalk -e -C /path/to/extract firmware.bin
# Ignore certain signatures
binwalk --exclude=signature1,signature2 firmware.bin
# Display detailed information
binwalk -vv firmware.bin
binwalk Output Parsing:
binwalk output typically includes the following information:
- Offset: Offset within the firmware
- Entropy: Entropy value (0–8, higher values indicate more randomness)
- Description: Identified file type or structure
- Size: Size of the identified region
JTAG Interface Firmware Extraction
JTAG (Joint Test Action Group) is a hardware debugging interface used to directly access the flash memory chip of embedded devices to extract firmware.
JTAG Extraction Steps:
- Hardware Preparation:
- JTAG adapter (such as those supported by OpenOCD)
- Connection cables (ensure correct connection of TCK, TMS, TDI, TDO, and GND)
- Target device (with JTAG interface)
- Software Preparation:
- Install OpenOCD (Open On-Chip Debugger)
sudo apt-get install openocd- Configure OpenOCD (may require writing configuration files for specific devices)
- Connect Device:
- Connect the JTAG adapter to the target device’s JTAG pins
- Connect the adapter to the computer
- Start OpenOCD:
openocd -f interface/adapter.cfg -f target/target.cfg(Adjust configuration files based on the actual adapter and target device used) - Connect GDB:
arm-none-eabi-gdb (gdb) target remote :3333(OpenOCD provides a GDB server on port 3333 by default) - Read Flash Memory Content:
- Use GDB commands to read flash memory content
(gdb) dump memory firmware.bin 0x08000000 0x080FFFFF(Address range needs to be adjusted based on the target device’s flash layout)
JTAG Extraction Precautions:
- Ensure correct JTAG pin connections; incorrect connections may damage the device
- JTAG configurations vary between devices; consult the device manual
- Some devices may have JTAG protection mechanisms that need to be disabled first
Other Firmware Extraction Methods
In addition to binwalk and JTAG, other firmware extraction methods include:
- Serial Port Extraction:
- Obtain firmware updates or debug information via the device’s serial port interface
- May require reverse engineering the device’s communication protocol
- Network Extraction:
- Obtain firmware updates via the device’s network interface
- May require intercepting or simulating the firmware update process
- Direct Chip Reading:
- Read data directly from the flash chip (requires a chip programmer)
- Suitable for devices without JTAG interfaces
Embedded System Analysis
ARM Architecture Analysis
ARM is one of the most commonly used processor architectures in embedded systems. Understanding ARM architecture is crucial for firmware analysis.
ARM Architecture Features:
- Load/Store Architecture: Operands must be loaded into registers before operation
- Conditional Execution: Most instructions can be executed based on condition codes
- Multiple Modes: Supports various processor modes (e.g., user mode, privileged mode)
- Thumb Instruction Set: 16-bit instruction set for code density optimization
ARM Registers:
- R0-R12: General-purpose registers
- R13(SP): Stack pointer
- R14(LR): Link register (stores return address)
- R15(PC): Program counter
- CPSR: Current program status register
ARM Instruction Set Analysis:
; ARM assembly code example
start:
MOV R0, #0x10 ; Load immediate value 0x10 into R0
LDR R1, =0x20000000 ; Load address 0x20000000 into R1
LDR R2, [R1] ; Load value from address pointed by R1 into R2
ADD R2, R2, R0 ; R2 = R2 + R0
STR R2, [R1] ; Store R2 value to address pointed by R1
BX LR ; Return (restore PC from LR)
ARM Firmware Analysis Tools:
- IDA Pro: Supports disassembly and reverse engineering for ARM architecture
- Ghidra: NSA open-source decompiler, supports ARM
- ARMulator: ARM official emulator for debugging
MIPS Architecture Analysis
MIPS is another common embedded processor architecture known for its simplicity and efficiency.
MIPS Architecture Features:
- Load/Store Architecture: Similar to ARM
- Fixed-Length Instructions: All instructions are 32 bits
- Delay Slot: The instruction following a branch executes before the branch takes effect
- Register File: 32 general-purpose registers
MIPS Registers:
- $0: Zero register (always returns 0)
- $1-$31: General-purpose registers
- $at: Assembler temporary register
- v0-v1: Function return values
- a0-a3: Function arguments
- t0-t9: Temporary registers
- s0-s7: Saved registers
- k0-k1: Kernel reserved registers
- $gp: Global pointer
- $sp: Stack pointer
- $fp: Frame pointer
- $ra: Return address
MIPS Instruction Set Analysis:
; MIPS assembly code example
start:
li $t0, 0x10 # Load immediate value 0x10 into $t0
la $t1, 0x20000000 # Load address 0x20000000 into $t1
lw $t2, 0($t1) # Load value from address pointed by $t1 into $t2
add $t2, $t2, $t0 # $t2 = $t2 + $t0
sw $t2, 0($t1) # Store $t2 value to address pointed by $t1
jr $ra # Return (restore PC from $ra)
MIPS Firmware Analysis Tools:
- IDA Pro: Supports disassembly and reverse engineering for MIPS architecture
- Ghidra: Supports MIPS architecture decompilation
- QEMU: Can simulate MIPS environment for debugging
Firmware Patching and Modification
Firmware Modification Basics
Firmware modification typically involves the following steps:
- Extract firmware
- Analyze firmware structure
- Locate code or data to modify
- Make modifications
- Repackage firmware
- Flash modified firmware
Firmware Modification Tools:
- Hex Editor: Such as HxD, 010 Editor
- Binary Patching Tools: Such as dd, xxd
- Filesystem Tools: Such as unsquashfs (for squashfs file systems)
Firmware Patching Techniques
Static Patching:
Directly modify code or data in firmware, suitable for simple modifications.
Dynamic Patching:
Modify program behavior at runtime, typically implemented via hooks or patch code.
Firmware Patching Example:
Suppose we need to modify a string in the firmware:
- Open the firmware file with a hex editor
- Search for the target string (e.g., “old_string”)
- Find the string’s offset
- Modify the string content (e.g., change to “new_string”)
- Save the modified firmware
Automated Patching Tools:
Use Python scripts to automate the firmware modification process:
import mmap
def patch_firmware(firmware_path, old_str, new_str):
with open(firmware_path, 'r+b') as f:
# Use memory mapping to access file
mm = mmap.mmap(f.fileno(), 0)
# Find old string
offset = mm.find(old_str.encode())
if offset == -1:
print("String not found")
return
# Calculate length difference of new string
old_len = len(old_str)
new_len = len(new_str)
delta = new_len - old_len
# If lengths differ, need to adjust subsequent content
if delta != 0:
# More complex handling needed, such as moving subsequent content
print("String length changed, need more complex patching")
return
# Modify string
mm[offset:offset+old_len] = new_str.encode()
# Close memory mapping
mm.close()
# Usage example
patch_firmware("firmware.bin", "old_string", "new_string")
Firmware Repackaging
After modifying firmware, the filesystem usually needs to be repackaged:
- For squashfs filesystem:
# Unpack unsquashfs -d extracted firmware.bin # Modify content # Repackage mksquashfs extracted modified_firmware.bin -comp xz - For custom filesystems:
- May need to understand the specific structure of the filesystem
- Use appropriate tools to repackage
Firmware Flashing
The modified firmware needs to be flashed onto the device:
- Flash via JTAG:
- Use OpenOCD or other JTAG tools to write firmware to flash
- Flash via Serial Port:
- Some devices support receiving firmware updates via serial port
- Flash via Network:
- Simulate the firmware update process to send modified firmware to the device
- Flash via USB:
- Some devices support flashing firmware via USB interface
Comprehensive Case Study
Case: Analyzing and Modifying an IoT Device Firmware
Goal: Analyze an IoT device’s firmware, modify its version information, and reflash it to the device
Steps:
- Firmware Extraction:
- Use binwalk to scan the firmware file
binwalk firmware.bin- Extract firmware content
binwalk -e firmware.bin - Firmware Analysis:
- Analyze the extracted filesystem
- Locate where version information is stored
- Use hex editor or Python script to modify version information
- Firmware Modification:
- Modify version string
- Ensure filesystem integrity
- Firmware Repackaging:
- Use mksquashfs to repackage the modified filesystem
mksquashfs extracted modified_firmware.bin -comp xz - Firmware Flashing:
- Use JTAG tools to write modified firmware to the device
openocd -f interface/adapter.cfg -f target/target.cfg- Execute flashing commands in GDB
- Verify Modification:
- Restart the device
- Check if version information has been modified
Case Analysis Results:
- Successfully extracted firmware
- Accurately located and modified version information
- Repackaged firmware and successfully flashed
- Verified modified version information
Advanced Firmware Reverse Engineering Techniques
Firmware Encryption and Decryption
Many firmwares use encryption protection and need to be decrypted before analysis:
- Identify Encryption Algorithm:
- Analyze firmware loading code
- Identify encryption algorithm and key
- Extract Key:
- Find hard-coded keys through static analysis
- Capture runtime keys through dynamic analysis
- Decrypt Firmware:
- Implement decryption algorithm
- Decrypt firmware content
Firmware Vulnerability Discovery
Firmware reverse engineering is often used for vulnerability discovery:
- Buffer Overflow:
- Analyze input handling code
- Look for unsafe function calls
- Memory Corruption:
- Analyze memory management code
- Look for use-after-free (UAF) issues
- Authentication Bypass:
- Analyze authentication logic
- Look for authentication bypass vulnerabilities
Firmware Protection Technology Analysis
Analyzing firmware protection techniques helps understand its security:
- Code Obfuscation:
- Analyze code obfuscation techniques
- Attempt to reverse obfuscation
- Anti-Debugging:
- Analyze anti-debugging techniques
- Attempt to bypass anti-debugging
- Integrity Checking:
- Analyze integrity check code
- Attempt to bypass checks
Summary and Learning Path
Firmware reverse engineering is a complex but highly valuable technical field involving multiple areas of knowledge and skills. This tutorial has covered in detail:
- Firmware extraction techniques (binwalk, JTAG, etc.)
- Embedded system analysis (ARM, MIPS architectures)
- Firmware patching and modification methods
- Comprehensive case study
- Advanced techniques (encryption, vulnerability discovery, protection analysis)
Learning Path Recommendations:
- Basic Stage:
- Learn assembly language (ARM, MIPS)
- Master firmware analysis tools like binwalk
- Understand basic embedded system architecture
- Intermediate Stage:
- Deeply study ARM and MIPS architectures
- Master firmware extraction and modification techniques
- Learn fundamentals of reverse engineering
- Advanced Stage:
- Research firmware encryption and decryption techniques
- Learn vulnerability discovery techniques
- Analyze firmware protection techniques
- Practice Stage:
- Participate in open-source firmware analysis projects
- Conduct actual firmware reverse engineering
- Research the latest firmware protection techniques
Through continuous learning and practice, you will master advanced firmware reverse engineering techniques and contribute to IoT security research and embedded system development.



