Instalar y configurar fail2ban para Asterisk

Actualizado 12 Septiembre 2009

Fail2ban es un programa que lee los registros (por ejemplo /var/log/asterisk/messages) y si hay un numero determinado de intentos de conexión sin éxito, banea (bloquea) las IP de donde se han originados los intentos interactuando con iptables (cortafuegos).

Fail2ban está escrito en Python y para que funcione tenemos que instalar este lenguaje de programación:

yum install python

luego bajamos el programa:

cd /usr/src

wget http://downloads.sourceforge.net/project/fail2ban/fail2ban-stable/fail2b...

lo descomprimimos

tar -xf fail2ban-0.8.4.tar.bz2

entramos en la carpeta

cd fail2ban-0.8.4

y lo instalamos

python setup.py install

ahora instalamos el script para arrancarlo como un servicio (para Centos)

cp /usr/src/fail2ban-0.8.4/files/redhat-initd /etc/init.d/fail2ban

Ahora podemos configurarlo para que lea los registros de Asterisk

cd /etc/fail2ban/filter.d

nano asterisk.conf

y copiamos estas líneas

# Fail2Ban configuration file
#
#
# $Revision: 250 $
#

[INCLUDES]

# Read common prefixes. If any customizations available -- read them from
# common.local
#before = common.conf

[Definition]

#_daemon = asterisk

# Option:  failregex
# Notes.:  regex to match the password failures messages in the logfile. The
#          host must be matched by a group named "host". The tag "<HOST>" can
#          be used for standard IP/hostname matching and is only an alias for
#          (?:::f{4,6}:)?(?P<host>\S+)
# Values:  TEXT
#

failregex = NOTICE.* .*: Registration from '.*' failed for '<HOST>' - Wrong password
            NOTICE.* .*: Registration from '.*' failed for '<HOST>' - No matching peer found
            NOTICE.* .*: Registration from '.*' failed for '<HOST>' - Username/auth name mismatch
            NOTICE.* <HOST> failed to authenticate as '.*'$
            NOTICE.* .*: No registration for peer '.*' (from <HOST>)
            NOTICE.* .*: Host <HOST> failed MD5 authentication for '.*' (.*)

# Option:  ignoreregex
# Notes.:  regex to ignore. If this regex matches, the line is ignored.
# Values:  TEXT
#
ignoreregex =

con estas líneas le decimos a fail2ban que tiene que controlar eventuales accesos indeseados en el archivo de registro de Asterisk.

Ahora tenemos que modificar el archivo de configuración de fail2ban

cd /etc/fail2ban

nano jail.conf

y añadimos al final del archivo de texto estas líneas

[asterisk-iptables]

enabled  = true
filter   = asterisk
action   = iptables-allports[name=ASTERISK, protocol=all]
           sendmail-whois[name=ASTERISK, dest=root@localhost, sender=fail2ban@pbx.dyndns.org]
logpath  = /var/log/asterisk/messages
maxretry = 5
bantime = 1800

En la línea en negrita aparece el archivo de registro que fail2ban leerá para averiguar intentos frustrados de conexión a nuestra centralita. En la configuración tenemos que modificar la dirección de correo electrónico. En lugar de root@localhost ponemos la dirección de correo electrónico donde queremos recibir los avisos. Para enviar los correos, fail2ban está configurado con Sendmail

Para que funcione tenemos que modificar la configuración del los archivos de registro de Asterisk:

nano /etc/asterisk/logger.conf

y averiguar que la línea messages tenga el parámetro que aparece en negrita

messages => notice,debug

Terminamos con la configuración del script de arranque:

chkconfig fail2ban on

/etc/init.d/fail2ban start

Starting fail2ban:                                         [  OK  ]

Si sale algún error tenemos que revisar la configuración.

Via Nerd Vittles

 

Etiquetas de Technorati: ,

Vota el Articulo: 

Sin votos (todavía)
Evalúa la calidad del articulo

4 comentarios

Añadir linea en el filtro

NOTICE.* .*: Sending fake auth rejection for device .*@\\>;.*

Si añadimos esta linea tambien bloqueamos los intentos cuando enviamos fake auth para que no hagan cracking de extensiones. Eliminad las \ , no me deja poner la linea original al postear por considerarlo un tag html.

Re: Añadir linea en el filtro

Estimado,

gracias por la info. De hecho el articulo es un poco obsoleto.

En la versión de Asterisk 1.8.X el archivo completo sería:

# Fail2Ban configuration file
#
[INCLUDES]
# Read common prefixes. If any customizations available -- read them from
# common.local
#before = common.conf

[Definition]
#_daemon = asterisk

# Option:  failregex
# Notes.:  regex to match the password failures messages in the logfile. The
#          host must be matched by a group named "host". The tag "<HOST>" can
#          be used for standard IP/hostname matching and is only an alias for
#          (?:::f{4,6}:)?(?P<host>\S+)
# Values:  TEXT
#
failregex = NOTICE.* .*: Registration from '.*' failed for '<HOST>(:[0-9]{1,5})?' - Wrong password
            NOTICE.* .*: Registration from '.*' failed for '<HOST>(:[0-9]{1,5})?' - No matching peer found
            NOTICE.* .*: Registration from '.*' failed for '<HOST>(:[0-9]{1,5})?' - Username/auth name mismatch
            NOTICE.* .*: Registration from '.*' failed for '<HOST>(:[0-9]{1,5})?' - Device does not match ACL
            NOTICE.* .*: Registration from '.*' failed for '<HOST>(:[0-9]{1,5})?' - Peer is not supposed to register
            NOTICE.* .*: <HOST> failed to authenticate as '.*'$
            NOTICE.* .*: No registration for peer '.*' \(from <HOST>\)
            NOTICE.* .*: Host <HOST> failed MD5 authentication for '.*' (.*)
            NOTICE.* .*: Failed to authenticate user .*@<HOST>.*
            NOTICE.* .*: Sending fake auth rejection for device .*@<HOST>.*
# Option:  ignoreregex
# Notes.:  regex to ignore. If this regex matches, the line is ignored.
# Values:  TEXT
#
ignoreregex =

que incluye la linea "NOTICE.* .*: Sending fake auth rejection for device .*@<HOST>.*"

Saludos

Suscribirse a Comentarios de "Instalar y configurar fail2ban para Asterisk" Suscribirse a VozToVoice - Todos los comentarios