
TCP Handshake:
Understanding TCP Handshake
TCP/IP
DJAS
2/28/2025
The Transmission Control Protocol (TCP) handshake is the foundation of reliable communication over the internet. This blog breaks down the handshake process, its variants, security considerations, and real-world implications.
Understanding TCP Handshake: A Deep Dive
SYN β Initiates connection.
ACK β Acknowledges received data.
FIN β Requests connection termination.
RST β Abruptly resets the connection.
URG β Marks urgent data.
PSH β Pushes data immediately to the application.
Every TCP packet has a Sequence Number (SEQ) and an Acknowledgment Number (ACK).
SEQ represents the byte number in the data stream.
ACK confirms receipt by sending the next expected byte number (previous SEQ + data length).
EQ = 1000 ACK = 0
Client -----------------> Server
SEQ = 5000 ACK = 1001
Client <----------------- Server
2. Sequence and Acknowledgment Numbers
SYN (Synchronize) β The client sends a TCP segment with the SYN flag set. It includes:
Initial Sequence Number (ISN) β A randomly chosen 32-bit number.
Window Size β The clientβs advertised TCP window.
Maximum Segment Size (MSS) β Defines the maximum payload per segment.
SYN-ACK (Synchronize-Acknowledge) β The server responds with:
SYN flag set β Serverβs own ISN.
ACK flag set β Acknowledges clientβs ISN by sending ISN + 1.
ACK (Acknowledgment) β The client acknowledges the serverβs ISN by sending ISN + 1, completing the handshake.
Once this process is complete, data transmission begins.
Client Server
|-------SYN------->|
|<----SYN-ACK-----|
|-------ACK------->|
Connection Established
1. The Three-Way Handshake
TCP establishes a connection using a three-step process between a client and a server:
Conclusion
Understanding the TCP handshake is crucial for optimizing network performance, diagnosing connectivity issues, and mitigating security threats. By leveraging modern enhancements like TCP Fast Open and Window Scaling, networks can improve efficiency and reduce latency.
10. Performance Optimizations
Enable Window Scaling β Crucial for high-bandwidth, long-distance networks.
Use TCP Fast Open β Reduces latency for repeat connections.
Adjust MSS and MTU β Prevents packet fragmentation.
Enable SACK β Improves efficiency in packet loss scenarios.
9. Real-World TCP Handshake Scenarios
High Latency Networks β Delayed ACKs can slow initial data transfer.
TLS Over TCP β Adds additional round-trips for encryption setup.
Load Balancers and Proxies β May terminate and establish backend connections separately.
8. Security Considerations
SYN Flood Attacks β Attackers send multiple SYNs without completing the handshake, exhausting server resources.
Mitigations: SYN Cookies, rate limiting.
RST Injection β Attackers forge RST packets to terminate connections.
Session Hijacking β Predictable ISNs can allow attackers to hijack connections.
| State | Description |
|-----------------|-----------------------------------------|
| LISTEN | Waiting for connections |
| SYN-SENT | Client sent SYN, waiting for reply |
| SYN-RECEIVED | Server received SYN, sent SYN-ACK |
| ESTABLISHED | Connection is active |
| FIN-WAIT-1 | Waiting for termination request |
| FIN-WAIT-2 | Awaiting final termination |
| TIME-WAIT | Ensuring no duplicate packets |
| CLOSED | Connection fully terminated |
7. TCP Connection States
FIN (Finish) β One side initiates termination.
ACK β The other side acknowledges.
FIN β The second side sends its own FIN.
ACK β Final acknowledgment, closing the connection.
Client Server
|-------FIN-------->|
|<------ACK--------|
|<------FIN---------|
|-------ACK-------->|
Connection Closed
6. Connection Termination (Four-Way Handshake)
Simultaneous Open β Both sides send SYN at the same time, leading to a SYN-SYN-ACK-ACK exchange.
TCP Fast Open (TFO) β Reduces latency by sending data in the SYN phase.
Half-Open Connections β Occurs when one side completes the handshake while the other does not respond (often exploited in SYN flood attacks).
5. Handshake Variants
MSS (Maximum Segment Size) β Limits payload size to prevent fragmentation.
Window Scaling β Increases TCP window beyond 64 KB for high-bandwidth networks.
Selective Acknowledgment (SACK) β Allows acknowledgment of individual packets in case of loss.
Timestamps β Helps in Round Trip Time (RTT) calculation and sequence number protection.
4. TCP Options in Handshake
3. TCP Flags in the Handshake
| Option | Description |
|--------------|------------------- --------|
| MSS | Maximum Segment Size |
| WS | Window Scaling |
| SACK | Selective Acknowledgment|
| TS | Timestamps |
cybrescue@gmail.com
Ping us incase of any.....
Connect with us at
support@cybrescue.com