| Home | FAQ | Statistics | Various | Contact |
Security : Linux : Floodmon (SYN flood attacks monitoring, alert and mitigation)
Floodmon is a small Perl daemon used to monitor a server and to detect SYN flood attacks, to alert the administrator and to mitigate the attack in order to allow legitimate connections to succeed (HTTP, POP3, SMTP...). It includes a passive and active defense system able to adjust itself according to the attack magnitude.
Floodmon works mainly by tweaking the TCP/IP stack : activation/deactivation, queues size, timeout and retransmissions etc.
All modifications are done in real time in the '/proc' pseudo file system throughout the SYN flood attack. It can also null-route entire subnets (/8, /16 or /24) at a high rate. It has 4 protection levels, each one having its own configuration and can be quite aggressive if needed. It can alert the administrator of a possible attack by email and/or SMS, email detailed reports and even include a capture of the SYN segments for analysis. It can also display some useful network stats.
The daemon, floodmon, must be installed in '/usr/sbin/' directory, chmoded to 0700 and only accessible to 'root' (chown root:root). Its configuration file 'floodmon.conf' has to be edited and moved to '/etc/'. Two other scripts are also included, one for munin-node and one to start the daemon aat boot time (see 'README' for more informations).
Perl modules :
The configuration file 'floodmon.conf' must be edited and then copied into the '/etc/' directory. It contains some variables that must be configured as soon as floodmon is installed (network, email etc) and others that are already configured by default.
Those variables are used to mitigate the attack and have been configured with the best values. They have been tested during both real and simulated attacks and have given some pretty good results. Don't change them unless you know what you are doing !
* Apache v1.x uses the same variable since version 1.3 but named DEFAULT_LISTEN_BACKLOG and having a value of 512 (see httpd.h). It can also be changed with the ListenBacklog directive as described above.
There are many iptables rules on the Internet to help to 'fight' against a SYN flood, some of which you really should avoid, especially if you use floodmon. For example :
View floodmon source
If floodmon has been useful to you, specially if you have been under attack, you can participate in its development :
Latest version : v0.9.4 (06-Jan-2010) - changelog
I - Overview :
II - Setup :
Floodmon creates a log file '/var/log/floodmon.log' and backup all user system configuration in '/var/cache/floodmon/' before doing any modification.
III - Programs & Perl modules needed :
- Debian : # apt-get install libtime-hires-perl
- RH/CentOS : # yum install perl-Time-HiRes
- Debian : # apt-get install libnet-pcap-perl
- RH/CentOS : # yum install perl-Net-Pcap
Programs :
Floodmon does not need any other command or third party program. It doesn't use iptables or the netstat command either (it gets all connection informations from '/proc/net' and '/proc/sys/net', just like netstat does).
- Debian : # apt-get install iproute
- RH/CentOS : # yum install iproute
IV - Parameters :
floodmon :
Usage :
--help : display this menu.
--debug : run in debug mode (no daemon).
--daemon : run in daemon mode (default).
--reload : reload configuration file (/etc/floodmon.conf).
--stop : stop and restore user configuration (default).
--stop-save : stop but doesn't restore user configuration
(TCP/IP stack, routing table).
--stats : display network statistics.
--munin-node : daemon statistics to use with munin-node.
--sms-test : test SMS alerts.
--capture <num> : capture <num> SYN packets.
--version : check for floodmon updates
* The last 5 options can be executed regardless of the status of floodmon (active or not).
V - User configuration file :
If you make changes to this file while floodmon is running, use the # floodmon --reload command so that the daemon will reload it.
VI - Variables to configure :
VII - Variables configured by default :
Important : if you are running floodmon from an openVZ or Virutozzo container, only MAX_SYN, LOOP_DELAY and NULLROUTE_SUBNET variables apply. Such VPS are using a single Linux kernel, therfore it is not possible to modify the TCP/IP stack parameters. Floodmon will run in 'light mode', that is, it will monitor TCP connections, send alerts (including packets capture) and block offending IPs.
By default, 350 simultaneous SYN are allowed in the 1st level before the alert is triggered. Even if that value may seem a bit low, there shouldn't be any reason to change it, except of course if you receive too many false alerts.
For the second level, the maximum amount of SYN_RECV allowed is 500 and 1,000 for the 3rd level. For the 4th last level, 'MAX_SYN' value just doesn't matter but of course should be kept higher than the value of the previous (3rd) level.
Floodmon deactivates them and will only activate them when an attack will be detected. It should also be noted that even when enabled, syncookies are not used until the tcp_max_syn_backlog and the application listen queue are getting full. Finally, Linux is an OS that has also implemented some strong features to help to mitigate SYN flood attacks (minisock etc).
This value is quite low, hence floodmon adjust it from 10,000 to 200,000 depending on the current alert level.
When an application calls the listen() function, it gives 2 parameters : sockfd (file descriptor that refers to a socket) and backlog (queue length for completely established
sockets waiting to be accepted). If backlog has a higher value than SOMAXCONN, it will be lowered to the value of SOMAXCONN.
During a SYN flood attack, SOMAXCONN is as important as tcp_max_syn_backlog : if there are enough warm entries in syn queue, new connection attempts will be dropped (see tcp_ipv4.c).
In Linux, SOMAXCONN size is hard-coded to a very low value : 128 (see socket.h). Fortunately, its value can be dynamically adjust in '/proc/sys/net/core/somaxconn'. Floodmon set it from 10,000 to 60,000 depending on the current level alert.
Important : you must also at least increase your HTTP server (Apache, Nginx etc) listen backlog. See "VIII - Server setup" chapter below.
Upon receipt of a SYN packet, the server will reply with a SYN/ACK. If the client doesn't reply with a ACK packet, just like in the case of a SYN flood attack, the server will send it again several times. By default in Linux a SYN/ACK is retransmitted 5 times (see tcp.h) : first time 3s after the first one, then 6s after the first retransmission, then 12s after the 2nd one, 24s after the 3rd one and, finally, 48s after the 4th one. That last retransmission will only timeout after another 96s. That is a total of 189 seconds and also the reason why, during an attack, the netstat command will display so many SYN_RECV.
Floodmon will reduce the number of retransmissions to 3 (45s), and will keep lowering it at higher alert levels. In the last one, 4th level, it will not allow any SYN/ACK retransmission. Even if that seems a bit harsh, it is simply identical to the use of syncookies which also prevent any retransmission of a SYN/ACK packet.
At start-up, floodmon will check which, if any, table the server is using (ip_conntrack which handles IPv4 or nf_conntrack which handles both IPv4 and IPv6). If your server doesn't use the connection tracking table, you are advised to set the NO_CONNTRACK variable in floodmon.conf.
Floodmon handles 3 variables related to that table :
Floodmon handles those timeouts as well in order to avoid the table to quickly get full during an attack. It will simply lower them according to MAX_SYNACK_RETRY timeout value.
Note that increasing the size of CONNTRACK_HASHSIZE and CONNTRACK_MAX will also enlarge the size of RAM memory needed. At level 4, floodmon give them both a value of 131072 that will use approximately 38Mb of RAM. If you need to increase those values, use preferably a power of two : 65536 (2^16), 131072 (2^17) etc.
Note that floodmon retrieves all connection states directly inside '/proc/net' and '/proc/sys/net' without using a third-party program and therefore, even if LOOP_DELAY has a very low value, it uses very little CPU and RAM (approx 2MB in standby).
IPs are not blocked individually, but by entire subnets using /8, /16 or /24 network mask (see NETMASK above). The default value is 16, which means a whole class B (255.255.0.0 mask, CIDR /16). If there are more than NULLROUTE_SUBNET IPs from the same subnet, the whole class will be null-routed. For example, if you set NETMASK == 16 and NULLROUTE_SUBNET == 200, if there are more than 200 SYN from remote IP addresses 200.10.xx.xx, floodmon will block all IPs from 200.10.0.0 to 200.10.255.255
Null-routed IPs are flushed by default every 10mn (FLUSH_FREQ variable in 'floodmon.conf').
VIII - Server setup :
When using floodmon, there are some important points you should take into consideration :
That difference between Apache and Linux default values makes some problems : if you are under a SYN flood attack on port 80, you will notice that Apache will quickly become irresponsive. In fact it is not Apache but the kernel that will reject any new connection attempt because the listen() and SYN backlogs are both linked together when it comes to handle incoming connections. Apache isn't responsible (it will never know about the attack !) because, and that is a SYN flood attack goal, no connection is established. SYN packets will remain in the kernel half-open connection backlog.
Fortunately, DEFAULT_LISTENBACKLOG can be changed by adding the ListenBacklog directive to apache2.conf (or httpd.conf). Since floodmon gives generous values to both SOMAXCONN and tcp_max_syn_backlog (10,000 minimum), you are advised to greatly increase Apache backlog as well, specially when using floodmon if you want to increase your chances to handle an attack on port 80. A minimal value of 5,000 seems to be a comfortable one, but it will depends on your server capacity, the number of visitors and of course, the magnitude of the attack you are facing as well.
You must edit your apache2.conf or httpd.conf and add the value you want to use. For example, with a value of 5,000 :
ListenBacklog 5000
Then modify SOMAXCONN value (only if floodmon is not already running !) :
echo 5000 > /proc/sys/net/core/somaxconn
You must restart Apache after the modification.
backlog=5000
For more info, see Nginx online help.
Change this value, change SOMAXCONN (only if floodmon is not already running !) and restart Nginx.
if (-1 == listen(srv_socket->fd, 128 * 8)) {
log_error_write(srv, __FILE__, __LINE__, "ss", "listen failed: ", strerror(errno));
return -1;
}
Change this value, recompile, then modify SOMAXCONN and restart Lighttpd.
If the attack targets another port (POP3 server, FTP, SMTP...) check the server doc to see if you can increase its backlog. Otherwise, you can always change its value in the source code and recompile (just look for a call to the listen() function).
Since version 0.9.4, floodmon does not use iptables any longer.
iptables -N syn-flood
iptables -A INPUT -p tcp --syn -j syn-flood
iptables -A syn-flood -m limit --limit 5/s -j RETURN
iptables -A syn-flood -j DROP
That rule rates limit any new TCP connection to 5 per seconds. There is no real difference between using that rule and shooting yourself in the foot.
Even if a website receives only few hundreds visitors pers day, such a rule will affect them, slowing down their attempts to get connected, forcing them to retransmit SYN packets several times and, during an attack, those retransmissions will even increase the flood. Also, the SYN rate limit will not only affect the HTTP server but the whole TCP protocol (POP3, SMTP, FTP etc). An attacker could send an as little as 10 SYN/s flood, that's a ridiculous 80bits/s bandwidth, to block any access to the server, including SSH access. In other words, a script-kiddie could 'flood' the firewall only with his iPhone...
If you are using such a rule, you must remove it. It is not compatible at all with floodmon whose purpose is to mitigate the attack to allow legitimate connections to succeed.
IX - Download :
floodmon.tgz - v0.9.4 - 26 Kb - (changelog)
X - Daemon code :
XI - Floodmon users :
- your server configuration (CPU, RAM, bandwidth etc)
- the attack strength (number of Kbits/s or Mbits/s)
- please include floodmon log file (/var/log/floodmon.log)
- your comments and/or any problem encountered
![]()