In the vast and complex ecosystem of network security, the Transmission Control Protocol (TCP) remains the bedrock of reliable communication on the internet. While modern applications often abstract the underlying network layers behind HTTPS APIs and HTTP/2 multiplexing, the fundamental mechanisms that govern data transport remain deeply rooted in the TCP protocol. For security engineers, system administrators, and network architects, understanding the granular controls embedded within the TCP protocol header is not merely an academic exercise; it is a critical operational necessity.
At the heart of TCP communication lie the "flags." These single-bit control signals dictate the state and intent of every packet traversing a network. To the casual observer, a network packet is just a stream of binary data. To the security professional, the flags within that header are the conversation. They reveal intent, signal state, and often, betray malicious behavior before a payload is even transmitted.
This comprehensive guide explores the technical anatomy of TCP flags (SYN, ACK, FIN, RST, PSH, URG) and their profound implications for security analysis. We will dissect how these signals drive the TCP state machine, how they are exploited in common attack vectors, and how you can leverage tools like tcpdump, Wireshark, and iptables to detect and mitigate threats.
As part of our commitment to robust digital infrastructure, SwissLayer integrates advanced security protocols to protect the data hosted on our Swiss servers. Understanding these low-level protocols allows us to configure our firewalls and DDoS mitigation systems effectively, ensuring that our customers benefit from both Swiss privacy laws and cutting-edge network defense. By mastering TCP flag analysis, you empower yourself to defend against the subtle and sophisticated attacks that target the very handshake that connects the internet.
To fully appreciate the role of TCP flags, one must first understand where they reside. TCP operates at Layer 4 of the OSI model, sitting above the Internet Protocol (IP) at Layer 3. Unlike UDP, which is connectionless and unreliable, TCP is connection-oriented and ensures data delivery through sequencing, flow control, and acknowledgments.
The standard TCP header is 20 bytes long and contains various fields including source port, destination port, sequence number, acknowledgment number, data offset, reserved bits, and flags. It is within the flags field where the security analysis begins.
The flags field occupies bits 24 through 29 of the 32-bit word known as the "Control Bits." Each bit represents a specific instruction sent from one endpoint to another:
While these bits are binary (0 or 1), their combinations tell a complex story about the connection state. The TCP state machine relies heavily on these flags to transition between states such as CLOSED, LISTEN, SYN-SENT, SYN-RECEIVED, ESTABLISHED, FIN-WAIT-1, and CLOSING. When an anomaly occurs in these transitions—such as receiving a FIN flag when the connection is already closed—it is a strong indicator of a security incident.
Security analysis relies on interpreting these states correctly. A packet capture that shows a burst of packets with the SYN flag but no corresponding SYN-ACK response is not just "traffic"; it is a signature of a SYN flood attack. Conversely, a sudden RST flag during an established session might indicate a Denial of Service (DoS) injection or a man-in-the-middle attempt.
Before delving into individual flags, it is essential to understand the lifecycle of a TCP connection. The standard TCP three-way handshake is:
This handshake is the foundation of TCP security. Because the handshake requires state to be maintained on both ends, it introduces a point of vulnerability that attackers exploit to exhaust server resources. Understanding the normal flow is critical to identifying deviations in the abnormal.
To conduct a meaningful security analysis, one must move beyond the names of the flags and understand their functional behavior and security implications.
The SYN flag is the initiator of a TCP connection. When a packet is transmitted with the SYN flag set, the sender is asking the receiver to synchronize sequence numbers to begin a reliable data stream.
Technical Implication:
The sequence number in a SYN packet is chosen randomly by the client (often referred to as ISN, Initial Sequence Number). Upon receiving a SYN, the receiver creates a new socket entry and reserves resources for the incoming connection.
Security Implications:
The SYN flag is the most critical flag for DDoS attacks. Because the server must allocate memory and state for every SYN received before the handshake is complete, malicious actors can exploit this by sending a high volume of SYN packets without completing the handshake. These are known as "half-open" connections. If the server's backlog queue fills up, legitimate traffic is dropped.
Furthermore, the SYN flag is used heavily in SYN Scans (often called "Half-Open Scans") by tools like Nmap. Unlike a full connect scan, a SYN scan sends a SYN packet and waits for a response. If the port is open, the target replies with SYN-ACK. If closed, it replies with RST. If the source sends no further ACKs after the SYN-ACK, the port is recorded as open without establishing a full connection, which can sometimes evade logging on systems that only track established connections.
The ACK flag is used to acknowledge the receipt of TCP segments. When the ACK flag is set, the acknowledgment number field in the header is valid. This flag is present in almost every segment after the initial handshake.
Technical Implication:
The ACK flag confirms that the data from a previous packet has been received. It also moves the connection forward along the timeline of sequence numbers.
Security Implications:
In the context of security analysis, ACK flags are often used to verify firewall behavior. ACK Scans involve sending packets with only the ACK flag set. Unlike SYN packets, which create new states, ACK packets are typically routed based on existing rules in stateful firewalls. If a firewall returns an RST packet in response to an isolated ACK packet, it implies the port is open (the firewall is likely not blocking it as part of stateful inspection for new connections). If it returns nothing, the port may be filtered by a packet filter that blocks stateless packets.
Additionally, detecting unusual patterns of ACK traffic can indicate a "stealth" attack where an attacker attempts to bypass stateful inspection rules by injecting packets into what they perceive as an established session.
The FIN flag indicates that the sender has no more data to transmit and wishes to close the connection. This is the "graceful termination" signal.
Technical Implication:
When a FIN is received, the receiver sends an ACK. The connection then enters a "FIN-WAIT" state. Both sides must eventually send a FIN to fully close the stream (two-way closure).
Security Implications:
FIN flags are notoriously used in port scanning techniques known as FIN Scans or FIN/NULL/XMAS attacks. In a FIN scan, an attacker sends packets with the FIN flag set (and sometimes no other flags) to an open port. If the port is closed, the system responds with a RST. If the port is open, according to RFC 793 standards, the system should ignore the FIN and send no response (since an established connection should not be receiving a termination signal).
This behavior allows attackers to map open ports without completing a handshake, which is why it is called a "stealth" scan. However, modern firewalls often behave differently, and this technique is less effective than it once was. Nevertheless, a high volume of FIN packets without a prior SYN-ACK connection in the session log is a strong indicator of scanning activity.
The RST flag is used to immediately terminate a connection. It is the network equivalent of a hang-up, often in response to an error or an unexpected state.
Technical Implication:
When a packet with RST is received, the connection is forcibly closed. All buffers are cleared, and the socket is destroyed.
Security Implications:
RST injection is a severe security concern. If an attacker can successfully inject a spoofed RST packet into a stream of traffic between two endpoints, they can drop the connection entirely. This is often used in TCP Reset attacks to block communication or as part of a Man-in-the-Middle (MITM) attack.
For defenders, an influx of RST packets can signal a connection flood attack. For example, during a SYN flood, if the server cannot allocate resources to process the incoming SYNs, it may send out RST packets to abort the attempted half-open connections. Furthermore, detecting RST packets from an internal IP towards an external server during a normal session might indicate internal malware attempting to communicate with a command-and-control (C2) server and being blocked by a honeypot or firewall.
The PSH flag is a request to deliver data immediately to the application, bypassing buffering delays. It signals that the receiving application should pass the data to the user process as soon as possible.
Technical Implication:
This flag is used when data is sent in small chunks or when immediate delivery is necessary. It often appears at the beginning or end of data transfers.
Security Implications:
While less frequently exploited as a direct attack vector, the PSH flag is useful for traffic analysis. During a data exfiltration attack, an attacker might use PSH flags to send stolen data in small packets to evade detection thresholds based on packet size. Security Information and Event Management (SIEM) systems often monitor PSH activity to understand application behavior and latency.
A sudden spike in PSH flags from a single source without significant payload size might indicate a scripted bot or an automated script trying to maintain a connection or transmit data in small, rapid bursts. It is also used during HTTP requests, where a client "pushes" a request to the server and expects a prompt "push" response.
The URG flag is the least used of the standard flags. It indicates that the data in the packet should be treated as "urgent," and the Urgent Pointer field in the header is valid.
Technical Implication:
The urgent pointer points to the sequence number of the last byte of urgent data. It is intended for out-of-band data transmission.
Security Implications:
In modern network stacks, the URG flag is often ignored by applications due to security risks and implementation complexities. However, in security analysis, the presence of URG flags is often a signature of legacy behavior or specific exploit payloads.
Attacks involving TCP Urgent Pointer attacks (rare in the wild now but historically significant) attempted to manipulate the application's data stream by confusing the buffer handling of urgent data. Modern operating systems handle this by discarding the pointer unless specific flags are passed to the application. For a security analyst, seeing URG flags on standard web traffic (like HTTP/HTTPS) should trigger an alert. It is highly anomalous in normal application traffic and could indicate a malformed packet or an attempt to test the vulnerability of older network stacks.
Understanding flags is one thing; applying this knowledge to active security operations is where value is realized.
IDS signatures are heavily based on flag combinations. An IDS engine parses the TCP header of every packet and checks specific flag patterns against a database of known attack signatures.
For example, an intrusion signature for a NULL Scan might look for packets where no flags are set. An IDS might flag this traffic because RFC 793 implies that packets with no flags set should be discarded by the operating system. If the system accepts them, it might be running an outdated kernel or misconfigured firewall.
In XMAS Scans, the IDS looks for packets where the FIN, PSH, and URG flags are all set. This is known as an "XMAS tree" because the flags are lit up like a Christmas tree. Detecting these flag patterns allows the IDS to block the IP address attempting the scan automatically without allowing the connection to establish.
Stateful firewalls rely on the relationship between flags to make allow/deny decisions. A basic rule set allows established traffic (ACK flag present) to bypass the filter, while new connections require a SYN flag.
However, security engineers often tune these rules to be more restrictive. For example, a "default deny" firewall rule might only allow traffic with a SYN flag for new connections, blocking any other initial flag combinations. This prevents port scans that try to sneak past firewalls using FIN or RST flags.
Configuration rules must also account for ACK scanning. A strict firewall might drop packets containing an ACK flag without a corresponding established session state. This prevents scanning tools from inferring firewall rules by testing packet responses.
Packet capture tools are the forensic microscopes of network security. They allow engineers to reconstruct the handshake and identify where a connection failed or if it was hijacked.
Wireshark display filters are incredibly powerful when working with flags. A simple filter like tcp.flags.syn == 1 isolates all packets initiating connections. A filter like tcp.flags.syn == 1 && tcp.flags.ack == 0 filters for pure SYN packets (no ACK), which is useful for spotting potential SYN floods.
In the event of a suspected breach, a security engineer can use these tools to trace the origin of an attack. If a server suddenly receives a packet with a URG flag set to 1 and a suspicious Urgent Pointer value, the engineer can trace the packet to its source IP. By analyzing the full packet stream, one can reconstruct the timing of the attack and correlate it with system logs.
Understanding the vulnerabilities associated with specific flags is key to defense. Below are the most common attack patterns utilizing TCP flags.
Mechanism:
The attacker sends a flood of TCP packets with the SYN flag set to a victim server. The server responds with SYN-ACK and waits for the final ACK to complete the handshake. The attacker does not send the ACK. This leaves the connection in a half-open state, consuming memory and CPU resources in the server's connection queue.
Signature:
Detection:
Monitor the ratio of SYN packets to SYN-ACK packets. If the ratio drops significantly below normal, a flood is likely occurring. Tools like iptables or Linux kernel parameters (net.ipv4.tcp_syncookies) can mitigate this at the OS level.
Mechanism:
These are port scanning techniques used to map open ports on a target system without triggering traditional logging.
Signature:
Response:
If the target port is open, the packet is ignored (no response). If closed, the target responds with RST. This non-intrusive nature makes it difficult to detect via standard connection logs.
Detection:
Network sensors must be configured to alert on "unexpected" flag combinations. A server that receives a FIN flag for a session that is in the SYN-SENT or ESTABLISHED state is behaving anomalously.
Mechanism:
An attacker intercepts a connection or guesses the sequence numbers to send a spoofed RST packet. If the receiver accepts the spoofed packet (often due to predictable sequence numbers in older OS versions), the connection is severed.
Signature:
Detection:
Monitor for "Out-of-Window" RST packets. If a connection is active, an external RST without the correct sequence number might be an attempt to drop the link. Security tools can flag RST packets that arrive with sequence numbers that do not match the expected window.
Mechanism:
Attackers send ACK packets to determine if a port is filtered or open. If an ACK is sent to a closed port, the system returns an RST. If the port is open but the firewall is stateless, the packet might pass or be dropped depending on configuration.
Signature:
Detection:
This is used for reconnaissance. Security monitoring systems should track the frequency of ACK-only packets from unknown sources. A spike in ACK scanning suggests a network mapping attempt.
Theoretical knowledge must be translated into practical command-line usage. Below are real-world examples of how to leverage TCP flags for security monitoring.
tcpdump is a powerful command-line packet analyzer. By filtering specifically on flags, you can narrow down vast amounts of traffic.
Example 1: Isolate SYN Flood Traffic
To capture only new connection attempts, use the tcpflags filter:
tcpdump -n -i eth0 'tcp[13] & 2 != 0 and tcp[13] & 4 == 0'
Explanation: tcp[13] refers to the flags byte. & 2 checks for SYN (bit 1). & 4 checks for ACK (bit 2). This command captures packets with SYN set but ACK not set.
Example 2: Detect Port Scanning (FIN Scan)
tcpdump -n 'tcp[13] == 0x01'
Explanation: This captures packets where the FIN flag (bit 0) is set. Note: Hex 0x01 corresponds to the FIN bit position depending on the byte ordering in the capture context. In tcpdump, flags are extracted as tcp[13] & 0x01 for FIN.
A more precise filter for XMAS scans:
tcpdump -n 'tcp[tcpflags] = (tcp-syn|tcp-ack|tcp-fin|tcp-rst|tcp-urg|tcp-psh)'
tcp.flags.syn == 1 and tcp.flags.ack == 0
This displays only the initiating packets of a handshake.
Example 2: RST Packets
tcp.flags.reset == 1
Use this to investigate unexpected connection drops.
Example 3: XMAS Scan Detection
tcp.flags.fin == 1 && tcp.flags.psh == 1 && tcp.flags.urg == 1
If you see packets matching this in the packet list, your network is likely being scanned.
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
Explanation: This rule checks if the incoming TCP packet has all flags (ALL) reset (NONE). This drops traffic that violates standard protocol behavior.
Example: Drop SYN Flooders
You can limit the rate of SYN packets using the recent module:
iptables -A INPUT -p tcp --syn -m recent --name SYN_FLOOD --set
iptables -A INPUT -p tcp --syn -m recent --name SYN_FLOOD --update --seconds 60 --hitcount 20 -j DROP
Explanation: This counts new SYN connections. If more than 20 SYN packets arrive from a specific source IP within 60 seconds, the subsequent packets are dropped.
sysctl -w net.ipv4.tcp_syncookies=1). This protects your server against SYN floods without requiring a large buffer queue.
4. Normalize Traffic Analysis: Security analysts should not treat packets in isolation. Always analyze the TCP handshake. A single FIN packet is meaningless; a FIN packet followed by another FIN packet without a handshake is an anomaly.
5. Monitor Anomalies in Urgent Flags: Because URG is rarely used, any traffic containing URG flags should be investigated as high-risk.
6. Automate Response: When a pattern like a FIN scan is detected, automate the blocking of the source IP for 24 hours using firewall scripts.