fail2ban:protect_an_apache_server_with_fail2ban
Differences
This shows you the differences between two versions of the page.
fail2ban:protect_an_apache_server_with_fail2ban [2016/10/19 15:22] – created peter | fail2ban:protect_an_apache_server_with_fail2ban [2019/11/29 11:58] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Fail2Ban - Protect an Apache Server with Fail2Ban ====== | ||
- | |||
- | ===== Introduction ===== | ||
- | |||
- | When operating a web server, it is important to implement security measures to protect your site and users. | ||
- | |||
- | Setting up **fail2ban** can help alleviate this problem. | ||
- | |||
- | ===== Install Fail2Ban ===== | ||
- | |||
- | <code bash> | ||
- | sudo apt-get update | ||
- | sudo apt-get install fail2ban | ||
- | </ | ||
- | |||
- | This will install the software. | ||
- | |||
- | |||
- | ===== Adjusting the General Settings within Fail2Ban ===== | ||
- | |||
- | Adjust the configuration file that fail2ban uses to determine what application logs to monitor and what actions to take when offending entries are found. | ||
- | |||
- | To make modifications, | ||
- | |||
- | <code bash> | ||
- | sudo cp / | ||
- | </ | ||
- | |||
- | Open the newly copied file so that we can set up our Apache log monitoring: | ||
- | |||
- | <code bash> | ||
- | sudo vi / | ||
- | </ | ||
- | |||
- | |||
- | ===== Changing Defaults ===== | ||
- | |||
- | Start by evaluating the defaults set within the file to see if they suit your needs. | ||
- | |||
- | One of the first items to look at is the list of clients that are not subject to the fail2ban policies. | ||
- | |||
- | <file bash / | ||
- | [DEFAULT] | ||
- | |||
- | . . . | ||
- | ignoreip = 127.0.0.1/8 your_home_IP | ||
- | </ | ||
- | |||
- | Another item that you may want to adjust is the **bantime**, | ||
- | |||
- | <file bash / | ||
- | [DEFAULT] | ||
- | |||
- | . . . | ||
- | bantime = 3600 | ||
- | </ | ||
- | |||
- | The next two items determine the scope of log lines used to determine an offending client. | ||
- | |||
- | <file bash / | ||
- | [DEFAULT] | ||
- | |||
- | . . . | ||
- | findtime = 3600 # These lines combine to ban clients that fail | ||
- | maxretry = 6 # to authenticate 6 times within a half hour. | ||
- | </ | ||
- | |||
- | |||
- | ===== Setting Up Mail Notifications (Optional) ===== | ||
- | |||
- | You can enable email notifications if you wish to receive mail whenever a ban takes place. | ||
- | |||
- | Once you have your MTA set up, you will have to adjust some additional settings within the **[DEFAULT]** section of the **/ | ||
- | |||
- | <file bash / | ||
- | [DEFAULT] | ||
- | |||
- | . . . | ||
- | mta = mail | ||
- | </ | ||
- | |||
- | You need to select the email address that will be sent notifications. | ||
- | |||
- | <file bash / | ||
- | [DEFAULT] | ||
- | |||
- | . . . | ||
- | destemail = youraccount@email.com | ||
- | sendername = Fail2BanAlerts | ||
- | </ | ||
- | |||
- | In fail2ban parlance, an " | ||
- | |||
- | You can use the **action_mw** action to ban the client and send an email notification to your configured account with a " | ||
- | |||
- | <file bash / | ||
- | [DEFAULT] | ||
- | |||
- | . . . | ||
- | action = %(action_mwl)s | ||
- | </ | ||
- | |||
- | |||
- | ===== Configuring Fail2Ban to Monitor Apache Logs ===== | ||
- | |||
- | Now that you have some of the general fail2ban settings in place, we can concentrate on enabling the Apache-specific jails that will monitor our web server logs for specific behavior patterns. | ||
- | |||
- | Each jail within the configuration file is marked by a header containing the jail name in square brackets (every section but the **[DEFAULT]** section indicates a specific jail's configuration). | ||
- | |||
- | To enable log monitoring for Apache login attempts, we will enable the **[apache]** jail. Edit the enabled directive within this section so that it reads " | ||
- | |||
- | <file bash / | ||
- | [apache] | ||
- | |||
- | enabled | ||
- | port = http,https | ||
- | filter | ||
- | logpath | ||
- | maxretry = 6 | ||
- | . . . | ||
- | </ | ||
- | |||
- | If your Apache server is writing to the default log location (**/ | ||
- | |||
- | <file bash / | ||
- | [apache] | ||
- | |||
- | enabled | ||
- | port = http,https | ||
- | filter | ||
- | logpath | ||
- | maxretry = 3 | ||
- | findtime = 600 | ||
- | . . . | ||
- | </ | ||
- | |||
- | The above jail will take care of banning basic authentication failures. | ||
- | |||
- | The **[apache-noscript]** jail is used to ban clients that are searching for scripts on the website to execute and exploit. | ||
- | |||
- | <file bash / | ||
- | [apache-noscript] | ||
- | |||
- | enabled | ||
- | . . . | ||
- | </ | ||
- | |||
- | The **[apache-overflows]** jail is used to block clients who are attempting to request unusually long and suspicious URLs. These are often signs of attempts to exploit Apache by trying to trigger a buffer overflow. | ||
- | |||
- | <file bash / | ||
- | [apache-overflows] | ||
- | |||
- | enabled | ||
- | . . . | ||
- | </ | ||
- | |||
- | Some additional checks can be made by copying and pasting the **[apache-overflows]** entry and modifying it slightly. | ||
- | |||
- | <file bash / | ||
- | [apache-overflows] | ||
- | |||
- | enabled | ||
- | port = http,https | ||
- | filter | ||
- | logpath | ||
- | maxretry = 2 | ||
- | |||
- | [apache-badbots] | ||
- | |||
- | enabled | ||
- | port = http,https | ||
- | filter | ||
- | logpath | ||
- | maxretry = 2 | ||
- | </ | ||
- | |||
- | If you do not use Apache to provide access to web content within users' home directories, | ||
- | |||
- | <file bash / | ||
- | [apache-overflows] | ||
- | |||
- | enabled | ||
- | port = http,https | ||
- | filter | ||
- | logpath | ||
- | maxretry = 2 | ||
- | |||
- | [apache-badbots] | ||
- | |||
- | enabled | ||
- | port = http,https | ||
- | filter | ||
- | logpath | ||
- | maxretry = 2 | ||
- | |||
- | [apache-nohome] | ||
- | |||
- | enabled | ||
- | port = http,https | ||
- | filter | ||
- | logpath | ||
- | maxretry = 2 | ||
- | </ | ||
- | Lastly, if you are using Apache with PHP, you may want to enable the **[php-url-fopen]** jail, which blocks attempts to use certain PHP behavior for malicious purposes. | ||
- | |||
- | <file bash / | ||
- | [php-url-fopen] | ||
- | |||
- | enabled = true | ||
- | port = http,https | ||
- | filter | ||
- | logpath = / | ||
- | </ | ||
- | |||
- | When you are finished making the modifications you need, save and close the file. | ||
- | |||
- | |||
- | ===== Implementing your Apache Jails ===== | ||
- | |||
- | To implement your configuration changes, you'll need to restart the fail2ban service. You can do that by typing: | ||
- | |||
- | <code bash> | ||
- | sudo service fail2ban restart | ||
- | </ | ||
- | |||
- | The service should restart, implementing the different banning policies you've configured. | ||
- | |||
- | |||
- | ===== Getting Info About Enabled Jails ===== | ||
- | |||
- | You can see all of your enabled jails by using the **fail2ban-client** command: | ||
- | |||
- | <code bash> | ||
- | sudo fail2ban-client status | ||
- | </ | ||
- | |||
- | You should see a list of all of the jails you enabled: | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | Status | ||
- | |- Number of jail: 7 | ||
- | `- Jail list: | ||
- | </ | ||
- | |||
- | You can see that fail2ban has modified your firewall rules to create a framework for banning clients. | ||
- | |||
- | <code bash> | ||
- | sudo iptables -S | ||
- | </ | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | -P INPUT ACCEPT | ||
- | -P FORWARD ACCEPT | ||
- | -P OUTPUT ACCEPT | ||
- | -N fail2ban-apache | ||
- | -N fail2ban-apache-badbots | ||
- | -N fail2ban-apache-nohome | ||
- | -N fail2ban-apache-noscript | ||
- | -N fail2ban-apache-overflows | ||
- | -N fail2ban-php-url-fopen | ||
- | -N fail2ban-ssh | ||
- | -A INPUT -p tcp -m multiport --dports 80,443 -j fail2ban-apache-nohome | ||
- | -A INPUT -p tcp -m multiport --dports 80,443 -j fail2ban-apache-badbots | ||
- | -A INPUT -p tcp -m multiport --dports 80,443 -j fail2ban-php-url-fopen | ||
- | -A INPUT -p tcp -m multiport --dports 80,443 -j fail2ban-apache-overflows | ||
- | -A INPUT -p tcp -m multiport --dports 80,443 -j fail2ban-apache-noscript | ||
- | -A INPUT -p tcp -m multiport --dports 80,443 -j fail2ban-apache | ||
- | -A INPUT -p tcp -m multiport --dports 22 -j fail2ban-ssh | ||
- | -A fail2ban-apache -j RETURN | ||
- | -A fail2ban-apache-badbots -j RETURN | ||
- | -A fail2ban-apache-nohome -j RETURN | ||
- | -A fail2ban-apache-noscript -j RETURN | ||
- | -A fail2ban-apache-overflows -j RETURN | ||
- | -A fail2ban-php-url-fopen -j RETURN | ||
- | -A fail2ban-ssh -j RETURN | ||
- | </ | ||
- | |||
- | If you want to see the details of the bans being enforced by any one jail, it is probably easier to use the **fail2ban-client** again: | ||
- | |||
- | <code bash> | ||
- | sudo fail2ban-client status apache | ||
- | </ | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | Status for the jail: apache | ||
- | |- filter | ||
- | | |- File list: / | ||
- | | |- Currently failed: 0 | ||
- | | `- Total failed: | ||
- | `- action | ||
- | |- Currently banned: 0 | ||
- | | ||
- | `- Total banned: | ||
- | </ | ||
- | |||
- | |||
- | ===== Testing Fail2Ban Policies ===== | ||
- | |||
- | It is important to test your fail2ban policies to ensure they block traffic as expected. | ||
- | |||
- | If you look at the status with the **fail2ban-client** command, you will see your IP address being banned from the site: | ||
- | |||
- | <code bash> | ||
- | sudo fail2ban-client status apache | ||
- | </ | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | Status for the jail: apache | ||
- | |- filter | ||
- | | |- File list: / | ||
- | | |- Currently failed: 0 | ||
- | | `- Total failed: | ||
- | `- action | ||
- | |- Currently banned: 1 | ||
- | | ||
- | `- Total banned: | ||
- | </ | ||
- | |||
- | When you are satisfied that your rules are working, you can manually un-ban your IP address with the **fail2ban-client** by typing: | ||
- | |||
- | <code bash> | ||
- | sudo fail2ban-client set apache unbanip 111.111.111.111 | ||
- | </ | ||
- | |||
- | You should now be able to attempt authentication again. | ||
- | |||
- | |||
- | ===== Conclusion ===== | ||
- | |||
- | Setting up fail2ban to protect your Apache server is fairly straight forward in the simplest case. However, fail2ban provides a great deal of flexibility to construct policies that will suit your specific security needs. | ||
- | |||
- | |||
- | ===== References ===== | ||
- | |||
- | https:// | ||
- | |||
- | To learn more about fail2ban, take a look at some of these links: | ||
- | |||
- | * [[https:// | ||
- | * [[https:// | ||
- | * [[https:// | ||
fail2ban/protect_an_apache_server_with_fail2ban.1476890544.txt.gz · Last modified: 2020/07/15 09:30 (external edit)