Configuration Fail2ban avancée
Ce guide couvre les configurations avancées de Fail2ban pour protéger vos services au-delà du SSH.
Installation
sudo apt update
sudo apt install fail2ban
Structure des fichiers
/etc/fail2ban/
├── fail2ban.conf # Configuration globale
├── jail.conf # Jails par défaut (ne pas modifier)
├── jail.local # Vos personnalisations
├── jail.d/ # Jails additionnelles
├── filter.d/ # Filtres (regex)
└── action.d/ # Actions (ban, notifications)
Important
Ne modifiez jamais jail.conf directement. Créez plutôt jail.local qui sera prioritaire.
Configuration globale
sudo nano /etc/fail2ban/jail.local
[DEFAULT]
# Ignorer ces IP (votre IP fixe, localhost)
ignoreip = 127.0.0.1/8 ::1 VOTRE_IP_FIXE
# Durée de bannissement par défaut (24h)
bantime = 86400
# Fenêtre d'observation
findtime = 600
# Tentatives avant ban
maxretry = 3
# Backend de détection
backend = systemd
# Action par défaut (ban + log)
banaction = iptables-multiport
# Email pour notifications (optionnel)
destemail = admin@votredomaine.com
sender = fail2ban@votredomaine.com
mta = sendmail
action = %(action_mwl)s
Jails pour services courants
SSH (amélioré)
[sshd]
enabled = true
port = ssh,2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
findtime = 300
bantime = 86400
Nginx - Authentification
[nginx-http-auth]
enabled = true
port = http,https
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 3
bantime = 3600
Nginx - Anti-bot/scanner
[nginx-botsearch]
enabled = true
port = http,https
filter = nginx-botsearch
logpath = /var/log/nginx/access.log
maxretry = 2
bantime = 86400
Nginx - Limite de requêtes
Créez d'abord le filtre :
sudo nano /etc/fail2ban/filter.d/nginx-limit-req.conf
[Definition]
failregex = limiting requests, excess:.* by zone.*client: <HOST>
ignoreregex =
Puis la jail :
[nginx-limit-req]
enabled = true
port = http,https
filter = nginx-limit-req
logpath = /var/log/nginx/error.log
maxretry = 5
findtime = 60
bantime = 7200
Apache
[apache-auth]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache2/error.log
maxretry = 3
bantime = 3600
[apache-overflows]
enabled = true
port = http,https
filter = apache-overflows
logpath = /var/log/apache2/error.log
maxretry = 2
bantime = 86400
MySQL/MariaDB
[mysqld-auth]
enabled = true
port = 3306
filter = mysqld-auth
logpath = /var/log/mysql/error.log
maxretry = 3
bantime = 86400
Postfix (Email)
[postfix]
enabled = true
port = smtp,465,587
filter = postfix
logpath = /var/log/mail.log
maxretry = 3
bantime = 86400
[postfix-sasl]
enabled = true
port = smtp,465,587,imap,imaps
filter = postfix-sasl
logpath = /var/log/mail.log
maxretry = 3
bantime = 86400
Dovecot (IMAP)
[dovecot]
enabled = true
port = imap,imaps,pop3,pop3s
filter = dovecot
logpath = /var/log/mail.log
maxretry = 3
bantime = 86400
Bannissement progressif
Pour augmenter la durée de ban à chaque récidive :
[recidive]
enabled = true
filter = recidive
logpath = /var/log/fail2ban.log
action = iptables-allports[name=recidive]
bantime = 604800 # 1 semaine
findtime = 86400 # Cherche dans les dernières 24h
maxretry = 3 # 3 bans = recidive
Créer un filtre personnalisé
Exemple pour protéger une API contre le brute force :
sudo nano /etc/fail2ban/filter.d/api-auth.conf
[Definition]
# Détecte les erreurs 401/403 dans les logs
failregex = ^<HOST> .* "(GET|POST) /api/auth.*" (401|403)
^<HOST> .* "POST /api/login.*" (401|403)
ignoreregex =
datepattern = %%d/%%b/%%Y:%%H:%%M:%%S %%z
Testez le filtre :
fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/api-auth.conf
Puis activez la jail :
[api-auth]
enabled = true
port = http,https
filter = api-auth
logpath = /var/log/nginx/access.log
maxretry = 5
findtime = 300
bantime = 3600
Notifications par email
Configuration avec Postfix :
[DEFAULT]
destemail = admin@votredomaine.com
sender = fail2ban@votredomaine.com
mta = sendmail
# Actions disponibles :
# action_ : ban uniquement
# action_mw : ban + email avec whois
# action_mwl : ban + email avec whois + logs
action = %(action_mwl)s
Notifications Discord/Slack
Créez une action personnalisée :
sudo nano /etc/fail2ban/action.d/discord.conf
[Definition]
actionban = curl -X POST -H "Content-Type: application/json" \
-d '{"content": "**Fail2ban** - IP `<ip>` bannie sur `<name>` (<failures> tentatives)"}' \
"VOTRE_WEBHOOK_DISCORD"
actionunban = curl -X POST -H "Content-Type: application/json" \
-d '{"content": "**Fail2ban** - IP `<ip>` débannie de `<name>`"}' \
"VOTRE_WEBHOOK_DISCORD"
Utilisez-la dans une jail :
[sshd]
enabled = true
action = iptables-multiport[name=sshd, port="ssh", protocol=tcp]
discord
Commandes utiles
# Statut global
sudo fail2ban-client status
# Statut d'une jail
sudo fail2ban-client status sshd
# Liste des IP bannies
sudo fail2ban-client status sshd | grep -E "Banned IP"
# Bannir manuellement une IP
sudo fail2ban-client set sshd banip 192.168.1.100
# Débannir une IP
sudo fail2ban-client set sshd unbanip 192.168.1.100
# Recharger la configuration
sudo fail2ban-client reload
# Voir les logs en temps réel
sudo tail -f /var/log/fail2ban.log
# Tester un filtre
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf
Intégration avec UFW
Par défaut, Fail2ban utilise iptables. Pour utiliser UFW :
sudo nano /etc/fail2ban/jail.local
[DEFAULT]
banaction = ufw
Monitoring avec fail2ban-client
Script de rapport quotidien :
#!/bin/bash
# /usr/local/bin/fail2ban-report.sh
echo "=== Rapport Fail2ban $(date) ==="
echo ""
for jail in $(fail2ban-client status | grep "Jail list" | sed 's/.*://;s/,//g'); do
echo "--- $jail ---"
fail2ban-client status $jail
echo ""
done
Ajoutez au cron :
0 8 * * * /usr/local/bin/fail2ban-report.sh | mail -s "Rapport Fail2ban" admin@votredomaine.com
Dépannage
Fail2ban ne démarre pas
# Vérifier la syntaxe
sudo fail2ban-client -d
# Voir les erreurs
sudo journalctl -u fail2ban -f
Les bans ne fonctionnent pas
# Vérifier les règles iptables
sudo iptables -L -n | grep fail2ban
# Vérifier que le filtre matche
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf --print-all-matched
Performance
Pour les serveurs à fort trafic, utilisez backend = systemd et évitez de surveiller des logs trop volumineux sans filtres précis.