Using Fail2Ban for SSH Brute-force Protection

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

Fail2Ban is an intrusion prevention framework written in Python that protects Linux systems and servers from brute-force attacks. You can setup Fail2Ban to provide brute-force protection for SSH on your server. This ensures that your server is secure from brute-force attacks. It also allows you to monitor the strength of the attacks in regards to the number of authentication attempts that are being made.

Brute-force attacks can be extremely powerful and may result in thousands of failed authentication attempts per day. It is therefore vital to understand how to protect your server from these attacks and how to block IP addresses. Fail2Ban allows you to automate the process of blocking brute-force attacks by limiting the number of failed authentication attempts a user can make before being blocked. This is extremely useful for servers that have user accounts that utilize passwords for remote authentication as opposed to SSH key-pair authentication.

Before You Begin

Note
This guide uses Ubuntu, but the commands are similar for other systems.
  1. Complete the Getting Started guide.

  2. Follow the Setting Up and Securing a Compute Instance guide to create a standard user account, and harden SSH access, but do not create a basic firewall.

  3. Log into your Linode via SSH and update and upgrade.

    sudo apt update && sudo apt upgrade
    
Note
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, see our Users and Groups guide.

Installing And Configuring Fail2Ban

Fail2Ban is free to use and can be installed through most of the popular package managers.

  1. Install Fail2Ban by running the following command:

    sudo apt-get install fail2ban
    
  2. To ensure that Fail2ban runs on system startup, use the following command:

    sudo systemctl enable fail2ban.service
    

After the installation is complete, you can begin configuring Fail2Ban to set up a jail for your SSH server. The Fail2Ban configuration files are located in the /etc/fail2ban directory, as shown in the output below.

/etc/fail2ban$ ls -alps
total 68
 4 drwxr-xr-x  6 root root  4096 Oct 12 18:21 ./
 4 drwxr-xr-x 94 root root  4096 Oct 12 18:21 ../
 4 drwxr-xr-x  2 root root  4096 Oct 12 18:21 action.d/
 4 -rw-r--r--  1 root root  2334 Jan 18  2018 fail2ban.conf
 4 drwxr-xr-x  2 root root  4096 Apr  4  2018 fail2ban.d/
 4 drwxr-xr-x  3 root root  4096 Oct 12 18:21 filter.d/
24 -rw-r--r--  1 root root 22897 Jan 18  2018 jail.conf
 4 drwxr-xr-x  2 root root  4096 Oct 12 18:21 jail.d/
 4 -rw-r--r--  1 root root   645 Jan 18  2018 paths-arch.conf
 4 -rw-r--r--  1 root root  2827 Jan 18  2018 paths-common.conf
 4 -rw-r--r--  1 root root   573 Jan 18  2018 paths-debian.conf
 4 -rw-r--r--  1 root root   738 Jan 18  2018 paths-opensuse.conf

Fail2Ban uses the default configuration in the jail.conf file. However, it is not recommended to use the default configuration files as they can be overwritten by newer updates to the Fail2Ban package. The preferred approach to creating configurations for a particular service is by creating a new configuration file in the /etc/fail2ban directory with the .local extension.

Note
A Fail2ban jail is a configuration file that contains filters or arguments that protect your system or a particular service

Creating SSH Jails With Fail2Ban

  1. Begin by creating a new file within the same directory called jail.local. You can then add the necessary security configurations for the sshd jail.

    sudo nano /etc/fail2ban/jail.local
    
  2. You can explore the options that Fail2Ban provides to customize the security and blocking of the SSH service.

    Fail2Ban Configuration Options:

    ConfigurationsFunction
    enabledJail status (true/false) - This enables or disables the jail
    portPort specification
    filterService specific filter (Log filter)
    logpathWhat log to use
    maxretryNumber of attempts to make before a ban
    findtimeAmount of time between failed login attempts
    bantimeNumber of seconds an IP is banned for
    ignoreipIP to be allowed
  3. With the information in table above you can create the jail.local configuration for OpenSSH server (sshd). Once you have entered the configuration options, the values used in this guide example are listed in the sample file below.

    Note
    You can customize the Fail2Ban configuration options and values as per your security requirements.
    File: /etc/fail2ban/jail.local
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    [sshd]
    enabled = true
    port = ssh
    filter = sshd
    logpath = /var/log/auth.log
    maxretry = 3
    findtime = 300
    bantime = 3600
    ignoreip = 127.0.0.1
    Note
    You can disable a Fail2Ban jail by setting the enabled configuration to false
  4. After you have specified the configuration options and their respective values, save the file and restart the Fail2Ban service with the following command:

    sudo systemctl restart fail2ban.service
    
  5. After restarting the OpenSSH server service, Fail2Ban uses this new configuration and the jail for the sshd service is activated and runs.

  6. You can now test this functionality by re-enabling PasswordAuthentication in the OpenSSH Configuration file found in /etc/ssh/sshd_config. Do this by changing the value from no to yes using the text editor of your choice. Make sure these lines are uncommented.

    File: /etc/ssh/sshd_config
    1
    2
    3
    
    #To disable tunneled clear text passwords, change to no here!
    PasswordAuthentication yes
    PermitEmptyPasswords no

    This allows users to use passwords for authentication in addition to SSH key-pairs. Fail2Ban automatically detects brute-force attempts on SSH and blocks the users automatically. This greatly improves the security of both password based authentication and the server and is useful for user accounts that do not have administrator privileges.

Testing Fail2Ban

  1. To test this, create a new user account, let’s call it dev.

  2. Attempt to log into the dev account with an incorrect password three times.

  3. After three failed attempts you are blocked from authentication for an hour.

    ssh dev@192.168.1.107
    dev@192.168.1.107's password:
    Permission denied, please try again.
    dev@192.168.1.107's password:
    Permission denied, please try again.
    dev@192.168.1.107's password:
    dev@192.168.1.107: Permission denied (publickey,password).
    ssh dev@192.168.1.107
    dev@192.168.1.107's password:
    Permission denied, please try again.
    dev@192.168.1.107's password:
    Connection closed by 192.168.1.107 port 22
    sh dev@192.168.1.107
    ssh: connect to host 192.168.1.107 port 22: Connection refused

    As you can see in the output above, after three consecutive failed attempts, Fail2Ban actively blocks the SSH connection. After three consecutive failed attempts the connection times out and the user is blocked for the specified time. If you try connecting again within the blocked period, you get a “Connection refused” error and are not able to establish an SSH connection to the server.

    This demonstrates the power and robust nature of Fail2Ban and how it can be used to create elegant and effective firewalls for services like SSH. You can customize your service jails to meet your security requirements and easily implement new configuration options.

  4. After implementing and testing Fail2Ban you can now take a look at how to monitor and analyze the various failed authentication attempts and blocked IP’s with the Fail2Ban-client.

Monitoring With Fail2Ban-Client

One of Fail2Ban’s greatest advantages is that it allows you to actively monitor all the failed authentication attempts and the various IP addresses that have been blocked. This information helps you understand the scale of attacks you are facing and the geolocation of the attacks by analyzing the origins of the IP addresses.

  1. You can use the Fail2Ban-client tool to check the status of Fail2Ban and the active jails. This can be done by running the following command:

    sudo fail2ban-client status
    
    Status
    |- Number of jail:	1
    `- Jail list:	sshd

    As shown in the output above, the active jail list is displayed with the names of the respective jails. In the case above you can see that the sshd jail is active.

  2. To view the status and information regarding a particular jail like sshd, you can use the following command:

    sudo fail2ban-client status sshd
    
    Status for the jail: sshd
    |- Filter
    |  |- Currently failed:	1
    |  |- Total failed:	4
    |  `- File list:	/var/log/auth.log
    `- Actions
       |- Currently banned:	1
       |- Total banned:	1
       `- Banned IP list:	192.168.1.101

    The output above shows you the status and information regarding the sshd jail. You can see that you have four total failed authentication attempts and one banned IP address. This is helpful as it can alert you to potential targeted attacks.

You have successfully been able to set up, implement, test, and analyze Fail2Ban for brute-force protection. You have completed setting up your remote authentication security.

Next Steps

For more detailed information on Fail2Ban, including setting up email alerts and writing regular expressions to filter and parse log files, see the Using Fail2ban to Secure Your Server - A Tutorial guide.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.