Protegiendo nuestro servidor con fail2ban


fail2ban es una herramienta que nos da una buena mano al momento de proteger nuestros servidores. Trabaja baneando las ips de los atacantes por un determinado periodo de tiempo.

Por ejemplo, supongamos que tenemos un ataque por fuerza bruta a nuestro servicio ssh, esta herramienta, agrega una regla a nuestro firewall iptables, denegando la ip del atacante, en el puerto 22, por un periodo de tiempo configurable.

Instalación:

Necesitaremos tener los repositorios epel en nuestro CentOS / RHEL para poder instalarlo con yum. A partir de ahí es coser y cantar…

# yum install gamin fail2ban

Configuración:

En el /etc/fail2ban, tenemos:

fail2ban.conf: Este es el archivo de configuración general y podemos dejarlo con los valores por defecto.

jail.conf: Este archivo es el que debemos modificar de acuerdo a los servicios que queremos monitorear, o inclusive configurar nuevos servicios. Cada servicio definido entre [ ] tiene una linea filter que indica que cadenas (definidas en el directorio filter.d) buscar en los archivos de log para detectar la intrusión.

Dejo un ejemplo de archivos de configuración para detectar ataques por ssh, http y vpopmail y vsftpd:

jail.conf:

# Fail2Ban configuration file
#
# Author: Cyril Jaquier
#
# $Revision: 747 $
#

# 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. Fail2ban will not
# ban a host which matches an address in this list. Several addresses can be
# defined using space separator.
ignoreip = 127.0.0.1

# "bantime" is the number of seconds that a host is banned.
bantime  = 600

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime  = 600

# "maxretry" is the number of failures before a host get banned.
maxretry = 5 

# "backend" specifies the backend used to get files modification. Available
# options are "gamin", "polling" and "auto". This option can be overridden in
# each jail too (use "gamin" for a jail and "polling" for another).
#
# gamin:   requires Gamin (a file alteration monitor) to be installed. If Gamin
#          is not installed, Fail2ban will use polling.
# polling: uses a polling algorithm which does not require external libraries.
# auto:    will choose Gamin if available and polling otherwise.
backend = gamin

# This jail corresponds to the standard configuration in Fail2ban 0.6.
# The mail-whois action send a notification e-mail with a whois request
# in the body.

[ssh-iptables]

enabled  = true
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
           sendmail-whois[name=SSH, dest=fail2ban@hvivani.com.ar]
logpath  = /var/log/secure
maxretry = 5

# This jail demonstrates the use of wildcards in "logpath".
# Moreover, it is possible to give other files on a new line.

[apache-tcpwrapper]

enabled  = true
filter	 = apache-nohome
action   = hostsdeny
           sendmail-whois[name=APACHE, dest=fail2ban@hvivani.com.ar]
logpath  = /var/log/httpd*/*error.log
#           /home/www/myhomepage/error.log
maxretry = 3

# Same as above but with banning the IP address.

[vsftpd-iptables]

enabled  = true
filter   = vsftpd
action   = iptables[name=VSFTPD, port=ftp, protocol=tcp]
           sendmail-whois[name=VSFTPD, dest=fail2ban@hvivani.com.ar]
logpath  = /var/log/secure
maxretry = 3
bantime = 1800

[vpopmail]
enabled = true
port    = pop3
filter  = vpopmail
action   = iptables[name=VPOPMAIL, port=pop3, protocol=tcp]
           sendmail-whois[name=VPOPMAIL, dest=fail2ban@hvivani.com.ar]
logpath = /var/log/maillog
maxretry = 3
bantime = 1800

Para el vpopmail, vamos a tener que crear un vpopmail.conf en el ./filter.d porque no viene por default. El mio es asi:

# Fail2Ban configuration file for vpopmail
#
# Author: Lawrence Sheed
#
# $Revision: 1.0 $
#

[Definition]

# Option: failregex
# Notes.: regex to match the password failures messages in the logfile.
# Values: TEXT
#
failregex = vchkpw-pop3: vpopmail user not found .*@:<HOST>$

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

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s