If the Apache server is set up so that it is not Apache that deals with 404s, e.g. dynamic server or reverse proxy, these do not go to the error_log, so the apache-noscript
filter is not the best solution. So a quick custom solution is an option. And by implementing one learns how to do it as often one may have a unique problem worth making the filter more elaborate.
Before even starting make sure the 404s responses have a status_code of 404 as opposed to 200 while directed to a "404 page".
To make a 404 jail we need to make a custom filter, for which we need a working regex.
sudo fail2ban-regex /var/log/httpd/access_log "^<HOST> .* /.* 4\d\d .*$"
(The logs are /var/log/httpd/access_log
in CentOS, but /var/log/apache2/access.log
in Ubuntu)
This will test the regex ^<HOST> .* /.* 4\d\d .*$
on the access_log and the matches will be something like:
Lines: 2959 lines, 0 ignored, 1207 matched, 1752 missed
If later, you have a complex issue, say 404 against a given URL are okay, a third argument is for the ignore regex.
If it does not match anything then see what a log entry looks like:
sudo tail /var/log/httpd/access_log
<HOST>
matches the IP of the visitor and /
is the requested page.
Once we are happy we can make a custom filter in etc/fail2ban/filter.d
folder, say called app404.conf
(not .config
like for Apache2 etc., the precise extension is important):
[Definition]
failregex = ^<HOST> .* /.* 4\d\d .*$
Note the lack of double quotes. Test it again with
sudo fail2ban-regex /var/log/httpd/access_log /etc/fail2ban/filter.d/app404.config
Adding a new jail to jail.local
[app404]
enabled = true
filter = app404
port = http,https
logpath = %(apache_access_log)s
banTime = 3600
findtime = 60
maxRetry = 10
And restart fail2ban
fail2ban-client reload
Test it on a different machine/IP (one never knows) and unban that ip
sudo fail2ban-client set app404 unbanip x.x.x.x
Mostly harmless
In a modern web app 404 are harmless and blocking them makes the logs cleaner and reduces odd errors. However, one cannot discount that there is no vulnerability, just because one is using a the latest tech. I.e. it is nice to make sure for oneself. As a result using a tool misused by the hackers, a vulnerability scanner, on one's own server is a good call. A popular one is the open-source Nuclei project.