Configurando Fail2ban Para Autentificaciones SMTP Fallidas en Mailcleaner
Para prevenir que estando nuestro servidor SMTP en internet tengamos bloqueos por ataques DoS al puerto 25 podemos implementar el servicio fail2ban para bloquear todo tipo de conexión que falle en la autentificación de forma reiterativa realizando lo siguiente:
Instalar fail2ban en el servidor Mailcleaner
1
aptgetupdate;aptgetinstallfail2ban
Nos dirigimos al directorio /etc/fail2ban, donde creamos/editamos los siguientes archivos:
Archivo filter.d/exim2.conf: Contiene la reglas para el match del jail
123456789101112131415161718
# Fail2Ban filter for exim## This includes the rejection messages of exim. For spam and filter# related bans use the exim-spam.conf#[INCLUDES]# Read common prefixes. If any customizations available -- read them from# exim-common.local# before = exim-common.conf[Definition]failregex=\[<HOST>\]:535Incorrectauthenticationdataignoreregex=
Archivo action.d/iptables-repeater.conf: Configura toda la acción a realizar con las ip que fallan con la auth
# Fail2ban configuration file## Author: Phil Hagen <phil@identityvector.com>#[Definition]# Option: actionstart# Notes.: command executed once at the start of Fail2Ban.# Values: CMD#actionstart=iptables-Nfail2ban-REPEAT-<name>iptables-Afail2ban-REPEAT-<name>-jRETURNiptables-IINPUT-jfail2ban-REPEAT-<name>#setupfromthestaticfilecat/etc/fail2ban/ip.blocklist.<name>|grep-v^\s*#|awk'{print$1}'|whilereadIP;doiptables-Ifail2ban-REPEAT-<name>1-s$IP-jDROP;done# Option: actionstop# Notes.: command executed once at the end of Fail2Ban# Values: CMD#actionstop=iptables-DINPUT-jfail2ban-REPEAT-<name>iptables-Ffail2ban-REPEAT-<name>iptables-Xfail2ban-REPEAT-<name># Option: actioncheck# Notes.: command executed once before each actionban command# Values: CMD#actioncheck=iptables-n-LINPUT|grep-qfail2ban-REPEAT-<name># Option: actionban# Notes.: command executed when banning an IP. Take care that the# command is executed with Fail2Ban user rights.# Tags: <ip> IP address# <failures> number of failures# <time> unix timestamp of the ban time# Values: CMD#actionban=iptables-Ifail2ban-REPEAT-<name>1-s<ip>-jDROP#alsoputintothestaticfiletore-populateafterarestart!grep-Fq<ip>/etc/fail2ban/ip.blocklist.<name>&&echo"<ip> # fail2ban/$( date '+%%Y-%%m-%%d %%T' ): auto-add for repeat offender">>/etc/fail2ban/ip.blocklist.<name># Option: actionunban# Notes.: command executed when unbanning an IP. Take care that the# command is executed with Fail2Ban user rights.# Tags: <ip> IP address# <failures> number of failures# <time> unix timestamp of the ban time# Values: CMD#actionunban=/bin/true[Init]# Defaut name of the chain#name=REPEAT
Archivo ip.blocklist.exim2: Contiene las direcciones ip que se van bloqueando por intentos fallidos y que se va autocompletando con los bloqueos propios del fail2ban
Lo anterior leerá del archivo /var/mailcleaner/log/exim_stage1/mainlog todos los errores del tipo 535 Incorrect authentication data si la authenticación falla al intento 10 y además la ip será almacenada en el archivo iptables-repeater.conf .