Reading an unknown binary protocol in an hour

3 min read188 courses on OnlySecurity

You open a capture and one conversation will not decode. It is a TCP stream on an unusual port, the bytes are not text, and no dissector claims it. You have an hour. What do you do?

Start with the shape. Export the stream payloads in each direction, for example with the Follow TCP Stream view set to hex dump, or with tshark -r file.pcap -Y "tcp.port==XXXXX" -T fields -e tcp.payload where XXXXX is the port. Count the messages, note their lengths, and look at the length distribution. A protocol with fixed-size frames shows one or two lengths. A protocol with a length field shows many.

Next, find the framing. Look at the first two to four bytes of every message. If they are constant, you have a magic number or a version. If the next field changes with message length, you have found the length field, and you can check it by subtracting header size and comparing. Test the guess on every message in the stream, not just three. One mismatch means your idea is wrong.

Then find the request and response pairing. Many industrial protocols echo a transaction or sequence identifier. A field that increments by one on each request and appears again in the reply is the identifier. A field that has a handful of distinct values and correlates with message length is a function or command code. Make a table: code, count, typical length, direction.

Now look for things you recognise. Floating point values appear as four bytes that, read as IEEE 754 either way round, give plausible numbers such as 20.5 or 101.3. Timestamps appear as eight bytes near a known epoch. Strings are visible in ASCII. ASCII tag names often reveal what the device thinks it is measuring.

The most useful single move is comparison. Capture or find a second conversation of the same protocol, ideally with one thing changed, and diff the messages. The field that changes is the field that carries the change.

Write a small Python parser as you go, using the struct module from the standard library. A parser forces you to state your assumptions, and it fails loudly when an assumption is wrong. Ten lines that parse every message in the stream without an exception are worth more than an hour of staring.

Finally, say what you do not know. A reverse engineered format is a hypothesis. Mark each field as confirmed by multiple samples, plausible, or guessed. If the protocol is a published standard, you may find the specification and discover that your guessed field names were wrong; that is fine, and it is cheaper than the alternative.

For structured practice on this topic, see the OnlySecurity course ICS Forensics from the CTF Circuit II: Other Protocols and Live-Lab Triage.

More from this author