Länder IP Ban mit ipset
Posted: Tue 17. Jun 2025, 00:13
Das offizielle KeyHelp Forum der Keyweb AG / The official KeyHelp forum of Keyweb AG
https://community.keyhelp.de/
Genau das war auch mein Gedanke als ich das Skript gesehen hab.
Eigentlich war's nicht geplant, da ich dachte, es wird jeder (kompetente) Serveradministrator mit etwas Hirnschmalz selbst hinkriegen.
Code: Select all
apt update
apt install ipset iptables-persistent ipset-persistent curl
Code: Select all
ipset create abuseipdb hash:ip family inet hashsize 4096 maxelem 500000
Code: Select all
iptables -I INPUT -m set --match-set abuseipdb src -j DROP
netfilter-persistent save
Code: Select all
#!/usr/bin/env bash
set -eu
LIST_URL="https://raw.githubusercontent.com/borestad/blocklist-abuseipdb/refs/heads/main/abuseipdb-s100-30d.ipv4"
SET="abuseipdb"
TMP="${SET}_tmp"
ipset create "$TMP" hash:ip family inet hashsize 4096 maxelem 500000 -exist
curl -s "$LIST_URL" | grep -v '^#' | cut -d' ' -f1 | while read -r ip; do
[[ -n "$ip" ]] && ipset ad
d "$TMP" "$ip" -exist
done
ipset swap "$SET" "$TMP"
ipset destroy "$TMP"
Code: Select all
crontab -e
Code: Select all
30 4 * * * update-abuseipdb.sh >/dev/null 2>&1
Danke!
11
Natürlich!
Kuckuck
Code: Select all
curl -s "$LIST_URL" | grep -v '^#' | cut -d' ' -f1 | while read -r ip; do
[[ -n "$ip" ]] && ipset ad
d "$TMP" "$ip" -exist
done
Code: Select all
curl -s "$LIST_URL" | grep -v '^#' | cut -d' ' -f1 | while read -r ip; do
[[ -n "$ip" ]] && ipset add "$TMP" "$ip" -exist
done
Code: Select all
# --- Prüfung und Regelsetzung ---
IPTABLES_VERSION_OUTPUT=$(iptables -V 2>&1)
if echo "$IPTABLES_VERSION_OUTPUT" | grep -q "(nf_tables)"; then
echo "Info: iptables (nf_tables backend) erkannt. Setze Regel mit iptables-nft."
# Die Regel für iptables-nft ist die gleiche wie für iptables-legacy
iptables -I INPUT -m set --match-set abuseipdb src -j DROP
elif echo "$IPTABLES_VERSION_OUTPUT" | grep -q "(legacy)"; then
echo "Info: iptables (legacy backend) erkannt. Setze Regel mit iptables-legacy."
iptables -I INPUT -m set --match-set abuseipdb src -j DROP
elif command -v nft &>/dev/null && nft list ruleset &>/dev/null; then
echo "Info: nftables direkt erkannt. Setze Regel mit nft."
# Hier wäre der nftables-spezifische Befehl, falls du ihn verwenden möchtest
# Beispiel (ersetze 'filter' und 'input' durch deine tatsächlichen Tabellen/Ketten, falls abweichend):
# nft add rule ip filter input ip saddr @abuseipdb drop
# Für dieses spezielle Szenario (ipset mit DROP) würde der iptables-Befehl über die Kompatibilitätsschicht immer noch funktionieren.
# Daher ist es meist nicht nötig, hier auf nft umzuschwenken, wenn die iptables-Regel auch mit nftables-Backend funktioniert.
iptables -I INPUT -m set --match-set abuseipdb src -j DROP # Weiterhin iptables-Befehl nutzen
else
echo "Warnung: Weder iptables-legacy noch nftables eindeutig erkannt. Versuche trotzdem iptables-Befehl."
echo "Bitte überprüfen Sie Ihre Firewall-Konfiguration manuell."
iptables -I INPUT -m set --match-set abuseipdb src -j DROP # Versuchen Sie es trotzdem
fi
Aii peinlicher Fehler mit dem Zeilenumbruch. phpBB hat mich scheinbar wieder mit den Tags überfordert.
Sehr gute Idee übrigens mit der Prüfung auf iptables oder nftables. Ich hatte tatsächlich schon überlegt, beim Einfügen auch direkt die Validität zu prüfen, wobei ich (hoffentlich) davon ausgehe, dass das Repository eine plausible/korrekte Quelle ist.Jolinar wrote: ↑Tue 1. Jul 2025, 20:01 Nachtrag:
Wenn du einmal am Verbessern deines Scripts bist, könntest du ja noch eine Prüfung einbauen, ob auf dem System iptables oder nftables zum Einsatz kommt. So wäre das Script auch dann noch valide, wenn iptables irgendwann aus den Systemen verschwindet.
Vielleicht sowas in der Art (Codesnippet mit KI erstellt, nicht getestet^^):
Code: Select all
#!/bin/bash
cd /tmp
ipset -N -exist china hash:net
timeout 10 curl -s https://www.ipdeny.com/ipblocks/data/countries/cn.zone | grep -oP '^[0-9\./]+' > cn.zone
for i in $(cat cn.zone ); do ipset -A china $i; done
ipset -N -exist bangladesh hash:net
timeout 10 curl -s https://www.ipdeny.com/ipblocks/data/countries/bd.zone | grep -oP '^[0-9\./]+' > bd.zone
for i in $(cat bd.zone ); do ipset -A bangladesh $i; done
ipset -N -exist brazil hash:net
timeout 10 curl -s https://www.ipdeny.com/ipblocks/data/countries/br.zone | grep -oP '^[0-9\./]+' > br.zone
for i in $(cat br.zone ); do ipset -A brazil $i; done
iptables -I INPUT -p all -m set --match-set china src -j DROP
iptables -I OUTPUT -p all -m set --match-set china src -j DROP
iptables -I FORWARD -p all -m set --match-set china src -j DROP
iptables -I INPUT -p all -m set --match-set bangladesh src -j DROP
iptables -I OUTPUT -p all -m set --match-set bangladesh src -j DROP
iptables -I FORWARD -p all -m set --match-set bangladesh src -j DROP
iptables -I INPUT -p all -m set --match-set brazil src -j DROP
iptables -I OUTPUT -p all -m set --match-set brazil src -j DROP
iptables -I FORWARD -p all -m set --match-set brazil src -j DROP
rm -rf /tmp/*.zone
Code: Select all
ipset -N -exist china6 hash:net family inet6
timeout 10 curl -s https://raw.githubusercontent.com/ipverse/rir-ip/master/country/cn/ipv6-aggregated.txt > cn1.zone
awk '/^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}(:\/[0-9]{1,2})?([[:space:]]|$)/{print $1}' /tmp/cn1.zone > /tmp/cn.zone
for i in $(cat cn.zone ); do ipset -A china6 $i; done
ip6tables -I INPUT -p all -m set --match-set china6 src -j DROP
ip6tables -I OUTPUT -p all -m set --match-set china6 src -j DROP
ip6tables -I FORWARD -p all -m set --match-set china6 src -j DROP
Code: Select all
ipset list china | wc -l
ipset list china
ipset list china6 | wc -l
ipset list china6
iptables -L -n --line-numbers
ip6tables -L -n --line-numbers