I was wondering if I could prevent small (D)DoS attacks with a simple IP tables rule?
By small I mean that they are flooding my web server with about 400+ requests from one or two IP addresses. I can drop the IP addresses after I notice that they have started hitting my web server, but it normally takes a few minutes for IP tables to kick in against that IP, and start dropping it completely so that it doesn't impact that web server.
I drop the IP with the following command:
iptables -I INPUT -s "IP HERE" -j DROP
And then obviously save it:
/etc/init.d/iptables save
I normally find out the attacking IP address(es) with the following command:
netstat -plan|grep :80|awk '{print $5}'|cut -d: -f 1|sort|uniq -c|sort -n
The issue with doing it that way is that I have to be there, and it requires me to act after the fact. Is there an IP tables rule that I could use to drop an IP address right after it hits 150 connections? That way I don't have to worry about it overwhelming the web server, and I also don't have to be there at the time to block it.
By the way, I'm using Apache on CentOS if that matters.
Thank you for your time.
netstatcommand does not necessarily list "attacking" IP addresses. The main problem is thatgrep :80doesn't care whether you are connecting to someone or if they are connecting to you. The second problem is that it doesn't care what state the connections are in. Try this:netstat -plan | awk '$4 ~ /:80/ {print $6,$5}' | cut -d: -f1,3 | sort | uniq -c | sort -nAlso, verify that the IP addresses are malicious before you block them. Companies like Websense and CloudFlare can funnel a lot of users through a single IP address. – Ladadadada Dec 31 '11 at 13:15