Aktionen

Ufw Geoblocking mit Ubuntu 24.04

Aus znilwiki

Vorwort

Unter Apache2 nutze ich ein Geoblocking - ich habe aber nach einer Möglichkeit gesucht, das ganz schon vor dem Apache zu blockieren (und mir die Logeinträge dafür damit zu sparen).
Dafür habe ich diese Anleitung gefunden:

https://www.seenlyst.com/blog/geo-blocking-ufw-iptables/

Die aber für Ubuntu 24.04 geschrieben wurde und so nicht mehr funktioniert.
Ich habe die Fehler und Änderungen herausgesucht und hier ist meine Version des Artikels:


Vorwort

Ich wechsle einmal am Anfang zum root, alle nachfolgenden Befehle werden also als dieser ausgeführt!

sudo -i



Voraussetzungen installieren

apt update -y
apt upgrade -y
apt install curl perl unzip xtables-addons-common libtext-csv-xs-perl libmoosex-types-netaddr-ip-perl



Verzeichnis für unsere Daten erstellen

mkdir -p /usr/share/xt_geoip



Downloadskript erstellen

nano /usr/local/bin/update-geoip.sh

mit folgendem Inhalt:

#!/bin/bash

# Create temporary directory
mkdir -p /usr/share/xt_geoip/tmp/
mkdir -p /usr/share/xt_geoip/tmp/ip2loc/

# Download latest from db-ip.com
cd /usr/share/xt_geoip/tmp/
/usr/libexec/xtables-addons/xt_geoip_dl

# Download maxmind legacy csv and process
wget https://mailfud.org/geoip-legacy/GeoIP-legacy.csv.gz -O /usr/share/xt_geoip/tmp/GeoIP-legacy.csv.gz
gunzip /usr/share/xt_geoip/tmp/GeoIP-legacy.csv.gz
cat /usr/share/xt_geoip/tmp/GeoIP-legacy.csv | tr -d '"' | cut -d, -f1,2,5 > /usr/share/xt_geoip/tmp/GeoIP-legacy-processed.csv
rm /usr/share/xt_geoip/tmp/GeoIP-legacy.csv
# rm /usr/share/xt_geoip/tmp/GeoIP-legacy.csv.gz

# Download latest from https://github.com/sapics/ip-location-db
wget -P /usr/share/xt_geoip/tmp/ https://cdn.jsdelivr.net/npm/@ip-location-db/geo-whois-asn-country/geo-whois-asn-country-ipv4.csv
wget -P /usr/share/xt_geoip/tmp/ https://cdn.jsdelivr.net/npm/@ip-location-db/geo-whois-asn-country/geo-whois-asn-country-ipv6.csv
wget -P /usr/share/xt_geoip/tmp/ https://cdn.jsdelivr.net/npm/@ip-location-db/iptoasn-country/iptoasn-country-ipv4.csv
wget -P /usr/share/xt_geoip/tmp/ https://cdn.jsdelivr.net/npm/@ip-location-db/iptoasn-country/iptoasn-country-ipv6.csv
wget -P /usr/share/xt_geoip/tmp/ https://cdn.jsdelivr.net/npm/@ip-location-db/dbip-country/dbip-country-ipv4.csv
wget -P /usr/share/xt_geoip/tmp/ https://cdn.jsdelivr.net/npm/@ip-location-db/dbip-country/dbip-country-ipv6.csv
wget -P /usr/share/xt_geoip/tmp/ https://cdn.jsdelivr.net/npm/@ip-location-db/geolite2-country/geolite2-country-ipv4.csv
wget -P /usr/share/xt_geoip/tmp/ https://cdn.jsdelivr.net/npm/@ip-location-db/geolite2-country/geolite2-country-ipv6.csv

# Combine all csv and remove duplicates
cd /usr/share/xt_geoip/tmp/
cat *.csv > geoip.csv
sort -u geoip.csv -o /usr/share/xt_geoip/dbip-country-lite.csv

# Remove temp directory and update geoip xtables
cd /usr/share/xt_geoip/
rm -r /usr/share/xt_geoip/tmp/
/usr/libexec/xtables-addons/xt_geoip_build -D /usr/share/xt_geoip/ -i /usr/share/xt_geoip/dbip-country-lite.csv
rm /usr/share/xt_geoip/dbip-country-lite.csv

# reload ufw
ufw reload

Das Skript noch ausführbar machen und testen:

chmod +x /usr/local/bin/update-geoip.sh
/usr/local/bin/update-geoip.sh



Blocking in Firewallregeln einbauen

nano /etc/ufw/before.rules

und unten vor dem COMMIT in der letzten Zeile folgendes einfügen:

#GeoBlocking
-A ufw-before-input -m geoip --src-cc RU,CN,HK -j DROP

Das gleiche für IPv6:

nano /etc/ufw/before6.rules

und unten das hier einfügen (ist nicht das gleiche wie bei IPv4!)

#GeoBlocking
-A ufw6-before-input -m geoip --src-cc RU,CN,HK -j DROP

Damit ist Russland (RU), China (CN) und Hongkong (HK) für alles gesperrt!


Regel aktivieren

ufw reload

Gibt es dabei eine Fehler, so ist die Firewall ausgeschaltet! In der Fehlermeldung wird die Datei und die Zeile stehen in der etwas falsch ist.
Korrigieren und dann die Firewall mit

ufw enable
ufw reload

neu starten und die Regeln prüfen!


Beispiele für weitere Regeln

/etc/ufw/before.rules

-A ufw-before-input -p tcp --dport 3000 -m geoip --src-cc RU,CN -j DROP

/etc/ufw/before6.rules

-A ufw6-before-input -p tcp --dport 3000 -m geoip --src-cc RU,CN -j DROP

Sperrt TCP Datenverkehr auf Port 3000 für RU und CN


Cronjob erstellen

Einmal im Monat sollte man die Geodaten aktualisieren:

Kommt noch - es muss das Skript einfach noch einmal gestartet werden



Kommentare

Loading comments...