fail2ban-logo
fail2ban-logo

Als je VPS maar even actief is, dan zie je in de logging al de inbraakmeldingen komen. Met Fail2ban, kun je deze servers bannen voor een bepaalde tijd. Fail2ban werkt d.m.v. het monitoren van je logfiles. Als er bijv. 5 maal wordt geprobeerd in te loggen met foute logingegevens, dan kun je de VPS met dat IP nr niet meer bereiken voor bijv. 24 uur.

Installeren van fail2ban:

$ apt-get install fail2ban

Wijzigingen op de default instellingen (jail.conf) moeten gedaan worden in jail.local. In de sectie [DEFAULT] moeten een aantal zaken ingesteld worden:

  • ignoreip: IP adressen die nooit geblokkeerd zullen worden, dus bijvoorbeeld je eigen (dynamische) thuis IP adres via duckdns.org
  • destemail: je eigen email adres, waar de Fail2ban notificaties naar toe moeten
  • action: zet een commentaar teken ‘#’ voor de regel die naar wens, action_mwl stuurt bij alle bans een notificatie, met action_ zet je dit uit.

Maak de jail.local file aan als volgt:

$ vi /etc/fail2ban/jail.local

[DEFAULT]
# Tooway IP range, localhost
ignoreip = 127.0.0.1/8 myip.duckdns.org
bantime = 86400
maxretry = 3
mta = mail
destemail = mijn@emailadres.nl
#action = %(action_mwl)s
action = %(action_)s
[ssh]
enabled = true

[pam-generic]
enabled = true
banaction = iptables-allports

[ssh-ddos]
enabled = true

[postfix]
enabled = true

[sasl]
enabled = true

[ssh-repeater]
enabled = true
filter = sshd
action = iptables-repeater[name=ssh]
%(mta)s-whois-lines[name=%(__name__)s, dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s"]
logpath = /var/log/auth.log
maxretry = 21
findtime = 31536000
bantime = 31536000

[nginx-auth]
enabled = true
filter = nginx-auth
action = iptables-multiport[name=NoAuthFailures, port="http,https"]
logpath = /var/log/nginx*/*error*.log
bantime = 600 # 10 minutes
maxretry = 6

[nginx-login]
enabled = true
filter = nginx-login
action = iptables-multiport[name=NoLoginFailures, port="http,https"]
logpath = /var/log/nginx*/*access*.log
bantime = 600 # 10 minutes
maxretry = 6

[nginx-badbots]
enabled = true
filter = apache-badbots
action = iptables-multiport[name=BadBots, port="http,https"]
logpath = /var/log/nginx*/*access*.log
bantime = 86400 # 1 day
maxretry = 1

# Warning: this filter blocks ALL script access from other IP addresses than ignoreip
[nginx-noscript]
enabled = false
action = iptables-multiport[name=NoScript, port="http,https"]
filter = nginx-noscript
logpath = /var/log/nginx*/*access*.log
maxretry = 6
bantime = 86400 # 1 day

[nginx-proxy]
enabled = true
action = iptables-multiport[name=NoProxy, port="http,https"]
filter = nginx-proxy
logpath = /var/log/nginx*/*access*.log
maxretry = 0
bantime = 86400 # 1 day

[wp-auth]
enabled = true
port = http,https
filter = wp-auth
logpath = /var/log/nginx/*access*.log
maxretry = 3
bantime = 3600

Filter scripts

Ter ondersteuning van bovenstaande instellingen, hebben we filter configuratie files nodig. Deze staan in /etc/fail2ban/filter.d/. Maak onderstaande filters als volgt aan:

Nginx-proxy filter

$ vi nginx-proxy.conf
# Proxy filter /etc/fail2ban/filter.d/nginx-proxy.conf:
#
# Block IPs trying to use server as proxy.
#
# Matches e.g.
# 192.168.1.1 - - "GET http://www.something.com/
#
[Definition]
failregex = ^<HOST> -.*GET http.*
ignoreregex =

Nginx-noscript filter

$ vi /etc/fail2ban/filter.d/nginx-noscript.conf
# Noscript filter /etc/fail2ban/filter.d/nginx-noscript.conf:
#
# Block IPs trying to execute scripts such as .php, .pl, .exe and other funny scripts.
#
# Matches e.g.
# 192.168.1.1 - - "GET /something.php
#
[Definition]
failregex = ^<HOST> -.*GET.*(\.php|\.asp|\.exe|\.pl|\.cgi|\scgi)
ignoreregex =

Nginx-auth filter

$ vi /etc/fail2ban/filter.d/nginx-auth.conf
#
# Auth filter /etc/fail2ban/filter.d/nginx-auth.conf:
#
# Blocks IPs that fail to authenticate using basic authentication
#
[Definition]

failregex = no user/password was provided for basic authentication.*client: <HOST>
 user .* was not found in.*client: <HOST>
 user .* password mismatch.*client: <HOST>

ignoreregex =

Nginx-login filter

$ vi /etc/fail2ban/filter.d/nginx-login.conf
#
# Login filter /etc/fail2ban/filter.d/nginx-login.conf:
#
# Blocks IPs that fail to authenticate using web application's log in page
#
# Scan access log for HTTP 200 + POST /sessions => failed log in
[Definition]
failregex = ^<HOST> -.*POST /sessions HTTP/1\.." 200
ignoreregex =

WP-auth filter

$ vi /etc/fail2ban/filter.d/wp-auth
# fail2ban filter configuration for WordPress Logins
[Definition]
failregex = .*POST.*(wp-login\.php|xmlrpc\.php).* 401
ignoreregex =

Action scripts

Naast de filter scripts, hebben we ook de action scripts nodig. Deze staan in /etc/fail2ban/action.d/. Maak onderstaande actions als volgt aan:

Iptables-repeater

$ vi /etc/fail2ban/action.d/iptables-repeater.conf
# Fail2ban configuration file
#
# Author: Phil Hagen <phil@identityvector.com>
#
[Definition]
# Option: actionstart
# Notes.: command executed once at the start of Fail2Ban.
# Values: CMD
#
actionstart = iptables -N fail2ban-REPEAT-<name>
iptables -A fail2ban-REPEAT-<name> -j RETURN
iptables -I INPUT -j fail2ban-REPEAT-<name>
# set up from the static file
cat /etc/fail2ban/ip.blocklist.<name> |grep -v ^\s*#|awk '{print $1}' | while read IP; do iptables -I fail2ban-REPEAT-<name> 1 -s $IP -j DROP; done

# Option: actionstop
# Notes.: command executed once at the end of Fail2Ban
# Values: CMD
#
actionstop = iptables -D INPUT -j fail2ban-REPEAT-<name>
iptables -F fail2ban-REPEAT-<name>
iptables -X fail2ban-REPEAT-<name>

# Option: actioncheck
# Notes.: command executed once before each actionban command
# Values: CMD
#
actioncheck = iptables -n -L INPUT | grep -q fail2ban-REPEAT-<name>

# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: <ip> IP address
# <failures> number of failures
# <time> unix timestamp of the ban time
# Values: CMD
actionban = iptables -I fail2ban-REPEAT-<name> 1 -s <ip> -j DROP
# also put into the static file to re-populate after a restart
! grep -Fq <ip> /etc/fail2ban/ip.blocklist.<name> && echo "<ip> # fail2ban/$( date '+%%Y-%%m-%%d %%T' ): auto-add for repeat offender" >> /etc/fail2ban/ip.blocklist.<name>

$ Option: actionunban
$ Notes.: command executed when unbanning an IP. Take care that the
$ command is executed with Fail2Ban user rights.
$ Tags: <ip> IP address
$ <failures> number of failures
$ <time> unix timestamp of the ban time
$ Values: CMD
#
actionunban = /bin/true

[Init]
$ Defaut name of the chain
name = REPEAT

Aanmaken Wordpress Fail2ban plugin

Hierboven staat in de configuratie jail.local: [wp-auth], deze heeft het filter wp-auth.conf nodig (en staat hierboven in de lijst van filters. Om alles compleet te maken, staat hieronder hoe je de Fail2ban plugin voor Wordpress kunt maken. Het leuke idee is van Konstantin Kovshenin en heb ik iets aangepast om aan de standaarden te voldoen (http status code gewijzigd). Het werkt als volgt, als iemand probeert in te loggen op Wordpress, wordt met onderstaande plugin een http 401 status code gegenereerd, die weer door Fail2ban uit de logging gehaald wordt. Na een ingesteld aantal keren proberen, wordt er op dit IP adres een ban gezet.

De Wordpress Fail2Ban plugin

Hieronder maken we de plugin voor Fail2ban aan:

$ cd /var/www/www.example.com/wp-content/plugins/
$ mkdir fail2ban
$ cd fail2ban

Maak nu de fail2ban.php file aan als volgt:

$ vi fail2ban.php
<?php
/*
Plugin Name: Fail2Ban
Version: 1.0
Author: Arjan Wooning
Description: Sets a 401 Status Code which shows in access logs for use with fail2ban
Author URI: http://arjanwooning.nl
Basic idea from Konstantin Kovshenin - http://kovshenin.com/2014/fail2ban-wordpress-nginx/
ModificationL changed the http status code to 401 (Unauthorized)
*/
function fail2ban_login_failed_401() {
 status_header( 401 );
}
add_action( 'wp_login_failed', 'fail2ban_login_failed_401' );

En zet de rechten goed van de plugin:

$ cd ..
$ chown -R www-data:www-data fail2ban

Het uittesten van Fail2ban

De Fail2ban regels zijn als volgt op de commandline uit te testen :

$ fail2ban-regex <log file> <filter configuration>

Voorbeeld:

$ fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wp-auth.conf

De gids “VPS opzetten” bestaat uit de volgende artikelen: