Intro
Fail2Ban is an open-source intrusion prevention framework that helps protect Linux servers from various types of attacks. It works by monitoring log files for patterns that may indicate malicious activity, such as repeated failed login attempts, and then takes action to block the source of the suspicious activity.
Run Fail2Ban in Docker
Create a folder fail2ban
and navigate into it:
Create a .env
file with the following content:
Create a docker-compose.yml
with the following content:
fail2ban container need access to NET_ADMIN
and NET_RAW
to be able to manipulate the firewall that will block all undesired IPs.
To initiate fail2ban, execute the following command:
For halting fail2ban, execute:
To monitor the logs, employ:
Add a Jail
In this example, I will show how to protect your SSH service.
Predefined filters
fail2ban docker image is shipped with already predefined filters, that can be used to filter logs and detect malicious behavior of common softwares like
sshd
. They are located in the/etc/fail2ban/filter.d
folder.You can check it using the command:
Firstly, enable the sshd
jail
by creating a file in data/jail.d/sshd.conf
with the following content:
Since fail2ban watch logs to detect intrusive behavior, you will need to give access to the sshd
log file /var/log/auth.log
(may vary from your distribution).
In the docker-compose.yml
file, add the line:
Now, you can restart your fail2ban service with:
Monitoring
You can check the status of fail2ban using the command:
You can also specify a jail
:
Ressources
- Docker image from crazy-max
- Photo by Marco Chilese on Unsplash