The TCP Handshake and State Machine
The three-way handshake does more than start a connection. When it completes, the server knows the client could receive packets at the address it claimed, because the client echoed a number only the server knew. That property is what makes a TCP connection's source address meaningful in a log (unlike a UDP packet's), and every handshake-level attack, from blind spoofing to SYN floods to port scans, is about some part of it: guessing the number, not finishing the handshake, or reading how a host answers the first packet. The basics are in TCP Handshake & Teardown.
Watch it
The lab draws one connection between a lab client and server: SYN, SYN-ACK, ACK, one data segment and its acknowledgement, then the four-segment teardown. Each side's state changes as it sends or receives. Change the initial sequence numbers and the data size and follow the arithmetic.
With client ISN x = 1000 and server ISN y = 5000:
| Segment | Flags | seq | ack | Client state after | Server state after |
|---|---|---|---|---|---|
| 1 client → server | SYN | 1000 | – | SYN_SENT | SYN_RCVD |
| 2 server → client | SYN, ACK | 5000 | 1001 | ESTABLISHED | SYN_RCVD |
| 3 client → server | ACK | 1001 | 5001 | ESTABLISHED | ESTABLISHED |
| 4 data, 100 bytes | PSH, ACK | 1001 | 5001 | ||
| 5 its acknowledgement | ACK | 5001 | 1101 | ||
| 6 client closes | FIN, ACK | 1101 | 5001 | FIN_WAIT_1 | CLOSE_WAIT |
| 7, 8, 9 | ACK; FIN, ACK; ACK | FIN_WAIT_2 → TIME_WAIT | LAST_ACK → CLOSED |
SYN and FIN each consume one sequence number, data consumes one per byte. The side that closes first waits in TIME_WAIT (twice the maximum segment lifetime) so that late duplicates of the old connection can't be mistaken for a new one.
The same numbers in the capture
Wireshark and tshark show relative sequence numbers by default (starting at 0). Turning that off shows the absolute ISNs of the first login connection in http.pcap:
tshark -r $SAMPLES/pcap/http.pcap -Y 'tcp.port == 50414' -o tcp.relative_sequence_numbers:FALSE \
-T fields -e frame.number -e ip.src -e tcp.flags.str -e tcp.seq -e tcp.ack -e tcp.len11 10.10.0.5 ··········S· 2000000 0 0
12 10.10.0.10 ·······A··S· 4000000 2000001 0
13 10.10.0.5 ·······A···· 2000001 4000001 0
14 10.10.0.5 ·······AP··· 2000001 4000001 173
15 10.10.0.10 ·······A···· 4000001 2000174 0
16 10.10.0.10 ·······AP··· 4000001 2000174 114
17 10.10.0.5 ·······A···· 2000174 4000115 0
18 10.10.0.5 ·······A···F 2000174 4000115 0
19 10.10.0.10 ·······A···F 4000115 2000175 0
20 10.10.0.5 ·······A···· 2000175 4000116 0
The client's ISN is 2000000 and the server's 4000000. The request is 173 bytes (seq 2000001), so the server acknowledges 2000174; the server's 114-byte response makes the client acknowledge 4000115; each FIN adds one more.
States on a real host
ss shows the kernel's view of each socket. The session starts a web server on loopback, opens one connection from a fixed client port (45001) so the output is repeatable, closes it, and looks at the states. Everything happens inside the sandbox on 127.0.0.1.
python3 -m http.server 8000 --bind 127.0.0.1 >http.log 2>&1 &
for i in $(seq 50); do ss -Htln 'sport = :8000' | grep -q . && break; sleep 0.1; done
echo "1) listening:"; ss -tln 'sport = :8000'
python3 -c "import socket, time
s = socket.create_connection(('127.0.0.1', 8000), source_address=('127.0.0.1', 45001))
time.sleep(1.5)
s.close()" &
sleep 0.5
echo "2) connected (sorted, the order varies):"; ss -tan '( sport = :45001 or dport = :45001 )' | { read -r head; echo "$head"; sort; }
wait %2
sleep 0.2
echo "3) after the client closed:"; ss -tan '( sport = :45001 or dport = :45001 )'
kill %11) listening:
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 5 127.0.0.1:8000 0.0.0.0:*
2) connected (sorted, the order varies):
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
ESTAB 0 0 127.0.0.1:45001 127.0.0.1:8000
ESTAB 0 0 127.0.0.1:8000 127.0.0.1:45001
3) after the client closed:
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
TIME-WAIT 0 0 127.0.0.1:45001 127.0.0.1:8000
Both ends of the connection appear in step 2 because both are on this host. In step 3 the client, which closed first, sits in TIME-WAIT, exactly as in the lab. On a busy server, counting sockets per state (ss -Htan | awk '{print $1}' | sort | uniq -c) is a quick health and attack check: thousands in SYN-RECV point to a SYN flood, thousands in CLOSE-WAIT to an application that never closes its sockets.
Why the handshake matters for security
- Return routability. The final ACK must carry
ack = y + 1. A spoofer who forges the source never sees the SYN-ACK and so never learns y. That is why TCP-based services can trust, within limits, that a completed connection came from its source address. - Unpredictable ISNs. In the 1980s and 1990s many stacks chose ISNs by a simple counter, so an attacker could predict y, complete a handshake blind and inject commands into trust-based services (the well-known 1994 attack on rlogin trust). RFC 6528 specifies ISNs derived from a secret hash of the connection's addresses and ports plus a clock (a SHOULD), which the major modern stacks implement.
- Half-open connections. A SYN that never gets its final ACK leaves the server in SYN_RCVD holding memory: the basis of SYN floods (next lesson).
- The answer to a SYN is information. SYN-ACK means open, RST means closed, silence means filtered. Port scanners are machines that read that answer (Nmap Connect Scans).
How it is detected
- Handshake completion ratio per source: many SYNs and few ACKs means scanning or flooding. Zeek's
conn.logrecords each connection's history (S= SYN only,ShAD…= full handshake with data), which makes this a simple count. - Sockets per state on servers (
ss,netstat), with alerts on SYN-RECV counts. - Connections that never complete to many ports of one host (vertical scan) or one port of many hosts (horizontal scan).
How it is protected
- Random ISNs (default), SYN cookies or SYN proxies for floods, backlog sizing.
- Stateful firewalls that only allow packets matching an established connection, so out-of-state segments are dropped.
- Don't build trust on source addresses alone; authenticate at the application layer (SSH keys, TLS client certificates).
Pitfalls
- Mixing relative and absolute sequence numbers when comparing two captures of the same connection. Wireshark's relative numbers start at 0 for each capture.
- Treating TIME-WAIT sockets as a leak. They are normal and expire after about a minute on Linux.
- Assuming "connection refused" means no service. It means RST: nothing is listening or a firewall rejected the SYN.
Practice
1. In the lab, set the data size to 0. Which acknowledgement number does the server's FIN-ACK carry?
With no data, the client's FIN has seq = x + 1 = 1001 and consumes one number, so the server acknowledges 1002 (and its own FIN has seq = y + 1 = 5001).
2. The final ACK of a handshake carries ack = 4000001. What was the server's ISN?
4000000: the client acknowledges ISN + 1 because the server's SYN consumed one sequence number.
3. Why can't a spoofer simply send the final ACK with a guessed number today?
The ISN is 32 bits derived from a keyed hash (RFC 6528); a blind guess succeeds with probability about 1 in , and each wrong guess is answered with a RST to the real owner of the address.
Test yourself
Client ISN 1000, server ISN 5000. What acknowledgement number does the SYN-ACK carry?
- 1000
- 1001
- 5000
- 5001
Which side ends in TIME_WAIT?
- The side that closes first (sends the first FIN)
- Always the server
- Always the client
- Neither; TIME_WAIT is only for UDP
Why does a completed TCP handshake give some confidence in the source address?
- TCP encrypts the addresses
- The client had to echo the server's secret ISN, which it could only see if it received packets at that address
- Routers check TCP source addresses
- The SYN carries a certificate
A server shows thousands of sockets in SYN-RECV. What is the likely cause?
- An application not closing sockets
- A SYN flood or a scan leaving half-open connections
- Normal TIME_WAIT
- A DNS outage