Fail2ban et SSH

Salut à tous,

J’ai installé fail2ban et j’essai de le faire fonctionner. J’ai paramétré les fichiers /etc/fail2ban/fail2ban.conf et /etc/fail2ban/jail.conf.local

J’ai un petit problème avec SSH. J’ai fait plusieurs tentative de login erroné, dans le fichier /var/log/fail2ban.log j’ai bien les infos suivantes :

Si je liste les infos de mon firewall :

[code](root - 0) iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
fail2ban-ssh tcp – anywhere anywhere tcp dpt:ssh
fail2ban-ssh tcp – anywhere anywhere tcp dpt:22
ACCEPT 0 – anywhere anywhere state RELATED,ESTABLISHED
ACCEPT 0 – anywhere anywhere
ACCEPT icmp – 88.191.254.0/24 anywhere
ACCEPT tcp – anywhere anywhere tcp dpt:4242
ACCEPT tcp – anywhere anywhere tcp dpt:www
ACCEPT tcp – anywhere anywhere tcp dpt:https
ACCEPT tcp – anywhere anywhere tcp dpts:ftp-data:ftp
ACCEPT 0 – anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp – anywhere anywhere tcp dpt:ssmtp
ACCEPT tcp – anywhere anywhere tcp dpt:imaps
ACCEPT tcp – anywhere anywhere tcp dpt:pop3s
ACCEPT tcp – 88.191.254.0/24 anywhere tcp dpt:snmp state NEW,ESTABLISHED
ACCEPT udp – 88.191.254.0/24 anywhere udp dpt:snmp state NEW,ESTABLISHED

Chain FORWARD (policy DROP)
target prot opt source destination
ACCEPT tcp – anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN limit: avg 1/sec burst 5
ACCEPT udp – anywhere anywhere limit: avg 1/sec burst 5

Chain OUTPUT (policy DROP)
target prot opt source destination
ACCEPT 0 – anywhere anywhere state RELATED,ESTABLISHED
ACCEPT 0 – anywhere anywhere
ACCEPT icmp – anywhere 88.191.254.0/24
ACCEPT tcp – anywhere anywhere tcp dpt:4242
ACCEPT tcp – anywhere anywhere tcp dpt:domain
ACCEPT udp – anywhere anywhere udp dpt:domain
ACCEPT udp – anywhere anywhere udp dpt:ntp
ACCEPT tcp – anywhere anywhere tcp dpt:www
ACCEPT tcp – anywhere anywhere tcp dpt:https
ACCEPT tcp – anywhere anywhere tcp dpts:ftp-data:ftp
ACCEPT tcp – anywhere anywhere tcp dpt:smtp
ACCEPT tcp – anywhere anywhere tcp dpt:ssmtp
ACCEPT tcp – anywhere anywhere tcp dpt:imaps
ACCEPT tcp – anywhere anywhere tcp dpt:pop3s
ACCEPT tcp – anywhere 88.191.254.0/24 tcp spt:snmp state ESTABLISHED
ACCEPT udp – anywhere 88.191.254.0/24 udp spt:snmp state ESTABLISHED

Chain SPOOFED (0 references)
target prot opt source destination
DROP 0 – loopback/8 anywhere
DROP 0 – 169.240.0.0/12 anywhere
DROP 0 – 172.16.0.0/12 anywhere
DROP 0 – 192.168.0.0/16 anywhere
DROP 0 – 10.0.0.0/8 anywhere

Chain fail2ban-ssh (2 references)
target prot opt source destination
DROP 0 – {MYIP} anywhere
RETURN 0 – anywhere anywhere[/code]

A première vue je devrait etre bloqué et bien non, je peut continuer à m’identifier…

J’ai essayer de tester la regexp pour voir mais cella ne fonctionne pas non plus :

[code](root - 0) fail2ban-regex /etc/fail2ban/filter.d/sshd.conf /var/log/auth.log

Sorry, no match
[/code]

Je ne sais plus quoi faire là.
Merci d’avance pour vos réponses.

bonsoir,
tu ne dis pas si tu as redémarré le service aprés l’avoir (re)configuré, ça coule de source mais bon ?

Je l’ai fait et j’ai même rebooter le serveur.

Chez moi, je n’ai paramétré que le jail.conf, je n’ai pas de jail.conf.local ( :question: ).
A tout hasard, voilà ce que j’ai :

apt-cache policy fail2ban fail2ban: Installé : 0.7.5-2etch1 Candidat : 0.7.5-2etch1 Table de version : 0.8.2-1~bpo40+1 0 1 http://www.backports.org etch-backports/main Packages *** 0.7.5-2etch1 0 500 http://ftp.fr.debian.org etch/main Packages 500 http://security.debian.org etch/updates/main Packages 100 /var/lib/dpkg/status

[code]cat /etc/fail2ban/fail2ban.conf

Fail2Ban configuration file

Author: Cyril Jaquier

$Revision: 412 $

[Definition]

Option: loglevel

Notes.: Set the log level output.

1 = ERROR

2 = WARN

3 = INFO

4 = DEBUG

Values: NUM Default: 3

loglevel = 3

Option: logtarget

Notes.: Set the log target. This could be a file, SYSLOG, STDERR.

Values: STDERR SYSLOG file Default: /var/log/fail2ban.log

logtarget = /var/log/fail2ban.log

Option: socket

Notes.: Set the socket file. This is used to communication with the

daemon.

Values: FILE Default: /tmp/fail2ban.sock

socket = /tmp/fail2ban.sock
[/code]

[code]cat /etc/fail2ban/jail.conf

Fail2Ban configuration file.

This file was composed for Debian systems from the original one

provided now under /usr/share/doc/fail2ban/examples/jail.conf

for additional examples.

To avoid merges during upgrades DO NOT MODIFY THIS FILE

and rather provide your changes in /etc/fail2ban/jail.local

Author: Yaroslav O. Halchenko debian@onerussian.com

$Revision: 281 $

The DEFAULT allows a global definition of the options. They can be override

in each jail afterwards.

[DEFAULT]

“ignoreip” can be an IP address, a CIDR mask or a DNS host

ignoreip = 127.0.0.1
bantime = 600
maxretry = 3

“backend” specifies the backend used to get files modification. Available

options are “gamin”, “polling” and “auto”.

yoh: For some reason Debian shipped python-gamin didn’t work as expected

This issue left ToDo, so polling is default backend for now

backend = polling

Destination email address used solely for the interpolations in

jail.{conf,local} configuration files.

destemail = root@localhost

Default action to take: ban only

action = iptables[name=%(name)s, port=%(port)s]

Following actions can be chosen as an alternatives to the above action.

To activate, just copy/paste+uncomment chosen 2 (excluding comments) lines

into jail.local

Default action to take: ban & send an e-mail with whois report

to the destemail.

action = iptables[name=%(name)s, port=%(port)s]

mail-whois[name=%(name)s, dest=%(destemail)s]

Default action to take: ban & send an e-mail with whois report

and relevant log lines to the destemail.

action = iptables[name=%(name)s, port=%(port)s]

mail-whois-lines[name=%(name)s, dest=%(destemail)s, logpath=%(logpath)s]

Next jails corresponds to the standard configuration in Fail2ban 0.6

which was shipped in Debian. Please enable any defined here jail by including

[SECTION_NAME]

enabled = true

in /etc/fail2ban/jail.local.

[ssh]

enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3

HTTP servers

[apache]

enabled = true
port = http
filter = apache-auth
logpath = /var/log/apache*/*access.log
maxretry = 3

[apache-noscript]

enabled = true
port = http
filter = apache-noscript
logpath = /var/log/apache*/*error.log
maxretry = 3

FTP servers

[vsftpd]

enabled = true
port = ftp
filter = vsftpd
logpath = /var/log/auth.log
maxretry = 3

[proftpd]

enabled = false
port = ftp
filter = proftpd
logpath = /var/log/proftpd/proftpd.log
maxretry = 3

[wuftpd]

enabled = false
port = ftp
filter = wuftpd
logpath = /var/log/auth.log
maxretry = 3

Mail servers

[postfix]

enabled = true
port = smtp
filter = postfix
logpath = /var/log/postfix.log

[couriersmtp]

enabled = true
port = smtp
filter = couriersmtp
logpath = /var/log/mail.log

[sasl]

enabled = true
port = smtp
filter = sasl
logpath = /var/log/mail.log
[/code]

Remplace la sortie de iptables -L par celle de iptables-save, qui est à la fois plus complète et compacte.

Il y a quand même un truc bizarre dans ta sortie de iptables -L :

fail2ban-ssh tcp -- anywhere anywhere tcp dpt:ssh fail2ban-ssh tcp -- anywhere anywhere tcp dpt:22
Normalement ssh correspond au port 22, or il semble que ce ne soit pas le cas ici. Qu’est-ce qui est défini dans /etc/services ? Et quel port sshd utilise-t-il ?

Dans ce que j’ai trouvé sur le net, les services à monitorer sont stockés dans jail.conf. Il est possible d’en effectuer une copie nommée jail.conf.local qui sera automatiquement utilisée à la place du fichier par défaut. Ce qui me permet de conserver le fichier d’origine.

Par comparaison par rapport à tes fichier, j’ai changé les informations suivante :

[code][ssh]

enabled = true
port = 9229
filter = sshd
logpath = /var/log/auth.log
maxretry = 3[/code]

[quote=“PascalHambourg”]Il y a quand même un truc bizarre dans ta sortie de iptables -L,
Normalement ssh correspond au port 22, or il semble que ce ne soit pas le cas ici. Qu’est-ce qui est défini dans /etc/services ? Et quel port sshd utilise-t-il ?[/quote]

En effet j’ai changé le port par défaut de ssh (j’ai mit le port 9229) et j’ai mit à jour le fichier /etc/services. J’ai commenté les lignes avec la référence au port 22 pour SSH pour mettre celle avec le port 9229.

Je ne crois pas que ce soit une bonne idée de modifier les ports figurant dans /etc/services, car ce sont des ports officiels attribués par l’IANA. De plus, ce n’est pas utile pour modifier le port d’écoute d’un service.

Et concernant la sortie de iptables-save ? La sortie de iptables -L laisse penser que les règles sont correctes et devraient bloquer les connexions SSH depuis l’adresse {MYIP}, mais elle est incomplète (il manque les interfaces d’entrée et de sortie) et ambiguë (les numéros de ports sont traduits en noms de services).

Voici le contenu de la commande iptables-save. J’arrive toujours à me connecter alors qu’a première vue je devrais etre banni !!!

[code]# Generated by iptables-save v1.3.6 on Fri Apr 25 18:33:11 2008
*filter
:INPUT DROP [3624:1682786]
:FORWARD DROP [0:0]
:OUTPUT DROP [0:0]
:SPOOFED - [0:0]
:fail2ban-ssh - [0:0]
-A INPUT -p tcp -m tcp --dport 9229 -j fail2ban-ssh
-A INPUT -p tcp -m tcp --dport 22 -j fail2ban-ssh
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -s 88.191.254.0/255.255.255.0 -p icmp -j ACCEPT
-A INPUT -p tcp -m tcp --dport 4242 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 20:21 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 465 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 993 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 995 -j ACCEPT
-A INPUT -s 88.191.254.0/255.255.255.0 -i eth0 -p tcp -m tcp --dport 161 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -s 88.191.254.0/255.255.255.0 -i eth0 -p udp -m udp --dport 161 -m state --state NEW,ESTABLISHED -j ACCEPT
-A FORWARD -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m limit --limit 1/sec -j ACCEPT
-A FORWARD -p udp -m limit --limit 1/sec -j ACCEPT
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -d 88.191.254.0/255.255.255.0 -p icmp -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 9229 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 53 -j ACCEPT
-A OUTPUT -p udp -m udp --dport 53 -j ACCEPT
-A OUTPUT -p udp -m udp --dport 123 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 20:21 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 25 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 465 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 993 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 995 -j ACCEPT
-A OUTPUT -d 88.191.254.0/255.255.255.0 -o eth0 -p tcp -m tcp --sport 161 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -d 88.191.254.0/255.255.255.0 -o eth0 -p udp -m udp --sport 161 -m state --state ESTABLISHED -j ACCEPT
-A SPOOFED -s 127.0.0.0/255.0.0.0 -j DROP
-A SPOOFED -s 169.240.0.0/255.240.0.0 -j DROP
-A SPOOFED -s 172.16.0.0/255.240.0.0 -j DROP
-A SPOOFED -s 192.168.0.0/255.255.0.0 -j DROP
-A SPOOFED -s 10.0.0.0/255.0.0.0 -j DROP
-A fail2ban-ssh -s 88.X.X.X -j DROP
-A fail2ban-ssh -j RETURN
COMMIT

Completed on Fri Apr 25 18:33:11 2008[/code]

Dans le fichier /var/log/fail2ban.log j’ai bien les lignes :

2008-04-25 18:32:28,858 fail2ban.actions: WARNING [ssh] Ban 88.X.X.X 2008-04-25 18:32:56,869 fail2ban.actions: WARNING [ssh] 88.X.X.X already banned

Edit :
J’ai bien remit le fichier /etc/services comme à l’origine

Je suis d’accord, avec ces règles l’adresse devrait être bannie. Tu es sûr que c’est le bon port ?

Oui, sur à 100% pour le port

Là, je commence à sécher sérieusement. A ce stade, je regarderais l’évolution des compteurs lors des tentatives de connexion SSH

Et si ça ne suffit pas, j’insèrerais des règles LOG avant et après les règles “stratégiques” pour visualiser le chemin des paquets SSH à travers les règles.

Je vient de refaire un test et maintenant ca fonction. Je suis bien bloqué mais je ne comprend pas trop pourquoi. Par contre je post quand même ce que j’obtiens avec ‘iptables-save -c’ car je ne comprend pas d’où vient ce port 4222.

[code](root - 0) iptables-save -c | grep fail2ban-ssh
:fail2ban-ssh - [0:0]
[581:74812] -A INPUT -p tcp -m tcp --dport 9229 -j fail2ban-ssh
[0:0] -A INPUT -p tcp -m tcp --dport 4222 -j fail2ban-ssh
[581:74812] -A fail2ban-ssh -j RETURN

(root - 0) iptables-save -c | grep fail2ban-ssh
:fail2ban-ssh - [0:0]
[651:83856] -A INPUT -p tcp -m tcp --dport 9229 -j fail2ban-ssh
[0:0] -A INPUT -p tcp -m tcp --dport 4222 -j fail2ban-ssh
[11:1740] -A fail2ban-ssh -s {MONIP} -j DROP
[640:82116] -A fail2ban-ssh -j RETURN[/code]

Déjà un grand merci pour votre aide.