Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Protocol Analysis

TCP: Handshake and Sequence Numbers

Read TCP in Wireshark: header fields, flags, the three-way handshake, options like MSS and window scaling, relative sequence numbers and connection teardown.

TCP carries most of the traffic you will analyze: web pages, email, databases, SSH, APIs. It provides a reliable, ordered byte stream on top of unreliable IP, and every bit of that reliability is visible in a capture: handshakes, sequence numbers, acknowledgments, windows, retransmissions and resets. Learn to read a normal connection first, and the abnormal ones in the troubleshooting chapter will stand out.

The TCP header

The minimum header is 20 bytes. Important fields and their filters:

Field Filter Meaning
Source / destination port tcp.srcport, tcp.dstport Identify the applications
Sequence number tcp.seq, tcp.seq_raw Position of the first data byte (or of the SYN/FIN)
Acknowledgment number tcp.ack The next byte the sender expects to receive
Header length tcp.hdr_len 20 to 60 bytes (options)
Flags tcp.flags, tcp.flags.syn ... SYN, ACK, FIN, RST, PSH, URG, plus ECE, CWR
Window size tcp.window_size How many more bytes the sender of this packet can receive
Checksum tcp.checksum Covers header, data and a pseudo header
Options tcp.options.mss_val, tcp.options.wscale.shift ... Negotiated at the handshake

A connection is identified by four values: source IP, source port, destination IP, destination port. Wireshark numbers each connection tcp.stream.

The three-way handshake

sequenceDiagram
    participant C as Client (port 50321)
    participant S as Server (port 80)
    C->>S: SYN, seq=x, options (MSS, window scale, SACK)
    S->>C: SYN+ACK, seq=y, ack=x+1
    C->>S: ACK, seq=x+1, ack=y+1
    Note over C,S: Connection established

The handshake: SYN, SYN+ACK, ACK.

  1. SYN: the client picks an initial sequence number (ISN) and proposes options.
  2. SYN, ACK: the server picks its own ISN, acknowledges the client's ISN plus one, and answers the options.
  3. ACK: the client acknowledges the server's ISN plus one. Now both sides can send data.

A SYN and a FIN each use up one sequence number, which is why the acknowledgment is "ISN + 1". In Wireshark the handshake is easy to find with tcp.flags.syn == 1, and the time between the first SYN and the SYN-ACK is the network round-trip time (RTT) to the server (tcp.analysis.initial_rtt shows it on the final ACK).

Options negotiated in the handshake

  • MSS (maximum segment size): the largest data chunk a side will receive in one segment, typically 1460 on Ethernet.
  • Window scale: a shift count that multiplies the 16-bit window size so windows can exceed 64 KB. Wireshark shows both the raw Window value and the Calculated window size. It is only valid if both SYNs carried the option, so a capture that starts after the handshake cannot compute it.
  • SACK permitted: allows selective acknowledgments for efficient recovery of lost packets.
  • Timestamps: used for RTT measurement and protection against old duplicate segments.

Sequence and acknowledgment numbers

The sequence number counts bytes. The raw ISN is a large random 32-bit value, which is hard to read, so Wireshark shows relative sequence numbers by default: Seq = 1 is the first data byte after the SYN (turn it off in Edit > Preferences > Protocols > TCP > Relative sequence numbers; the raw numbers stay in tcp.seq_raw).

The rule is simple:

  • A segment with N bytes of data and sequence number S carries bytes S through S+N-1.
  • The receiver acknowledges with ack = S + N, "I have everything before this byte; send this one next".
  • Acks are cumulative: one ack covers everything before it. TCP stacks often send one ack per two segments (delayed ACK).

The stand-in simulates a request and a response and prints rows in the style of the packet list, with relative numbers. The model is simplified (one data segment each way, no loss).

Python Handshake, data and teardown with relative numbers (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check the arithmetic against the rule: the request has seq 1, length 100, so the server acknowledges 101. The response has seq 1 and length 300, so the client acknowledges 301. The two FIN packets each add one.

Parsing a real header with options

TCP options follow the 20-byte header as a list of kind-length-value entries. The stand-in builds a typical SYN, with MSS 1460, SACK permitted and window scale 7, and decodes it with flags and options, the same fields shown in Packet Details.

Python Decode a TCP SYN with options (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Closing a connection

  • FIN: a graceful close. Each direction is closed separately (FIN, ACK), so you normally see two FINs. A connection that has sent a FIN can still receive data (half-close).
  • RST: an abrupt close. A reset means "this connection does not exist or I refuse it". Common causes: connecting to a closed port, an application crashing or closing with unread data, a firewall or load balancer timing out the session, or an intrusion prevention system injecting a reset. Wireshark colors resets, and tcp.flags.reset == 1 finds them.
  • TIME_WAIT is a state on the host, not a packet; it explains why ports remain "in use" for about a minute after a close.

Handy filters and columns

TEXT
tcp.flags.syn == 1 and tcp.flags.ack == 0     connection attempts
tcp.flags.syn == 1 and tcp.flags.ack == 1     server accepted
tcp.flags.fin == 1                            graceful closes
tcp.flags.reset == 1                          resets
tcp.stream eq 3                               one connection
tcp.len > 0                                   packets that carry data
tcp.analysis.initial_rtt                      handshake round-trip time

Add columns for tcp.stream, tcp.len, and tcp.window_size to see the story of each connection at a glance.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three packets of the TCP handshake, in order?

    The client sends SYN, the server answers SYN+ACK, and the client confirms with ACK.
  2. 2. A segment has relative seq = 1 and carries 100 bytes of data. What ack number does the receiver send back?

    The ack is the next expected byte: 1 + 100 = 101.
  3. 3. Why can Wireshark fail to compute the scaled window size for a connection?

    The scale factor is negotiated in the SYN packets. Without them Wireshark cannot know it.
  4. 4. What does a TCP RST normally mean?

    RST ends a connection immediately, for example when the port is closed or the application aborted.

Summary

  • A TCP connection is identified by two IP addresses and two ports; Wireshark numbers them as tcp.stream.
  • The three-way handshake (SYN, SYN+ACK, ACK) also negotiates MSS, window scaling, SACK and timestamps.
  • Sequence numbers count bytes; the ack is the next byte expected; SYN and FIN consume one number each.
  • Wireshark shows relative sequence numbers by default; raw ones are in tcp.seq_raw.
  • FIN closes gracefully in each direction, RST aborts immediately.

Next: UDP and DNS