Fail2ban: Implementing automated iptables rules to counter bruteforce attacks
Introduction
Fail2ban scans log files (e.g. /var/log/apache/error_log) and bans IPs that show malicious signs – too many password failures, seeking for exploits, etc. Generally Fail2Ban is then used to update firewall rules to reject the IP addresses for a specified amount of time, although any arbitrary other action (e.g. sending an email) could also be configured. Out of the box Fail2Ban comes with filters for various services (apache, courier, ssh, etc).
Fail2Ban is able to reduce the rate of incorrect authentication attempts however it cannot eliminate the risk that weak authentication presents. Configure services to use only two factor or public/private authentication mechanisms if you really want to protect services.
Installation
|
|
Configuration
You may want to add your own rules. Here are examples.
Wordpress
I want to block bruteforce on my Wordpress installation. Unfortunately Wordpress does not return 403 errors when an authentication fails. So we have to:
Jail
Add this in your jail.conf to check access and error log files (/etc/fail2ban/jail.conf
):
|
|
Filters
Here is the filter for access. It’s a regex to catch the IP address in the log file (/etc/fail2ban/filter.d/wp-auth-access.conf
):
|
|
And for error logs (/etc/fail2ban/filter.d/wp-auth-error.conf
):
|
|
Validate filters and configuration
You can validate the configuration of your filters like this:
|
|
Usage
Unban someone
This solution is to ask iptables to unban an IP. But Fail2ban won’t be aware of that and will still think that the attacker is blocked if you do not use solution one, until the maximum blocking retention time is reached.
Get the current chains list:
|
|
If you do not know on which chain your IP has been blocked, remove the grep command.
Then ask iptables to see the current blocked IPs on a specific chain:
|
|
Now I want to remove the second line:
|
|
To finish, inform fail2ban to unban someone:
|
|
Modify nginx-naxsi with the name of the fail2ban jail name.
Resources
Last updated 22 Aug 2014, 09:12 CEST.