geoIP
Bebilderte Schritt für Schritt Anleitung die Zeigt wie man den Zugriff (SSH, Webseite) auf einzelne Länder eingrenzt.
Inhalt
- geoIP für iptables installieren
- geoIP für iptables konfigurieren
- geoIP für iptables update
- iptables beim Rechnerstart laden
- iptables INPUT auf DROP
- geoIP für apache installieren
- geoIP für apache testen
- geoip für apache in Webseite integrieren
- geoip für apache updaten
- Zugriff auf Web Seiten IP basiert eingrenzen
- Whoami Seite / IP des Webusers anzeigen
geoIP für iptables installieren
Ihr meldet Euch als root am System an bzw. nutzt das su- Kommando um zum root user zu wechseln.
Installieren mit
apt-get install iptables-dev xtables-addons-common
Testen könnt Ihr geoip mit
iptables -m geoip –help
Weiteres Paket mit
apt-get install libtext-csv-xs-perl
installieren.
geoIP für iptables konfigurieren
Verzeichnis erstellen: mkdir /usr/share/xt_geoip
Ins Verzeichnis wechseln: cd /usr/lib/xtables-addons/
./xt_geoip_build -D /usr/share/xt_geoip *.csv
ausführen.
geoIP für iptables aktivieren (Sperre alle Länder ausser Deutschland und Schweiz): iptables -I INPUT 1 -p tcp --dport 22 -m geoip ! --source-country DE,CH -j DROP
geoIP für iptables update
!!!Wichtig ist dass Ihr ab sofort sobald Ihr etwas an den iptables ändert diese anschliessend sichert!!! iptables sichern mit iptables-save > /etc/iptables.local
Neue Datei erstellen: vi /etc/geoIPupdateIPtables
/bin/rm /usr/lib/xtables-addons/GeoIPv6.csv /usr/lib/xtables-addons/xt_geoip_dl /usr/lib/xtables-addons/xt_geoip_build -D /usr/share/xt_geoip *.csv /sbin/iptables-restore < /etc/iptables.local
Datei ausführbar machen: chmod +x /etc/geoIPupdateIPtables
Update automatisch per Crontab: crontab -e
# Update geoIP Data iptables jeden ersten des Monates um 0:45 45 0 1 * * /etc/geoIPupdateIPtables >> /var/log/geoIPupdate 2>&1
iptables beim Rechnerstart laden
Neue Datei erstellen: vi /etc/network/if-up.d/iptables
#!/bin/sh /sbin/iptables-restore < /etc/iptables.local service fail2ban restart
Datei ausführbar machen: chmod +x /etc/network/if-up.d/iptables
iptables INPUT auf DROP
Nach der Installation erlauben die iptables alle Ports. Dies ändern wir hiermit in dem wir alles verbieten und nur das was wir benötigen erlauben.
SSH, ICMP, Traffik zum Loopback und ausgehenden Traffik erlauben:
iptables -I INPUT 1 -i lo -j ACCEPT iptables -A INPUT -p icmp -j ACCEPT iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT iptables -A INPUT -p tcp --dport ssh -j ACCEPT iptables -P INPUT DROP
Anschliessend die iptables wieder sichern: iptables-save > /etc/iptables.local
geoIP für apache installieren
!!!Bitte zuerst Apache installieren!!!
Ihr meldet Euch als root am System an bzw. nutzt das su- Kommando um zum root user zu wechseln.
Zuerst führt Ihr ein
apt-get update
durch damit das Inhaltsverzeichnis von apt-get auf dem
Mit
apt-get install libapache2-mod-geoip
geoIP für Apache installieren.
Apache neu starten:
service apache2 restart
geoIP für apache testen
Testdatei anlegen: vi /var/www/eigener-server.ch/public_html/geoip.php
<?php $country_name = apache_note("GEOIP_COUNTRY_NAME"); print "Country: " . $country_name; ?>
Zum Testen Deine Eigene Seite
https://www.eigener-server.ch/geoip.php
aufrufen.
geoip für apache in Webseite integrieren
File anpassen: vi /etc/apache2/mods-available/geoip.conf
<IfModule mod_geoip.c> GeoIPEnable On # GeoIPDBFile /usr/share/GeoIP/GeoIP.dat GeoIPScanProxyHeaders On SetEnvIf GEOIP_COUNTRY_CODE AE BlockCountry SetEnvIf GEOIP_COUNTRY_CODE AF BlockCountry SetEnvIf GEOIP_COUNTRY_CODE AL BlockCountry SetEnvIf GEOIP_COUNTRY_CODE AM BlockCountry SetEnvIf GEOIP_COUNTRY_CODE AZ BlockCountry SetEnvIf GEOIP_COUNTRY_CODE BA BlockCountry SetEnvIf GEOIP_COUNTRY_CODE BD BlockCountry SetEnvIf GEOIP_COUNTRY_CODE BG BlockCountry SetEnvIf GEOIP_COUNTRY_CODE BY BlockCountry SetEnvIf GEOIP_COUNTRY_CODE CD BlockCountry SetEnvIf GEOIP_COUNTRY_CODE CF BlockCountry SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry SetEnvIf GEOIP_COUNTRY_CODE GR BlockCountry SetEnvIf GEOIP_COUNTRY_CODE HK BlockCountry SetEnvIf GEOIP_COUNTRY_CODE IL BlockCountry SetEnvIf GEOIP_COUNTRY_CODE IQ BlockCountry SetEnvIf GEOIP_COUNTRY_CODE IR BlockCountry SetEnvIf GEOIP_COUNTRY_CODE JO BlockCountry SetEnvIf GEOIP_COUNTRY_CODE KE BlockCountry SetEnvIf GEOIP_COUNTRY_CODE KG BlockCountry SetEnvIf GEOIP_COUNTRY_CODE KR BlockCountry SetEnvIf GEOIP_COUNTRY_CODE KZ BlockCountry SetEnvIf GEOIP_COUNTRY_CODE LB BlockCountry SetEnvIf GEOIP_COUNTRY_CODE LY BlockCountry SetEnvIf GEOIP_COUNTRY_CODE MA BlockCountry SetEnvIf GEOIP_COUNTRY_CODE MD BlockCountry SetEnvIf GEOIP_COUNTRY_CODE ME BlockCountry SetEnvIf GEOIP_COUNTRY_CODE MN BlockCountry SetEnvIf GEOIP_COUNTRY_CODE OM BlockCountry SetEnvIf GEOIP_COUNTRY_CODE PK BlockCountry SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry SetEnvIf GEOIP_COUNTRY_CODE SA BlockCountry SetEnvIf GEOIP_COUNTRY_CODE SD BlockCountry SetEnvIf GEOIP_COUNTRY_CODE SN BlockCountry SetEnvIf GEOIP_COUNTRY_CODE SY BlockCountry SetEnvIf GEOIP_COUNTRY_CODE TJ BlockCountry SetEnvIf GEOIP_COUNTRY_CODE TM BlockCountry SetEnvIf GEOIP_COUNTRY_CODE TN BlockCountry SetEnvIf GEOIP_COUNTRY_CODE TW BlockCountry SetEnvIf GEOIP_COUNTRY_CODE UA BlockCountry SetEnvIf GEOIP_COUNTRY_CODE UZ BlockCountry SetEnvIf GEOIP_COUNTRY_CODE VN BlockCountry SetEnvIf GEOIP_COUNTRY_CODE DE AllowCountry SetEnvIf GEOIP_COUNTRY_CODE CH AllowCountry SetEnvIf GEOIP_COUNTRY_CODE EU AllowEU </Ifmodule>
An Datei für virtual host anfügen: vi /etc/apache2/sites-available/eigener-server.ch
<Directory /> Deny from all Allow from env=AllowCountry </Directory>
geoip für apache updaten
Binary geoip installieren mit
apt-get install geoip-bin
Neue Datei anlegen: vi /etc/geoIPupdate
#!/bin/bash cd /tmp #downloads the archive wget -q -O "/tmp/GeoIP.dat.gz" http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz #tests the archive if ( gzip -tq "/tmp/GeoIP.dat.gz" ) then #uncompresses the archive gzip -df "/tmp/GeoIP.dat.gz" #checks if the .dat file works; requires geoip-bin if ( geoiplookup -f /tmp/GeoIP.dat -v 1.1.1.1 | grep -oq 'GeoIP' ) then echo `date` "The file is OK. Proceeding to copy it..." cp -f "/usr/share/GeoIP/GeoIP.dat" "/usr/share/GeoIP/GeoIP-old.dat" cp -f "/tmp/GeoIP.dat" "/usr/share/GeoIP/GeoIP.dat" else echo `date` "The downloaded GeoIP library is broken." fi else echo `date` "The downloaded GeoIP archive is broken." fi
Datei ausführbar machen: chmod +x /etc/geoIPupdate
Alte Datei mit Geodaten sichern: cp -f /usr/share/GeoIP/GeoIP.dat /usr/share/GeoIP/GeoIP-old.org
Download mittels der neuen Daten testen:
/etc/geoIPupdate
Job im Cronjob anfügen: crontab -e
# Update geoIP Data jeden ersten des Monates um 0:30 30 0 1 * * /etc/geoIPupdate >> /var/log/geoIPupdate 2>&1
Zugriff auf Web Seiten IP basiert eingrenzen
Damit nur Du / Deine IP auf bestimmte Seiten (z.B. admin Seiten) kommst benötigt es nur ein paar weitere Zeilen.
IP Abfragen mit
https://www.eigener-server.ch/tools/whoami.php
Zum File in der Sektion <IfModule …> hinzufügen (IP einsetzen): vi /etc/apache2/mods-available/geoip.conf
SetEnvIf Remote_Addr "^1\.2\.3\.4$" AllowAdmin SetEnvIf Remote_Addr "^1\.2\.3\.6$" AllowAdmin #falls Ihr weitere IPs habt
File in der Sektion <Directory …> anpassen: vi /etc/apache2/sites-available/eigener-server.ch
Deny from all Allow from env=AllowAdmin
Neue Variablen einlesen: service apache2 restart
Whoami Seite / IP des Webusers anzeigen
Neues Verzeichen auf dem Server mkdir /var/www/eigener-server.ch/wordpress/tools
Neue Datei anlegen vi /var/www/eigener-server.ch/wordpress/tools/whoami.php
<?php //Gets the IP address $ip = getenv("REMOTE_ADDR") ; Echo "Your IP is " . $ip; ?>
IP Abfragen mit
https://www.eigener-server.ch/tools/whoami.php