TCP Security & Hardening
Mitigations against SYN flood, RST injection, sequence prediction attacks; SYN cookies and best practices.
A Foundation of Trust: TCP-s Inherent Vulnerabilities
The Transmission Control Protocol (TCP) is a masterpiece of engineering designed to provide reliable data transport over unreliable networks. It is like an incredibly meticulous and formal courier service, ensuring every package is numbered, acknowledged upon receipt, and delivered in the correct order. However, TCP was designed in the 1970s and 1980s, an era when the internet was a small, academic community built on a foundation of mutual trust.
This inherent trust is the source of many of its security weaknesses. The protocol-s designers focused primarily on solving the problems of packet loss and network congestion, not on defending against malicious actors actively trying to disrupt, intercept, or manipulate communications. TCP-s formal, stateful processes, such as the three-way handshake, are elegant for reliability but can be exploited by attackers who do not follow the rules of a polite conversation.
Understanding TCP security involves looking at its carefully constructed mechanisms and seeing how they can be twisted and abused. In this section, we will explore some of the most classic and impactful attacks against TCP and the countermeasures that have been developed over the years to protect this cornerstone of the internet.
SYN Flood: The Overwhelmed Receptionist Attack
The SYN flood is a classic that directly exploits the mechanics of the TCP three-way handshake.
Reviewing the Handshake-s Vulnerability
To understand the attack, let us recall the server-s behavior in a normal handshake.
- A client sends a SYN packet to the server.
- The server receives the SYN. Crucially, at this moment, the server must allocate resources. It creates a data structure in its memory (called a Transmission Control Block, or TCB) to store information about this pending connection, such as the client-s IP address and port number. It then places this connection in the 'SYN_RCVD' state in its connection table, often called the backlog queue.
- The server sends a SYN-ACK packet back to the client.
- The server waits for the final ACK from the client to complete the handshake and move the connection to the 'ESTABLISHED' state.
The vulnerability lies in Step 2. The server commits memory and state before verifying that the client is legitimate and capable of completing the handshake. It is acting in good faith.
Executing the SYN Flood Attack
An attacker exploits this good faith behavior with a simple yet effective strategy.
- The attacker uses one or more machines to send a massive number of SYN packets to the target server.
- The key to the attack is that these SYN packets contain a spoofed (fake) source IP address. The attacker has no control over these fake addresses and has no intention of ever receiving a reply.
- The target server receives this flood of SYN packets. For each one, it dutifully allocates memory, creates a half-open connection in its backlog queue, and sends a SYN-ACK packet to the spoofed source IP address.
- These SYN-ACK packets travel across the internet to nonexistent or non-responsive machines. The final ACK packet never comes back to the server.
The Result: Resource Exhaustion. The server is left holding thousands of half-open connections in the 'SYN_RCVD' state. Each of these connections consumes memory and, more importantly, a slot in the finite backlog queue. Eventually, this queue fills up completely. When legitimate users try to connect, their valid SYN packets arrive at the server, which now has no space to process new connections. The server is forced to drop their requests. The service, be it a website, email, or game server, becomes completely unavailable to real users.
The Defense: SYN Cookies
How can a server defend against a SYN flood without fundamentally breaking the TCP handshake? The solution is an ingenious mechanism called SYN Cookies. The core idea is to handle an initial SYN request without allocating any state. The server makes the client prove its legitimacy before any resources are committed.
How SYN Cookies Work
- Receiving the SYN: When a server with SYN cookies enabled receives a SYN packet, it does not immediately create a TCB or allocate memory. It acts as if it has no memory of the request.
- Crafting the Cookie: Instead of generating a truly random Initial Sequence Number (ISN), the server crafts a special, calculated sequence number. This "cookie" is a cryptographic hash of several pieces of information:
- The client's source IP address and port number.
- The server's destination IP address and port number.
- A timestamp and a secret, slowly changing value known only to the server.
- Sending the SYN-ACK: The server sends back a standard SYN-ACK packet to the client, using this crafted cookie as its Initial Sequence Number. It then completely discards any information about the request. No memory is used.
The Two Outcomes
- If the sender was an attacker (spoofed IP): The SYN-ACK is sent to a fake, unreachable address. No final ACK ever returns. The server does not care, because it never allocated any resources. The SYN flood packet had zero effect on the server's state.
- If the sender was a legitimate client: The client receives the SYN-ACK. As a normal part of TCP, it will construct a final ACK packet. The Acknowledgment Number in this packet will be the server's sequence number (the cookie) plus one. The client sends this final ACK back to the server.
Verification and Connection Establishment
When the server receives the final ACK from the legitimate client, it performs a verification check. It takes the acknowledgment number from the packet, subtracts one to retrieve the original cookie, and then re-calculates the hash using the client's IP/port and its own secret key. If the re-calculated hash matches the cookie received from the client, the server knows the client is legitimate (it must have received the SYN-ACK to be able to send the correct acknowledgment). Only now, after this successful verification, does the server allocate memory and fully establish the connection.
SYN cookies are a highly effective defense, making the server stateless during the initial phase of the handshake and thus immune to resource exhaustion from SYN flood attacks.
Sequence Number Prediction and Session Hijacking
Another major class of TCP attacks involves predicting the sequence numbers that TCP uses to order and acknowledge data. If an attacker can successfully guess the next sequence number in a connection, they can inject malicious data into the conversation. This is known as TCP Session Hijacking.
The Vulnerability: Predictable ISNs
TCP's security relies on the assumption that an outside attacker does not know the current sequence numbers for an established connection. The connection itself is identified only by the of addresses and ports. The sequence numbers act as a weak form of authentication for each segment.
In early TCP implementations, the Initial Sequence Numbers (ISNs) were often chosen in a simple, predictable way (e.g., by incrementing a global counter). An attacker could observe a few connections and easily predict the ISN for the next one.
The Hijacking Attack in Action
- A legitimate client establishes an authenticated connection with a server (e.g., a Telnet session after logging in).
- An attacker, who wants to take over this session, first observes the traffic to learn the IP addresses and port numbers.
- The attacker then launches a brief DoS attack against the client to temporarily silence it. This is important to prevent the real client's machine from sending RST packets that would kill the hijacked session.
- The attacker now needs to inject data. They predict the next sequence number the server is expecting from the client.
- The attacker crafts a malicious packet. This packet has the client's spoofed source IP, the correct ports, and the predicted sequence number. The payload of this packet contains a malicious command (e.g., a command to delete files).
- If the prediction is correct, the server receives the forged packet. Since all the identifying information (4-tuple and sequence number) appears valid, the server accepts the malicious payload and executes the command. The session has been hijacked.
The Defense: Randomized ISNs
The primary defense against sequence number prediction is to make the ISN unpredictable. Modern operating systems no longer use simple counters. Instead, they use cryptographically secure pseudo-random number generators to create ISNs. This makes it computationally infeasible for an off-path attacker to guess the correct sequence number, effectively thwarting this entire class of attacks.
RST Injection: Forcibly Terminating Connections
A TCP Reset (RST) attack is a method used to abruptly terminate a TCP connection between two victims. Unlike a graceful FIN closure, a RST packet immediately tears down the connection, causing all buffered data to be discarded.
The Vulnerability: The Acceptance Window
To be considered valid by a receiver, a TCP segment must have a sequence number that falls within the receiver's current receive window. This is a logical range, not just a single number. For an RST packet, this rule means an attacker does not need to guess the exact next sequence number. They only need to guess a number that is "plausible", one that lands anywhere inside the current window.
Executing a RST Injection Attack
- The attacker identifies the target connection by its source and destination IP addresses and ports.
- The attacker begins sending a barrage of forged RST packets to one or both of the connection endpoints.
- Each forged packet has the spoofed source IP of the other party.
- Crucially, each packet is sent with a different, guessed sequence number.
- The attacker sprays these guesses across a plausible range. Since the TCP receive window can be large (tens of thousands of bytes), there is a good chance that one of these guesses will eventually fall within the valid window.
- When the victim machine receives a RST packet with a valid 4-tuple and a sequence number within its current window, it will accept the packet as legitimate and immediately terminate the connection.
This attack can be used to disrupt long-running connections like database transfers, large downloads, or persistent connections used by routing protocols like BGP.
Defenses Against RST Injection
Mitigating RST injection is more difficult than preventing sequence prediction. Defenses are often implemented at the network level. Firewalls and Intrusion Detection Systems (IDS) can be configured to monitor for unusual patterns, such as a large number of RST packets for a single connection, and block the suspicious traffic. The most robust defense is encryption. When a connection is wrapped in , as in HTTPS, an attacker can no longer inject a valid RST because they cannot create the necessary encrypted records.
Conclusion: A Layered Approach to Security
The TCP protocol, while brilliant in its design for reliability, was not built for the hostile environment of the modern internet. The attacks discussed, SYN floods, session hijacking, and RST injection, demonstrate how its foundational mechanisms can be exploited.
Over the years, countermeasures like SYN cookies and randomized ISNs have been integrated into modern TCP implementations, providing strong protection against some of the most common threats. However, the ultimate lesson of TCP security is that no single layer can be solely responsible for protection. True network security relies on a layered defense-in-depth approach.
While TCP secures the transport of data, robust security is achieved by adding cryptographic layers on top (like TLS/SSL) and deploying intelligent network devices (like firewalls and IDS) that can monitor and respond to malicious traffic patterns. TCP provides the reliable foundation, but security is a collaborative effort across the entire network stack.