Lesson 6 of 8 · 8 min read
TCP/IP for Security Engineers
In this lesson
TL;DR: IP will carry a packet with any source address the sender writes, so the address is a claim, not proof. That makes spoofing possible wherever networks skip ingress filtering (BCP 38), and spoofed UDP turns servers into reflectors and amplifiers. TCP's handshake and 32-bit sequence numbers stop an attacker who cannot see the traffic from injecting into a connection, but only by making guessing expensive, and side channels have repeatedly made it cheaper. Neither layer protects against anyone on the path: they can read, change and reset at will. So availability defences (SYN cookies, filtering, rate limits) live here, and confidentiality and integrity live above, in TLS or an equivalent.
Before you read
Pick an answer before you read further. The lesson below explains it, and guessing first, right or wrong, helps the explanation stay with you.
1An internal service accepts commands over plain TCP only from the IP address of an admin server. Why is that weak, even inside a private network?
What each layer promises, and what it does not
Most protocol attacks start from a gap in what the layers below promise. Four facts carry most of the weight.
- IP delivers packets on a best-effort basis to a destination address. It does not check that the source address belongs to the sender. Routers forward on the destination.
- UDP adds ports and a checksum, nothing else. There is no handshake, so a server answers whatever address the request claims to come from.
- TCP adds a handshake, sequence numbers, ordering and retransmission. The handshake proves the other side can receive packets at the claimed address, which is why spoofing a full TCP conversation is much harder than spoofing UDP.
- ICMP reports errors and diagnostics. Those messages change how endpoints behave, which makes them both useful and a source of side channels.
None of them provides confidentiality or integrity against an attacker who can see the traffic. The checksums detect accidental corruption, not deliberate change.
- 1
1 · IP
Routing by destination address
- Source address is just a field
- Fragmentation splits packets
No proof of who sent it
- 2
2 · UDP
Ports and a checksum
- No handshake
- Replies go to the claimed source
Spoofable, reflectable
- 3
3 · TCP
Handshake, sequence numbers, ordering
- Blind injection is hard, not impossible
- On-path attackers unaffected
Probabilistic protection off-path
- 4
4 · ICMP
Errors and diagnostics
- Changes endpoint behaviour
- Rate limits can leak state
Useful and abusable
- 5
5 · TLS and above
Authenticated encryption
- Confidentiality and integrity
- Peer identity by certificate
Where real guarantees start
Addresses are claims
IP and UDP accept any source address, which enables spoofing and reflection.
Spoofing and ingress filtering
Because the source address is just a field, an attacker can send packets claiming to come from anywhere. They will not see the replies, which go to the real owner of that address, but for many attacks they do not need to.
The defence is at the edge of the network the packets come from. BCP 38 (RFC 2827, May 2000) asks networks to drop packets leaving their customers whose source addresses those customers could not legitimately use, and RFC 3704 extends the idea to networks with several upstream providers. Where it is deployed, spoofing from that network is impossible. Where it is not, the whole internet inherits the problem, which is why spoofed traffic still underpins large denial-of-service attacks. Inside your own network, the same idea appears as anti-spoofing checks on routers and switches: a port or subnet may only send from its own addresses.
The handshake and SYN floods
A TCP connection starts with three packets: the client sends SYN with its initial sequence number, the server replies SYN-ACK with its own, and the client sends ACK. Between the second and third packets the server holds a half-open connection.
A SYN flood sends many SYNs, often from spoofed addresses, so the replies go nowhere and the server's queue of half-open connections fills; legitimate clients are then turned away. RFC 4987 (2007) surveys the defences. The best known, SYN cookies, removes the state: the server encodes what it needs (a time counter, the negotiated maximum segment size and a keyed hash over the connection's addresses and ports) into its own initial sequence number, keeps nothing, and reconstructs the connection only when a valid ACK arrives that echoes the cookie. A spoofed SYN then costs the server a computation and a reply packet, not memory.
Attacker
Spoofed sources
Server
SYN cookies on
Real client
Legitimate user
- Flood
Attacker to Server
1Server to Attacker
2
Spoofed SYNs cost the server nothing to remember
The cookie replaces the half-open queue entry, so forged SYNs cannot exhaust memory.
SYN cookies handle exhaustion of connection state. They do not help when the flood saturates the link itself; that needs upstream scrubbing or a provider that can absorb the volume, and, at the source, BCP 38.
Sequence numbers and blind injection
Every byte in a TCP connection is numbered in a 32-bit space, about 4.3 billion values, and each side starts at a randomised initial sequence number. A packet is accepted only if its sequence number falls in the receiver's current window. RFC 9293 (August 2022), which consolidated TCP's specification and replaced RFC 793, includes the requirement to generate initial sequence numbers unpredictably.
An off-path attacker, who cannot see the traffic, must guess. An on-path attacker reads the numbers directly and can inject, alter or reset at will; sequence numbers do nothing against them.
Worked example: the arithmetic of a blind reset
Suppose an attacker wants to tear down a long-lived connection, such as a BGP session between two routers, by injecting a reset (RST) packet. They know both addresses and the server port.
| Situation | Guesses needed | At 100,000 packets a second |
|---|---|---|
| Before RFC 5961: any RST inside a 65,535-byte window was accepted, client port known | 2^32 / 2^16 = 65,536 | under one second |
| Same, but the client port is one of about 28,000 ephemeral ports | 65,536 × 28,232 ≈ 1.85 billion | about five hours |
| After RFC 5961: an RST must match the expected sequence number exactly; an in-window mismatch gets a challenge ACK instead | up to 2^32 ≈ 4.3 billion per port | far longer |
RFC 5961 (2010) changed the rules for that reason: an in-window but inexact RST or SYN is answered with a challenge ACK, and only a real peer can respond correctly. Then CVE-2016-5696 showed how arithmetic can be undone. Linux limited challenge ACKs with a single global counter shared by all connections. By watching how many challenge ACKs its own connection received, an off-path attacker could tell whether a spoofed probe to someone else's connection had triggered one, and so learn the port and sequence numbers far faster than guessing. Linux 4.7 fixed it with per-connection limits and randomness. The lesson for interviews: shared rate limits can turn into side channels.
Any in-window RST accepted
Addresses and ports known
One guess per window size
about 65,536 guesses
Session torn down in a second
Outcome, unsafe: Reset in under a second
The window made guessing cheap.
Exact match, challenge ACK, authenticated session
Addresses and ports known
Inexact RST draws a challenge ACK
RFC 5961 behaviour
TCP-AO rejects forged segments
Outcome, safe: Guessing no longer works
Per-connection limits avoid the shared-counter side channel.
Probability as protection
Accepting any reset within the window cut the attacker's work by a factor of the window size.
For sessions where a reset is costly, such as routing, the stronger answer is authentication at the TCP layer: the TCP Authentication Option (RFC 5925) adds a keyed MAC to each segment, and the Generalized TTL Security Mechanism (RFC 5082) drops packets that could not have come from a directly connected neighbour.
Reflection and amplification
UDP's lack of a handshake makes it the natural tool for reflection: send a request with the victim's address as the source, and the server sends its reply to the victim. If the reply is much larger than the request, the attack is also amplified. The amplification factor is simply response bytes divided by request bytes. With an illustrative factor of 50, an attacker who can send 1 Gbit/s of spoofed requests directs 50 Gbit/s of replies at the victim, from servers the victim cannot block one by one.
Protocol designers now build the limit in. QUIC, the transport under HTTP/3, does not let a server send more than three times the bytes it has received from an address until that address is validated, and requires a client's first packets to be padded to at least 1,200 bytes, so a small spoofed packet cannot draw a large reply. Operators of UDP services apply the same idea: rate-limit responses per source, avoid answering large queries over UDP to unvalidated sources, and close services that do not need to be public.
Fragmentation and off-path DNS poisoning
Large IP packets may be split into fragments and reassembled at the destination. An off-path attacker who can predict how a reply will fragment can try to substitute a forged later fragment, which carries no new port or DNS transaction ID to guess. RFC 9715 (January 2025) therefore advises DNS over UDP to avoid IP fragmentation altogether, by keeping responses small and falling back to TCP.
SAD DNS (CVE-2020-25705) is the other classic. Off-path DNS cache poisoning normally needs the attacker to guess both the 16-bit transaction ID and the resolver's random source port. The researchers used the global rate limit on ICMP error messages as a side channel to discover which port the resolver was using, removing most of the randomness. It is the same pattern as the challenge-ACK bug: a shared limit leaking per-connection state. See DNS security for DNSSEC and encrypted DNS.
What TCP/IP does not give you
- No confidentiality or integrity against the path. Anyone on the path, a Wi-Fi access point, an ISP, a compromised router, can read and modify traffic and reset connections. See attacker positions on the network.
- No identity. An address says where to deliver, not who is sending. Address-based allowlists are a coarse filter, not authentication.
- NAT is not a firewall. It rewrites addresses; protection comes from the stateful filtering that usually accompanies it, and outbound connections remain open.
The practical conclusion is to authenticate and encrypt every channel that matters, with TLS or mutual TLS, IPsec or WireGuard, and to treat network-layer controls as protection for availability and as defence in depth, not as proof of who is talking.
Misconceptions interviewers listen for
- "TCP sequence numbers are a security feature." They make blind guessing expensive; they do nothing against on-path attackers.
- "UDP services cannot be used against third parties." Spoofed requests turn them into reflectors and amplifiers.
- "NAT hides and protects us." It changes addresses; filtering and authentication do the protecting.
- "An internal IP allowlist authenticates the caller." Addresses can be spoofed or shared; authenticate the channel.
What if · interview follow-ups
Hold your answer as the constraints change
You are responsible for an internet-facing service, its DNS resolvers and the BGP sessions of its edge routers. The interviewer probes what the network layers protect and where they fail.
Start here
A SYN flood arrives at 2 million packets a second. What protects the server, and what does not?
Connection state versus link capacity.
SYN cookies stop the half-open queue from filling, because the server stores no state until a client echoes the cookie. They do not help if the flood saturates the link, which needs upstream scrubbing or a provider that can absorb it. At the source, BCP 38 ingress filtering would have prevented the spoofed addresses in the first place.
Carry it forwardSYN cookies protect state; capacity and filtering protect the link.
Weak answerSaying a firewall rule on the server will stop it.
Constraint changes
How would an off-path attacker reset a BGP session, and how do you prevent it?
Guessing, exact matching and authentication.
By spoofing a reset with a guessed sequence number. Before RFC 5961, any in-window value was accepted, which reduced the work to about 65,536 guesses with known ports. Exact matching with challenge ACKs raises the work, and TCP-AO adds a keyed MAC so forged segments are rejected; GTSM drops packets that could not come from a directly connected neighbour.
Carry it forwardAuthenticate long-lived critical sessions rather than relying on sequence numbers.
Weak answerRelying on random initial sequence numbers alone.
Constraint changes
Your UDP service is being used to reflect traffic at others. What do you change?
Response size, rate and exposure.
Measure the ratio of response to request size and reduce it, for example by refusing large answers over UDP to unvalidated sources; rate-limit responses per source; and close the service to the internet if it does not need to be public. QUIC's rule, never more than three times the received bytes before validation, is the pattern to copy.
Carry it forwardBound what an unvalidated address can make you send.
Weak answerBlocking the victim's address, which is the spoofed source.
Constraint changes
How can an off-path attacker poison a DNS resolver despite random ports and IDs?
Fragments and side channels.
By removing randomness rather than guessing it: forging a later IP fragment that carries no port or ID, or, as in SAD DNS, using a global ICMP rate limit to discover the resolver's source port. Defences are DNSSEC validation, avoiding fragmentation as RFC 9715 advises, falling back to TCP, and patched kernels without shared rate-limit side channels.
Carry it forwardSide channels and fragments erode the randomness that off-path protection relies on.
Weak answerSaying source port randomisation solved off-path poisoning.
Constraint changes
An internal service talks plain TCP to a database, trusting the source IP. What is the risk?
Who is on the path, and what does an address prove?
Anyone on the path can read and modify traffic or reset connections, and anyone who can spoof or reuse the address can talk to the database. Authenticate and encrypt the channel with mutual TLS so both ends prove identity with certificates, and keep network rules as defence in depth.
Carry it forwardNetwork addresses filter traffic; cryptography authenticates it.
Weak answerProposing a tighter IP allowlist as the fix.
Now defend it
As the defender, which network signals would you monitor at this layer?
SYN rates and cookie activation, ratios of inbound to outbound bytes on UDP services, challenge ACK and reset counts on critical sessions, ICMP error volumes, traffic with source addresses that should not appear on an interface, and DNS responses arriving fragmented or over UDP above expected sizes.
How this shows up in interviews
Expect "what happens in a TCP handshake?", "how does a SYN flood work and how do SYN cookies help?", "why is UDP used for amplification?" and "can someone inject into a TCP connection they cannot see?". Strong answers separate off-path from on-path attackers, give the arithmetic of guessing, name a side channel that undid it, and end by placing real protection in an authenticated layer above.
Say it in 30 seconds: "IP doesn't authenticate source addresses, so spoofing works wherever networks skip BCP 38, and spoofed UDP makes reflection and amplification possible. TCP's handshake and random sequence numbers stop blind injection by making guessing expensive, and RFC 5961's exact matching made resets harder still, but side channels like the shared challenge-ACK counter have undone that arithmetic before. None of it protects against someone on the path, so availability defences like SYN cookies live here, and confidentiality and integrity come from TLS above."
Check yourself
Say or write your answer first, then reveal. Explaining it from memory, in your own words, is the skill an interviewer is checking.
1How do SYN cookies let a server survive a SYN flood?
2What did RFC 5961 change about accepting resets?
3Why was CVE-2016-5696 significant beyond Linux?
4How does QUIC limit amplification?