fail2ban

Posted by moritz on Wednesday, November 12, 2014

Wie, was, fail2ban?

Wir experimentieren neuerdings mit fail2ban, um Bruteforce-Angriffe auf bestimmte Dienste abzufangen. Das Programm durchsucht Logs nach einem vorgegebenen Schema und kann, wenn dieses Schema mehr als x mal in Zeitraum y gefunden wurde, Aktionen durchführen, wie beispielsweise die IP-Adresse blockieren. Die Konfiguration ist auf allen unseren Hosts die gleiche, hier mal am Beispiel von crater:

[root@crater ~]# fail2ban-client status
Status
|- Number of jail:	6
`- Jail list:		apache-joomla-login, apache-wp-login, apache-auth, apache-friendica-load-prevention, ssh-iptables, wp-spam

Unsere Regeln

Das Prinzip ist immer das selbe. Da der Filter für die Wordpress-Loginmaske am meisten anschlägt, hier mal die dahinter liegende Konfiguration:

[apache-wp-login]
enabled  = true
filter   = apache-wp-login
action   = iptables-multiport[name=apache-wp-login,port="80,443"]
logpath  = /var/log/httpd/virtual_log
bantime  = 3600
maxretry = 10
findtime = 120

Durchsucht wird also das Log vom Webserver und wenn das Schema aus dem Filter apache-wp-login innerhalb von 120 Sekunden 10x gefunden wird, wird die IP-Adresse auf Port 80 und 443 für eine Stunde geblockt. Hier der Filter (und hier die Vorlage):

[Definition]
failregex = ^.*\.uberspace\.de <HOST>.*] "POST .*/wp-login.php HTTP/.*" 200

Hier mal ein Eintrag aus dem Log des Webservers, das auf das Schema passt:

xxxx.crater.uberspace.de x.x.x.x - - [12/Nov/2014:11:26:22 +0100] "POST /wp-login.php HTTP/1.1" 200 4007 "https://some_url/wp-login.php" "some_browser"

Wie viel bringt das?

Einiges:

[root@crater ~]# cat /var/log/messages | grep fail2ban.actions | tail
Nov 12 09:08:35 crater fail2ban.actions[10946]: WARNING [apache-wp-login] Ban x.x.x.x
Nov 12 09:19:24 crater fail2ban.actions[10946]: WARNING [wp-spam] Unban x.x.x.x
Nov 12 09:52:19 crater fail2ban.actions[10946]: WARNING [apache-wp-login] Ban x.x.x.x
Nov 12 09:53:04 crater fail2ban.actions[10946]: WARNING [apache-wp-login] Ban x.x.x.x
Nov 12 10:08:35 crater fail2ban.actions[10946]: WARNING [apache-wp-login] Unban x.x.x.x
Nov 12 10:52:19 crater fail2ban.actions[10946]: WARNING [apache-wp-login] Unban x.x.x.x
Nov 12 10:53:04 crater fail2ban.actions[10946]: WARNING [apache-wp-login] Unban x.x.x.x
Nov 12 10:53:25 crater fail2ban.actions[10946]: WARNING [ssh-iptables] Ban x.x.x.x
Nov 12 11:03:25 crater fail2ban.actions[10946]: WARNING [ssh-iptables] Unban x.x.x.x
Nov 12 11:04:34 crater fail2ban.actions[10946]: WARNING [apache-wp-login] Ban x.x.x.x

Wenn ihr euch also wundert, warum ihr euch nach einigen fehlgeschlagenen Versuchen nicht mehr einloggen könnt, weil keine Daten mehr vom Server kommen: It’s not a bug - it’s a feature.