Last updated on March 13, 2019
Let’s assume that you already installed fail2ban, you can check here how to do that: – https://ep.gnt.md/index.php/how-to-setup-fail2ban-on-centos/
We need to copy this to a file called jail.local for Fail2Ban to find it.
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Configure defaults in jail.local
Open up the new Fail2Ban configuration file:
You can see the default section below:
[DEFAULT] # "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not # ban a host which matches an address in this list. Several addresses can be # defined using space separator. ignoreip = 127.0.0.1 # Override /etc/fail2ban/jail.d/00-firewalld.conf: banaction = iptables-multiport # "bantime" is the number of seconds that a host is banned. bantime = 600 # A host is banned if it has generated "maxretry" during the last "findtime" # seconds. findtime = 600 # "maxretry" is the number of failures before a host get banned. maxretry = 5
Configure Fail2ban For ssh
Although you can add this parameters in the global jail.local file, it is a good practice to create seperate jail files for each of the services we want to protect with Fail2Ban.
So lets create a new jail for SSH with the vi editor.
In the above file, add the following lines of code:
[sshd] enabled = true port = ssh action = iptables-multiport logpath = /var/log/secure maxretry = 5 bantime = 600
After making any changes to the Fail2Ban config, always be sure to restart Fail2Ban.
systemctl restart fail2ban
You can see the rules that fail2ban puts in effect within the IP table:
iptables -L -n
Check Fail2Ban Status
Use fail2ban-client command to query the overall status of the Fail2Ban jails.
You can also query a specific jail status using the following command:
fail2ban-client status sshd
Configure Fail2ban For Apache
Edit this file:
sudo nano /etc/fail2ban/jail.local
Add the following content. Note: Substitute your own static IP address for the sample address (127.0.0.1) in this example:
# detect password authentication failures [apache] enabled = true filter = apache-auth action = iptables-multiport[name=auth, port="http,https"] logpath = /var/log/httpd/fail2ban_log bantime = 3600 maxretry = 3 ignoreip = 127.0.0.1 # detect spammer robots crawling email addresses [apache-badbots] enabled = true filter = apache-badbots action = iptables-multiport[name=badbots, port="http,https"] logpath = /var/log/httpd/fail2ban_log bantime = 3600 maxretry = 1 ignoreip = 127.0.0.1 # detect potential search for exploits [apache-noscript] enabled = true filter = apache-noscript action = iptables-multiport[name=noscript, port="http,https"] logpath = /var/log/httpd/fail2ban_log bantime = 3600 maxretry = 6 ignoreip = 127.0.0.1 # detect Apache overflow attempts [apache-overflows] enabled = true filter = apache-overflows action = iptables-multiport[name=overflows, port="http,https"] logpath = /var/log/httpd/fail2ban_log bantime = 3600 maxretry = 2 ignoreip = 127.0.0.1
Save and close the file, then restart Fail2ban for the changes to take effect:
sudo systemctl restart fail2ban
Now, configure the Fail2ban service to start on boot with the command:
sudo systemctl enable fail2ban
To verify the rules that were added to iptables by Fail2ban, use the following command:
sudo iptables -L