Sitemap
Redouane OTMANI

Redouane OTMANI

Cybersecurity analyst

Fail2ban

6 min readAug 6, 2022

--

Fail2ban is a framework to prevent against intrusions (written in Python by the way). It could be installed on Linux, to detect automatically any suspect behaviors, so that it blocks (or only) sends alerts.

For example, it could detect X authentication attempts on a service like (ssh) then it’ll block the IP address source of theses attempts, it could also detect abnormal queries (on Apache for ex), and so on!

In this tutorial, I’ll go through the installation and configuration of Fail2ban.

Plan

  1. Overall functioning
  2. Installation
  3. Configuration
  4. Tests
  5. Managing banned IP addresses

1. Overall functioning

Fail2ban works with “jails”. Globally a jail is one or more services/ports on which we’ll apply rules, in which we’ll place IP addresses that do not respect these rules. In fact, once a suspect IP address is detected, measures to treat it will be taken.

By default, we’ll block IP address using IPtables rules(Firewall on Linux) for 600 seconds (10 minutes). Example : Blocking IP address after 3 attempts with a wrong password.

2. Installation

Press enter or click to view image in full size
Network Topology

All of the phases of installation and configuration of Fail2ban are going to take place in Ubuntu. As for Kali, it we’ll help us during tests.

So lets start, and as always

# sudo apt update && sudo apt upgrade

Then,

# sudo apt-get install fail2ban

Lets access then comment the content of the directory “/etc/fail2ban/”

Press enter or click to view image in full size
“/etc/fail2ban” content

As we can see, we have the two configuration files : “fail2ban.conf” and “jail.conf”. Also, the two directories “action.d” and “filter.d”

  • fail2ban.conf. Contains configuration of some operational settings like the way the deamon logs info, and the socket and pid file it will use.
  • jail.conf. Contains the parameters (like bantime)
  • action.d : Contains actions to take in case o intrusion attempts.
  • filter.d : Contains Regexp to parse logs.

We’ll be more interested in “jail.conf” file, since it allows us to configure behaviors of jails. Note that it’s recommended to create a copy of this file, otherwise, we’ll lose its content after updates.

#sudo cp jail.conf jail.local

Press enter or click to view image in full size

3. Configuration

Lets edit the new file “jail.local” (copy of “jail.conf”)

# sudo nano -l /etc/fail2ban/jail.local

In this file we need to uncomment some options like : ignoreip, ignorecommand, bantime, findtime, maxretry, desemail, sender, mta, protocol

Press enter or click to view image in full size
Press enter or click to view image in full size
  • ignoreip useful to allow some specific IP addresses, as we’ve put above, 127.0.0.1/8 is our localhost machine, it make sense since that we don’t want to block our server. We could by the way, by the way, add more IP addresses, separated by spaces.
  • destemail email to receive alerts.
  • sender obviously the sender of the email.

Notice that the file “jail.local” contains so many commands & options, each one is preceded by a comment to explain how it works.

The directory “/etc/fail2ban/filter.d” contains the rules to be used by Fail2ban to parse log files

Press enter or click to view image in full size

Filter files must have “.conf” extension, and need to be identified by its name, so that we could activate/disactivate jails in the “jail.local” file.

Get Redouane OTMANI’s stories in your inbox

Join Medium for free to get updates from this writer.

Lets take ssh filter as example, we need to go back to the file “jail.local” to jails section

Press enter or click to view image in full size
  • [sshd] : is the filter name.
  • enabled : to activate the jail.
  • port : for service name.
  • logpath : refers to the authentication file, to identify all ssh connection, as result we can read than extract connection failures.
  • maxretry : Number of failures before a host get banned.

We could add more options, otherwise, default options are going to be taken into account, like findtime, bantime

  • bantime : Number of seconds a host is banned
  • findtime : Number of maxretry during the last “findtime”
Press enter or click to view image in full size

Finally, we need to run fail2ban server, then restart the fail2ban-client then check its status

# sudo service fail2ban start

# sudo fail2ban-client reload

# sudo fail2ban-client status

As we can see, the sshd jail is activated.

— — — —

The directory “action.d” contains actions to be taken in case of abnormal behavior, theses action files specify which commands are executed to ban or unban an IP address. If we desire local changes, we could create an [actionname].local file in the “action.d” directory. Each action file contains two sections : Definition & Init. For more details check this link

Press enter or click to view image in full size

4. Tests

From another machine, lets try to connect to SSH service with more than 3 attempts to activate fail2ban (and see if it works)

It is possible that ssh service is not installed

# sudo apt install openssh-server

# sudo systemctl status ssh

Press enter or click to view image in full size

Our ssh server is running!

Lets jump to our Kali machine

Press enter or click to view image in full size

Then go back to our Ubuntu to see logs on sshd jail

# sudo fail2ban-client status sshd

As we can see, it’s our Kali’s IP address that is being blocked. We can no longer (at least for 10 minutes) try to connect to ssh.

5. Managing banned IP addresses

As we’ve talked above, we can see the active jails with status, and know if a jail has banned IP addresses, using status followed by the jail name.

Now, if one of our IP addresses is blocked due to a repeated and manipulation or tests, we can remove it from the Fail2ban banned IP address with this command :

# sudo fail2ban-client set sshd unbanip 192.168.205.128

We could also ban manually an IP address :

# sudo fail2ban-client set sshd banip 192.168.205.128

To conclude, Fail2ban is great and simple tool to install then use. It provides large number of predefined rules, easy to implement.

If you enjoy my stories, you can follow me on Twitter

--

--