A SYN flood is a type of DoS attack where an attacker overwhelms a target system by continuously sending SYN requests without completing the connection.
During a typical TCP handshake, the following steps occur:
1️⃣ The client initiates a connection by sending a SYN (synchronize) request to the server.
2️⃣ The server responds with a SYN-ACK (synchronize-acknowledge) to confirm the request.
3️⃣ The client replies with an ACK (acknowledge), successfully establishing the connection.
How to Detect a SYN Attack on a Server
To quickly check if your server is under a DDoS (Denial-of-Service) attack, use the following command:
netstat -anp | grep ‘tcp|udp’ | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n
This will display a list of IP addresses making the highest number of connections to your server. However, it’s important to note that modern DDoS attacks have become more sophisticated, often using multiple attacking IPs with fewer connections per IP. This means that even if the connection count appears low, your server may still be under attack.
Another crucial aspect to monitor is the number of active connections your server is currently handling:
netstat -n | grep :80 | wc -l
netstat -n | grep :80 | grep SYN | wc -l
The first command shows the total number of active connections to your server. While the exact number varies, exceeding 500 connections is often a sign of trouble.
The second command filters for SYN requests, indicating potential SYN flood attacks. If this number exceeds 100, it suggests that your server is experiencing a SYN flood attack.
Sol.: To protect your server from a SYN flood attack, follow these steps:
✅ 1. Enable TCP SYN Cookies
This prevents the server from allocating resources for half-open connections. Run the following command:
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
This enables SYN cookies, helping to mitigate SYN flood attacks.
✅ 2. Apply IPTables Rules to Block Malicious Packets
The following IPTables rules will drop packets with suspicious TCP flags, helping prevent various types of attacks:
iptables -A INPUT -p tcp –tcp-flags ALL NONE -j DROP # Block NULL packets
iptables -A INPUT -p tcp –tcp-flags SYN,FIN SYN,FIN -j DROP # Block SYN-FIN packets
iptables -A INPUT -p tcp –tcp-flags SYN,RST SYN,RST -j DROP # Block SYN-RST packets
iptables -A INPUT -p tcp –tcp-flags FIN,RST FIN,RST -j DROP # Block FIN-RST packets
iptables -A INPUT -p tcp –tcp-flags ACK,FIN FIN -j DROP # Block XMAS packets
iptables -A INPUT -p tcp –tcp-flags ACK,PSH PSH -j DROP # Block ACK-PSH packets
iptables -A INPUT -p tcp –tcp-flags ACK,URG URG -j DROP # Block ACK-URG packets
✅ 3. Save and Restart IPTables
After applying the rules, save and restart IPTables to ensure they take effect:
service iptables save
service iptables restart