2019-09-19 15:16:32 +00:00
#!/bin/sh
2019-09-20 16:07:09 +00:00
# /volume1/homes/admin/script/update_blocklist.sh
# Script import IP's from blocklist.de
2019-09-18 22:18:56 +00:00
# https://www.synology-forum.de/showthread.html?103687-Freigabe-Blockierliste-automatisch-updaten&p=837478&viewfull=1#post837478
2019-09-19 15:16:32 +00:00
# version 0.1 by Ruedi61, 15.11.2016 / DSM 6.0.3
# version 0.2 by AndiHeitzer, 18.09.2019 / DSM 6.2.1 > add further Vars for DB
2019-09-20 16:07:09 +00:00
# version 0.3 by geimist, 20.09.2019 / DSM 6.2.2 > add Stats / Loglevel / speed improvement
2019-09-18 22:18:56 +00:00
2019-09-20 21:00:26 +00:00
# Deny=1 > Blacklist / Deny=0 > Whitelist
Deny = 1
2019-09-18 22:18:56 +00:00
2019-09-19 15:16:32 +00:00
# Download from www.blocklist.de | Select Typ: {all} {ssh} {mail} {apache} {imap} {ftp} {sip} {bots} {strongips} {ircbot} {bruteforcelogin}
BLOCKLIST_TYP = "all"
# Delete IP after x Day's OR use 0 for permanent block
DELETE_IP_AFTER = "7"
2019-09-18 22:18:56 +00:00
2019-09-19 15:16:32 +00:00
# Loglevel 1: Show Stats at the bottom / Loglevel 2: Show all / Loglevel 0: disable
LOGLEVEL = 1
2019-09-18 22:18:56 +00:00
2019-09-20 21:00:26 +00:00
TYPE = 0
META = ''
2019-09-18 22:18:56 +00:00
###############################################################################################################
2019-09-20 16:07:09 +00:00
# Do NOT change after here!
2019-09-19 15:16:32 +00:00
2019-09-20 21:00:26 +00:00
# SQL Create-Statement for restore:
# 'CREATE TABLE AutoBlockIP(IP varchar(50) PRIMARY KEY,RecordTime date NOT NULL,ExpireTime date NOT NULL,Deny boolean NOT NULL,IPStd varchr(50) NOT NULL,Type INTEGER,Meta varchar(256))'
2019-09-19 15:22:55 +00:00
if [ $( whoami) != "root" ] ; then
echo "WARNING: this script must run from root!" >& 2
exit 1
fi
2019-09-20 16:07:09 +00:00
2019-09-19 15:16:32 +00:00
countadded = 0
countskipped = 0
UNIXTIME = $( date +%s)
UNIXTIME_DELETE_IP = $( date -d " + $DELETE_IP_AFTER days " +%s)
2019-09-20 16:07:09 +00:00
# current IP-list:
2019-09-20 21:00:26 +00:00
sqlite3 -header -csv /etc/synoautoblock.db "select IP FROM AutoBlockIP WHERE Deny='1' ORDER BY 'IP' ASC;" | sed -e '1d' | sort > /tmp/before.txt
2019-09-20 16:07:09 +00:00
# load online IP-list:
curl -s " https://lists.blocklist.de/lists/ ${ BLOCKLIST_TYP } .txt " | sort > /tmp/onlinelist.txt
# filter diffs:
2019-09-20 20:28:54 +00:00
diff "/tmp/before.txt" "/tmp/onlinelist.txt" | grep '^>' | sed -e 's/> //' > /tmp/blocklist.txt # only diffs from left to right
2019-09-18 22:18:56 +00:00
2019-09-19 15:16:32 +00:00
while read BLOCKED_IP
do
# Check if IP valid
VALID_IPv4 = $( echo " $BLOCKED_IP " | grep -Eo " ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} $" | wc -l)
if [ [ $VALID_IPv4 -eq 1 ] ] ; then
# Convert IPv4 to IPv6 :)
IPv4 = $( echo $BLOCKED_IP | sed 's/\./ /g' )
IPv6 = $( printf "0000:0000:0000:0000:0000:FFFF:%02X%02X:%02X%02X" $IPv4 )
CHECK_IF_EXISTS = $( sqlite3 /etc/synoautoblock.db " SELECT DENY FROM AutoBlockIP WHERE IP = ' $BLOCKED_IP ' " | wc -l)
if [ [ $CHECK_IF_EXISTS -lt 1 ] ] ; then
2019-09-20 21:00:26 +00:00
INSERT = $( sqlite3 /etc/synoautoblock.db " INSERT INTO AutoBlockIP VALUES (' $BLOCKED_IP ',' $UNIXTIME ',' $UNIXTIME_DELETE_IP ',' $Deny ',' $IPv6 ',' $TYPE ',' $META ') " )
2019-09-19 15:16:32 +00:00
countadded = $(( $countadded + 1 ))
if [ [ $LOGLEVEL -eq 2 ] ] ; then
echo " IP added to Database! --> $BLOCKED_IP "
2019-09-20 16:49:32 +00:00
elif [ [ $LOGLEVEL -eq 1 ] ] ; then
echo -n "."
2019-09-19 15:16:32 +00:00
fi
else
countskipped = $(( $countskipped + 1 ))
2019-09-24 15:49:07 +00:00
# if [[ $LOGLEVEL -eq 2 ]]; then
2019-09-19 15:16:32 +00:00
echo " IP already in Database! --> $BLOCKED_IP "
2019-09-24 15:49:07 +00:00
# elif [[ $LOGLEVEL -eq 1 ]]; then
# echo -n "."
# fi
2019-09-19 15:16:32 +00:00
fi
2019-09-18 22:18:56 +00:00
fi
2019-09-19 15:16:32 +00:00
done < /tmp/blocklist.txt
2019-09-18 22:18:56 +00:00
2019-09-24 15:45:32 +00:00
# stats …
2019-09-19 15:16:32 +00:00
if [ [ $LOGLEVEL -eq 1 ] ] || [ [ $LOGLEVEL -eq 2 ] ] ; then
END = $( date +%s)
2019-09-18 22:18:56 +00:00
RUNTIME = $(( END-UNIXTIME))
2019-09-24 22:09:15 +00:00
echo -e;
echo "stats:----------------------------------"
2019-09-19 15:16:32 +00:00
echo " duration of the process: $RUNTIME Seconds "
2019-09-24 15:45:32 +00:00
echo " count of IPs in list: $( cat "/tmp/onlinelist.txt" | grep -Eo " ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} $" | wc -l) "
echo " count of diffs: $( cat "/tmp/blocklist.txt" | grep -Eo " ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} $" | wc -l) "
2019-09-19 15:16:32 +00:00
echo " added IPs: $countadded "
echo " skipped IPs: $countskipped "
2019-09-20 21:00:26 +00:00
echo " count of blocked IPs: $( sqlite3 /etc/synoautoblock.db "SELECT count(IP) FROM AutoBlockIP WHERE Deny='1' " ) "
2019-09-20 16:07:09 +00:00
fi
2019-09-19 15:16:32 +00:00
2019-09-24 15:45:32 +00:00
rm /tmp/blocklist.txt
rm /tmp/before.txt
rm /tmp/onlinelist.txt
2019-09-20 21:00:26 +00:00
exit 0