Fail2Ban for Dovecot


yum install fail2ban  
chkconfig fail2ban on


Now add this to /etc/fail2ban/jail.conf. Change the sender email.

enabled  = true  
filter   = dovecot  
action   = iptables-multiport[name=dovecot, port="pop3,pop3s,imap,imaps", protocol=tcp]  
           sendmail-whois[name=dovecot, dest=root,]  
logpath  = /var/log/maillog  
maxretry = 10  
findtime = 1200  
bantime  = -10

See the manual
for an explanation of the options. In this configuration, anyone
attempting to authenticate unsuccessfully 10 times will be banned

Fail2Ban will try looking for a configuration/filter file called
dovecot.conf” in the filters directory, /etc/fail2ban/filters.d.
Create it and add this if it doesn’t exist for some reason:

failregex = .*(?:pop3-login|imap-login):.*(?:Authentication failure|Aborted login \(auth failed|Aborted login \(tried to use disabled|Disconnected \(auth failed).*rip=(?P<host>\S*),.*  
ignoreregex =


Fail2Ban comes with a handy-dandy regex testing tool.

fail2ban-regex /vaar/log/maillog /etc/fail2ban/filter.d/dovecot.conf

You should issue iptables -L to verify that there’s a Fail2Ban chain.
Now telnet to the POP3 port from another machine and try logging in
with some junk.

Connected to (  
Escape character is '^]'.  
+OK Hello. Please be nice.  
user hahahaha**  
-ERR Plaintext authentication disallowed on non-secure (SSL/TLS) connections.

You’ll see a bunch of these in /var/log/maillog:

Sep 17 15:05:14 user dovecot: pop3-login: Aborted login (tried to use disabled plaintext auth): rip=, lip=

When you see 10 of them, wait a bit. You’ll see an email from Fail2Ban
informing you that it’s blocked an IP. To verify, issue
iptables -L -n. You’ll see this somewhere:

Chain fail2ban-dovecot (1 references)  
target     prot opt source               destination  
DROP       all  --

Nice. To unban, just remove the rule from the chain:

iptables -D fail2ban-dovecot -s -j DROP

Using the Service

service fail2ban start

Check its status

[root@example ~]# service fail2ban status  
Fail2ban (pid 15919) is running...  
|- Number of jail:  1  
`- Jail list:       dovecot 

You should have gotten an email from the service with the subject
[Fail2Ban] dovecot: started”. Check /var/log/messages for
banned IPs