Preventing Brute Force Attacks With Fail2ban On OpenSUSE 10.3
Version 1.0
Author: Falko Timme
In this article I will show how to install and configure fail2ban on an OpenSUSE 10.3 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: https://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 OpenSUSE 10.3. 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
Fail2ban is available from the Packman repository, so we must enable that first:
yast2
In YaST, go to Software > Community Repositories:
Then activate the Packman Repository and hit [Finish]:
Leave YaST afterwards:
Afterwards fail2ban can be installed as follows:
yast2 -i fail2ban
Then we must create the system startup links for fail2ban and start it:
chkconfig --add fail2ban
/etc/init.d/fail2ban start
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:
vi /etc/fail2ban/jail.conf
# Fail2Ban configuration file # # Author: Cyril Jaquier # # $Revision: 611 $ # # 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, [email protected], [email protected]] logpath = /var/log/messages maxretry = 5 [proftpd-iptables] enabled = true filter = proftpd action = iptables[name=ProFTPD, port=ftp, protocol=tcp] sendmail-whois[name=ProFTPD, [email protected]] logpath = /var/log/messages maxretry = 6 # This jail forces the backend to "polling". [sasl-iptables] enabled = true filter = sasl backend = polling action = iptables[name=sasl, port=smtp, protocol=tcp] sendmail-whois[name=sasl, [email protected]] logpath = /var/log/mail # Here we use TCP-Wrappers instead of Netfilter/Iptables. "ignoreregex" is # used to avoid banning the user "myuser". [ssh-tcpwrapper] enabled = false filter = sshd action = hostsdeny sendmail-whois[name=SSH, [email protected]] ignoreregex = for myuser from logpath = /var/log/messages # This jail demonstrates the use of wildcards in "logpath". # Moreover, it is possible to give other files on a new line. [apache-tcpwrapper] enabled = true filter = apache-auth action = hostsdeny logpath = /var/log/apache2/error_log maxretry = 6 # The hosts.deny path can be defined with the "file" argument if it is # not in /etc. [postfix-tcpwrapper] enabled = true filter = postfix action = hostsdeny sendmail[name=Postfix, [email protected]] logpath = /var/log/mail bantime = 300 # Do not ban anybody. Just report information about the remote host. # A notification is sent at most every 600 seconds (bantime). [vsftpd-notification] enabled = false filter = vsftpd action = sendmail-whois[name=VSFTPD, [email protected]] logpath = /var/log/messages maxretry = 5 bantime = 1800 # Same as above but with banning the IP address. [vsftpd-iptables] enabled = false filter = vsftpd action = iptables[name=VSFTPD, port=ftp, protocol=tcp] sendmail-whois[name=VSFTPD, [email protected]] logpath = /var/log/messages maxretry = 5 bantime = 1800 # Ban hosts which agent identifies spammer robots crawling the web # for email addresses. The mail outputs are buffered. [apache-badbots] enabled = true filter = apache-badbots action = iptables-multiport[name=BadBots, port="http,https"] sendmail-buffered[name=BadBots, lines=5, [email protected]] logpath = /var/log/apache2/access_log bantime = 172800 maxretry = 1 [courierpop3] enabled = true port = pop3 filter = courierlogin action = iptables[name=%(__name__)s, port=%(port)s] logpath = /var/log/mail maxretry = 5 [courierimap] enabled = true port = imap2 filter = courierlogin action = iptables[name=%(__name__)s, port=%(port)s] logpath = /var/log/mail maxretry = 5 |
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 OpenSUSE 10.3.
Whenever we modify the fail2ban configuration, we must restart fail2ban, so this is what we do now:
/etc/init.d/fail2ban restart
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:
2007-10-07 17:49:09,466 fail2ban.actions: WARNING [apache-tcpwrapper] Ban 1.2.3.4
2007-10-07 18:08:33,213 fail2ban.actions: WARNING [sasl-iptables] Ban 1.2.3.4
2007-10-07 18:26:37,769 fail2ban.actions: WARNING [courierlogin] Ban 1.2.3.4
2007-10-07 18:39:06,765 fail2ban.actions: WARNING [courierimap] Ban 1.2.3.4
You can also check your firewall to see if any hosts are currently blocked. Simply run
iptables -L
For services that use TCPWrappers to block hosts, take a look at /etc/hosts.deny.
Links
- Fail2ban: http://www.fail2ban.org
- OpenSUSE: http://www.opensuse.org