4. Configurar Fail2ban (sshd + servidor web)¶
Protección activa con jaulas integradas en UFW.
Traefik en Docker
Si tu servidor web corre detrás de Traefik en contenedores, fail2ban no lee los logs del host de forma directa — este paso no cubre ese caso todavía. La parte de sshd de abajo sí aplica igualmente.
-
Comprobar si ya está instalado:
dpkg -l | grep fail2ban -
Instalar si hace falta:
sudo apt install fail2ban -
Comprobar que existe la acción
ufwde fail2ban:ls /etc/fail2ban/action.d/ufw.confSi no existe, usa
banaction = iptables-multiporten vez deufwen el paso siguiente. -
Crear el filtro para rutas sensibles:
sudo vim /etc/fail2ban/filter.d/apache-sensitive-paths.confContenido:
[Definition] failregex = ^<HOST> .* "(GET|POST) .*(wp-config\.php|\.env|\.git/config|xmlrpc\.php|\.htpasswd).*" (403|404) ignoreregex =sudo vim /etc/fail2ban/filter.d/nginx-sensitive-paths.confContenido (mismo patrón, adaptado al log de nginx):
[Definition] failregex = ^<HOST> .* "(GET|POST) .*(wp-config\.php|\.env|\.git/config|xmlrpc\.php|\.htpasswd).*" (403|404) ignoreregex = -
Crear
jail.localdesde cero (no copiarjail.conf— solo va aquí lo que sobreescribes):sudo vim /etc/fail2ban/jail.localContenido:
[DEFAULT] banaction = ufw [sshd] enabled = true port = 22 maxretry = 5 bantime = 3600 [apache-auth] enabled = true [apache-badbots] enabled = true [apache-sensitive-paths] enabled = true port = http,https filter = apache-sensitive-paths logpath = /var/log/apache2/*access.log maxretry = 1 bantime = 86400[DEFAULT] banaction = ufw [sshd] enabled = true port = 22 maxretry = 5 bantime = 3600 [nginx-http-auth] enabled = true [nginx-botsearch] enabled = true [nginx-sensitive-paths] enabled = true port = http,https filter = nginx-sensitive-paths logpath = /var/log/nginx/*access.log maxretry = 1 bantime = 86400Fail2ban trae de serie los filtros
nginx-http-authynginx-botsearch(equivalentes aapache-auth/apache-badbots), así que no hace falta crearlos a mano — solo el de rutas sensibles del paso anterior. -
Reiniciar y verificar:
sudo service fail2ban restart sudo fail2ban-client status sudo fail2ban-client status sshd sudo ufw status numbered