here's my setup with ipset
1) download and setup as per instructions
https://github.com/trick77/ipset-blacklist2) my /etc/ipset-blacklist/ipset-blacklist.conf is this:
IPSET_BLACKLIST_NAME=blacklist # change it if it collides with a pre-existing ipset list
IPSET_TMP_BLACKLIST_NAME=${IPSET_BLACKLIST_NAME}-tmp
# ensure the directory for IP_BLACKLIST/IP_BLACKLIST_RESTORE exists (it won't be created automatically)
IP_BLACKLIST_RESTORE=/etc/ipset-blacklist/ip-blacklist.restore
IP_BLACKLIST=/etc/ipset-blacklist/ip-blacklist.list
VERBOSE=yes # probably set to "no" for cron jobs, default to yes
FORCE=yes # will create the ipset-iptable binding if it does not already exist
let IPTABLES_IPSET_RULE_NUMBER=1 # if FORCE is yes, the number at which place insert the ipset-match rule (default to 1)
# Sample (!) list of URLs for IP blacklists. Currently, only IPv4 is supported in this script, everything else will be filtered.
BLACKLISTS=(
# "file:///etc/ipset-blacklist/ip-blacklist-custom.list" # optional, for your personal nemeses (no typo, plural)
"https://www.projecthoneypot.org/list_of_ips.php?t=d&rss=1" # Project Honey Pot Directory of Dictionary Attacker IPs
"https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=1.1.1.1" # TOR Exit Nodes
"http://danger.rulez.sk/projects/bruteforceblocker/blist.php" # BruteForceBlocker IP List
"https://www.spamhaus.org/drop/drop.lasso" # Spamhaus Don't Route Or Peer List (DROP)
"https://cinsscore.com/list/ci-badguys.txt" # C.I. Army Malicious IP List
"https://lists.blocklist.de/lists/all.txt" # blocklist.de attackers
"https://blocklist.greensnow.co/greensnow.txt" # GreenSnow
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level1.netset" # Firehol Level 1
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/stopforumspam_7d.ipset" # Stopforumspam via Firehol
# "https://raw.githubusercontent.com/ipverse/rir-ip/master/country/zz/ipv4-aggregated.txt" # Ban an entire country(-code), see https://github.com/ipverse/rir-ip
# "https://raw.githubusercontent.com/ipverse/asn-ip/master/as/1234/ipv4-aggregated.txt" # Ban a specific autonomous system (ISP), see https://github.com/ipverse/asn-ip
)
MAXELEM=196608
be aware that default MAXELEM value can be too small, and be aware too that this file is not templated (and not included in standard backup set)
3) create the following fragment in /etc/e-smith/templates-custom/etc/rc.d/init.d/masq path
nano 40DenyRiffRaff
/sbin/iptables -I INPUT 1 -m set --match-set blacklist src -j DROP
4) if you're using fail2ban, you must create 2 custom fragments in the same folder:
nano 40Fail2Ban
# A blacklist chain for fail2ban
/sbin/iptables --new-chain Fail2Ban
/sbin/iptables --new-chain Fail2Ban_1
/sbin/iptables --append Fail2Ban -j Fail2Ban_1
/sbin/iptables --insert INPUT 2 \
-j Fail2Ban
(we're forcing fail2ban's iptables rule at position 2)
nano 90adjustFail2Ban
{
my $f2bdb = esmith::ConfigDB->open_ro('fail2ban') ||
esmith::ConfigDB->create('fail2ban');
# Find the current Fail2Ban_$$ chain, and create a new one.
$OUT .=<<'EOF';
OLD_Fail2Ban=$(get_safe_id Fail2Ban filter find)
NEW_Fail2Ban=$(get_safe_id Fail2Ban filter new)
/sbin/iptables --new-chain $NEW_Fail2Ban
EOF
if ( ($fail2ban{'status'} || 'disabled') eq 'enabled' ){
foreach my $ban ( $f2bdb->get_all_by_prop(type=>('ban')) ){
my $ip = $ban->prop('Host');
my $proto = $ban->prop('Protocol') || '';
my $port = $ban->prop('Port') || '';
$OUT .= " /sbin/iptables --append \$NEW_Fail2Ban -s $ip";
$OUT .= " -p $proto" if ($proto =~ m/^tcp|udp|icmp$/);
$OUT .= " -m multiport --dports $port" if ($proto =~ m/^tcp|udp$/ && $port =~ m/^\d+(,\d+)*$/);
$OUT .= " -j denylog\n";
}
$OUT .= " /sbin/iptables --append \$NEW_Fail2Ban" .
" -j RETURN\n";
}
# Having created a new Fail2Ban chain, activate it and destroy the old.
$OUT .=<<'EOF';
/sbin/iptables --replace Fail2Ban 2 \
--jump $NEW_Fail2Ban
/sbin/iptables --flush $OLD_Fail2Ban
/sbin/iptables --delete-chain $OLD_Fail2Ban
EOF
}
5) create 2 more custom fragments in /etc/e-smith/templates-custom/etc/crontab/ path:
nano 99_ipset
33 23 * * * root /usr/local/sbin/update-blacklist.sh /etc/ipset-blacklist/ipset-blacklist.conf
for dailiy update
and
nano zz_boottime
@reboot /usr/sbin/ipset restore < /etc/ipset-blacklist/ip-blacklist.restore
to reload ipset at boot time
to be sure everything is correctly expanded, a post-upgrade event followed by a reboot will do the job
you can check the result with
iptables -L INPUT -v --line-numbers
command, which spits out something like this:
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 381K 19M DROP all -- any any anywhere anywhere match-set blacklist src
2 13M 4585M XTGeoIP all -- any any anywhere anywhere
3 12M 4584M state_chk all -- any any anywhere anywhere
4 1343K 93M Fail2Ban all -- any any anywhere anywhere
(yes, ipset, geoip and fail2ban in place
)
HTH