Posts Tagged ‘pld firmware reverse engineering’

PostHeaderIcon Protocol Reverse Engineering: Captures, Fields, State Machines

A customer who asks for firmware reverse engineering usually wants to talk to a device, replace a device, or build something that interoperates with a device. In all three cases the useful output is a description of the interface, and in all three cases the description is built from three artefacts in a fixed order.

The order matters because each artefact is the input to the next, and because a mistake in one propagates.

Artefact one: the capture

Everything begins with recorded traffic. Without a capture there is nothing to analyse, and the quality of the capture determines the ceiling on everything that follows.

A capture needs three properties. It has to be complete, meaning it includes the framing and not just the payload. It has to be synchronised, meaning each message has a timestamp relative to a common clock. And it has to be annotated, meaning each capture is associated with the action that produced it.

Capture property Consequence if missing
Complete framing Field boundaries cannot be found
Timestamps Sequence and timeouts cannot be inferred
Annotated actions The stimulus-response relationship is lost
Repeated runs Transient behaviour is mistaken for protocol

The fourth row is the one that distinguishes a professional capture from a log. A single capture of a session shows what happened once; five captures of the same session show what is stable, and only the stable parts are protocol.

Where to take the capture

The point of capture determines what can be seen, and the choice is usually made by convenience rather than by information.

A capture at the host side of a cable sees the protocol as the host implements it, including retries and error handling. A capture at the device side sees the same messages but with the device’s timing. A capture on the internal bus between a processor and its peripheral sees the raw register accesses, which is more information and harder to interpret.

Capture point Sees Difficulty
Host serial port The protocol, with host behaviour mixed in Low
Device pins The protocol, with device timing Low
Internal SPI or I2C Register-level operations Medium
Memory bus Everything, including code fetches High

The second row is usually the best choice, because it isolates the protocol from the host’s own retry logic. A host that retries three times and then reports a failure produces a capture with three identical requests, and an analyst who does not know about the retries will conclude that the protocol requires repetition.

Artefact two: the field map

With captures in hand, the second artefact is a description of the message format: where each field begins, how wide it is, and what it means.

The method is differential. Compare messages that differ in exactly one respect — a value changed, a length changed, a command changed — and the bytes that move are the field that carries it.

Comparison Reveals
Same command, different payload value The value field and its width
Same command, different payload length A length field, and whether it is a byte or a word
Different command, same structure The command field’s position
Same command, different sequence number The sequence field
Same command, different device The address or identifier field

The third row is the anchor for everything else, because the command field is the one field whose position is fixed by the protocol’s design. Once the command byte is known, messages can be grouped by it, and each group can be analysed separately.

Field type How to find it Confidence signal
Command Varies with the operation Fixed position across sessions
Length Correlates with the message size Linear relationship
Address Varies with the target Matches a known address
Payload The remainder Fills the declared length
Checksum At a fixed offset from the end Recomputable

The fifth row is the one that stops projects, and it deserves its own discussion.

The checksum problem

A checksum at the end of a message is easy to identify and hard to reproduce, because the algorithm is a choice rather than a standard.

The identification is straightforward: the last one, two or four bytes change whenever any other byte changes, and they do not change when the message is repeated unchanged. That pattern is conclusive.

Reproducing it requires the algorithm, and there are three ways to get it. The first is to guess from a shortlist: sum, XOR, CRC-8, CRC-16 with the common polynomials. The second is to derive it from enough samples by solving for the parameters, which works for CRCs because the polynomial, initial value and reflection are a small search space. The third is to read it out of the firmware, which is definitive and requires the image.

Route Requirement Reliability
Guess from a shortlist A few samples High for common algorithms
Solve from samples A few dozen samples High, if it is a CRC
Read from firmware The image Definitive
Capture during a valid session and replay Nothing Only for replay

The fourth row is worth noting because it is the shortcut that works when the checksum cannot be derived: if the goal is to send a limited set of messages, capture them from a real session and replay. That produces a working interface and no understanding, which is sometimes enough.

Artefact three: the state machine

The third artefact is the description of how the device moves between states, and it is the one that makes an implementation possible.

The method is stimulus-response: put the device in a known state, apply one input, observe the response and the new state. Repeat for every input from every state. The result is a directed graph.

Transition type How to detect
Accepted command A response that differs from the idle response
Rejected command An error response, or the same response as idle
Timeout A state change with no input
Session end A transition to the initial response pattern

The third row is the one that captures have to be long enough to contain. A device that drops a session after five seconds of inactivity has a timeout transition, and a capture of a fast interactive session will not show it. Deliberately pausing mid-session is the way to find these, and it is the test most often omitted.

Omitted test What is missed
Pause mid-session Timeouts and session expiry
Send commands out of order Ordering enforcement
Send a malformed message Error handling and recovery
Power cycle at each state Whether the state is persisted
Two hosts at once Contention behaviour

What the deliverable looks like

A specification that another engineer can implement from, plus a conformance test that proves the implementation matches.

Component Content
Frame description Field table with offsets, widths and meanings
Command set Each command with its parameters and responses
State machine States, transitions, guards, timeouts
Error behaviour What the device does when a message is invalid
Conformance test Vectors and expected responses

The fifth component is what separates a specification from notes. A test suite that a second implementation can be run against converts “we think it works like this” into “we know it works like this”, and it is the only artefact that survives contact with a real product.

Where this fits

The protocol route is often the cheapest path to interoperability, and it does not require the firmware at all; the comparison is in attacking MCU firmware.

Where the checksum has to come from the firmware, the image is obtained by the routes in extracting MCU firmware, and the routine that computes it is found by the search in credential hunting in firmware. Where the device is undocumented to the point that identification is the first problem, the public record is the place to start, as set out in discovering firmware from public sources.

What we deliver is the specification and the conformance test, and what we do not deliver is a claim that the state machine is complete. A state machine derived from observation is complete with respect to the observations, and the report says which transitions were tested and which were not.