Fail2ban : notifier chaque connexion SSH

Bien que Fail2Ban fasse correctement son travail, il peut arriver qu’une intrusion se fasse avec succès. Il peut être utile, par précaution ou aussi pour du suivi, de savoir quand quelqu’un se connecte sur SSH.

fail2ban_logo

Si vous connaissez bien Fail2ban, vous savez que tout se fait via des actions et des filtres, donc notre objectif ne sera pas dur à atteindre. Commençons par ajouter notre filtre dans /etc/fail2ban/jail.conf :

Le formatage des mails utilisé ici est disponible sur ce billet.

[ssh-notify]

enabled  = true
filter   = sshd-notify
action   = mail-notify[name=SSH, dest=%(emailt)s, from=%(fromt)s, server=%(servert)s, serverip=%(serveript)s]
maxretry = 1
bantime  = 1
logpath  = /var/log/sshd/current

 

  • maxretry = 1 : Executer  » actionban  » à chaque connexion
  • bantime = 1 : Ne pas garder l’état de bannissement
  • logpath = .. : A modifier en fonction de votre système, journal courant des connexions SSH

Continuons avec /etc/fail2ban/filter.d/sshd-notify.conf qui contient l’expression régulière pour les connexions SSH correctement établies :

UPDATE : la regex a été mise à jour en reprenant le manuel, HOST est devenu obligatoire.

[Definition]

failregex = Accepted [-/\w]+ for .* from <HOST>

ignoreregex =

 

Il ne reste plus que le fichiers des actions à créer. Éditez /etc/fail2ban/action.d/mail-notify.conf :

[Definition]

actionstart =
actionstop =
actioncheck =

actionban = echo -en "***** Fail2Ban *****\n\nNotification Type: NOTIFY\n\nService: <name>\nHost: <server>\nAddress: <serverip>\nState: OK\n\nDate/Time: `date`\n\nAdditional Info:\n\n<ip>" | mail -a "From: <from>" -s "** NOTIFY alert - new connection on <server>/<name> **" <dest>

actionunban =

[Init]
# Default name of the chain
#
name = default
# Destination/Addressee of the mail
#
dest = root

 

Vous pouvez dès à présent redémarrer Fail2ban, désormais à chaque connexion vous devriez recevoir un email de ce genre :

ssh-fail2ban-notify

Enjoy it ;-)

VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)
Fail2ban : notifier chaque connexion SSH, 3.0 out of 5 based on 1 rating
  • Share/Bookmark
Leave a comment

9 Comments.

  1. salut, il est très bien ton tuto mais chez moi j’ai une erreur, voici la stack:

    aliel@xxxxx:~$ sudo /etc/init.d/fail2ban restart
    * Restarting authentication failure monitor fail2ban Traceback (most recent call last):
    File « /usr/bin/fail2ban-client », line 401, in
    if client.start(sys.argv):
    File « /usr/bin/fail2ban-client », line 370, in start
    return self.__processCommand(args)
    File « /usr/bin/fail2ban-client », line 180, in __processCommand
    ret = self.__readConfig()
    File « /usr/bin/fail2ban-client », line 375, in __readConfig
    ret = self.__configurator.getOptions()
    File « /usr/share/fail2ban/client/configurator.py », line 65, in getOptions
    return self.__jails.getOptions(jail)
    File « /usr/share/fail2ban/client/jailsreader.py », line 64, in getOptions
    ret = jail.getOptions()
    File « /usr/share/fail2ban/client/jailreader.py », line 70, in getOptions
    self.__opts = ConfigReader.getOptions(self, self.__name, opts)
    File « /usr/share/fail2ban/client/configreader.py », line 84, in getOptions
    v = self.get(sec, option[1])
    File « /usr/lib/python2.5/ConfigParser.py », line 525, in get
    return self._interpolate(section, option, value, d)
    File « /usr/lib/python2.5/ConfigParser.py », line 593, in _interpolate
    self._interpolate_some(option, L, rawval, section, vars, 1)
    File « /usr/lib/python2.5/ConfigParser.py », line 625, in _interpolate_some
    option, section, rest, var)
    ConfigParser.InterpolationMissingOptionError: Bad value substitution:
    section: [ssh-notify]
    option : action
    key : emailt
    rawval : , from=%(fromt)s, server=%(servert)s, serverip=%(serveript)s]

    —————

    si ca te dit quelque chose … merci

  2. @aliel
    Salut,
    Assure-toi d’avoir bien suivi ce billet.
    Sinon niveau configuration j’ai Python 2.5.6 et Fail2ban 0.8.0.

  3. c’est ok merci,

    en faite il faut mettre

    fromt = Fail2Ban
    servert = localhost
    serveript = 127.0.0.1
    emailt = Me

    dans la section [DEFAULT] pour que les variable soit visible

    # The DEFAULT allows a global definition of the options. They can be override
    # in each jail afterwards.

    Merci encore

  4. Salut !

    Après avoir suivi ce tuto, je ne reçois pas de mail, mais j’ai l’erreur suivante:

    ERROR No ‘host’ group in ‘Accepted .* for .* from .*$’

    Je tourne avec Debian, une idée ?

  5. Oui effectivement, la regex est dépendante de l’OS et de l’outil qui fait les logs.

    Exemple sur une CentOS (OVH) :
    Aug 27 20:37:33 ns303xxx sshd[246xx]: Accepted password for kdecherf from 90.1.x.x port xx ssh2
    La regex devient : Accepted .* for .* from .* port .* ssh2$

  6. mwarf mwarf mwarf…
    Sous Debian j’ai:
    Aug 27 22:17:32 debian sshd[8430]: Accepted password for rei from 10.25.x.x port 59925 ssh2

    j’ai donc l’erreur:
    ERROR No ‘host’ group in ‘Accepted .* for .* from .* port .* ssh2$’

    Rien a changé…
    D’où vient le ‘host’ ?

  7. Ah oui ok … Il semble qu’ils ont changé leur fichier …

    Cette regex devrait passer :
    Accepted [-/\w]+ for .* from

  8. Heu ok … Les commentaires n’aiment pas les caractères spéciaux. Pas grave, je modifie directement mon article …

Leave a Reply


[ Ctrl + Enter ]

Trackbacks and Pingbacks: