Posts Tagged ‘fpga mcu source code attack’
Finding the Code: Boundaries, Pools, and Empty Space in a Dump
A flash dump is a byte array. A program is a set of instructions with an entry point, functions and data. The distance between the two is entirely made of boundary decisions, and every one of them is a judgement that affects everything downstream.
This is the least glamorous part of the work and the part that decides whether the rest is possible. A disassembly started at the wrong offset is not slightly wrong; it is wrong everywhere, because instruction decoding on a variable-length or word-aligned architecture depends on alignment. Being one word out turns every instruction into a plausible but incorrect one.
The four things in a dump
An image contains four kinds of content, and they are interleaved.
Code is instruction words, reached by the processor’s fetch.
Literal pools are blocks of constants embedded in the code region, placed there because the instruction encoding cannot carry a thirty-two-bit immediate. They are read by load instructions, not executed.
Tables are structured data: vector tables, jump tables, function pointer arrays, configuration blocks, calibration records. They are addressed rather than executed, and they are frequently the most valuable content in the image.
Erased or unused space is the remainder. It reads as all ones on flash and all zeros on some masked parts, and it marks the end of the used region.
| Content | Reached by | Distinguishing property |
|---|---|---|
| Code | Fetch | Decodes to valid instructions with sensible flow |
| Literal pool | Load from the code region | Looks like code but is referenced as data |
| Table | Indexed address | Regular structure, aligned entries |
| Erased space | Nothing | Uniform value, no references |
The anchors
Boundaries are established from anchors, not from heuristics, because heuristics fail exactly where it matters.
The vector table is the first anchor and the strongest. On an ARM Cortex-M part it sits at address zero of the image, its first word is the initial stack pointer, and its second word is the reset handler’s address with the Thumb bit set. Every subsequent word is an exception handler address, terminated by a recognisable pattern. The table gives you the entry points directly, and each entry is code by definition.
| Anchor | What it establishes | Reliability |
|---|---|---|
| Vector table at offset zero | Initial SP, reset handler, exception entries | Very high |
| Function prologue pattern | Function starts | High, with false positives |
| Return instruction | Function ends | High |
| Load-from-here instruction | Literal pool start | High |
| Alignment padding run | Region end | Medium |
The prologue pattern is the second most useful anchor on compiled code. A function that uses the link register saves it, and on ARM that produces a push of a specific register set as the first instruction. Scanning for that encoding finds function starts with few false positives, and each one is confirmed by checking that the preceding instruction is a branch, a return or padding.
Why heuristics alone mislead
The tempting approach is to disassemble from offset zero and let the decoder decide, marking everything it successfully decodes as code. That works on simple images and fails on real ones for three reasons.
Decoding rarely fails outright. Most byte sequences decode to something on a fixed-width instruction set, so “it decoded” is not evidence of anything. A literal pool of thirty-two-bit constants decodes as thirty-two-bit instructions with probability close to one.
Control flow does not cover all code. A function reached only through a computed branch has no incoming direct edge, so a recursive-descent disassembler never visits it, and its bytes remain undecoded.
Data referenced from code looks like code. A table of addresses, read as instructions, produces branch instructions to plausible addresses, which is exactly what a decoder wants to see.
| Failure | Cause | Mitigation |
|---|---|---|
| Literal pool decoded as code | Fixed-width decoding never fails | Recognise loads that point into the pool |
| Unreachable function missed | Only indirect references | Scan for prologues, not just flow |
| Table decoded as branches | Addresses look like branch targets | Check that targets are aligned and inside the image |
| Function split in two | Pool between the halves | Treat the pool as part of the function |
The end of the code
Finding where the code stops is easier than finding where it starts, and it is worth doing explicitly because it bounds the analysis.
On flash parts the boundary is usually a run of erased bytes. The run has to be long enough to be meaningful — a single erased word can be padding between functions — and it has to be followed by either the end of the array or a region that is clearly data.
On parts with a bootloader and an application, there are two regions with a gap between them, and the gap is often filled with a signature, a version record or a checksum. That record is a boundary marker in itself, and it tells you that the region before it is complete.
| Boundary pattern | Meaning |
|---|---|
| Long run of 0xFF to the end | End of the used region |
| Short 0xFF run between functions | Alignment padding |
| Structured record then more code | Bootloader, then application |
| Structured record then 0xFF | Application, then unused |
What the boundary map is for
The output of this analysis is a region map: which address ranges are code, which are pools, which are tables, which are empty, and where each function begins.
That map is the input to every subsequent step. Disassembly uses it to start decoding at the right places. String extraction uses it to search only the data regions. Entropy analysis uses it to distinguish compressed data from code. Patching uses it to find a location where a byte can be changed without corrupting a neighbouring literal.
| Downstream task | What it needs from the map |
|---|---|
| Disassembly | Function entry points |
| String search | Data regions only |
| Entropy analysis | Code versus data separation |
| Patching | Boundaries with slack |
| Comparison between versions | Region correspondence |
The last row is underappreciated. Comparing two firmware versions is straightforward when the region maps correspond and nearly impossible when one version has shifted a pool by four bytes and moved everything after it.
How this is done in practice
The procedure for a typical image is four passes, each cheap, each narrowing the uncertainty.
The first pass reads the vector table and marks every entry as code. The second scans the image for function prologues and marks those, then validates each against the surrounding bytes. The third follows every direct branch from the marked set and marks the destinations. The fourth examines the regions that remain unclaimed and classifies them by structure: regular and aligned is a table, uniform is padding or erased, high entropy is compressed data or a pool.
| Pass | Method | Output |
|---|---|---|
| One | Read the vector table | Entry points |
| Two | Scan for prologues | Candidate function starts |
| Three | Follow direct branches | Reachable code |
| Four | Classify the remainder | Tables, pools, padding |
Regions that survive all four passes without classification are reported as unknown rather than guessed at, and the report says what would resolve them: a reference from code, a structure that matches a known format, or a comparison against a second image.
Where the service fits
For an engagement that begins with a dump, we deliver the region map before anything else, because a customer who intends to ask questions about behaviour needs to know that the code was located correctly. The map is a table of address ranges with a classification and a confidence for each.
The work continues into the analysis described in MCU break, which is what happens after the boundaries are known. Where the image came from an external flash, the read details that determine whether the dump is trustworthy at all are in extracting MCU code, and a dump with a one-byte shift will produce a region map that is confidently wrong. Where the input is a CPLD or PLD fuse map rather than a processor image, the boundary problem is different and is treated in reading PLD binary into equations.