I’ve noticed a pattern on a number of websites that I host … crawlers that look for pages that don’t exist (and never existed).
Most of the time, the crawlers are looking for WordPress vulnerabilities but I’ve also seen them just trying to find php files that would indicate a vulnerability.
I’ve been using fail2ban for a long time to block remote attacks against ssh and smtp, so it wasn’t hard to create a configuration that detects & blocks this new attack.
The following requires a fair bit of Linux system administration experience. You shouldn’t attempt this if you’re not comfortable working with firewall rules and configuration files.
I’m assuming you have the following installed …
- Apache 2 http server
- iptables
- fail2ban
We need to create & modify files in the /etc/fail2ban
First, create a new filter that will detect the 404 errors in the access log.
I put this in filters.d/apache-404.conf.
[Definition]
failregex = ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/.*" 404 \d+
ignoreregex =.*(apple-touch|robots.txt|favicon.ico|jpg|png).*Code language: HTML, XML (xml)
Basically, the filter matches any GET, POST, or HEAD, request that results in a 404 error.
The ignoreregex setting excludes some common files that may be missing.
Next, we need to create a new jail configuration to tell fail2ban how to handle log entries that the filter identifies.
Create, or add to, a new local jail configuration … I put the following in jail.d/local.conf.
[apache-404]
port=http,https
logpath=/path/to/apache/access_log_1
/path/to/apache/access_log_2
enabled=true
maxretry = 5Code language: JavaScript (javascript)
This tells fail2ban that it should block the ports for http (80) & https (443) if any entries in the log path’s specified are matched, using the apache-404 filter, 5 times or more.
If you want to specify more than one log file, they need to go on separate lines preceded with spaces.
After you’ve created these files, restart fail2ban.
When you run iptables -L -n, you should see an additional firewall chain named f2b-apache-404. When crawlers hit your site, and get 5 or more 404 errors, fail2ban will add rules to the chain blocking the host.