Data inside the handshake

TUCTF 2016 The Nack: Rebuilding a GIF from TCP SYN Payloads

A packet capture of failed connections looks empty until each SYN turns out to carry payload. Stripping a five-byte marker and joining the rest produces an animated GIF with the flag in one frame.

Hexfield editorial

EventTUCTF 2016
CategoryForensics, network
Points100
CarrierTCP SYN payload
Forensics100 pts
  1. 01

    Inspect

    The pcapng holds only connection attempts to a closed port, and each SYN packet carries a few bytes of data.

  2. 02

    Hypothesize

    A constant five-byte prefix is framing, and the bytes after it are one file split across packets in order.

  3. 03

    Test

    Joining the remainder with tshark and xxd gives a file that begins with a GIF header and opens as an animation.

  4. 04

    Solve

    Coalescing the frames separates a short-lived overlay, and the flag is visible in the 17th frame.

Entropy profile of a compressed image file, high and nearly flat after the header region
Compressed image payloads sit near maximum entropy, which separates them from the readable marker bytes around them.

The capture

The Nack was a 100-point forensics and network task at TUCTF 2016. The description speaks of mysterious traffic and supplies a single pcapng file. Opening it in Wireshark takes seconds to summarize. The file contains almost nothing but SYN packets sent to a port that is closed, and the replies from the other side, which refuse the connection. One host keeps retrying and the other keeps saying no.

Failed handshakes have no application layer, so on a normal capture there is nothing to read. A useful reflex is to ask which packets carry payload. Applying a filter for packets with a non-empty TCP data field shows that these SYN packets are not empty. Each one has a few bytes attached, which is unusual. The TCP specification, RFC 793, permits data in a SYN segment, though the receiving stack is not supposed to deliver it before the handshake completes. Later, TCP Fast Open, described in RFC 7413, made data in the SYN a sanctioned optimization. In a capture of refused connections it is a covert channel.

Pulling the payload out

Tshark can print any dissected field, and the data bytes of the SYN packets are one of them. A display filter selects the handshake packets and the field output lists the payloads in capture order, one hex string per packet.

tshark -r capture.pcapng -Y 'tcp.connection.syn' -T fields -e data

The output is a column of 10-byte values in hex. The first 5 bytes are the same in each line, 47 4F 41 54 01. That prefix is ASCII for GOAT, followed by a byte with value 1, and it fits the theme of the contest. Confirming it takes one more pipeline step: cut the first ten hex characters and remove duplicates, and a single line remains. A constant prefix like this is framing, either a sequence marker or a magic value, and the five bytes after it are the real payload.

Reassembly

Dropping the first ten hex characters of each line, joining everything and converting from hex to binary rebuilds the carried file.

tshark -r capture.pcapng -Y 'tcp.connection.syn' -T fields -e data \
  | cut -b 11- | tr -d '\n' | xxd -r -p > temp

The first packet’s remainder reads 47 49 46 38, the ASCII text GIF8. The second contributes 39 61, which completes the signature GIF89a. Right after the signature come the logical screen width and height as little-endian 16-bit numbers, and the second and third packets supply them. Further into the stream, the bytes 4E 45 54 53 43 41 50 45 32 2E 30 spell NETSCAPE2.0, the application extension that tells a viewer to loop an animation. Those are the marks of an animated GIF, and the result opens as one: the well-known ROFLcopter animation.

One caution applies to any reassembly of this kind. The order of packets in the capture is the order of the data, and retransmitted SYN packets would duplicate a segment. If the output file is corrupt, check for repeated packets with identical sequence numbers and remove them.

Looking inside the animation

The picture alone contains no flag, yet the player shows something that flickers for a moment. A GIF is a sequence of frames, each preceded by a graphic control extension that sets a delay in hundredths of a second and a disposal method. Many files store only the changed rectangle for each frame, so viewers composite them. A brief, small overlay is easy to miss while watching, and impossible to read if it lives in a partial frame.

ImageMagick can compose every frame onto the full canvas and write each as its own file.

convert -coalesce temp out%05d.pgm

The numbering starts at zero, so the 17th frame is out00016. Opening it shows the flag text drawn on top of the animation. The format of the flag is TUCTF followed by braces, with the content left out here.

Wrong turns to avoid

Analysts often spend time on the refusing side of the conversation, looking for responses that carry data, when the replies are empty. Others try to follow a TCP stream in Wireshark, which shows nothing because no connection is ever established. Reading the SYN data field directly avoids both. Extracting the payload with a GUI export is also error-prone, as each packet has to be handled separately, while a field listing keeps order and removes the work.

What transfers to other challenges

In capture challenges, check which fields are populated that should not be. Payload in handshakes, data in ICMP echo requests, text in DNS labels and sizes of packets are all common carriers. Look for a constant prefix, strip it, and see what file magic appears at the start of the rest. File signatures such as GIF89a, the PNG signature or PK for zip identify the container at once. Once the file is rebuilt, treat it as a second challenge and examine its structure: for animations, that means looking at every frame. A similar two-stage pattern of extracting a file and then analyzing its contents occurs in the PNG difference task. Recognizing compressed payload bytes by their flat distribution is covered in the guide to binary data visualization.

Questions readers ask

Can a SYN packet legitimately carry data?

The TCP specification allows payload in a SYN segment, although a receiver must not hand it to the application before the connection is established. TCP Fast Open, defined in RFC 7413, later made it a deliberate feature to save a round trip on repeat connections.

Why does the GIF need to be coalesced?

Animated GIFs often store each frame as a small rectangle that updates only the changed region. Coalescing composes every frame onto a full canvas, so a frame that contains only a brief overlay becomes a complete picture that can be read.

What is the first thing to check in a capture with odd traffic?

Look at the protocol hierarchy and conversation list, then find out which packets carry payload. Fields that should be empty, such as data in handshake packets, ICMP echo bodies or DNS names, are the usual covert channels in capture challenges.

New to capture the flag? Formats, teams and a first-month plan in one place.

Read the CTF guide