#!/bin/bash -e
declare -rx PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin
declare -rx BASE_PATH=/root/blocklist.de
declare -rx LIST_URL_BDE=https://lists.blocklist.de/lists/mail.txt
declare -rx LIST_URL_ADB="https://api.abuseipdb.com/api/v2/blacklist?confidenceMinimum=100&limit=10000"
declare -rx LIST_FILE_BDE=$BASE_PATH/mail_blocked_ips.txt
declare -rx LIST_FILE_ADB=$BASE_PATH/abuseipdb_blocklist.txt
declare -rx LIST_FILE_FULL=$BASE_PATH/blocklist_full.txt
declare -rx ABUSEIPDB_KEY=***REDACTED***
declare -rx ABUSEIPDB_MAX=10000 # Limit für kostenfreie accounts von abuseipdb: 10000 entries
declare -rx ADB_MIN_SECS=7200 # Minimale Wartezeit für den download von abuseipdb (kostenfrei: 5x, supporter: 10x, webmaster: 20x)
#declare -rx ADB_MIN_SECS=17280 # Minimale Wartezeit für den download von abuseipdb (5 downloads pro Tag für kostenfreie accounts)
mylog () { echo "$(date) : $1"; }
list_valid() {
local file="$1"
local COUNT=$(grep -Ec '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' "$file")
mylog "Fuehre sicherheitscheck auf der Liste aus"
if [ "$COUNT" -lt 1000 ]; then
mylog "Liste $file ungueltig. Nur $COUNT IP-Adressen gefunden – alte Liste bleibt aktiv"
return 1
else
mylog "Liste $file gueltig. Anzahl IP-Adressen: $COUNT"
return 0
fi
}
update_blocklist_de_file() {
mylog "Hole neue Blockliste von $LIST_URL_BDE"
wget -q -O - "$LIST_URL_BDE" \
| grep -v : \
| grep -Ev '^[[:space:]]*(#|$)' >"$LIST_FILE_BDE.tmp"
if list_valid "$LIST_FILE_BDE.tmp" ; then
mv "$LIST_FILE_BDE.tmp" "$LIST_FILE_BDE"
fi
}
get_abuseipdb_blocklist_file() {
# liste from abuseipdb.com holen, IPv6-addressen entfernen
mylog "Hole neue Blockliste von abuseipdb.com"
if find "$LIST_FILE_ADB" -mmin -$(($ADB_MIN_SECS/60)) -print -quit | grep -q . ; then
mylog "Kein Download von abuseipdb.com, Download nur alle $(awk 'BEGIN { printf "%.1f Stunde(n)",'"$ADB_MIN_SECS"/3600' }')"
return
fi
wget -qO- \
--header="Key: $ABUSEIPDB_KEY" \
--header="Accept: application/json" "$LIST_URL_ADB" \
| jq -r '.|.data[] | .ipAddress' \
| grep -v : >"$LIST_FILE_ADB.tmp"
if list_valid "$LIST_FILE_ADB.tmp" ; then
mv "$LIST_FILE_ADB.tmp" "$LIST_FILE_ADB"
fi
}
main() {
mylog "Aktualisierungen von Blocklisten und IPSets"
cd $BASE_PATH
get_abuseipdb_blocklist_file
update_blocklist_de_file
mylog "merging block lists"
cat $LIST_FILE_BDE $LIST_FILE_ADB | sort -V | uniq >$LIST_FILE_FULL
list_valid "$LIST_FILE_FULL" # nochmal die gesamtliste zählen
mylog "Erstelles neues IP-Set"
ipset create mail_blocklist_new hash:net family inet
while read -r ip; do
ipset add mail_blocklist_new "$ip" -exist
done <$LIST_FILE_FULL
mylog "Aktiviere neues IP-Set"
ipset swap mail_blocklist_new mail_blocklist
ipset destroy mail_blocklist_new
mylog "Programm erfolgreich beendet"
}
main