Mit ‘iptables’ Tagged einträge

WordPress Angriffe filtern

Montag, November 30th, 2015

Ich habe im August 2014 bereits eine Lösung mit iptables und fail2ban geschrieben. Da diese aber zu viele „fals positiv“ produziert hat, habe ich mich noch einmal hingesetzt und eine sowohl auf Performance als auch ohne fail2ban optimierte Lösung gesucht.

Mein zweiter Ansatz geht so vor, dass zuerst alle post requests in eine eigene iptbatles chain gepackt werden. Anschließen werden die in der chain noch übrig bleibenden anfragen nach den Schlüsselwörtern wie wp-login.php oder xmlrpc.php untersucht und mit einem Counter von 3 versehen. Beim dritten versuch ein falsches Passwort anzugeben werden die IPs der Botts für 600sec gesperrt.

iptables -A INPUT -p tcp --dport 80 -m string --algo bm --string "POST " -j HTTPPOST

iptables -A HTTPPOST -m string --algo bm --string "/wp-login.php" -m state --state NEW,ESTABLISHED -m recent --set --name "wp-auth"
iptables -A HTTPPOST -m string --algo bm --string "/wp-login.php" -m state --state NEW,ESTABLISHED -m recent --update --seconds 600 --hitcount 3 --name "wp-auth" -j DROP

iptables -A HTTPPOST -m string --algo bm --string "/xmlrpc.php" -m state --state NEW,ESTABLISHED -m recent --set --name "wp-xmlrpc"
iptables -A HTTPPOST -m string --algo bm --string "/xmlrpc.php" -m state --state NEW,ESTABLISHED -m recent --update --seconds 600 --hitcount 3 --name "wp-xmlrpc" -j DROP

iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT -m comment --comment "HTTP"

UPDATE: Danke an Jens, die SSL Regeln für Port 443 können so natürlich nicht funktionieren und wurden entfernt.

SSH via VPN, HTTP und co. via default route

Freitag, November 28th, 2014

Um einen Host hinter einer Firewall zu administrieren habe ich ein VPN zwischen den beiden Punkten aufgebaut. Um aber den ausgehenden Netzwerktraffic, der über die Firewall läuft, testen zu können brauchte ich eine Routing via Ports. Genau genommen wollte ich Port 22 durch den VPN Tunnel und den HTTP traffic über das Standard Gateway routen.
asyncrones-routing

Zuerst braucht man eine separate routing table in unserem Fall foo
echo "1 foo" >> /etc/iproute2/rt_tables

In diese routing Tabelle legen wir zuerst eine default route ab
ip route add table foo default dev vpn

Dann braucht man eine rule zum die angibt wann die foo routing Tabelle verwendet werden soll.
ip rule add fwmark 1 table foo

Nun kommen wir zur Auswahl der Pakete, die anders geroutet werden sollen. Dies geschieht über die mangle table. Hier werden die einzelnen Pakte über fwmarks markiert.
iptables -t mangle -A OUTPUT -d 203.0.113.80/32 -p tcp -m tcp --dport 22 -j MARK --set-xmark 0x1/0xffffffff

Damit der Kernel auch die ausgehenden Pakete richtig benennt, werden alle Pakete die über das VPN geroutet werden nun auf die source Adresse des VPNs umgeschrieben.
Dies ist notwendig, da der Kernel beim bestimmen der Absenderadresse unserer TCP Verbindung die firewall Konfiguration bei der Bestimmung der ausgehenden route nicht verarbeitet.
iptables -t nat -A POSTROUTING ! -s 10.8.0.2/32 -o vpn -j SNAT --to-source 10.8.0.2

Zum Schluss müssen wir das reverse path filtering ausschalten, damit die Entgegennahme der Pakete, deren Antwort an ein anderes Interface gehen würde, erlaubt werden.
echo 0 | tee /proc/sys/net/ipv4/conf/*/rp_filter

wp-login.php serverseitg filtern

Freitag, August 15th, 2014

UPDATE: es liegt eine Aktualisierte lösung für diese Problem vor

Aktuell finden heftige Angriffe auf WordPress Accounts statt. Die schnellste und einfachste Möglichkeit dies zu unterbinden, ist das Droppen aller Aufrufe der wp-login.php mittels iptables:

iptables -I INPUT -i eth0 -p tcp -m tcp --dport 80 -m string --algo bm --string "/wp-login.php" -j DROP

[Update] Besser:

iptables -A INPUT -p tcp --dport 80 -m string --algo bm --string "/wp-login.php" -m state --state NEW,ESTABLISHED -m recent --set --name "wp-auth"
iptables -A INPUT -p tcp --dport 80 -m string --algo bm --string "/wp-login.php" -m state --state NEW,ESTABLISHED -m recent --update --seconds 240 --hitcount 10 --name "wp-auth" -j DROP

Alternativ kann man auf fail2ban eine Regel mitgeben um beispielsweise folgende Logeinträge zu filtern:

10.0.0.1 - - [15/Aug/2014:09:07:39 +0200] "POST https://blog.chr.istoph.de/wp-login.php/wp-login.php/ HTTP/1.1" 200 3552 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.2) Gecko/2008091620 Firefox/3.0.2"
10.0.0.1 - - [15/Aug/2014:09:08:28 +0200] "POST /blog/wp-login.php HTTP/1.1" 200 2653 "https://blog.chr.istoph.de/blog/wp-login.php" "[% tools.ua.random() %]"

/etc/fail2ban/filter.d/wp-auth.conf

# WordPress brute force auth filter
[Definition]
failregex = ^< HOST > .* "POST*/wp-login.php*
ignoreregex =

/etc/fail2ban/jail.local

[wp-auth]
enabled = true
filter = wp-auth
action = iptables-multiport[name=NoAuthFailures, port="http,https"]
logpath = /var/www/vhosts/*/statistics/logs/access_log
bantime = 1200
maxretry = 3

iptables ICMP Typen

Montag, Dezember 9th, 2013

Um mal zu verdeutlichen welche ICMP Anfragen an meinen Server gestellt werden, habe ich folgende Typenbeschreibung als Kommentar in iptables ausgeben lassen.

iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT -m comment --comment "ICMP  Echo Reply"
#iptables -A INPUT -p icmp --icmp-type 1-2 -j ACCEPT -m comment --comment "ICMP  Reserved"
iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT -m comment --comment "ICMP  Destination Unreachable"
iptables -A INPUT -p icmp --icmp-type 4 -j ACCEPT -m comment --comment "ICMP  Source Quench"
iptables -A INPUT -p icmp --icmp-type 5 -j ACCEPT -m comment --comment "ICMP  Redirect"
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT -m comment --comment "ICMP  Echo Request"
iptables -A INPUT -p icmp --icmp-type 9 -j ACCEPT -m comment --comment "ICMP  Router Advertisement"
iptables -A INPUT -p icmp --icmp-type 10 -j ACCEPT -m comment --comment "ICMP  Router Solicitation"
iptables -A INPUT -p icmp --icmp-type 11 -j ACCEPT -m comment --comment "ICMP  Time Exceeded"
iptables -A INPUT -p icmp --icmp-type 12 -j ACCEPT -m comment --comment "ICMP  Parameter Problem"
iptables -A INPUT -p icmp --icmp-type 13 -j ACCEPT -m comment --comment "ICMP  Timestamp (erleichtert die Zeitsynchronisation)"
iptables -A INPUT -p icmp --icmp-type 14 -j ACCEPT -m comment --comment "ICMP  Timestamp Reply"
iptables -A INPUT -p icmp --icmp-type 15 -j ACCEPT -m comment --comment "ICMP  Information Request"
iptables -A INPUT -p icmp --icmp-type 16 -j ACCEPT -m comment --comment "ICMP  Information Reply"
iptables -A INPUT -p icmp --icmp-type 17 -j ACCEPT -m comment --comment "ICMP  Address Mask Request"
iptables -A INPUT -p icmp --icmp-type 18 -j ACCEPT -m comment --comment "ICMP  Address Mask Reply"
iptables -A INPUT -p icmp --icmp-type 19 -j ACCEPT -m comment --comment "ICMP  Reserved (for Security)"
#iptables -A INPUT -p icmp --icmp-type 20-29 -j ACCEPT -m comment --comment "ICMP  Reserved (for Robustness Experiment)"
iptables -A INPUT -p icmp --icmp-type 30 -j ACCEPT -m comment --comment "ICMP  Traceroute"
iptables -A INPUT -p icmp --icmp-type 31 -j ACCEPT -m comment --comment "ICMP  Datagram Conversion Error"
iptables -A INPUT -p icmp --icmp-type 32 -j ACCEPT -m comment --comment "ICMP  Mobile Host Redirect"
iptables -A INPUT -p icmp --icmp-type 33 -j ACCEPT -m comment --comment "ICMP  Ursprünglich IPv6 Where-Are-You (ersetzt durch ICMPv6)"
iptables -A INPUT -p icmp --icmp-type 34 -j ACCEPT -m comment --comment "ICMP  Ursprünglich IPv6 I-Am-Here (ersetzt durch ICMPv6)"
iptables -A INPUT -p icmp --icmp-type 35 -j ACCEPT -m comment --comment "ICMP  Mobile Registration Request"
iptables -A INPUT -p icmp --icmp-type 36 -j ACCEPT -m comment --comment "ICMP  Mobile Registration Reply"
iptables -A INPUT -p icmp --icmp-type 37 -j ACCEPT -m comment --comment "ICMP  Domain Name Request"
iptables -A INPUT -p icmp --icmp-type 38 -j ACCEPT -m comment --comment "ICMP  Domain Name Reply"
iptables -A INPUT -p icmp --icmp-type 39 -j ACCEPT -m comment --comment "ICMP  SKIP"
iptables -A INPUT -p icmp --icmp-type 40 -j ACCEPT -m comment --comment "ICMP  Photuris"
iptables -A INPUT -p icmp --icmp-type 41 -j ACCEPT -m comment --comment "ICMP  ICMP messages utilized by experimental mobility protocols such as Seamoby"
#iptables -A INPUT -p icmp --icmp-type 42-255 -j ACCEPT -m comment --comment "ICMP  Reserved"

Mit iptables -L -vn kann man sich dann die anzahl ausgeben lassen.
Quelle: wikipedia.org