SYN Floods and SYN Cookies
A server that receives a SYN has to remember it: it allocates a small record, sends a SYN-ACK and waits for the final ACK. A SYN flood sends SYNs, usually from spoofed source addresses, and never completes the handshake. The waiting records pile up in the listen socket's SYN backlog, and once it is full, real clients' SYNs are dropped. The attack is old (widely seen in 1996, documented in RFC 4987) and still common, because it is cheap and needs no reply traffic. The defence that changed the game is equally clever: SYN cookies let the server keep no state at all until the handshake completes.
This lesson builds on The TCP Handshake and State Machine; denial of service in general is introduced in Denial of Service and DDoS. Everything below is a simulation, a synthetic capture or a read of the sandbox's own kernel settings.
The mechanism, in the lab
Switch the lab to SYN-flood mode. Spoofed SYNs from 198.51.100.x arrive at rate per second; each holds a backlog slot in SYN_RCVD until a timeout (shortened to 5 s on screen; Linux retries the SYN-ACK for about a minute). A real client tries every 1.5 s. Watch the backlog fill, then turn cookies on.
With the defaults (8 slots, 4 spoofed SYNs per second, 5 s timeout) the backlog is full after two seconds and stays full: 4 per second × 5 s = 20 half-open connections would be needed, and only 8 fit. Most of the real client's attempts are dropped. A bigger backlog only moves the threshold: the attacker raises the rate. With cookies on, the backlog stays empty and every client gets in.
How SYN cookies work
Instead of storing the SYN, the server encodes what it needs into its own initial sequence number y and forgets the connection. If the final ACK arrives with ack = y + 1, the server recomputes the cookie, checks it, and only then creates the connection.
Linux packs three things into the 32-bit ISN: a slowly increasing counter (minutes, so old cookies expire), an index for the MSS the client offered (the only option it must remember), and a keyed hash of the connection's addresses and ports. The program shows the idea with HMAC-SHA-256 and a fixed lab key (the kernel uses its own hash function and a random secret; this is a model, not its exact format).
import hmac, hashlib, struct, ipaddress
SECRET = b"lab-only-fixed-secret" # real servers draw a fresh secret at boot
MSS_TABLE = [536, 1300, 1440, 1460] # a small table of common MSS values
def mac24(t, src, sport, dst, dport):
msg = struct.pack("!I4sH4sH", t, ipaddress.IPv4Address(src).packed, sport,
ipaddress.IPv4Address(dst).packed, dport)
return int.from_bytes(hmac.new(SECRET, msg, hashlib.sha256).digest()[:3], "big")
def make_cookie(t, src, sport, dst, dport, mss):
idx = max(i for i, m in enumerate(MSS_TABLE) if m <= mss)
return ((t % 32) << 27) | (idx << 24) | mac24(t % 32, src, sport, dst, dport)
def check_ack(now, ack, src, sport, dst, dport):
y = (ack - 1) & 0xFFFFFFFF
t, idx, h = y >> 27, (y >> 24) & 7, y & 0xFFFFFF
if (now - t) % 32 > 1: # older than about 2 minutes: expired
return "expired"
if idx >= len(MSS_TABLE) or h != mac24(t, src, sport, dst, dport):
return "bad cookie: dropped"
return f"valid: create connection with MSS {MSS_TABLE[idx]}"
client = ("10.10.0.5", 50500, "10.10.0.10", 80)
y = make_cookie(17, *client, mss=1460)
print(f"server ISN (cookie) y = {y} = 0x{y:08x}; server stores nothing")
print("real client's ACK: ", check_ack(17, y + 1, *client))
print("ACK from another port: ", check_ack(17, y + 1, "10.10.0.5", 50501, "10.10.0.10", 80))
print("guessed ACK: ", check_ack(17, ((17 << 27) | (3 << 24) | 0x123456) + 1, *client))
print("ACK 5 minutes later: ", check_ack(22, y + 1, *client))server ISN (cookie) y = 2340602573 = 0x8b82c2cd; server stores nothing
real client's ACK: valid: create connection with MSS 1460
ACK from another port: bad cookie: dropped
guessed ACK: bad cookie: dropped
ACK 5 minutes later: expired
The costs: the server can encode only a few MSS values and, in the classic form, loses other SYN options (window scaling, SACK) unless TCP timestamps are used to carry them, which Linux does. That is why Linux's default, tcp_syncookies = 1, switches to cookies only when the backlog overflows.
How it is detected
On the wire, a SYN flood is a large number of SYNs with few matching ACKs, often from many sources that never send anything else. The session builds a synthetic capture (fixed timestamps): two normal handshakes from lab clients, and fifty SYNs from spoofed documentation addresses, then measures SYNs and final ACKs per source.
python3 - <<'PY'
from scapy.all import Ether, IP, TCP, wrpcap
t0, pkts = 1768471200.0, []
C, S = "02:00:00:0a:0a:01", "02:00:00:0a:0a:0a" # router side, server side
def add(p, dt):
p.time = t0 + dt; pkts.append(p)
for i, cli in enumerate(["10.10.0.5", "10.10.0.6"]): # two real clients complete handshakes
add(Ether(src=C, dst=S)/IP(src=cli, dst="10.10.0.10")/TCP(sport=50600 + i, dport=80, flags="S", seq=1000), i)
add(Ether(src=S, dst=C)/IP(src="10.10.0.10", dst=cli)/TCP(sport=80, dport=50600 + i, flags="SA", seq=9000, ack=1001), i + 0.001)
add(Ether(src=C, dst=S)/IP(src=cli, dst="10.10.0.10")/TCP(sport=50600 + i, dport=80, flags="A", seq=1001, ack=9001), i + 0.002)
for n in range(50): # spoofed SYNs, never completed
add(Ether(src=C, dst=S)/IP(src=f"198.51.100.{n + 10}", dst="10.10.0.10")/TCP(sport=1024 + n, dport=80, flags="S", seq=777 * n), 2 + n * 0.02)
wrpcap("flood.pcap", pkts)
PY
echo -n "SYN without ACK: "; tcpdump -nn -r flood.pcap 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn' 2>/dev/null | wc -l
echo -n "pure ACKs (completed handshakes): "; tcpdump -nn -r flood.pcap 'tcp[tcpflags] == tcp-ack' 2>/dev/null | wc -l
echo -n "distinct SYN sources: "
tshark -r flood.pcap -Y 'tcp.flags.syn == 1 && tcp.flags.ack == 0' -T fields -e ip.src | sort -u | wc -l
echo "SYNs per second:"
tshark -r flood.pcap -q -z io,stat,1,'tcp.flags.syn == 1 && tcp.flags.ack == 0' | grep -E '^\| +[0-9]'SYN without ACK: 52
pure ACKs (completed handshakes): 2
distinct SYN sources: 52
SYNs per second:
| 0 <> 1 | 1 | 54 | |
| 1 <> 2 | 1 | 54 | |
| 2 <> Dur| 50 | 2700 | |
52 SYNs, 2 completed handshakes, 52 distinct sources, and a burst of 50 per second from nowhere: the classic profile. On the server itself the sign is many sockets in SYN-RECV (ss -Htan state syn-recv | wc -l) and a kernel log line containing "Possible SYN flooding on port … Sending cookies." (the prefix and the port format vary between kernel versions)
How it is prevented
The sandbox can read (but not change) its own kernel's settings:
cd /proc/sys/net/ipv4
for k in tcp_syncookies tcp_max_syn_backlog tcp_synack_retries; do
printf '%-22s %s\n' "$k" "$(cat $k)"
donetcp_syncookies 1
tcp_max_syn_backlog 4096
tcp_synack_retries 5
- SYN cookies on (
tcp_syncookies = 1, the Linux default; 2 = always). - Bigger backlogs (
tcp_max_syn_backlogand the application'slisten()backlog, capped bynet.core.somaxconn) and fewer SYN-ACK retries (tcp_synack_retries; each retry keeps a half-open entry alive longer: with 5 retries it is about 63 seconds). - SYN proxies on firewalls and load balancers (nftables
synproxy, cloud DDoS services) that complete the handshake themselves and only pass real connections on. - Upstream filtering and BCP 38 to reduce spoofed traffic; scrubbing services for large volumetric floods, which fill the link before any host-level defence matters.
Pitfalls
- Turning cookies off "for performance" on Internet-facing servers.
- Rate-limiting SYNs per source address: the sources are spoofed and random, so each one sends only one SYN.
- Forgetting that a flood can fill the link rather than the backlog. Host settings don't help when the pipe itself is full.
Practice
1. A server has a SYN backlog of 1024 and keeps half-open entries for 63 s. What SYN rate fills it?
About 1024 / 63 ≈ 16 SYNs per second, trivial for any attacker. This is why backlog size alone never solves the problem.
2. In the cookie program, why does the ACK from port 50501 fail?
The hash covers the addresses and ports. The same cookie for a different 4-tuple produces a different hash, so it is rejected. An attacker can't forge a valid ACK without the secret.
3. Why are the spoofed sources in the capture all different?
Random spoofed addresses defeat per-source limits and hide the attacker. The SYN-ACKs go to those addresses (backscatter), which is how Internet telescopes measure SYN floods worldwide.
Test yourself
What resource does a classic SYN flood exhaust?
- The server's disk
- The listen socket's backlog of half-open connections
- The DNS cache
- The TLS session cache
Where does a server using SYN cookies keep the information about a new SYN?
- In a larger backlog
- Encoded in its own initial sequence number, returned by the client's ACK
- In a DNS TXT record
- In the client's IP options
In the synthetic capture, how many handshakes completed?
- 0
- 2
- 50
- 52
What does Linux's default tcp_syncookies = 1 mean?
- Cookies are never used
- Cookies are used only when the SYN backlog overflows
- Cookies are always used
- Cookies replace TLS