How to install and use Fail2Ban for Security on Ubuntu

Knowledgebase Articles and news

Views
31
Useful
0
By
Khawar Shahzad

How to install and use Fail2Ban for Security on Ubuntu

Fail2ban is an open-source intrusion prevention software that can protect your server from various types of attacks by monitoring log files and blocking IPs that exhibit malicious behavior. It works by analyzing log files, detecting failed login attempts, and then taking action, such as banning the IP address from further login attempts for a specific period. Fail2ban is an essential tool for server administrators who want to secure their servers against brute-force attacks, port scans, and other types of malicious activity.

This tutorial will cover the installation and configuration of Fail2ban on Ubuntu, as well as examples of how to use Fail2ban to protect various services, including SSH, Apache, and WordPress. We will also cover troubleshooting and common issues with Fail2ban. By the end of this tutorial, you should have a good understanding of how to use Fail2ban to secure your Ubuntu server.

Now you know How Fail2ban works to protect your server. Here are some of the practical use cases of Fail2ban on Ubuntu.

Use Cases of Fail2Ban
Fail2ban has a wide range of use cases, but its primary purpose is to protect your server from various types of attacks. Here are some common use cases for Fail2ban:

  • Brute-force attacks: Fail2ban is particularly effective at protecting against brute-force attacks, where an attacker repeatedly tries to guess a password or access a resource. Fail2ban can detect failed login attempts and block the IP address from further attempts, making it much harder for attackers to succeed.
  • Port scans: Attackers often use port scanning tools to identify open ports on a server that they can exploit. Fail2ban can detect repeated port scans and block the IP address from further attempts, reducing the attack surface of your server.
  • Distributed Denial of Service (DDoS) attacks: Fail2ban can also help protect your server from DDoS attacks by blocking IP addresses that are flooding your server with traffic.
  • Malicious bots: Some bots are designed to scrape your website or perform other malicious actions. Fail2ban can detect and block these bots, preventing them from causing harm to your server or website.
Unauthorized access attempts: Fail2ban can also detect and block unauthorized access attempts to various services running on your server, such as SSH, FTP, and web servers.
Overall, Fail2ban is an essential tool for any server administrator who wants to protect their server from a wide range of attacks. By monitoring log files and blocking malicious IPs, Fail2ban can help keep your server secure and reduce the risk of a successful attack.

How to install Fail2Ban on Ubuntu
Installing Fail2ban on Ubuntu is a very easy task. Essentially, it only requires one command to install Fail2ban. However, If your system is not up-to-date, execute the following commands first.

sudo apt-get update
sudo apt-get upgrade

It might take a few minutes to complete the process. Once the process is complete, execute the following command to install Fail2ban on your Ubuntu server.

sudo apt-get install fail2ban

This is all you have to do to install Fail2ban. It might take a few minutes to download, install and configure Fail2ban on your server with default settings. Once the process is complete, we can customize the Fail2ban configuration to match our requirements.

To work with fail2ban, You have to understand Fail2ban configuration files. Let’s learn more about Fail2ban configuration files.

Configuring Fail2Ban on Ubuntu
The Fail2ban configuration file is located at /etc/fail2ban/jail.conf. This file contains global settings for Fail2ban, such as the log file location, email address for notifications, and more.

Note that you should not edit this file directly. Instead, create a new file called /etc/fail2ban/jail.local and override the settings in the global configuration file. This will ensure that your changes are not overwritten when you update Fail2ban on the system.

Fail2ban uses “jails” to protect various services running on your server. Each jail corresponds to a specific service, such as SSH, Apache, or WordPress. Jails contain specific settings for that service, such as the log file location, maxretry (the number of failed attempts allowed before blocking), and bantime (the length of time to block an IP address).

To configure a jail, create a new file in /etc/fail2ban/jail.d/ with a .conf extension. For example, to configure the SSH jail, create a file called /etc/fail2ban/jail.d/sshd.conf with the following content:

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 5
bantime = 3600

Here we have created a Fail2ban jail containing all the information about the SSH service running on the server. Here is the explaination of reach directive we have defined in this jail.

  • [sshd]: This is the name of the jail. It’s important to note that the name should match the name of the service you’re trying to protect.
  • enabled = true: This directive enables the jail. If it’s set to false, the jail won’t be active.
  • port = ssh: This directive specifies the port number for the SSH service. In this case, it’s set to the default SSH port, which is 22.
  • filter = sshd: This directive specifies the filter to use. Filters are used to parse log files and identify failed login attempts. In this case, it’s set to the built-in sshd filter, which is designed to work with the OpenSSH server.
  • logpath = /var/log/auth.log: This directive specifies the log file to monitor. In this case, it’s set to the auth.log file, which is the default log file for SSH on Ubuntu.
  • maxretry = 5: This directive specifies the maximum number of failed login attempts allowed before an IP address is blocked. In this case, it’s set to 5.
  • bantime = 3600: This directive specifies the length of time to block an IP address, in seconds. In this case, it’s set to 1 hour (3600 seconds).
Overall, this jail configuration will monitor the SSH log file for failed login attempts and block any IP address that exceeds the maximum number of allowed attempts. The blocked IP address will be prevented from accessing the SSH service for 1 hour.

After creating the jail file containing all the information about the service you want to protect, Execute the following command to apply the changes.

sudo service fail2ban restart

Now, Fail2Ban will start monitoring auth.log and ban IP addresses based on the rules we have defined in a jail file. Let’s see some more examples.

PROTECT APACHE USING FAIL2BAN
Fail2ban can also be used to protect the Apache web server by monitoring the Apache access logs for malicious requests. Here’s an example of how to use Fail2ban to protect Apache.

Create a new jail configuration file in /etc/fail2ban/jail.d/apache.local with the following content:

[apache]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache2/access.log
maxretry = 10
bantime = 86400

Save the file and execute the following command to restart the Fail2Ban service. Restarting the service will apply all the changes happened in the configuraiton.

sudo service fail2ban restart

So, This is how you can protect Apache web server using Fail2Ban. If you are using Nginx, You just have to replace the filter and log file path in the above given configuration to protect Nginx web server.

PROTECT WORDPRESS USING FAIL2BAN
Fail2ban can be a useful tool to protect your WordPress site from various attacks, including brute-force login attempts, attempts to exploit known vulnerabilities in WordPress plugins or themes, and other malicious activity. Here’s an example of how to use Fail2ban to protect a WordPress site running on an Ubuntu server:

Create a new filter file for WordPress in /etc/fail2ban/filter.d/wordpress-auth.conf with the following content:

# Fail2Ban configuration file for WordPress authentication failures
#
# The regex below matches failed login attempts to WordPress sites.
# Adjust it as necessary to match your specific WordPress installation.
#
# Example log entry:
# 192.168.1.1 - - [01/Jan/2022:12:00:00 -0500] "POST /wp-login.php HTTP/1.1" 200 4000 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
#
[Definition]
failregex = ^ -.*"(GET|POST).*wp-login\.php.*
ignoreregex =

This filter uses a regular expression to match failed login attempts to WordPress sites by looking for log entries that contain the wp-login.php file. Adjust the regular expression as necessary to match your specific WordPress installation.

Now create a new Jail configuration file in /etc/fail2ban/jail.d/wordpress.local with the following content.

[wordpress]
enabled = true
port = http,https
filter = wordpress-auth
logpath = /var/log/apache2/access.log
maxretry = 10
bantime = 86400

This jail configuration specifies that Fail2ban should use the wordpress-auth filter to monitor the Apache access logs (/var/log/apache2/access.log) for failed login attempts to WordPress sites. It also specifies a maximum of 10 allowed attempts and a ban time of 24 hours (86400 seconds).

Finally, execute the following command to restart Fail2ban and apply changes.

sudo service fail2ban restart

Now, Fail2Ban will start reading the access log file of your WordPress file and Ban IP addresses that has more than 10 failed login attempts for 24 hours. If you are using any other web server for your WordPress site, You just have to modify the filter according to your log file.

Conclusion
In conclusion, Fail2ban is a powerful tool that can help enhance the security of your Ubuntu server by monitoring logs for suspicious activity and blocking IP addresses that show signs of malicious behavior. With the right configuration and setup, Fail2ban can protect your server from a range of attacks and provide valuable insights into the security of your system.

In this tutorial, we covered the basics of installing and configuring Fail2ban on Ubuntu, as well as some practical examples of how to use it to secure common services like SSH and WordPress. By following these guidelines, you can create a solid security strategy that helps to safeguard your server against malicious activity and keeps your data and services protected.

Remember that while Fail2ban can be an effective tool for improving security, it should not be your only line of defense. Make sure to use other security measures, such as firewalls and regular system updates, to keep your server secure and up to date with the latest security patches.