My Fail2Ban filter for anyone requesting PHP files on Apache
Published -
I used to use Wordpress, I haven't for a long time but that doesn't stop bots from probing for soft spots
WARNING:1 If you are running a site that uses php e.g. Wordpress or phpBB DON'T USE THIS FILTER.
Here's a step-by-step guide to create a Fail2Ban filter that bans IP addresses requesting .php files on your Apache web server, you will need to create a custom filter and a jail configuration for it.
Step 1: Create a Custom Filter
Create the filter file:
sudo nano /etc/fail2ban/filter.d/apache-php.confAdd the filter definition:
[Definition] failregex = ^<HOST> .* "GET .*\.php ignoreregex =This filter will match any line in the Apache access log where an IP address requests a
.phpfile.
Step 2: Create a Jail for the Custom Filter
Edit the jail.local file:
sudo nano /etc/fail2ban/jail.localAdd the jail configuration:
[apache-php] enabled = true port = http,https filter = apache-php logpath = /var/log/apache2/access.log maxretry = 1 bantime = 3600enabled = trueenables this jail.port = http,httpsspecifies the ports to monitor.filter = apache-phpreferences the custom filter created.logpath = /var/log/apache2/access.logspecifies the log file to monitor. Ensure this path matches your actual access log path.maxretry = 1bans after a single occurrence.bantime = 3600sets the ban duration to one hour (3600 seconds). Adjust this as needed.
Step 3: Restart Fail2Ban
Restart the Fail2Ban service:
sudo systemctl restart fail2banVerify the configuration:
sudo fail2ban-client status sudo fail2ban-client status apache-phpThe first command will show the overall status of Fail2Ban and the second will show the status of the specific jail.
Fail2Ban should now monitor your Apache access logs and ban any IP addresses that request a .php file. Adjust the bantime and maxretry settings according to your specific needs.
You know those warnings that you shouldn’t iron your clothes while still wearing them? There is someone out there that made it necessary to have them.↩