Fail2ban security tool

Ottimo tool per la sicurezza.
Come riporta il sito ufficiale :
Fail2ban scans log files like /var/log/pwdfail or /var/log/apache/error_log and bans IP that makes too many password failures.
It updates firewall rules to reject the IP address.
Ovvero controlla alcuni file di log  e secondo alcune regole è in grado di bannare l’IP per un tempo prestabilito.

Cercando di spiegare cosa fa fail2ban
riporto l’ottima descrizione della guida di Debianizzati
Il funzionamento è semplice, il software si occupa di effettuare il parsing di alcuni file di log (nel caso di ssh, /var/log/auth.log) che contengono le informazioni relative agli accessi falliti.
Se vengono contati un numero di tentativi maggiori ad una soglia l’indirizzo IP viene bloccato 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).

Ora parliamo della mia configurazione , dato che da parecchio tempo avevo fail2ban installato , ma ultimamente controllando i log mi sono accorto che gli attacchi che subivo sulla porta 21 FTP non venivano bannati.
Armato di panzienza ho controllato tutto e mi pareva a posto allora ho cominciato fare un po di ricerca sul web.
Dopo la guida di debianizzati ho trovato parecchie informazioni sia sul sito ufficiale che anche su questo articolo.

Io ho preferito usare il file di configurazione di default ovvero /etc/fail2ban/jail.conf invece di /etc/fail2ban/jail.local .

questo che riporto è il mio file jail.conf nelle parti essenziali che ho modificato :

# 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 = 1800 # tempo in secondi 1800/60=30 minuti
maxretry = 3   # numero di tentativi di default

# “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 = mionome@mioserver.it

#
# ACTIONS
#

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

# email action. Since 0.8.1 upstream fail2ban uses sendmail
# MTA for the mailing. Change mta configuration parameter to mail
# if you want to revert to conventional ‘mail’.
mta = sendmail

# Default protocol
protocol = tcp

#
# Action shortcuts. To be used to define action parameter

# The simplest action to take: ban only
action_ = %(banaction)s[name=%(__name__)s, port=”%(port)s”, protocol=”%(protocol)s]

# ban & send an e-mail with whois report to the destemail.
action_mw = %(banaction)s[name=%(__name__)s, port=”%(port)s”, protocol=”%(protocol)s]
%(mta)s-whois[name=%(__name__)s, dest=”%(destemail)s”, protocol=”%(protocol)s]

# ban & send an e-mail with whois report and relevant log lines
# to the destemail.
action_mwl = %(banaction)s[name=%(__name__)s, port=”%(port)s”, protocol=”%(protocol)s]
%(mta)s-whois-lines[name=%(__name__)s, dest=”%(destemail)s”, logpath=%(logpath)s]

# Choose default action. To change, just override value of ‘action’ with the
# interpolation to the chosen action shortcut (e.g. action_mw, action_mwl, etc) in jail.local
# globally (section [DEFAULT]) or per specific section
action = %(action_mwl)s  # io ho scelto la mail di avvertimento con il pezzo di file di log action_mwl

#
# JAILS
#

# Next jails corresponds to the standard configuration in Fail2ban 0.6 which
# was shipped in Debian. Enable any defined here jail by including
#
# [SECTION_NAME]
# enabled = true

#
# in /etc/fail2ban/jail.local.
#
# Optionally you may override any other parameter (e.g. banaction,
# action, port, logpath, etc) in that section within jail.local

[ssh]

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

#
# FTP servers
#

[proftpd]

enabled = true
port = ftp,ftp-data,ftps,ftps-data
filter = proftpd
logpath = /var/log/secure.log
maxretry = 3

Da questo file si può notare che lo uso solo per ssh e FTP
I parametri più importanti sono:
ignoreip = 127.0.0.1 permette di indicare un elenco di ip su cui non verrà fatto il controllo.
Utile per non tagliare fuori accidentalmente l’ufficio
bantime = 600 il tempo, in secondi, per cui un host verrà bannato
maxretry = 3 il numero massimo di tentativi dopo cui effettuare il ban dell’host
destemail = root@localhost l’indirizzo e-mail a cui inviare la mail di notifica
action =%(__name__)s. Quella di default blocca semplicemente l’accesso alla porta del servizio all’ip che ha sforato il numero di tentativi massimo.
enabled = true abilita i controlli all’interno della sezione
port = ssh indica la porta da bloccare
filter = sshd indica il filtro da utilizzare (contenuto all’interno della directory /etc/fail2ban/filter.d )
logpath = /var/log/auth.log il file contenente il log da controllare

Fino a qui tutto bene , ora volendo potete testare fail2ban con il comando:
fail2ban-regex “file di log” “filtro prescelto”
esempio :
fail2ban-regex /var/log/secure.log /etc/fail2ban/filter.d/proftpd.conf
il comando va eseguito con sudo o con i permessi di su.
Riporto ora l’output di queso comando :

barabba@server:~$ fail2ban-regex /var/log/secure.log /etc/fail2ban/filter.d/proftpd.conf

Running tests
=============

Use regex file : /etc/fail2ban/filter.d/proftpd.conf
Use log file : /var/log/secure.log

Results
=======

Failregex
|- Regular expressions:
| [1] (S+[])[: -]+ USER S+: no such user found from S+ [[0-9.]+] to S+:S+s+$
| [2] (S+[])[: -]+ USER S+ (Login failed): Incorrect password.$
| [3] (S+[])[: -]+ SECURITY VIOLATION: S+ login attempted.$
| [4] (S+[])[: -]+ Maximum login attempts (d+) exceeded$
| [5] USER S+: no such user found from S* ?[] to S+s*$
|
`- Number of matches:
[1] 0 match(es)
[2] 6 match(es)
[3] 0 match(es)
[4] 0 match(es)
[5] 28 match(es)

Ignoreregex
|- Regular expressions:
|
`- Number of matches:

Summary
=======

Addresses found:
[1]
[2]
xxx.xxx.xxx.88 (Mon Jun 30 22:09:54 2008)
xxx.xxx.xxx.88 (Mon Jun 30 22:10:15 2008)
xxx.xxx.xxx.88 (Mon Jun 30 22:10:31 2008)
xxx.xxx.xxx.253 (Tue Jul 01 10:43:51 2008)
xxx.xxx.xxx.253 (Tue Jul 01 11:10:58 2008)
xxx.xxx.xxx.253 (Tue Jul 01 11:14:12 2008)
[3]
[4]
[5]
xxx.xxx.xxx.253 (Tue Jul 01 09:26:54 2008)
xxx.xxx.xxx.253 (Tue Jul 01 09:27:09 2008)
xxx.xxx.xxx.253 (Tue Jul 01 09:27:24 2008)
xxx.xxx.xxx.253 (Tue Jul 01 10:43:08 2008)
xxx.xxx.xxx.253 (Tue Jul 01 10:43:22 2008)
xxx.xxx.xxx.253 (Tue Jul 01 11:14:34 2008)
xxx.xxx.xxx.193 (Tue Jul 01 13:02:16 2008)
xxx.xxx.xxx.193 (Tue Jul 01 13:02:47 2008)
xxx.xxx.xxx.193 (Tue Jul 01 13:03:05 2008)
xxx.xxx.xxx.99 (Tue Jul 01 14:19:12 2008)
xxx.xxx.xxx.99 (Tue Jul 01 14:19:14 2008)

Date template hits:
734 hit(s): Month Day Hour:Minute:Second
0 hit(s): Weekday Month Day Hour:Minute:Second Year
0 hit(s): Weekday Month Day Hour:Minute:Second
0 hit(s): Year/Month/Day Hour:Minute:Second
0 hit(s): Day/Month/Year Hour:Minute:Second
0 hit(s): Day/Month/Year:Hour:Minute:Second
0 hit(s): Year-Month-Day Hour:Minute:Second
0 hit(s): Day-Month-Year Hour:Minute:Second[.Millisecond]
0 hit(s): TAI64N
0 hit(s): Epoch

Success, the total number of match is 19

However, look at the above section ‘Running tests’ which could contain important
information.

Come potete vedere ho aggiunto una regola (numero 5 ) al filtro proftpd.conf :
USER S+: no such user found from S* ?[] to S+s*$
adesso con questa riga aggiunta riesce a bannarmi gli IP
che provano a loggarsi con nome utente sbagliato
cosa che prima non avveniva , infatti il test non rilevava
alcun errore dal file di log.

Come potete vadere grazie a questo semplice test potete già
testare se fail2ban è in grado di rilevare possibili errori
dai vostri file di log , altrimenti non vi resta che modificare
i vari filtri in  /etc/fail2ban/filter.d/ oppure cercarne uno che funzioni
e aggingerlo alla lista come ho fatto io per il mio file  /etc/fail2ban/filter.d/proftpd.conf

Per finire vi lascio alla mail che vi arriva in caso un IP venga bannato :

Subject: [Fail2Ban] proftpd: banned 58.252.70.99
From: Fail2Ban

Hi,

The IP 58.252.70.99 has just been banned by Fail2Ban after
3 attempts against proftpd.

Here are more information about 58.252.70.99:

% [whois.apnic.net node-2]
% Whois data copyright terms    http://www.apnic.net/db/dbcopyright.html

inetnum:      58.252.70.0 – 58.252.70.255
netname:      haidatong
country:      CN
descr:        haidatong, Foshan, Guangdong province
admin-c:      CG272-AP
tech-c:       CG272-AP
status:       ASSIGNED NON-PORTABLE
changed:      wangjj238@cnc.cn 20071221
mnt-by:       MAINT-CNCGROUP-GD
source:       APNIC

route:        58.252.0.0/17
descr:        CNC Group CHINA169 Guangdong Province Network
country:      CN
origin:       AS4837
mnt-by:       MAINT-CNCGROUP-RR
changed:      abuse@cnc-noc.net 20080620
source:       APNIC

role:         CNCGROUP GD
nic-hdl:      CG272-AP
e-mail:       abuse-gd@china-netcom.com
address:      5/F,TaiYang Plaza,No 142 Tianyang Rd. Guangzhou 510630,China
phone:        +86-20-22214226
fax-no:       +86-20-38825601
admin-c:      RP181-AP
tech-c:       RP181-AP
country:      CN
changed:      gdipnoc@china-netcom.com 20060424
mnt-by:       MAINT-CNCGROUP-GD
source:       APNIC

Lines containing IP:58.252.70.99 in /var/log/secure.log

Jul 01 14:19:09 kserver proftpd[25566] localhost.localdomain (::ffff:58.252.70.99[::ffff:58.252.70.99]): FTP session opened.
Jul 01 14:19:12 kserver proftpd[25566] localhost.localdomain (::ffff:58.252.70.99[::ffff:58.252.70.99]): no such user ‘Administrator’
Jul 01 14:19:12 kserver proftpd[25566] localhost.localdomain (::ffff:58.252.70.99[::ffff:58.252.70.99]): USER Administrator: no such user found from ::ffff:58.252.70.99 [::ffff:58.252.70.99] to ::ffff:192.168.0.50:21
Jul 01 14:19:14 kserver proftpd[25566] localhost.localdomain (::ffff:58.252.70.99[::ffff:58.252.70.99]): no such user ‘Administrator’
Jul 01 14:19:14 kserver proftpd[25566] localhost.localdomain (::ffff:58.252.70.99[::ffff:58.252.70.99]): USER Administrator: no such user found from ::ffff:58.252.70.99 [::ffff:58.252.70.99] to ::ffff:192.168.0.50:21
Jul 01 14:19:16 kserver proftpd[25566] localhost.localdomain (::ffff:58.252.70.99[::ffff:58.252.70.99]): no such user ‘Administrator’
Jul 01 14:19:16 kserver proftpd[25566] localhost.localdomain (::ffff:58.252.70.99[::ffff:58.252.70.99]): USER Administrator: no such user found from ::ffff:58.252.70.99 [::ffff:58.252.70.99] to ::ffff:192.168.0.50:21
Jul 01 14:19:16 kserver proftpd[25566] localhost.localdomain (::ffff:58.252.70.99[::ffff:58.252.70.99]): Maximum login attempts (3) exceeded, connection refused
Jul 01 14:19:16 kserver proftpd[25566] localhost.localdomain (::ffff:58.252.70.99[::ffff:58.252.70.99]): FTP session closed.

Regards,

Fail2Ban

Rispondi

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.