dannysheehan / iptables-ipset-blacklists

Script that uses iptables ipset to block IP addresses in known blacklists. Easily add new blacklist sources. Includes whitelist override.
19 stars 7 forks source link

iptables-ipset-blacklists

There are lots of tools and services that do a good job of identifying abusers, spammers and hackers. They provide lists of bad IPs in blacklists. By blocking these bad IPs from accessing your websites and servers you can go a long way to protecting them and also preventing a lot of useless traffic being logged in your logs. It also helps prevent a lot of noise so that your snort, ossec, logwatcher, mod-security, psad etc. tools can do some real work of finding legitimate and directed attacks to your servers.

NOTE: Some hosting companies will shutdown your VPS server if you use more than .9 load. So we recommending using cpulimit to invoke blacklists.sh

cpulimit -l 20 /usr/local/bin/blacklists.sh

Requires

Installation

/etc/logrotate.d/blacklist

/var/log/blacklists.log
{
    rotate 4
    weekly
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        invoke-rc.d rsyslog reload >/dev/null 2>&1 || true
    endscript
}

/etc/rsyslog.d/30-blacklist.conf

# Log kernel generated UFW log messages to file
:msg,contains,"[BL " /var/log/blacklists.log
& ~

Features

Example syslog messages

Jan 14 14:51:32 serverx [/usr/local/bin/blacklists.sh]: ftmon.org blacklist script started
Jan 14 14:52:17 serverx [/usr/local/bin/blacklists.sh]: ERROR Your whitelist IP 54.235.163.229 has been blacklisted in lists-blocklist-de-all
Jan 14 14:57:47 serverx [/usr/local/bin/blacklists.sh]: ERROR Your whitelist IP 67.207.202.9 has been blacklisted in infiltrated.net
Jan 14 15:02:03 serverx [/usr/local/bin/blacklists.sh]: bad_ips: current=53435   previous=53435   bad_nets: previous=1535   current=1535
Jan 14 15:02:03 serverx [/usr/local/bin/blacklists.sh]: ftmon.org blacklist script completed

Example email message

From: root
Date: Wed, Jan 1, 2015 at 3:09 PM
Subject: [/usr/local/bin/blacklists.sh] sever.org
To: root

bad_ips: current=29294   previous=57196   bad_nets: previous=1536   current=1536

ERROR Your whitelist IP 192.0.81.17 has been blacklisted in lists-blocklist-de-all
ERROR Your whitelist IP 192.0.81.57 has been blacklisted in lists-blocklist-de-all
ERROR Your whitelist IP 67.207.202.9 has been blacklisted in infiltrated.net

Firewall audit log of production ports

/var/log/blacklists.log

Jan 1 19:24:42 server kernel: [541334.229673] [BL DROP] IN=eth0 OUT= MAC=d4:be:d9:a1:62:06:78:da:6e:25:cc:00:08:00 SRC=124.232.142.220 DST=x.x.x.x LEN=58 TOS=0x00 PREC=0x00 TTL=234 ID=54321 PROTO=UDP SPT=47479 DPT=53 LEN=38

Firewall rules created

iptables -L ftmon-blacklists
Chain ftmon-blacklists (2 references)
target     prot opt source               destination
LOG        tcp  --  anywhere             anywhere             multiport dports http,https limit: avg 5/min burst 5 LOG level warning prefix "[BL DROP] "
LOG        udp  --  anywhere             anywhere             multiport dports domain limit: avg 5/min burst 5 LOG level warning prefix "[BL DROP] "
REJECT     tcp  --  anywhere             anywhere             state NEW multiport dports http,https reject-with icmp-port-unreachable
REJECT     udp  --  anywhere             anywhere             state NEW multiport dports domain reject-with icmp-port-unreachable
DROP       all  --  anywhere             anywhere             state NEW

References and Other blacklist scripts

blacklist script

ipset-blacklist

ipsets