Fail2ban: differenze tra le versioni

Vai alla navigazione Vai alla ricerca
(25 versioni intermedie di 8 utenti non mostrate)
Riga 1: Riga 1:
{{Versioni compatibili|}}
{{Versioni compatibili|Squeeze|Wheezy|Jessie|Stretch}}
== Introduzione ==
== Introduzione ==
'''Fail2ban''' ([http://www.fail2ban.org Sito ufficiale]) è un software che è nato per permettere di bloccare gli host che stanno tentando di effettuare un attacco di [[brute force]] via [[SSH]].  
'''Fail2ban''' ([http://www.fail2ban.org Sito ufficiale]) è un software che è nato per permettere di bloccare gli host che stanno tentando di effettuare un attacco di [[brute force]] via [[SSH]].  
Riga 9: Riga 9:
== Il funzionamento ==
== Il funzionamento ==
Il funzionamento è semplice: il software si occupa di effettuare il parsing di alcuni file di log (nel caso di ssh, <code>/var/log/auth.log</code>) che contengono le informazioni relative agli accessi falliti. Se vengono contati un numero di tentativi maggiori ad una soglia, l'indirizzo IP viene bloccato, attraverso una regola di iptables, per un tempo impostato e non potrà più accedere al servizio in ascolto su quella porta (ma potrà ancora accedere a quelli sulle altre, cosa molto importante nel caso di indirizzi IP condivisi, come avviene per Fastweb o all'interno delle reti aziendali).
Il funzionamento è semplice: il software si occupa di effettuare il parsing di alcuni file di log (nel caso di ssh, <code>/var/log/auth.log</code>) che contengono le informazioni relative agli accessi falliti. Se vengono contati un numero di tentativi maggiori ad una soglia, l'indirizzo IP viene bloccato, attraverso una regola di iptables, per un tempo impostato e non potrà più accedere al servizio in ascolto su quella porta (ma potrà ancora accedere a quelli sulle altre, cosa molto importante nel caso di indirizzi IP condivisi, come avviene per Fastweb o all'interno delle reti aziendali).
{{Warningbox|Quando si dice che l'IP viene bloccato su un determinato servizio, la macchina resta aperta sugli altri.
'''esempio'''  : se fail2ban blocca un attacco su FTP, blocca all'IP l'accesso alla  porta 21 lasciando aperte le altre sessioni possibili come SSH, HTTP o  altre porte aperte alla comunicazione.}}


== Installazione ==
== Installazione ==
Riga 43: Riga 46:
</pre>
</pre>


== Attivazione di nuovi filtri ==
== Attivazione di nuovi filtri (Da Debian Stretch) ==
Fail2ban utilizza una directory apposita per l'attivazione e la gestione di filtri personalizzati. Tutti i file inseriti in questa directory e aventi estensione <tt>.conf</tt> vengono letti e processati all'avvio del demone.
<br/>
Per attivare dei filtri su demoni diversi da SSH è sufficiente quindi creare un nuovo file di configurazione, inserendolo nella directory <tt>/etc/fail2ban/jail.d</tt>:
<pre>
# nano /etc/fail2ban/jail.d/custom.conf
</pre>
Un contenuto per il file, che ci garantisca un buon numero di filtri attivi può essere il seguente:
<pre>
[DEFAULT]
bantime = 14400
ignoreip = 127.0.0.1/8
maxretry = 5
 
[sshd]
enabled = true
port    = 2293
logpath = %(sshd_log)s
backend = %(sshd_backend)s
 
[sshd-ddos]
enabled = true
port    = 2293
logpath = %(sshd_log)s
backend = %(sshd_backend)s
 
[dropbear]
enabled = true
port    = 2293
logpath  = %(dropbear_log)s
backend  = %(dropbear_backend)s
 
[apache-auth]
enabled = true
port    = http,https
logpath  = %(apache_error_log)s
 
[apache-badbots]
enabled = true
port    = http,https
logpath  = %(apache_access_log)s
bantime  = 172800
maxretry = 1
 
[apache-noscript]
enabled = true
port    = http,https
logpath  = %(apache_error_log)s
 
[apache-overflows]
enabled = true
port    = http,https
logpath  = %(apache_error_log)s
maxretry = 2
 
[apache-nohome]
enabled = true
port    = http,https
logpath  = %(apache_error_log)s
maxretry = 2
 
[apache-botsearch]
enabled = true
port    = http,https
logpath  = %(apache_error_log)s
maxretry = 2
 
[apache-fakegooglebot]
port    = http,https
logpath  = %(apache_access_log)s
maxretry = 1
ignorecommand = %(ignorecommands_dir)s/apache-fakegooglebot <ip>
 
[apache-modsecurity]
enabled = true
port    = http,https
logpath  = %(apache_error_log)s
maxretry = 2
 
[apache-shellshock]
enabled = true
port    = http,https
logpath = %(apache_error_log)s
maxretry = 1
 
[php-url-fopen]
enabled = true
port    = http,https
logpath = %(nginx_access_log)s
          %(apache_access_log)s
 
[suhosin]
enabled = true
port    = http,https
logpath = %(suhosin_log)s
 
[roundcube-auth]
[roundcube-auth]
#enabled = true
port    = http,https
logpath  = %(roundcube_errors_log)s
 
[drupal-auth]
enabled = true
port    = http,https
logpath  = %(syslog_daemon)s
backend  = %(syslog_backend)s
 
[monit]
#enabled = true
port = 2812
logpath  = /var/log/monit
 
[webmin-auth]
enabled = true
port    = 10000
logpath = %(syslog_authpriv)s
backend = %(syslog_backend)s
 
[froxlor-auth]
enabled = true
port    = http,https
logpath  = %(syslog_authpriv)s
backend  = %(syslog_backend)s
 
[vsftpd]
enabled = true
port    = ftp,ftp-data,ftps,ftps-data
logpath  = %(vsftpd_log)s
 
[postfix]
#enabled = true
port    = smtp,465,submission
logpath  = %(postfix_log)s
backend  = %(postfix_backend)s
 
[postfix-rbl]
#enabled = true
port    = smtp,465,submission
logpath  = %(postfix_log)s
backend  = %(postfix_backend)s
maxretry = 1
 
[postfix-sasl]
#enabled = true
port    = smtp,465,submission,imap3,imaps,pop3,pop3s
logpath  = %(postfix_log)s
backend  = %(postfix_backend)s
 
[squirrelmail]
#enabled = true
port = smtp,465,submission,imap2,imap3,imaps,pop3,pop3s,http,https,socks
logpath = /var/lib/squirrelmail/prefs/squirrelmail_access_log
 
[recidive]
enabled = true
logpath  = /var/log/fail2ban.log
banaction = %(banaction_allports)s
bantime  = 604800  ; 1 week
findtime = 86400  ; 1 day
 
[pam-generic]
enabled = true
banaction = %(banaction_allports)s
logpath  = %(syslog_authpriv)s
backend  = %(syslog_backend)s
 
[xinetd-fail]
enabled = true
banaction = iptables-multiport-log
logpath  = %(syslog_daemon)s
backend  = %(syslog_backend)s
maxretry  = 2
</pre>
 
== Attivazione di nuovi filtri (Fino a Debian Jessie) ==
=== Server di posta Dovecot ===
=== Server di posta Dovecot ===
Creare il file di filtro <code>/etc/fail2ban/filter.d/dovecot-pop3imap.conf</code>:
Creare il file di filtro <code>/etc/fail2ban/filter.d/dovecot-pop3imap.conf</code>:
Riga 96: Riga 274:
</pre>
</pre>


=== Apache mod_security ===
=== Apache ===
Se avete configurato il modulo di sicurezza di Apache ''mod_security'' potreste voler attivare un filtro anche per lui.<br/>
Aggiungere in <code>/etc/fail2ban/jail.d/jail.local i seguenti filtri:
Create il file di configurazione <code> /etc/fail2ban/filter.d/mod_sec.conf</code>
<pre>
<pre>
# Fail2Ban configuration file for mod_security
##To block failed login attempts use the below jail.
 
[apache]
enabled  = true
port    = http,https
filter  = apache-auth
logpath  = /var/log/apache2/*error.log
maxretry = 3
bantime = 600
##To block the remote host that is trying to request suspicious URLs, use the below jail.
[apache-overflows]
enabled  = true
port    = http,https
filter  = apache-overflows
logpath  = /var/log/apache2/*error.log
maxretry = 3
bantime = 600
##To block the remote host that is trying to search for scripts on the website to execute, use the below jail.
[apache-noscript]
enabled  = true
port    = http,https
filter  = apache-noscript
logpath  = /var/log/apache2/*error.log
maxretry = 3
bantime = 600
##To block the remote host that is trying to request malicious bot, use below jail.
[apache-badbots]
enabled  = true
port    = http,https
filter  = apache-badbots
logpath  = /var/log/apache2/*error.log
maxretry = 3
bantime = 600
##To stop DOS attack from remote host.
[http-get-dos]
enabled = true
port = http,https
filter = http-get-dos
logpath  = /var/log/apache*/access.log
maxretry = 400
findtime = 400
bantime = 600
action = iptables[name=HTTP, port=http, protocol=tcp]
</pre>
Dovremo quindi creare la regola dell'unico filtro mancante nel pacchetto installato dai repository:
<pre>
# nano /etc/fail2ban/filter.d/http-get-dos.conf
</pre>
con questo contenuto:
<pre>
# Fail2Ban configuration file
[Definition]
[Definition]
 
failregex = \[.*?\]\s[\w-]*\s<HOST>\s
# Option: failregex
 
# Note: This regex will match any GET entry in your logs, so basically all valid and not valid entries are a match.
# You should set up in the jail.conf file, the maxretry and findtime carefully in order to avoid false positives.
failregex = ^<HOST> -.*"(GET|POST).*
# Option: ignoreregex
ignoreregex =
ignoreregex =
</pre>
</pre>
Aggiungete questa sezione al file <code>/etc/fail2ban/jail.conf</code>:
Salviamo e riavviamo:
<pre>
<pre>
[mod_sec]
# systemctl restart fail2ban
 
enabled  = true
filter  = mod_sec
action  = iptables-multiport[name=ModSec, port="http,https"]
simple-log[name=modsec]
logpath  = /var/log/httpd/modsec_audit.log
maxretry = 3
</pre>
</pre>


=== Server FTP===
=== Server FTP===
All' interno del file /etc/fail2ban/jail.conf é presente una sezione FTP in cui basta abilitare quello che si desidera.
All'interno del file /etc/fail2ban/jail.conf è presente una sezione FTP in cui basta abilitare quello che si desidera.
<pre>
<pre>
# FTP servers
# FTP servers
Riga 153: Riga 378:
logpath  = /var/log/auth.log
logpath  = /var/log/auth.log
maxretry = 6</pre>
maxretry = 6</pre>
Nel caso di proftpd e wuftpd sono già presenti i filtri in /etc/fail2ban/filter.d
Nel caso di proftpd e wuftpd sono già presenti i filtri in <code>/etc/fail2ban/filter.d</code>


Si può aggiungere al filtro di proftpd questa stringa in caso di ban non effettualti su tentativi di accesso non consentito
Si può aggiungere al filtro di proftpd questa stringa in caso di ban non effettuati su tentativi di accesso non consentito:
<pre>USER S+: no such user found from S* ?[] to S+s*$</pre>
<pre>USER S+: no such user found from S* ?[] to S+s*$</pre>


=== SSH ===
All'interno del file <code>/etc/fail2ban/jail.conf</code> è presente una sezione SSH in cui basta abilitare quello che si desidera.
<pre>
[ssh]
enabled  = true
port    = ssh
filter  = sshd
logpath  = /var/log/auth.log
maxretry = 3
[ssh-ddos]
enabled  = true
port    = ssh
filter  = sshd-ddos
logpath  = /var/log/auth.log
maxretry = 3
</pre>
Per ciò che riguarda la riga:
<pre>
port    = ssh
</pre>
"ssh" va sostituito con la porta effettivamente usata dall'utente. Nel caso si lasci "ssh", il ban avverrà sulla porta di default di SSH (porta 22).
=== Altri filtri consigliati ===
Alcuni filtri utili da inserire in <code>jail.local</code>:
<pre>
[pam-generic]
enabled  = true
banaction = iptables-allports
[apache-nohome]
enabled  = true
port      = http,https
filter    = apache-nohome
banaction = iptables-multiport
action    = %(action_mwl)s
logpath  = /var/log/apache*/*access.log
maxretry  = 1
[php-url-fopen]
enabled  = true
port      = http,https
filter    = php-url-fopen
logpath  = /var/log/apache*/*access.log
maxretry  = 1
</pre>


=== Attivazione dei nuovi filtri ===
=== Attivazione dei nuovi filtri ===
Riga 460: Riga 736:
== Sitografia ==
== Sitografia ==
[http://www.sistemistiindipendenti.org Sistemisti Indipendenti]
[http://www.sistemistiindipendenti.org Sistemisti Indipendenti]
<br/><br/>
 
{{Autori
|Autore = [[User:Maxer|Maxer]]
|Verificata_da=
:[[Utente:porkyhttp|porkyhttp]] 17:04, 06 mag 2012 (CEST)
: lula  12 gen 2017 (CET)
|Numero_revisori=2
|Estesa_da =
: [[Utente : mm-barabba|mm-barabba]] 06-04-2013
}}
[[Categoria:SSH server e amministrazione remota]]
[[Categoria:SSH server e amministrazione remota]]
[[Categoria:Firewall]]
[[Categoria:Firewall]]
[[Categoria:Monitoraggio]]
[[Categoria:Monitoraggio]]

Menu di navigazione