fail2ban
Step 1: Install Fail2Ban
Fail2Ban is an intrusion prevention software framework that protects your server from brute-force attacks. It monitors log files and bans IP addresses that show malicious signs.
Step 2: Configure Fail2Ban for Nginx
Fail2Ban comes with default configurations for various services, including Nginx. However, you need to enable them and configure them to work with your specific setup.
Create a Local Jail Configuration File:
To prevent your custom configuration from being overwritten by updates, create a local jail file:
Enable Nginx Protection in Fail2Ban:
Open the
/etc/fail2ban/jail.local
file in a text editor:Add or uncomment the following sections to protect your Nginx from various types of attacks:
These jails will monitor different aspects of Nginx logs and ban IPs based on the filters you set.
Step 3: Create Nginx Filters for Fail2Ban
Fail2Ban uses filter files to define what logs it should look for. The filters for Nginx may already exist, but if not, you can create them.
Create a New Filter for 404 Errors:
Create the filter file:
Add the following content to monitor 404 errors:
Create Other Filters (if necessary): You can define additional filters by checking Nginx logs and defining what patterns indicate malicious activity.
Step 4: Enable Fail2Ban with FastAPI
Since Fail2Ban works by monitoring logs, you can make sure that your FastAPI application logs critical events (such as failed login attempts, 404 errors, or unusual API requests).
Ensure your FastAPI app is logging to a file.
You can then configure Fail2Ban to monitor this log file in the same way as you did for Nginx.
Example jail for FastAPI:
You would also need to create a fastapi-auth.conf
filter file in /etc/fail2ban/filter.d/
that specifies what patterns to look for in your FastAPI logs.
Step 5: Restart and Test Fail2Ban
Restart the Fail2Ban service to apply your changes:
You can check the status of Fail2Ban with:
Step 6: Test the Configuration
To test that Fail2Ban is correctly banning IPs:
Trigger a few failed requests that match your filter (e.g., try accessing non-existing pages).
Check if Fail2Ban has banned the IP:
You should see the IPs that have been banned.
Step 7: Monitoring Fail2Ban
You can view the logs for Fail2Ban using:
This will show you all the recent bans and unbans performed by Fail2Ban.
Custom Fail2ban
Default Parameter
Override the base configurations: All default parameters and configurations are found in the file /etc/fail2ban/jail.conf
. Here is a list of important parameters to override and adapt according to the behavior you desire:
bantime: Defines the duration of an IP ban (default 10 minutes, recommended several hours or days).
findtime: Period during which anomalies are searched for in the logs.
ignoreip: List of IPs to ignore, including yours to avoid self-banning.
maxretry: Number of failed attempts allowed before banning. Also define the use of UFW to take control of the banning (banaction and banaction_allports).
Here is an example of a drastic configuration, banning any first intrusion attempt for 1 day. We also define the use of UFW, (note the local IP addresses that you may need to adjust according to your local network configuration):
Step 8: Add Jails to Your Configuration
To add these jails to the Fail2Ban configuration in the custom.conf
file, follow these steps:
Add jail configurations: Copy and paste the following configurations at the end of the file:
Configuring Custom Logpaths in Fail2Ban
In Fail2Ban, if you're creating a custom configuration file such as custom.conf
, and you want to set the logpath
for a specific jail to /var/log/nginx/access.log
, you can do it directly under the jail configuration.
For example, if you want to define the log path for monitoring Nginx access logs in your custom configuration (custom.conf
), you can structure it like this:
In this example:
Each
[nginx-*]
jail has alogpath
specified, which points to/var/log/nginx/access.log
for Nginx access logs.Ensure that
/var/log/nginx/access.log
exists and is the correct file where Nginx logs access attempts.
Custom Configuration File Placement
Important: Place custom.conf
in /etc/fail2ban/jail.d/
custom.conf
in /etc/fail2ban/jail.d/
Fail2Ban reads configuration files from this directory and combines them with the main configuration.
This custom configuration file should be placed in
/etc/fail2ban/jail.d/
ascustom.conf
. Fail2Ban reads configuration files from this directory and combines them with the main configuration.
Example steps:
Add the custom jail configurations there, save the file, and restart Fail2Ban:
Important Note:
Make sure that
/var/log/nginx/access.log
is being actively written by Nginx. You can check this by running:The filters like
nginx-4xx
,nginx-http-auth
, etc., should have matching patterns in/etc/fail2ban/filter.d/
to detect suspicious behavior.
This setup ensures Fail2Ban monitors the correct log file for blocking malicious access attempts.
UFW Ban IP
You ban him manually by adding his IP to the firewall. If you are using UFW, then you write something like this in your command line:
Last updated