Using iptables to block ips that spam or attack your server
Why should you do that while APF or CSF can do it automatically?
Because APF/CSF could block an important bot testing your server to add to search index. So reviewing every ip would be a daily task!
Ok, so how?
using RHEL4, centos4
-
#vi /etc/init.d/iptables
-
go to ’start() {'
-
#insert these lines before 'touch $VAR_SUBSYS_IPTABLES'
-
#banning BAD IPS
-
if [ -f /var/badips ]
-
then
-
for BAD_IP in `cat /var/badips`
-
do
-
iptables -I INPUT -s $BAD_IP -j DROP
-
echo 'ip '$BAD_IP' banned'
-
echo
-
done
-
else
-
echo "Can't read /var/badips"
-
fi
-
-
#echo '80.145.203.224′ >> /var/badip
-
/etc/init.d/iptables restart
Ok but how about a nice PHP script which would do that without getting into ssh?
-
$file = '/var/badips';
-
#remove IP use ?do=rm&ip=80.145.203.224
-
if($_GET['do']=='rm'){
-
}else{ #add ip ?ip=80.145.203.224
-
#duplicated ip
-
else{
-
if(file_put_contents($file,"{$_GET['ip']}\n",FILE_APPEND))
-
}
-
}
-
-
add the script to file ban.php and make sure file 'badips' is 666 writable then excute ban.php?ip=80.145.203.224 to ban this ip or ban.php?ip=80.145.203.224&do=rm to remove it from the banned ips


October 29th, 2007 at 11:12 am
[...] read more | digg story [...]
March 26th, 2008 at 9:32 am
cool,
just what I was looking for, thanks.
September 29th, 2009 at 9:43 pm
awesome!! =)
January 19th, 2010 at 9:53 pm
Hi
It can be made a little change in order to improve the performance. I only want to check the packet that are addressed to port 25.
If you don't want logging, just comment out the line.
iptables -N MAIL
iptables -N MAILLOG
iptables -A INPUT -i eth0 -j MAIL ! -s 127.0.0.1 -p tcp –dport 25
iptables -A MAILLOG -j LOG
iptables -A MAILLOG -j DROP
and then
iptables -A CORREO -i eth0 -j MAILLOG -s 188.54.0.0/15
Now, please is there any “badip" file to share.
Thanks
January 19th, 2010 at 9:54 pm
Sorry for the mistake, you should replace CORREO by MAIL