Table of Contents

fail2ban

Installation

apt install fail2ban

Commands

# Check regex
fail2ban-regex /var/log/nginx/access.log nginx-404.conf --print-all-matched

# Restart service
fail2ban-client -t
systemctl restart fail2ban

# Check banned IPs and unban
fail2ban-client status nginx-404
fail2ban-client set nginx-404 unbanip <IPADDR>

Configuration

A selection of configurations for localhost protection.

/etc/fail2ban/filter.d/nginx-404.conf
[Definition]
failregex = ^<ADDR> \S+ \S+ \[\] "[^"]*" 404(?:\s|$)
ignoreregex = ^<ADDR> \S+ \S+ \[\] "(?:GET|HEAD) /(?:favicon\.ico|robots\.txt|apple-touch-icon[^ ]*) HTTP/[^"]*" 404(?:\s|$)
/etc/fail2ban/jail.d/nginx-404.local
[nginx-404]
enabled = true
filter = nginx-404
port = http,https
logpath = /var/log/nginx/access.log
backend = auto
usedns = no
 
# 30 hits within 60 seconds
maxretry = 30
findtime = 60
bantime = 3600
 
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 7d
banaction = nftables-multiport
ignoreip = 127.0.0.1/8 ::1

For common probe patterns and password spray:

[nginx-botsearch]
enabled = true
port = http,https
logpath = /var/log/nginx/access.log

maxretry = 10
findtime = 300
bantime = 3600

[nginx-http-auth]
enabled = true
maxretry = 10
findtime = 300
bantime = 3600

For SSH:

/etc/fail2ban/jail.d/sshd.local
[sshd]
enabled = true
backend = systemd
 
maxretry = 5
findtime = 600
bantime = 3600
 
banaction = nftables-multiport