Preventing Brute Force Attacks With Fail2ban On Mandriva 2008.1
Author: Falko Timme
In this article I will show how to install and configure fail2ban on a Mandriva 2008.1 system. Fail2ban is a tool that observes login attempts to various services, e.g. SSH, FTP, SMTP, Apache, etc., and if it finds failed login attempts again and again from the same IP address or host, fail2ban stops further login attempts from that IP address/host by blocking it with an iptables firewall rule.
This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!
1 Preliminary Note
Fail2ban is similar to DenyHosts which I covered in this tutorial: http://www.howtoforge.com/preventing_ssh_dictionary_attacks_with_denyhosts, but unlike DenyHosts which focuses on SSH, fail2ban can be configured to monitor any service that writes login attempts to a log file, and instead of using /etc/hosts.deny only to block IP addresses/hosts, fail2ban can use iptables and /etc/hosts.deny.
In this example I will configure fail2ban to monitor login attempts to the SSH server, the Proftpd server, login attempts to .htaccess/.htpasswd protected web sites, to Courier POP3 and Courier IMAP, and to SASL (for sending emails). I will install the fail2ban package that is available for Mandriva 2008.1. It comes with a default configuration, but unfortunately that configuration doesn't quite work for most of the aforementioned services. Therefore I will create a customized fail2ban configuration that I have tested and that works for me.
2 Installing fail2ban
Before we install any packages, we must enable the main, main_updates, contrib, and contrib_updates repositories. Go to http://easyurpmi.zarb.org/ - it should give you the commands you need to run to enable these repositories. In my case, I ran
urpmi.addmedia contrib ftp://distrib-coffee.ipsl.jussieu.fr/pub/linux/MandrivaLinux/official/2008.1/i586/media/contrib/release with media_info/hdlist.cz
urpmi.addmedia --update contrib_updates ftp://distrib-coffee.ipsl.jussieu.fr/pub/linux/MandrivaLinux/official/2008.1/i586/media/contrib/updates with media_info/hdlist.cz
urpmi.addmedia main ftp://distrib-coffee.ipsl.jussieu.fr/pub/linux/MandrivaLinux/official/2008.1/i586/media/main/release with media_info/hdlist.cz
urpmi.addmedia --update main_updates ftp://distrib-coffee.ipsl.jussieu.fr/pub/linux/MandrivaLinux/official/2008.1/i586/media/main/updates with media_info/hdlist.cz
Fail2ban can be installed as follows:
Then we start fail2ban:
You will find all fail2ban configuration files in the /etc/fail2ban directory.
3 Configuring fail2ban
The default behaviour of fail2ban is configured in the file /etc/fail2ban/jail.conf. Take a look at it, it's not hard to understand. There's a [DEFAULT] section that applies to all other sections unless the default options are overriden in the other sections.
I explain some of the configuration options here:
- ignoreip: This is a space-separated list of IP addresses that cannot be blocked by fail2ban. For example, if the computer from which you're connecting to the server has a static IP address, you might want to list it here.
- bantime: Time in seconds that a host is blocked if it was caught by fail2ban (600 seconds = 10 minutes).
- maxretry: Max. number of failed login attempts before a host is blocked by fail2ban.
- filter: Refers to the appropriate filter file in /etc/fail2ban/filter.d.
- action: Refers to the appropriate action file in /etc/fail2ban/action.d.
- logpath: The log file that fail2ban checks for failed login attempts.
This is what my /etc/fail2ban/jail.conf file looks like:
# Fail2Ban configuration file # # Author: Cyril Jaquier # # $Revision: 617 $ # # The DEFAULT allows a global definition of the options. They can be override # in each jail afterwards. [DEFAULT] # "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not # ban a host which matches an address in this list. Several addresses can be # defined using space separator. ignoreip = 127.0.0.1 192.168.0.99 # "bantime" is the number of seconds that a host is banned. bantime = 600 # A host is banned if it has generated "maxretry" during the last "findtime" # seconds. findtime = 600 # "maxretry" is the number of failures before a host get banned. maxretry = 3 # "backend" specifies the backend used to get files modification. Available # options are "gamin", "polling" and "auto". This option can be overridden in # each jail too (use "gamin" for a jail and "polling" for another). # # gamin: requires Gamin (a file alteration monitor) to be installed. If Gamin # is not installed, Fail2ban will use polling. # polling: uses a polling algorithm which does not require external libraries. # auto: will choose Gamin if available and polling otherwise. backend = auto # This jail corresponds to the standard configuration in Fail2ban 0.6. # The mail-whois action send a notification e-mail with a whois request # in the body. [ssh-iptables] enabled = true filter = sshd action = iptables[name=SSH, port=ssh, protocol=tcp] sendmail-whois[name=SSH, dest=[email protected], sender=[email protected]] logpath = /var/log/auth.log maxretry = 5 [proftpd-iptables] enabled = true filter = proftpd action = iptables[name=ProFTPD, port=ftp, protocol=tcp] sendmail-whois[name=ProFTPD, dest=[email protected]] logpath = /var/log/proftpd/proftpd.log maxretry = 6 [sasl-iptables] enabled = true filter = sasl backend = polling action = iptables[name=sasl, port=smtp, protocol=tcp] sendmail-whois[name=sasl, dest=[email protected]] logpath = /var/log/mail/info.log [apache-tcpwrapper] enabled = true filter = apache-auth action = hostsdeny logpath = /var/log/httpd/*error_log maxretry = 6 [postfix-tcpwrapper] enabled = true filter = postfix action = hostsdeny sendmail[name=Postfix, dest=[email protected]] logpath = /var/log/mail/info.log bantime = 300 [courierpop3] enabled = true port = pop3 filter = courierlogin action = iptables[name=%(__name__)s, port=%(port)s] logpath = /var/log/mail/info.log maxretry = 5 [courierimap] enabled = true port = imap2 filter = courierlogin action = iptables[name=%(__name__)s, port=%(port)s] logpath = /var/log/mail/info.log maxretry = 5 [ssh-tcpwrapper] enabled = false filter = sshd action = hostsdeny sendmail-whois[name=SSH, dest=[email protected]] ignoreregex = for myuser from logpath = /var/log/auth.log [vsftpd-notification] enabled = false filter = vsftpd action = sendmail-whois[name=VSFTPD, dest=[email protected]] logpath = /var/log/vsftpd.log maxretry = 5 bantime = 1800 [vsftpd-iptables] enabled = false filter = vsftpd action = iptables[name=VSFTPD, port=ftp, protocol=tcp] sendmail-whois[name=VSFTPD, dest=[email protected]] logpath = /var/log/vsftpd.log maxretry = 5 bantime = 1800 [apache-badbots] enabled = false filter = apache-badbots action = iptables-multiport[name=BadBots, port="http,https"] sendmail-buffered[name=BadBots, lines=5, dest=[email protected]] logpath = /var/www/*/logs/access_log bantime = 172800 maxretry = 1 [apache-shorewall] enabled = false filter = apache-noscript action = shorewall sendmail[name=Postfix, dest=[email protected]] logpath = /var/log/apache2/error_log [ssh-ipfw] enabled = false filter = sshd action = ipfw[localhost=192.168.0.1] sendmail-whois[name="SSH,IPFW", dest=[email protected]] logpath = /var/log/auth.log ignoreip = 18.104.22.168 [named-refused-udp] enabled = false filter = named-refused action = iptables-multiport[name=Named, port="domain,953", protocol=udp] sendmail-whois[name=Named, dest=[email protected]] logpath = /var/log/named/security.log ignoreip = 22.214.171.124 [named-refused-tcp] enabled = false filter = named-refused action = iptables-multiport[name=Named, port="domain,953", protocol=tcp] sendmail-whois[name=Named, dest=[email protected]] logpath = /var/log/named/security.log ignoreip = 126.96.36.199
My client computer has the static IP address 192.168.0.99, and because I don't want to be locked out, I've added it to the ignoreip list.
I want to control login attempts to SSH, Apache, Proftpd, Courier-POP3, Courier-IMAP, and Sasl, so I've set enabled to true for these services and to false for all other services. Please note that some services such as SSH can be blocked either by iptables or by TCPWrappers (/etc/hosts.deny). Decide for yourself which method you prefer.
Make sure to replace the email address [email protected] with your own email address so that you get notified when someone gets blocked by fail2ban.
If you compare the file with the default /etc/fail2ban/jail.conf, you'll also notice that I've changed some log files because the log files in the default /etc/fail2ban/jail.conf are not correct for Mandriva 2008.1.
Whenever we modify the fail2ban configuration, we must restart fail2ban, so this is what we do now:
That's it already. Fail2ban logs to /var/log/fail2ban.log, so you can check that file to find out if/what hosts got blocked. If a host got blocked by fail2ban, it looks like this:
2008-08-12 17:49:09,466 fail2ban.actions: WARNING [apache-tcpwrapper] Ban 188.8.131.52
2008-08-12 18:08:33,213 fail2ban.actions: WARNING [sasl-iptables] Ban 184.108.40.206
2008-08-12 18:26:37,769 fail2ban.actions: WARNING [courierlogin] Ban 220.127.116.11
2008-08-12 18:39:06,765 fail2ban.actions: WARNING [courierimap] Ban 18.104.22.168
You can also check your firewall to see if any hosts are currently blocked. Simply run
For services that use TCPWrappers to block hosts, take a look at /etc/hosts.deny.