Premier Training & Business Partner Red Hat

FAIL2BAN

Lorenzo Fanni
Ti piacerebbe diventare anche tu uno di noi e
pubblicare i tuoi articoli nel blog degli RHCE italiani?

E’ sufficiente esporre per qualche ora il demone ssh attraverso un indirizzo ip pubblico raggiungibile da internet per accorgersi di come siano frequenti i robot che tentano di effettuare attacchi di brute-force attraverso l’utilizzo di password probabili.

fail2ban

Fail2ban è un software che, attraverso il monitoraggio di alcuni specifici files di log, permette di effettuare precise azioni rispetto agli indirizzi ip che stanno effettuando un numero eccessivo di autenticazioni errate. L’opzione più comune, nonché quella di default, è il ban dell’ip per alcuni minuti.

Analiziamo quindi il procedimenti di installazione e di configurazione di questo servizio su una macchina che sta eseguento CentOS 7.

I pacchetti di installazione sono disponibili tramite l’utilizzo dei repository epel. Eseguiamo quindi l’installazione con il seguente comando:

[root@CentOS]# yum -y install fail2ban-all

Questo comando permette di installare anche tutte le dipendenze necessarie: in questo modo fail2ban sarà in grado di funzionare anche in presenza di firewalld, per esempio.

Al momento della stesura di questo documento è presente un piccolo bug nel pacchetto di installazione: infatti selinux non permette la corretta esecuzione di fail2ban. Risolviamo il problema installado la console di gestione di selinux ed apportando le modifiche necessarie:

[root@CentOS]# yum install setools-console
[root@CentOS]# sesearch -A -s fail2ban_t -t syslogd_var_run_t -c dir

(documentazione: https://bugzilla.redhat.com/show_bug.cgi?id=1139577)

Il file di configurazione che prenderemo in considerazione è /etc/fail2ban/jail.conf

Le impostazioni di default offerte da CentOS 7 pemettono da subito un corretto funzionamento del sistema. Socrrendo il file troviamo una sezione di questo tipo:

[...]
#
# SSH servers
#

[sshd]
port= ssh
logpath = %(sshd_log)s

[sshd-ddos]
# This jail corresponds to the standard configuration in Fail2ban.
# The mail-whois action send a notification e-mail with a whois request
# in the body.
enabled = true
port = ssh
logpath = %(sshd_log)s

[dropbear]
port = ssh
logpath = %(dropbear_log)s
[...]

Come possiamo notare sono predisposte le configurazioni di tutti i principali servizi; possiamo farci un idea della vastità di script pronti leggendo il contenuto della directory /etc/fail2ban/filter.d

[root@CentOS]# ls -l -rw-r--r--. 1 root root 442 15 mar 2014 3proxy.conf
-rw-r--r--. 1 root root 3233 15 mar 2014 apache-auth.conf
-rw-r--r--. 1 root root 2736 15 mar 2014 apache-badbots.conf
-rw-r--r--. 1 root root 1537 15 mar 2014 apache-botsearch.conf
-rw-r--r--. 1 root root 813 15 mar 2014 apache-common.conf
-rw-r--r--. 1 root root 402 15 mar 2014 apache-modsecurity.conf
-rw-r--r--. 1 root root 596 15 mar 2014 apache-nohome.conf
-rw-r--r--. 1 root root 1187 15 mar 2014 apache-noscript.conf
-rw-r--r--. 1 root root 2000 15 mar 2014 apache-overflows.conf
-rw-r--r--. 1 root root 1156 15 mar 2014 assp.conf
-rw-r--r--. 1 root root 2270 15 mar 2014 asterisk.conf
-rw-r--r--. 1 root root 1671 15 mar 2014 common.conf
-rw-r--r--. 1 root root 238 15 mar 2014 counter-strike.conf
-rw-r--r--. 1 root root 393 15 mar 2014 courier-auth.conf
-rw-r--r--. 1 root root 352 15 mar 2014 courier-smtp.conf
-rw-r--r--. 1 root root 418 15 mar 2014 cyrus-imap.conf
-rw-r--r--. 1 root root 1440 15 mar 2014 dovecot.conf
-rw-r--r--. 1 root root 1696 15 mar 2014 dropbear.conf
-rw-r--r--. 1 root root 1282 15 mar 2014 ejabberd-auth.conf
-rw-r--r--. 1 root root 403 15 mar 2014 exim-common.conf
-rw-r--r--. 1 root root 1349 15 mar 2014 exim.conf
-rw-r--r--. 1 root root 2158 15 mar 2014 exim-spam.conf
-rw-r--r--. 1 root root 942 15 mar 2014 freeswitch.conf
-rw-r--r--. 1 root root 223 15 mar 2014 groupoffice.conf
-rw-r--r--. 1 root root 322 15 mar 2014 gssftpd.conf
-rw-r--r--. 1 root root 512 15 mar 2014 guacamole.conf
-rw-r--r--. 1 root root 404 15 mar 2014 horde.conf
-rw-r--r--. 1 root root 466 15 mar 2014 kerio.conf
-rw-r--r--. 1 root root 323 15 mar 2014 lighttpd-auth.conf
-rw-r--r--. 1 root root 886 15 mar 2014 mysqld-auth.conf
-rw-r--r--. 1 root root 400 15 mar 2014 nagios.conf
-rw-r--r--. 1 root root 1579 15 mar 2014 named-refused.conf
-rw-r--r--. 1 root root 422 15 mar 2014 nginx-http-auth.conf
-rw-r--r--. 1 root root 701 15 mar 2014 nsd.conf
-rw-r--r--. 1 root root 495 15 mar 2014 openwebmail.conf
-rw-r--r--. 1 root root 808 15 mar 2014 pam-generic.conf
-rw-r--r--. 1 root root 568 15 mar 2014 perdition.conf
-rw-r--r--. 1 root root 834 15 mar 2014 php-url-fopen.conf
-rw-r--r--. 1 root root 745 15 mar 2014 postfix.conf
-rw-r--r--. 1 root root 312 15 mar 2014 postfix-sasl.conf
-rw-r--r--. 1 root root 1054 15 mar 2014 proftpd.conf
-rw-r--r--. 1 root root 1725 15 mar 2014 pure-ftpd.conf
-rw-r--r--. 1 root root 795 15 mar 2014 qmail.conf
-rw-r--r--. 1 root root 1276 15 mar 2014 recidive.conf
-rw-r--r--. 1 root root 890 15 mar 2014 roundcube-auth.conf
-rw-r--r--. 1 root root 517 15 mar 2014 selinux-common.conf
-rw-r--r--. 1 root root 570 15 mar 2014 selinux-ssh.conf
-rw-r--r--. 1 root root 330 15 mar 2014 sendmail-auth.conf
-rw-r--r--. 1 root root 2424 15 mar 2014 sendmail-reject.conf
-rw-r--r--. 1 root root 371 15 mar 2014 sieve.conf
-rw-r--r--. 1 root root 472 15 mar 2014 sogo-auth.conf
-rw-r--r--. 1 root root 1093 15 mar 2014 solid-pop3d.conf
-rw-r--r--. 1 root root 193 15 mar 2014 squid.conf
-rw-r--r--. 1 root root 185 15 mar 2014 squirrelmail.conf
-rw-r--r--. 1 root root 2779 15 mar 2014 sshd.conf
-rw-r--r--. 1 root root 761 15 mar 2014 sshd-ddos.conf
-rw-r--r--. 1 root root 348 15 mar 2014 stunnel.conf
-rw-r--r--. 1 root root 645 15 mar 2014 suhosin.conf
-rw-r--r--. 1 root root 821 15 mar 2014 tine20.conf
-rw-r--r--. 1 root root 374 15 mar 2014 uwimap-auth.conf
-rw-r--r--. 1 root root 621 15 mar 2014 vsftpd.conf
-rw-r--r--. 1 root root 444 15 mar 2014 webmin-auth.conf
-rw-r--r--. 1 root root 514 15 mar 2014 wuftpd.conf
-rw-r--r--. 1 root root 503 15 mar 2014 xinetd-fail.conf/etc/fail2ban/filter.d/

Possiamo quindi capire che fail2ban ci verrà in aiuto per la maggior parte delle applicazioni di cui avremo bisogno!

Modificando questi files, o creandone di nuovi è possibile modificare le impostazioni di default di fail2ban o creare dei monitor personalizzati per i propri servizi, tuttavia l’ampia scelta offerta da questo software permetterà di trovare una soluzione pronta alle più svariate esigenze!

Per abilitare una di queste opzioni sarà sufficiente aggiungere la dicitura “enabled = true” per ogni servizio che vorremo proteggere.

Ad esempio, per abilitare il monitoraggio del demone ssh modificheremo il file /etc/fail2ban/jail.conf come segue:

[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s

[sshd-ddos]
# This jail corresponds to the standard configuration in Fail2ban.
# The mail-whois action send a notification e-mail with a whois request
# in the body.
enabled = true
port = ssh
logpath = %(sshd_log)s

Testiamo quindi il funzionamento, semplicemente proviamo ad effettuare delle connessioni ssh verso la notra macchine, sbagliando appositamente la password. Dopo qualche tentativo potremo controllare il file /var/log/fail2ban.log per analizzare il comportamento del sistema:

[root@CentOS]# tail -F /var/log/fail2ban.log
2014-12-01 17:44:56,701 fail2ban.server.filter[10817]: INFO [sshd] Found 10.0.0.155
2014-12-01 17:44:59,784 fail2ban.server.filter[10817]: INFO [sshd] Found 10.0.0.155
2014-12-01 17:45:02,698 fail2ban.server.filter[10817]: INFO [sshd] Found 10.0.0.155
2014-12-01 17:45:29,285 fail2ban.server.filter[10817]: INFO [sshd] Found 10.0.0.155
2014-12-01 17:45:31,793 fail2ban.server.filter[10817]: INFO [sshd] Found 10.0.0.155
2014-12-01 17:45:31,872 fail2ban.server.actions[10817]: NOTICE [sshd] Ban 10.0.0.155

Come possiamo notare il nostro ip è stato bannato, infatti non sarà più possibile tentare di effettuare il login.

Le direttive principali per modificare il comportamento di fail2ban sono:

ignoreip – permette di specificare uno o più ip o classi che verranno sempre considerati attendibili da fail2ban-all

bantime – permette di specificare la durata del tempo di ban per un ip

findtime – permette di specificare l’intervall di tempo utile per l’analisi dei log

maxretry – permette di specificare il numero massimo di tentativi di autenticazione errata prima che l’ip venga bannato

Info about author

Lorenzo Fanni