Ajout du support IPv6 sur Fail2ban

  1. Comment ça marche ?
  2. Patch de Fail2ban
  3. Modification des jails
  4. Vérification
  5. Références et remerciements

1. Comment ça marche ?

C'est très simple, pour le support de l'IPv6 il suffit de patcher 2 fichiers et d'en créer 3 nouveaux :

├── etc
│   └── fail2ban
│       ├── action.d
│       │   ├── iptables46-allports.conf
│       │   ├── iptables46-multiport.conf
├── usr
│   ├── bin
│   │   └── ip64tables.sh
│   ├── lib
│       │   ├── server
│       │   │   ├── failregex.py
│       │   │   ├── filter.py

Sur ce tree, nous avons :

  • Nouveaux fichiers : iptables46-allports.conf, iptables46-multiport.conf et ip64tables.sh
  • Fichiers patchés : failregex.py et filter.py

Le patch original a été créé par Thanatos en février 2012 et était fonctionnel au moins jusqu'à la version 0.8.6.

J'ai donc du le modifier très légèrement pour qu'il soit compatible avec Fail2ban 0.9.0.

Avertissement : La résolution de nom dans les logs ne doit pas être activée.

2. Patch de Fail2ban

Les procédures sont donc a effectuées en root :

Téléchargement :

[root@serveur ~]# wget --no-check-certificate https://data.tetsumaki.net/divers/fail2ban-ipv6.tar.xz
[root@serveur ~]# tar Jxvf fail2ban-ipv6.tar.xz

Test des patchs (--dry-run) :

[root@serveur ~]# cd /usr/lib/python3.4/site-packages/fail2ban/server/
[root@serveur server]# patch --verbose -p0 --dry-run < ~/fail2ban-ipv6/filter.py.patch
    Hmm...  Looks like a unified diff to me...
    The text leading up to this was:
    --------------------------
    |--- filter.py  2014-04-26 20:38:06.000000000 +0200
    |+++ filter.py.new  2014-05-03 13:37:44.315899520 +0200
    --------------------------
    checking file filter.py
    Using Plan A...
    Hunk #1 succeeded at 836.
    Hunk #2 succeeded at 864.
    done
[root@serveur server]# patch --verbose -p0 --dry-run < ~/fail2ban-ipv6/failregex.py.patch
    Hmm...  Looks like a unified diff to me...
    The text leading up to this was:
    --------------------------
    |--- failregex.py   2014-03-15 01:18:01.000000000 +0100
    |+++ failregex.py.new   2014-05-03 12:00:41.948787846 +0200
    --------------------------
    checking file failregex.py
    Using Plan A...
    Hunk #1 succeeded at 41.
    done

Aucune erreur ? on patch :

[root@serveur ~]# cd /usr/lib/python3.4/site-packages/fail2ban/server/
[root@serveur server]# patch --verbose -p0 < ~/fail2ban-ipv6/filter.py.patch
[root@serveur server]# patch --verbose -p0 < ~/fail2ban-ipv6/failregex.py.patch

Copie des 2 fichiers supplémentaires :

[root@serveur ~]# cd ~/fail2ban-ipv6/
[root@serveur fail2ban-ipv6]# cp ip64tables.sh /usr/bin/
[root@serveur fail2ban-ipv6]# chmod 755 /usr/bin/ip64tables.sh
[root@serveur fail2ban-ipv6]# cp iptables46-* /etc/fail2ban/action.d/
[root@serveur fail2ban-ipv6]# cd /etc/fail2ban/action.d/
[root@serveur action.d]# chmod 644 iptables46-*

Redémarrage du service :

[root@serveur ~]# systemctl restart fail2ban

3. Modification de jail.local

Pour que Fail2ban bannissent les IPv4 ainsi que les IPv6, il faut modifier banaction, soit dans chaque règle, soit dans une règle souhaitée, soit par défaut, en bref, les mélanges sont permis.

Les fichiers de configuration à modifier sont /etc/fail2ban/jail.conf et /etc/fail2ban/jail.local.

Par défaut :

Avant :

# Default banning action (e.g. iptables, iptables-new,
# iptables-multiport, shorewall, etc) It is used to define
# action_* variables. Can be overridden globally or per
# section within jail.local file
banaction = iptables-multiport

Après :

# Default banning action (e.g. iptables, iptables-new,
# iptables-multiport, shorewall, etc) It is used to define
# action_* variables. Can be overridden globally or per
# section within jail.local file
banaction = iptables46-multiport

Ou, par exemple, dans la règle sshd uniquement

Avant :

[sshd]
enabled = true
port    = ssh
logpath = /var/log/sshd.log

Après :

[sshd]
enabled   = true
port      = ssh
logpath   = /var/log/sshd.log
banaction = iptables46-multiport

Ou encore, dans la règle recidive uniquement

Avant :

[recidive]
enabled  = true
port     = all
protocol = all
logpath  = /var/log/fail2ban.log
bantime  = 604800
findtime = 86400
maxretry = 5

Après :

[recidive]
enabled  = true
port     = all
protocol = all
logpath  = /var/log/fail2ban.log
bantime  = 604800
findtime = 86400
maxretry = 5
banaction = iptables46-allports

Rechargement du service :

[root@serveur ~]# systemctl reload fail2ban

4. Vérification

Le service est-il démarré ?

[root@serveur ~]# systemctl status fail2ban
    ● fail2ban.service - Fail2ban Service
       Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled)
       Active: active (running) since lun. 2014-05-26 19:53:42 CEST; 13min ago
      Process: 371 ExecStart=/usr/bin/fail2ban-client -x start (code=exited, status=0/SUCCESS)
     Main PID: 511 (fail2ban-server)
       CGroup: /system.slice/fail2ban.service
               └─511 /usr/bin/python /usr/bin/fail2ban-server -b -s /var/run/fail2ban/fail2ban.sock -p /var/run/fail2ban/fail2ban.pid -x
    mai 26 19:53:40 ks395877 fail2ban-client[371]: 2014-05-26 19:53:40,285 fail2ban.server.server[509]: INFO    Starting Fail2ban v0.9.0
    mai 26 19:53:40 ks395877 fail2ban-client[371]: 2014-05-26 19:53:40,286 fail2ban.server.server[509]: INFO    Starting in daemon mode
    mai 26 19:53:42 ks395877 systemd[1]: Started Fail2ban Service.

Mes règles sont-elles visibles depuis iptables et ip6tables ?

[root@serveur ~]# iptables -S | grep f2b
    […]
    -N f2b-nginx-http-auth
    -N f2b-recidive
    -N f2b-tetsu-nginx-access
    […]
[root@serveur ~]# ip6tables -S | grep f2b
    […]
    -N f2b-nginx-http-auth
    -N f2b-recidive
    -N f2b-tetsu-nginx-access
    […]

Et depuis fail2ban-client ?

[root@serveur ~]# fail2ban-client status
    Status
    |- Number of jail:  3
    `- Jail list:   nginx-http-auth, recidive, tetsu-nginx-access

5. Références et remerciements