apt install fail2ban
vi /etc/fail2ban/jail.conf
/etc/fail2ban/jail.local
pour éviter l'écrasement de la config lors des mises à jour. Mais sous Debian ce fichier existe déjà sous le nom : vi /etc/fail2ban/jail.d/defaults-debian.conf
et contient déjà le service SSH activé.[DEFAULT] # Emails destemail = adminsys@<domaine-sinp> sender = mailer@<domaine-sinp> # Actions de banissement via Firewalld banaction = firewallcmd-multiport banaction_allports = firewallcmd-allports # Actions à réaliser en cas de banissement : mwl (= ban & send an e-mail with whois report and relevant log lines) action = %(action_mwl)s # Ajouter ses ip pour éviter de se faire bannir # Ex.: ignoreip = 127.0.0.1/8 10.0.1.10 10.0.1.20 <ip-v4-db> <ip-v4-web> ignoreip = 127.0.0.1/8 <ip-v4-private-web> <ip-v4-private-db> <ip-v4-db> <ip-v4-web> <ip-vpn-lpo> # 1 jour de bannissement #bantime = 86400 # 1 semaine de bannissement - Mise à jour 2020-12-04 [jpmilcent]. # bantime = 604800 # 1 an de banissement - Mise à jour 2021-01-06 [jpmilcent] => 31 536 000s bantime = 31536000 [sshd] enabled = true port = <port-ssh> [postfix] enabled = true port = smtp,submission
[DEFAULT] # Emails destemail = adminsys@<domaine-sinp> sender = mailer@<domaine-sinp> # Actions de banissement via Firewalld banaction = firewallcmd-multiport banaction_allports = firewallcmd-allports # Actions à réaliser en cas de banissement : mwl (= ban & send an e-mail with whois report and relevant log lines) action = %(action_mwl)s # Ajouter ses ip pour éviter de se faire bannir ignoreip = 127.0.0.1/8 <ip-v4-private-web> <ip-v4-private-db> <ip-v4-private-bkp> <ip-v4-web> <ip-v4-db> <ip-v4-bkp> # 1 jour de bannissement #bantime = 86400 # 1 semaine de bannissement - Mise à jour 2020-12-04 [jpmilcent]. #bantime = 604800 # 1 an de bannissement - Mise à jour 2021-01-06 [jpmilcent] => 31 536 000s bantime = 31536000 [sshd] backend=systemd enabled = true port = <port-ssh> [postfix] backend=systemd enabled = true port = smtp,submission # Spécifique à l'instance : web-srv # [nginx-http-auth] # enabled = true # port = http,https # logpath = /var/log/nginx/error.log # [nginx-limit-req] # enabled = true # port = http,https # logpath = /var/log/nginx/error.log # [nginx-botsearch] # enabled = true # port = http,https # logpath = /var/log/nginx/error.log # maxretry = 2
systemctl restart fail2ban
systemctl status fail2ban
vi /var/log/fail2ban.log
fail2ban-client status nginx-http-auth
fail2ban-client set <nom-de-prison> unbanip <IP-concernée>
fail2ban-client reload --unban <nom-de-prison>
grep "POST /wp-login.php" /var/log/nginx/access.log
vi /etc/fail2ban/filter.d/nginx-wordpress.conf
findtime
)[Definition] # Fail2Ban configuration file # # Preserve brute force on Wordpress site # Author: cam.lafit <cam.lafit@azerttyu.net> # Source: https://km.azerttyu.net/Wordpress-et-les-attaques-brute-force file_preserved = wp-login\.php|xmlrpc\.php # Option: failregex # Notes.: Regexp to catch url to prevent bot connection # that it is your intent to block IPs which were driven by # abovementioned bots. # Values: TEXT # failregex = ^<HOST> -.*"POST /(%(file_preserved)s) HTTP/[12]\.[01]" 200 .* # Option: ignoreregex # Notes.: regex to ignore. If this regex matches, the line is ignored. # Values: TEXT # ignoreregex =
fail2ban-regex --print-all-matched /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-wordpress.conf
vi /etc/fail2ban/jail.d/defaults-debian.conf
[nginx-wordpress] # Banni toute IP ayant accédée à wp_login.php au moins 3 fois dans un intervalle de 240 secondes (4mn) enabled = true port = http,https filter = nginx-wordpress logpath = /var/log/nginx/access.log maxretry = 3 findtime = 240
systemctl reload fail2ban.service
watch fail2ban-client status nginx-wordpress
Sous Debian 10, corriger le bug fail2ban-tmpfiles.conf points to /var/run/ instead of /run, en éditant les fichiers :10
vi /usr/lib/tmpfiles.d/fail2ban-tmpfiles.conf
et y remplacer le chemin comme suit : D /run/fail2ban 0755 root root -
vi /lib/systemd/system/fail2ban.service
et y remplacer les occurrences de /var/run/
par /run/
.systemctl daemon-reload
systemctl status fail2ban
) affiche le message fail2ban.configreader [874403]: WARNING 'allowipv6' not defined in 'Definition'. Using default one: 'auto'
vi /etc/fail2ban/fail2ban.conf
et décommenter la ligne allowipv6 = auto
.systemctl status fail2ban
) affiche le message failed during configuration: Have not found any log file for sshd jail
/etc/fail2ban/jail.d/defaults-debian.conf
ajouter à la section [sshd]
la propriété backend=systemd
, idem pour la section [postfix]