88 lines
2.9 KiB
Bash
Executable File
88 lines
2.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
|
|
|
if (( EUID != 0 )); then
|
|
echo 'Die Sperrlisten-Synchronisierung benötigt Root-Rechte.' >&2
|
|
exit 1
|
|
fi
|
|
|
|
exec 9>/run/bca-blocked-ips.lock
|
|
flock --exclusive 9
|
|
|
|
workdir=$(mktemp -d)
|
|
trap 'rm -rf -- "$workdir"' EXIT
|
|
chain=permanent-bans
|
|
|
|
update_set() (
|
|
set_name=$1
|
|
set_type=$2
|
|
url=$3
|
|
staging="${set_name}-staging"
|
|
|
|
curl --disable --fail --silent --show-error --location \
|
|
--proto '=https' --proto-redir '=https' --connect-timeout 10 \
|
|
--max-time 45 --max-filesize 4194304 --output "$workdir/list" "$url" || exit 1
|
|
|
|
python3 - "$workdir/list" "$staging" "$set_type" >"$workdir/restore" <<'PY' || exit 1
|
|
import ipaddress
|
|
import sys
|
|
|
|
path, staging, set_type = sys.argv[1:]
|
|
entries = set()
|
|
with open(path, encoding="utf-8") as source:
|
|
for number, line in enumerate(source, 1):
|
|
value = line.partition("#")[0].strip()
|
|
if not value:
|
|
continue
|
|
try:
|
|
if set_type == "hash:net" and "/" in value:
|
|
address, prefix = value.split("/", 1)
|
|
if not prefix.isascii() or not prefix.isdecimal() or int(prefix) == 0:
|
|
raise ValueError
|
|
entry = ipaddress.IPv4Network(f"{address}/{prefix}", strict=False)
|
|
else:
|
|
entry = ipaddress.IPv4Address(value)
|
|
except ValueError:
|
|
sys.exit(f"Ungültiger IPv4-Eintrag in Zeile {number}.")
|
|
entries.add(str(entry))
|
|
|
|
if not entries:
|
|
sys.exit("Die Sperrliste enthält keine Einträge.")
|
|
|
|
for entry in sorted(entries):
|
|
print(f"add {staging} {entry}")
|
|
PY
|
|
|
|
# Reste eines abgebrochenen Laufs entfernen, bevor das Set befüllt wird.
|
|
ipset destroy "$staging" 2>/dev/null || true
|
|
ipset create "$staging" "$set_type" family inet || exit 1
|
|
trap 'ipset destroy "$staging"' EXIT
|
|
ipset restore <"$workdir/restore" || exit 1
|
|
ipset swap "$staging" "$set_name" || exit 1
|
|
)
|
|
|
|
ipset -exist create bca-badguys hash:net family inet
|
|
ipset -exist create ci-badguys hash:ip family inet
|
|
iptables -w 10 -n -L "$chain" >/dev/null 2>&1 || iptables -w 10 -N "$chain"
|
|
|
|
for set_name in bca-badguys ci-badguys; do
|
|
iptables -w 10 -C "$chain" -m set --match-set "$set_name" src -j DROP 2>/dev/null ||
|
|
iptables -w 10 -A "$chain" -m set --match-set "$set_name" src -j DROP
|
|
done
|
|
iptables -w 10 -C INPUT -j "$chain" 2>/dev/null ||
|
|
iptables -w 10 -I INPUT 1 -j "$chain"
|
|
|
|
status=0
|
|
if ! update_set bca-badguys hash:net \
|
|
https://git.bcanet.tech/Lukas/BCA-Blocked-IPs/raw/branch/main/bca-banned-ips.txt; then
|
|
echo 'Die BCA-Sperrliste konnte nicht aktualisiert werden.' >&2
|
|
status=1
|
|
fi
|
|
if ! update_set ci-badguys hash:ip https://cinsscore.com/list/ci-badguys.txt; then
|
|
echo 'Die CINS-Sperrliste konnte nicht aktualisiert werden.' >&2
|
|
status=1
|
|
fi
|
|
exit "$status"
|