Fail2ban pour Nextcloud sur Ubuntu22
https://docs.nextcloud.com/server/latest/admin_manual/installation/harden_server.html#setup-fail2ban
source: https://linuxize.com/post/install-configure-fail2ban-on-debian-10/
# apt-get install fail2ban
whois
pour des infos plus détaillées sur les IPs bloquées # apt-get install whois
# cp /etc/fail2ban/jail.{conf,local}
# nano /etc/fail2ban/jail.local ... ignoreip = 127.0.0.1/8 IPs.reseau.lan.local/24 si besoin ... bantime = 60m ... findtime = 10m ... maxretry = 5 ... destemail = admin@domain.tld ... action = %(action_mw)s ... # systemctl restart fail2ban
Activer les jails: activer toutes les jails apache et SSL.
sshd
, dropbear
, apache-auth
, apache-badbots
, apache-noscript
, apache-overflows
, apache-nohome
, apache-botsearch
, apache-fakegooglebot
, apache-modsecurity
, apache-shellshock
.
# nano /etc/fail2ban/jail.local ---- [sshd] ... enabled = true ----
Malheureusement les jails de fail2ban ne suffisent pas car ce n'est pas Apache qui logge pas les tentatives d'identification mais Nextcloud!
# nano /etc/fail2ban/filter.d/nextcloud.conf [Definition] _groupsre = (?:(?:,?\s*"\w+":(?:"[^"]+"|\w+))*) failregex = ^\{%(_groupsre)s,?\s*"remoteAddr":"<HOST>"%(_groupsre)s,?\s*"message":"Login failed: ^\{%(_groupsre)s,?\s*"remoteAddr":"<HOST>"%(_groupsre)s,?\s*"message":"Trusted domain error. datepattern = ,?\s*"time"\s*:\s*"%%Y-%%m-%%d[T ]%%H:%%M:%%S(%%z)?"
# nano /etc/fail2ban/jail.d/nextcloud.local [nextcloud] backend = auto enabled = true port = 80,443 protocol = tcp filter = nextcloud logpath = /var/www/nextcloud-data/nextcloud.log
# systemctl restart fail2ban
# nano /etc/fail2ban/action.d/opnsense.conf # Fail2Ban configuration file # from triumvirat.org [Definition] actionban = /root/fail2ban-IP.sh ban <ip> actionunban = /root/fail2ban-IP.sh unban <ip>
Placer la nouvelle action dans /etc/fail2ban/jail.local
... banaction = opnsense ...
# systemctl restart fail2ban # systemctl status fail2ban
Il faut créer un petit script:
# nano fail2ban-IP.sh
#!/bin/sh KEY="la_clé" SECRET="le_mot_de_passe" FWIP="ip.de.la.OPNSense" FWPORT="n°_de_port" ALIAS="fail2ban_CT_Joomla" if [ $1 = "ban" ]; then TODO="add" elif [ $1 = "unban" ]; then TODO="delete" fi curl -X POST -d '{"address":"'$2'"}' -H "Content-Type: application/json" -k -u $KEY:$SECRET https://$FWIP:$FWPORT/api/firewall/alias_util/$TODO/$ALIAS
# chmod 600 fail2ban-IP.sh # chmod +x fail2ban-IP.sh
Pour tester le script:
# ./fail2ban-IP.sh ban 192.168.1.1 # ./fail2ban-IP.sh unban 192.168.1.1
et pour finir:
# fail2ban-client set nextcloud.local banip 192.168.1.1 # fail2ban-client set nextcloud.local unbanip 192.168.1.1