Koozali.org: home of the SME Server

Fail2Ban and Dovecot

Offline Peasant

  • *
  • 143
  • +2/-0
Fail2Ban and Dovecot
« on: September 09, 2023, 01:45:45 PM »
I'm getting a lot of attempts against Dovecot on my server, and I thought that fail2ban would stop them. On reading the wiki, it seems that Dovecot is not active out of the box on SME10 unless you use smeserver-dovecot. I followed this link, but it took me to a page in French. Am I missing something or is there a way to add dovecot to fail2ban? Thanks.
Jim

Offline ReetP

  • *
  • 3,740
  • +5/-0
Re: Fail2Ban and Dovecot
« Reply #1 on: September 09, 2023, 02:41:37 PM »
I'm getting a lot of attempts against Dovecot on my server, and I thought that fail2ban would stop them. On reading the wiki, it seems that Dovecot is not active out of the box on SME10 unless you use smeserver-dovecot. I followed this link, but it took me to a page in French. Am I missing something or is there a way to add dovecot to fail2ban? Thanks.

Have you tried these?

Code: [Select]
rpm -qa |grep dovecot
systemctl status dovecot

Looking at this page:

https://wiki.koozali.org/Fail2ban#Services

It looks like it needs amending slightly for the Services/Dovecot piece (open a documentation bug please)

If you search about you will see posts here regarding dovecot and why you see an increase in attempts.

It is regarding replacement to cvm-unix in smeserver-qpsmtpd - see elsewhere for why we had to do this. We now use a qpsmtpd imap plugin for authentication instead. So the attacks are now against dovecot and not cvm-unix.

If you check fail2ban via the scripts on the fail2ban page you can see what is active. eg:

Code: [Select]
fail2ban-client status
Code: [Select]
Status
|- Number of jail: 14
`- Jail list: ftp, http-auth, http-badbots, http-fakegooglebot, http-noscript, http-overflows, http-scan, http-shellshock, imap, pam-generic, qpsmtpd, recidive, ssh, ssh-ddos

Note 'imap'

You can check:

Code: [Select]
cat /etc/fail2ban/jail.conf
Code: [Select]
[imap]
enabled  = true
filter   = dovecot
logpath  = /var/log/dovecot/dovecot.log
action   = smeserver-iptables[port="143,993",protocol=tcp,bantime=1800]
           smeserver-sendmail[name="Dovecot",dest=root]

Check the triggers here:

Code: [Select]
cat /etc/fail2ban/filter.d/dovecot.conf

Code: [Select]
failregex = ^authentication failure; logname=<F-ALT_USER1>\S*</F-ALT_USER1> uid=\S* euid=\S* tty=dovecot ruser=<F-USER>\S*</F-USER> rhost=<HOST>(?:\s+user=<F-ALT_USER>\S*</F-ALT_USER>)?\s*$
            ^(?:Aborted login|Disconnected|Remote closed connection|Client has quit the connection)(?::(?: [^ \(]+)+)? \((?:auth failed, \d+ attempts(?: in \d+ secs)?|tried to use (?:disabled|disallowed) \S+ auth|proxy dest auth failed)\):(?: user=<<F-USER>[^>]*</F-USER>>,)?(?: method=\S+,)? rip=<HOST>(?:[^>]*(?:, session=<\S+>)?)\s*$
            ^pam\(\S+,<HOST>(?:,\S*)?\): pam_authenticate\(\) failed: (?:User not known to the underlying authentication module: \d+ Time\(s\)|Authentication failure \(password mismatch\?\)|Permission denied)\s*$
            ^[a-z\-]{3,15}\(\S*,<HOST>(?:,\S*)?\): (?:unknown user|invalid credentials|Password mismatch)
            <mdre-<mode>>

mdre-aggressive = ^(?:Aborted login|Disconnected|Remote closed connection|Client has quit the connection)(?::(?: [^ \(]+)+)? \((?:no auth attempts|disconnected before auth was ready,|client didn't finish \S+ auth,)(?: (?:in|waited) \d+ secs)?\):(?: user=<[^>]*>,)?(?: method=\S+,)? rip=<HOST>(?:[^>]*(?:, session=<\S+>)?)\s*$


And look at your logs here

Code: [Select]
/var/log/dovecot/dovecot.log
Fail2ban probably does stop some attacks but remember it needs to meet a trigger threshold.

As we have mentioned elsewhere a very good way of cutting this down is to use the xt tables contrib.

That will dramatically reduce the number of attacks you receive.
...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions of software
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation

Offline Peasant

  • *
  • 143
  • +2/-0
Re: Fail2Ban and Dovecot
« Reply #2 on: September 10, 2023, 11:55:55 AM »
Thanks for the detailed reply, much appreciated.

I'll read and digest and have a look. I'll report back (hopefully) with how I get on.

Cheers,
Jim

Offline Jean-Philippe Pialasse

  • *
  • 2,767
  • +11/-0
  • aka Unnilennium
    • http://smeserver.pialasse.com
Re: Fail2Ban and Dovecot
« Reply #3 on: September 11, 2023, 12:49:27 PM »
considering the last update of smeserver-qpsmtpd now auth smtp against imap you might see an increase of auth in imap but with 127.0.0.1 ip.
Those should trigger fail2ban at qpsmtpd level, while adding noise in imap/dovecot log