30 Mins

4. Transport Layer: TCP Deep Dive

TCP Header Flags and Control Bits

The TCP header contains several flag bits (control bits) that dictate the state of a connection. These bits act as the instructions for how the receiving stack should process a packet.

Key flags include: SYN (Synchronize sequence numbers), ACK (Acknowledgment of received data), FIN (Graceful shutdown), RST (Immediate connection reset), PSH (Push data to the application immediately), and URG (Urgent data pointer priority).

When auditing traffic with Wireshark or Tcpdump, these flags are the primary indicators of connection behavior and potential port scanning activity.

Security Operator Note: In Nmap, you can manipulate these flags directly using the -sS (SYN scan) or -sF (FIN scan) flags to bypass simple stateful firewalls.
Text
tcpdump -i eth0 'tcp[tcpflags] & (tcp-syn) != 0' -c 5

The TCP 3-Way Handshake and Termination

TCP uses a deterministic connection process. It begins with a SYN packet from the client, followed by a SYN-ACK from the server, and concludes with an ACK from the client to establish an ESTABLISHED state.

Connection termination follows a similar logic using the FIN flag. A common pattern is the FIN-ACK exchange where both sides agree to close the stream.

We can simulate a raw socket connection in Python to observe these transitions without relying on the OS's native networking stack.

Security Operator Note: Always monitor for Half-Open connections (SYN_RECV state) as these are often indicators of TCP SYN Flood DoS attacks.
Python 3
import socket; sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM); print(f'Socket created: {sock}'); sock.close()

Sequence Numbers, Acknowledgments, and Windows

TCP ensures reliability via Sequence Numbers (tracking bytes sent) and Acknowledgment Numbers (tracking bytes received). The Window size tells the sender how much data can be transmitted before an ACK is required.

The following table summarizes the primary control features in the TCP header.

FeatureFunction
Seq NumberByte index tracking
Window SizeFlow control limit
Security Operator Note: Sequence numbers are initialized randomly to prevent TCP Sequence Prediction attacks.
Python 3
import struct; header = struct.pack('!HHIIBBHHH', 80, 443, 100, 200, 5, 2, 65535, 0, 0); print(f'Raw TCP Header: {header.hex()}')

TCP State Transitions

Understanding states like LISTEN (waiting for connection), SYN_SENT (initiated), ESTABLISHED (active), TIME_WAIT (waiting for delayed packets), and CLOSE_WAIT (awaiting app close) is crucial for identifying zombie connections.

You can inspect active TCP states on your local machine using standard terminal utilities to identify leaked resources or rogue processes.

Below is a terminal command to filter the TCP state machine directly from the OS process table.

Security Operator Note: A large number of CLOSE_WAIT states often points to an application failure to properly call close() on the socket descriptor.
Text
ss -ntu | grep 'TIME-WAIT'

Knowledge Check

Ready to test your understanding of 4. Transport Layer: TCP Deep Dive?

Back