Protect yourself by SYN flood

Distributed Denial of Service (DDoS) attacks are becoming increasingly commonplace as business becomes more and more dependent on delivering services over the Internet. One of the most common types of DDoS attacks is the well-known SYN-flood attack. It is a basic end-host resource attack designed to bring your server to its knees. As a result, your server is unable to properly handle any new incoming connection requests.

SYN Protection in the past
In the past, SYN attacks, by major vendor, was mitigated using conntrack filtering on commodity or AICS hardware. With Netfilter’s connection tracking system (conntrack), we can start filtering out false SYN-ACK and ACK packets before they hit the “listen” state lock. The conntrack system actually has a scalability problem (like the “listen” lock) when it comes to creating (or deleting) connections, which the SYN-flood will hit.

Even after fixing the conntrack lock, the SYN packets will still be sent to the socket causing the “listen” socket lock to occur. The normal mitigation technique is to send SYN-cookies and avoid creating any state until the SYN-ACK packet is seen.

Unfortunately, SYN-cookies are sent under the same “listen” state lock, so the mitigation does not solve the scalability issue. How these limitations can be worked around will be discussed later.

SYNPROXY, New Filtering Era
With SYNPROXY we can increase 20x performance then old technique removing the “listen state lock” part catching packets that the connection tracking system has categorized as “INVALID” and not part of a known connection state. The matching against existing conntrack entries is very fast and completely scalable. The conntrack system actually does lockless RCU (read-copy update) lookups for existing connections.

Essentially, this solves all other TCP-flooding packets except SYN-flooding.

But NOW, How we can solve SYN-flooding?
SYNPROXY essentially does parallel SYN-cookies and not create a conntrack entry before the SYN-ACK packet is received thus avoiding the conntrack new connections lock. Once the initial connection is established the normal conntrack system will take over and do all the needed forwarding.

If you have CentOS 7 or any distribution with kernel > 3.13 and iptables 1.4.21 you have this module built-in.

To enable it we need to tweak sysctl.conf. Insert in /etc/sysctl.conf:
#SYN cookies
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog=4096
net.ipv4.tcp_syn_retries=5
net.ipv4.tcp_synack_retries=2

#SYNPROXY REQ
net.netfilter.nf_conntrack_tcp_loose=0
net.netfilter.nf_conntrack_max=2000000
net.ipv4.tcp_timestamps=1

#OPTIMIZE TCP
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.netdev_max_backlog = 250000


Now you can configure SYNPROXY using public online script avaiable here
github.com/netoptimizer/network-testing/blob/master/iptables/iptables_synproxy.sh

If you want configure yourself go head with these steps:
Step #1: In the “raw” table, we need to make sure connections that need protection don’t create new conntrack entries for SYN packets.
# iptables -t raw -I PREROUTING -i $DEV -p tcp -m tcp –syn –dport $PORT -j CT –notrack


Step #2: Now we need to catch these packets and direct them to the SYNPROXY target module. To do this, use the following rule to catch UNTRACKED SYN and INVALID packets that contain the ACK from 3WHS (and also others, but they will fall-through).
# iptables -A INPUT -i $DEV -p tcp -m tcp –dport $PORT -m state –state INVALID,UNTRACKED -j SYNPROXY –sack-perm –timestamp –wscale 7 –mss 1460


Step #3: Catch the INVALID state packets that fell-through the SYNPROXY module and drop those. Basically, this will drop SYN-ACK based floods.
# iptables -A INPUT -m state –state INVALID -j DROP

Considerations when using SYNPROXY

Enabling SYNPROXY does comes at a cost. The connection establishment phase is going to be slower due to the extra connection setup needed towards the end-host. When the end-host is localhost, then this extra step is obviously very fast but nonetheless adds latency.

The parameters to the SYNPROXY target module must match TCP options and settings supported by the end-host that the TCP connections are being proxied for. Detecting and setting this up is manually done per rule setting. (A helper tool “nfsynproxy” is part of iptables release 1.4.21). This unfortunately means the module cannot be easily deployed in DHCP-based firewall environments.