Thoughts — Three Cookies and a Handshake
This week's CS320 labs are all TCP — the three-way handshake, sequence numbers, retransmission. But while I was taking apart segments, the same word kept showing up in three different places, each one meaning something entirely different: the HTTP cookie, the SYN cookie, and the cookie wall a scraper hits. Three unrelated ideas sharing one name, all orbiting the same handshake. So here are some thoughts on ACK, SYN, and the cookies in between.
What an ACK actually says
The acknowledgement number is the quiet, patient field of the TCP header. It's thirty-two bits, and it says one thing and one thing only: the sequence number of the next byte the receiver expects. "I have everything up to byte N − 1. Send me byte N."
The part I used to gloss over is that TCP acknowledges cumulatively. It doesn't say "I got byte 7 and byte 9 but I'm missing 8" — it just keeps repeating the first byte it's still waiting for. Send bytes 1, 2, 3. If 2 is lost, the receiver keeps ACKing 2, over and over, no matter what arrives after it.
That repetition is not a bug; it's the mechanism. Those duplicate ACKs — three of them, by convention — are what trigger fast retransmit. The sender doesn't wait for a timeout to discover that a segment went missing; it infers it from the receiver asking for the same byte three times. The ACK field is an acknowledgement, a nudge, and an alarm, all in one thirty-two-bit number.
SYN is just a way of saying "let's start counting"
The SYN flag is the handshake's opening line, and the name is the whole story: synchronise sequence numbers. TCP's sequence numbers start at a random initial value — an ISN — so that two stale segments from an old, dead connection can't masquerade as fresh data. SYN is the client saying "I'd like to start counting at 51247", and the server saying back "fine, I'm counting from 90210, and by the way I acknowledge your 51248".
That's the three-way handshake: SYN, then SYN-ACK, then ACK. Two sequence numbers synchronised in three packets, after which bytes can flow in both directions.
The catch is that the server is expected to remember the half-open connection the moment it sees the SYN, before it knows the client is genuine. That single act of trust is the entire SYN flood attack: send a storm of SYNs, never finish the handshake, and watch the server's memory fill with half-open connections that will never become real. The handshake's optimism is its vulnerability.
Three things named cookie
The word crops up three times in my notes, and each time it means something unrelated.
First, the HTTP cookie — the one everyone knows. HTTP is stateless; every request stands alone. The cookie is a small piece of data the server hands the client in a Set-Cookie header, and which the client echoes back in a Cookie header on every later request. It's how a login persists, how a cart remembers itself, how a site pretends to know you. State, bolted onto a stateless protocol.
Second, the cookie wall — the scraper's annoyance. A site wraps its content behind a consent or authentication cookie, so a naive request that shows up with no cookies gets back the wall instead of the data. The content is there; it's just gated. To get past it you have to capture the cookie a real browser earns and replay it — or drive a real browser and let it earn one honestly.
Third, the SYN cookie — the clever one. This is the defense against the SYN flood, and it solves the trust problem by refusing to remember anything.
The trick: hide the state in the sequence number
A SYN flood works because the server stores state per half-open connection. The SYN cookie's answer is simple and almost rude: store nothing. When a SYN arrives, the server doesn't allocate a thing. Instead it bakes the connection's details — client IP and port, its own IP and port, a timestamp, a secret key — through a keyed hash, and stuffs the result into the ISN it sends back in the SYN-ACK.
If the client is real, its ACK echoes that number back (plus one, as ACKs always do). The server recomputes the hash from the acknowledged number and checks it matches. Only then does it allocate the buffers. An attacker who never completes the handshake has cost the server exactly zero bytes of state.
[!quote] The whole defense is a move: take the state you used to hold in memory, and instead carry it inside the sequence number, round-trip it through an untrusted party, and only believe it when it comes back intact.
It's a brilliant inversion. The server stops trusting the SYN and starts trusting the round trip. Memory, the thing the attacker was exhausting, is no longer touched until proof of life arrives.
Not RSA, not SecurID
I assumed the "cryptographic value" in a SYN cookie must be some heavyweight machinery — public-key crypto, or one of those rotating SecurID tokens. It's neither.
A SYN cookie uses a keyed cryptographic hash — a MAC, the HMAC family of things — not RSA, and not SecurID. RSA is asymmetric and slow; you can't afford it on every incoming SYN under a flood. SecurID is a time-based one-time password that identifies a user logging in, a different problem entirely. The SYN cookie needs to be cheap, and unforgeable only to someone without the server's secret key.
Bernstein's scheme lays the number out in bits: roughly a five-bit timestamp, a three-bit encoding of the maximum segment size, and about twenty-four bits of keyed hash over the source address, source port, destination address, destination port, timestamp, and the secret. Truncate it, slot it into the ISN, send it. The client echoes it back; the server recomputes and compares. Fast enough for every SYN, impossible to forge without the key, and verifiable by nothing more than arithmetic.
[!thought] The same instinct keeps appearing across the whole stack: if you don't want to pay for state, move it somewhere else. HTTP moves state into the client's pocket. The SYN cookie moves it into a sequence number. The cookie wall isn't even a defense — it's just the same property pointed at you instead.
What strikes me most is how one small word ties them together. Three different layers, three different problems, one idea: state is expensive, and everyone is trying to smuggle it somewhere cheaper. The ACK counts bytes. The SYN synchronises counters. And the cookie — whichever cookie — is the way the stateless pretend to remember.
— Eric