Aller au contenu principal

Configuration Fail2ban avancée

Fail2ban Avancé sur VPS RedHeberg

Fail2ban est un système de prévention d'intrusion qui analyse les logs système et bannit automatiquement les IP suspectes. Cette documentation vous guide dans une configuration avancée sur vos VPS RedHeberg.

Installation de Fail2ban

Sur Ubuntu/Debian :

sudo apt update
sudo apt install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Sur AlmaLinux/Rocky Linux :

sudo dnf install epel-release -y
sudo dnf install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Configuration Principale

Fichier jail.local Complet

Créez le fichier de configuration principal :

sudo nano /etc/fail2ban/jail.local

Configuration complète avec toutes les sections :

[DEFAULT]
# IP à ignorer (votre IP fixe + réseaux locaux)
ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 YOUR_ADMIN_IP

# Durée de bannissement (30 minutes)
bantime = 1800

# Fenêtre de temps pour compter les tentatives
findtime = 600

# Nombre maximum de tentatives
maxretry = 3

# Action par défaut avec notification email
action = %(action_mwl)s

# Configuration email (optionnel)
destemail = admin@votredomaine.fr
sender = fail2ban@votreserver.redheberg.fr
mta = sendmail

# Jail SSH - Protection contre les attaques par force brute
[sshd]
enabled = true
port = ssh,22
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600

# Jail Nginx - Protection HTTP
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 3
bantime = 1800

[nginx-noscript]
enabled = true
filter = nginx-noscript
logpath = /var/log/nginx/access.log
maxretry = 6
bantime = 86400

[nginx-badbots]
enabled = true
filter = nginx-badbots
logpath = /var/log/nginx/access.log
maxretry = 2
bantime = 86400

[nginx-noproxy]
enabled = true
filter = nginx-noproxy
logpath = /var/log/nginx/access.log
maxretry = 2
bantime = 86400

# Jail Apache (si utilisé)
[apache-auth]
enabled = false
filter = apache-auth
logpath = /var/log/apache2/error.log
maxretry = 3

[apache-badbots]
enabled = false
filter = apache-badbots
logpath = /var/log/apache2/access.log
maxretry = 2

# Protection contre les scans de ports
[port-scan]
enabled = true
filter = port-scan
logpath = /var/log/syslog
maxretry = 1
bantime = 86400

# Jail personnalisé pour les logs d'application
[custom-app]
enabled = false
filter = custom-app
logpath = /var/log/myapp/security.log
maxretry = 5
bantime = 3600

Filtres Personnalisés

Créez un filtre personnalisé pour détecter les tentatives d'intrusion :

sudo nano /etc/fail2ban/filter.d/port-scan.conf
[Definition]
failregex = .*kernel:.*\[UFW BLOCK\].*SRC=<HOST>
ignoreregex =

Filtre pour application personnalisée :

sudo nano /etc/fail2ban/filter.d/custom-app.conf
[Definition]
# Détecte les tentatives de connexion échouées dans vos logs applicatifs
failregex = ^.*Failed login attempt from <HOST>.*$
^.*Invalid credentials from <HOST>.*$
^.*Brute force detected from <HOST>.*$
ignoreregex =

[Init]
datepattern = ^%%Y-%%m-%%d %%H:%%M:%%S

Gestion et Surveillance

Commandes de Base

Redémarrer Fail2ban après modification :

sudo systemctl restart fail2ban
sudo fail2ban-client reload

Vérifier le statut global :

sudo fail2ban-client status

Statut détaillé d'un jail :

sudo fail2ban-client status sshd
sudo fail2ban-client status nginx-http-auth

Gestion Manuelle des Bannissements

Bannir une IP manuellement :

sudo fail2ban-client set sshd banip YOUR_SERVER_IP

Débannir une IP :

sudo fail2ban-client set sshd unbanip YOUR_SERVER_IP

Lister les IP bannies :

sudo fail2ban-client get sshd banip
sudo iptables -L fail2ban-sshd -n
attention

Attention à ne pas vous bannir vous-même ! Ajoutez toujours votre IP dans ignoreip.

Configuration Email Avancée

Notifications Personnalisées

Créez une action personnalisée avec template email :

sudo nano /etc/fail2ban/action.d/mail-custom.conf
[Definition]
actionstart = printf %%b "Subject: [Fail2ban] <name>: Démarré sur <fq-hostname>
Date: `LC_ALL=C date +"%%a, %%d %%h %%Y %%T %%z"`
From: <sender>
To: <dest>\n
Le service fail2ban a démarré sur <fq-hostname>" | /usr/sbin/sendmail -f <sender> <dest>

actionstop = printf %%b "Subject: [Fail2ban] <name>: Arrêté sur <fq-hostname>
Date: `LC_ALL=C date +"%%a, %%d %%h %%Y %%T %%z"`
From: <sender>
To: <dest>\n
Le service fail2ban s'est arrêté sur <fq-hostname>" | /usr/sbin/sendmail -f <sender> <dest>

actionban = printf %%b "Subject: [ALERTE] IP <ip> bannie par Fail2ban sur <fq-hostname>
Date: `LC_ALL=C date +"%%a, %%d %%h %%Y %%T %%z"`
From: <sender>
To: <dest>\n
BANNISSEMENT DÉTECTÉ\n
IP: <ip>
Jail: <name>
Serveur: <fq-hostname>
Tentatives: <failures>
Durée: <bantime>s\n
Logs:\n
<matches>" | /usr/sbin/sendmail -f <sender> <dest>

actionunban = printf %%b "Subject: [INFO] IP <ip> débannie sur <fq-hostname>
Date: `LC_ALL=C date +"%%a, %%d %%h %%Y %%T %%z"`
From: <sender>
To: <dest>\n
L'IP <ip> a été débannie du jail <name>" | /usr/sbin/sendmail -f <sender> <dest>

[Init]
name = default
dest = root
sender = fail2ban
matches =

Surveillance et Logs

Monitoring des Logs Fail2ban

Surveiller les bannissements en temps réel :

sudo tail -f /var/log/fail2ban.log

Aff