2014-10-01 11:46:26 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# IP blacklisting script for Linux servers
|
2015-01-07 10:57:00 +00:00
|
|
|
# Pawel Krawczyk 2014-2015
|
|
|
|
# documentation https://github.com/kravietz/blacklist-scripts
|
2014-10-01 11:46:26 +00:00
|
|
|
|
|
|
|
# Emerging Threats lists offensive IPs such as botnet command servers
|
|
|
|
urls="http://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt"
|
2015-07-09 13:54:42 +00:00
|
|
|
|
|
|
|
# URLs for further blocklists are appeneded below using the typical
|
|
|
|
# shell syntax: "$urls new_url"
|
|
|
|
|
2014-10-01 11:46:26 +00:00
|
|
|
# Blocklist.de collects reports from fail2ban probes, listing password brute-forces, scanners and other offenders
|
|
|
|
urls="$urls https://www.blocklist.de/downloads/export-ips_all.txt"
|
2015-01-09 12:16:57 +00:00
|
|
|
# badips.com, from score 2 up
|
|
|
|
urls="$urls http://www.badips.com/get/list/ssh/2"
|
2014-10-01 11:46:26 +00:00
|
|
|
|
2015-07-09 14:16:27 +00:00
|
|
|
# This is how it will look like on the server
|
|
|
|
|
|
|
|
# Chain blocklists (2 references)
|
|
|
|
# pkts bytes target prot opt in out source destination
|
|
|
|
# 0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 match-set manual-blacklist src,dst limit: avg 10/min burst 5 LOG flags 0 level 4 prefix "BLOCK manual-blacklist "
|
|
|
|
# 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 match-set manual-blacklist src,dst
|
|
|
|
# 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 match-set rules.emergingthreats src
|
|
|
|
# 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 match-set rules.emergingthreats dst
|
|
|
|
# 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 match-set www.blocklist.de src
|
|
|
|
# 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 match-set www.blocklist.de dst
|
|
|
|
# 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 match-set www.badips.com src
|
|
|
|
# 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 match-set www.badips.com dst
|
2014-10-01 11:46:26 +00:00
|
|
|
blocklist_chain_name=blocklists
|
|
|
|
|
2015-01-12 10:57:11 +00:00
|
|
|
if [ -z "$(which ipset 2>/dev/null)" ]; then
|
2014-10-01 11:46:26 +00:00
|
|
|
echo "Cannot find ipset"
|
|
|
|
echo "Run \"apt-get install ipset\" or \"yum install ipset\""
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2015-01-12 10:57:11 +00:00
|
|
|
if [ -z "$(which curl 2>/dev/null)" ]; then
|
2014-10-02 10:09:25 +00:00
|
|
|
echo "Cannot find curl"
|
|
|
|
echo "Run \"apt-get install curl\" or \"yum install curl\""
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2015-01-12 10:57:11 +00:00
|
|
|
if [ "$(which uci 2>/dev/null)" ]; then
|
2015-01-07 10:57:00 +00:00
|
|
|
# we're on OpenWRT
|
|
|
|
wan_iface=$(uci get network.wan.ifname)
|
|
|
|
IN_OPT="-i $wan_iface"
|
|
|
|
INPUT=input_rule
|
|
|
|
FORWARD=forwarding_rule
|
2015-01-07 10:58:42 +00:00
|
|
|
COMPRESS_OPT=""
|
2015-01-07 10:57:00 +00:00
|
|
|
else
|
2015-01-07 10:58:42 +00:00
|
|
|
COMPRESS_OPT="--compressed"
|
2015-01-07 10:57:00 +00:00
|
|
|
INPUT=INPUT
|
|
|
|
FORWARD=FORWARD
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2014-10-01 11:46:26 +00:00
|
|
|
# create main blocklists chain
|
2014-11-26 10:19:50 +00:00
|
|
|
if ! iptables -L | grep -q "Chain ${blocklist_chain_name}"; then
|
|
|
|
iptables -N ${blocklist_chain_name}
|
|
|
|
fi
|
2014-10-01 11:46:26 +00:00
|
|
|
|
|
|
|
# inject references to blocklist in the beginning of input and forward chains
|
2015-01-07 10:57:00 +00:00
|
|
|
if ! iptables -L ${INPUT} | grep -q ${blocklist_chain_name}; then
|
|
|
|
iptables -I ${INPUT} 1 ${IN_OPT} -j ${blocklist_chain_name}
|
2014-10-01 11:46:26 +00:00
|
|
|
fi
|
2015-01-07 10:57:00 +00:00
|
|
|
if ! iptables -L ${FORWARD} | grep -q ${blocklist_chain_name}; then
|
|
|
|
iptables -I ${FORWARD} 1 ${IN_OPT} -j ${blocklist_chain_name}
|
2014-10-01 11:46:26 +00:00
|
|
|
fi
|
|
|
|
|
2014-12-23 12:20:16 +00:00
|
|
|
# flush the chain referencing blacklists, they will be restored in a second
|
2014-10-01 11:46:26 +00:00
|
|
|
iptables -F ${blocklist_chain_name}
|
2014-12-23 12:20:16 +00:00
|
|
|
|
|
|
|
# create the "manual" blacklist set
|
|
|
|
set_name="manual-blacklist"
|
|
|
|
if ! ipset list | grep -q "Name: ${set_name}"; then
|
|
|
|
ipset create "${set_name}" hash:net
|
|
|
|
fi
|
|
|
|
iptables -A ${blocklist_chain_name} -m set --match-set "${set_name}" src,dst -m limit --limit 10/minute -j LOG --log-prefix "BLOCK ${set_name} "
|
|
|
|
iptables -A ${blocklist_chain_name} -m set --match-set "${set_name}" src,dst -j DROP
|
2014-10-01 11:46:26 +00:00
|
|
|
|
2014-12-23 12:20:16 +00:00
|
|
|
# now process the dynamic blacklists
|
2014-10-01 11:46:26 +00:00
|
|
|
for url in $urls; do
|
2014-11-26 10:19:50 +00:00
|
|
|
# initialize temp files
|
2014-11-26 10:25:03 +00:00
|
|
|
unsorted_blocklist=$(mktemp)
|
2014-11-26 10:19:50 +00:00
|
|
|
sorted_blocklist=$(mktemp)
|
|
|
|
new_set_file=$(mktemp)
|
2015-01-09 13:00:46 +00:00
|
|
|
headers=$(mktemp)
|
2014-11-26 10:19:50 +00:00
|
|
|
|
|
|
|
# download the blocklist
|
2015-01-12 13:46:59 +00:00
|
|
|
set_name=$(echo "$url" | awk -F/ '{print substr($3,0,21);}') # set name is derived from source URL hostname
|
2015-01-09 13:00:46 +00:00
|
|
|
curl -v -s ${COMPRESS_OPT} -k "$url" >"${unsorted_blocklist}" 2>"${headers}"
|
|
|
|
|
|
|
|
# this is required for blocklist.de that sends compressed content if asked for it or not
|
2015-01-12 13:45:28 +00:00
|
|
|
if [ -z "$COMPRESS_OPT" ]; then
|
|
|
|
if grep -qi 'content-encoding: gzip' "${headers}"; then
|
|
|
|
mv "${unsorted_blocklist}" "${unsorted_blocklist}.gz"
|
|
|
|
gzip -d "${unsorted_blocklist}.gz"
|
|
|
|
fi
|
2015-01-09 13:00:46 +00:00
|
|
|
fi
|
|
|
|
|
2015-06-02 09:15:44 +00:00
|
|
|
sort -u <"${unsorted_blocklist}" | egrep "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2})?$" >"${sorted_blocklist}"
|
2014-11-26 10:19:50 +00:00
|
|
|
|
|
|
|
# calculate performance parameters for the new set
|
2014-12-31 17:39:05 +00:00
|
|
|
tmp_set_name="tmp_${RANDOM}"
|
2014-11-26 10:25:03 +00:00
|
|
|
new_list_size=$(wc -l "${sorted_blocklist}" | awk '{print $1;}' )
|
2014-11-26 10:19:50 +00:00
|
|
|
hash_size=$(expr $new_list_size / 2)
|
|
|
|
|
2014-12-31 17:34:16 +00:00
|
|
|
if ! ipset -q list ${set_name} >/dev/null ; then
|
|
|
|
ipset create ${set_name} hash:net family inet
|
|
|
|
fi
|
|
|
|
|
2014-11-26 10:19:50 +00:00
|
|
|
# start writing new set file
|
2014-11-26 10:21:19 +00:00
|
|
|
echo "create ${tmp_set_name} hash:net family inet hashsize ${hash_size} maxelem ${new_list_size}" >>"${new_set_file}"
|
2014-11-26 10:19:50 +00:00
|
|
|
|
|
|
|
# convert list of IPs to ipset statements
|
2014-10-01 11:46:26 +00:00
|
|
|
while read line; do
|
2014-11-26 10:21:19 +00:00
|
|
|
echo "add ${tmp_set_name} ${line}" >>"${new_set_file}"
|
2014-11-26 10:19:50 +00:00
|
|
|
done <"$sorted_blocklist"
|
|
|
|
|
2014-12-31 17:39:05 +00:00
|
|
|
# replace old set with the new, temp one - this guarantees an atomic update
|
|
|
|
echo "swap ${tmp_set_name} ${set_name}" >>"${new_set_file}"
|
|
|
|
|
|
|
|
# clear old set (now under temp name)
|
|
|
|
echo "destroy ${tmp_set_name}" >>"${new_set_file}"
|
2014-11-26 10:19:50 +00:00
|
|
|
|
|
|
|
# actually execute the set update
|
2014-12-30 16:08:32 +00:00
|
|
|
ipset -! -q restore < "${new_set_file}"
|
2014-11-26 10:19:50 +00:00
|
|
|
|
2015-05-18 14:50:15 +00:00
|
|
|
if [ "$1" = "log" ]; then
|
2015-06-02 09:15:44 +00:00
|
|
|
iptables -A ${blocklist_chain_name} -m set --match-set "${set_name}" src -m limit --limit 10/minute -j LOG --log-prefix "BLOCK src ${set_name} "
|
|
|
|
iptables -A ${blocklist_chain_name} -m set --match-set "${set_name}" dst -m limit --limit 10/minute -j LOG --log-prefix "BLOCK dst ${set_name} "
|
2015-05-18 14:49:01 +00:00
|
|
|
fi
|
2015-06-02 09:15:44 +00:00
|
|
|
iptables -A ${blocklist_chain_name} -m set --match-set "${set_name}" src -j DROP
|
|
|
|
iptables -A ${blocklist_chain_name} -m set --match-set "${set_name}" dst -j DROP
|
2014-11-26 10:19:50 +00:00
|
|
|
|
|
|
|
# clean up temp files
|
2015-01-09 13:00:46 +00:00
|
|
|
rm "${unsorted_blocklist}" "${sorted_blocklist}" "${new_set_file}" "${headers}"
|
2014-10-01 11:46:26 +00:00
|
|
|
done
|
|
|
|
|
2014-11-13 11:28:42 +00:00
|
|
|
|