Recently my servers started getting bombarded by anonymous scanners/brute forces.
This is how my nginx access.log looked like after the attacks :
xxx.xxx.xxx.xxx - - [07/Sep/2019:23:30:16 +0200] "GET /phpMyAdmin/index.php HTTP/1.1" 404 548 "-" "Mozilla/5.0..
xxx.xxx.xxx.xxx - - [07/Sep/2019:23:30:18 +0200] "GET /admin/index.php HTTP/1.1" 404 548 "-" "Mozilla/5.0..
Along with tons of other urls/admin pages/random names they tried to access. I did some research and fail2ban came up. So I installed it.
I left it on for some time but attacks were happening again. I realized the default [nginx-botsearch]
does not catch all the attacks, not to mention it's log path was mis-configured.
I did some googling and settled on this custom filter:
# /etc/fail2ban/jail.local
[nginx-404-errors]
enabled = true
port = http,https
logpath = /var/log/nginx/*.log
maxretry = 2
# /etc/fail2ban/filter.d/nginx-404-errors.conf
[Definition]
failregex = ^<HOST>.*"(GET|POST|HEAD).*" (404|444|403|400) .*$
ignoreregex =
This worked very well, but the attacker/s decided to hit the http. But the thing is I have a http to https redirection rule on nginx.
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
So now I'm getting tons of 301 redirections logs in access.log
. I thought maybe I should just add a 301
to the regex I used above. So now it's :
failregex = ^<HOST>.*"(GET|POST|HEAD).*" (404|444|403|400|301) .*$
The attacks has stopped for now. But I'm worried the normal visitors might get into trouble when trying to reach the site on http and fail2ban might pick on them.
Is this the way I should mitigate this kind of brute force attacks? Or am I supposed to take a different route?