Bobcares

Fail2ban Postfix Dovecot: Configuration.

by | Aug 15, 2022

Let’s look at how to use fail2ban with postfix and dovecot in more detail. At Bobcares, we can give you a complete guide on how to do it with our Server Management service.

Fail2ban

fail2ban postfix dovecot

It is an effective tool that searches log files and bans IPs that exhibit malicious behavior such as too many password failures, looking for vulnerabilities, and so on. It can update firewall rules to refuse the IP addresses for a set period of time.

Postfix and dovecot

It is an open-source mail transfer agent (MTA), which is a service for sending and receiving emails. Dovecot is an IMAP/POP3 server that will also handle local delivery and user authentication in our configuration. <divstyle=”height: 20px;”>

Fail2ban Configuration for dovecot (POP/IMAP) and postfix (SMTP):

The example given below utilizes the logs security events to /var/log/secure and mail related events to /var/log/maillog. For Fail2ban; Dovecot for POP3/IMAP and postfix for SMTP are the Daemons in this example. Assuming fail2ban is setup and operational, so iptables.

Firslty, set up and add jail.conf to /etc/fail2ban/jail.conf.

[sasl-iptables] enabled = true
filter = sasl
backend = polling
action = iptables[name=sasl, port=smtp, protocol=tcp] sendmail-whois[name=sasl, dest=admin@ourdomain.com]
logpath = /var/log/maillog
bantime = 36000
maxretry = 2

[dovecot-secure]

enabled = true
filter = dovecot-secure
action = iptables-multiport[name=dovecot, port="smtp,pop3,imap", protocol=tcp] sendmail-whois[name=Dovecot-Secure, dest=admin@ourdomain.com] logpath = /var/log/secure
maxretry = 2
findtime = 600
bantime = 36000
ignoreip = 192.168.0.0/16 127.0.0.1

[dovecot-maillog]

enabled = true

 filter = dovecot-maillog 

action = iptables-multiport[name=dovecot-maillog, port="smtp,pop3,imap", protocol=tcp] sendmail-

whois[name=Dovecot-Maillog, 

dest=admin@ourdomain.com] 

logpath = /var/log/maillog 

maxretry = 2 

findtime = 600

 bantime = 36000

 ignoreip = 192.168.0.0/16 127.0.0.1


To configure up the Fail2ban postfix:

enabled = true
filter = postfix
action = iptables-multiport[name=postfix, port="smtp,pop3,imap", protocol=tcp] sendmail-whois[name=Postfix, dest=admin@ourdomain.com]
logpath = /var/log/maillog
maxretry = 2
findtime = 600
bantime = 36000
ignoreip = 192.168.0.0/16 127.0.0.1

After that, configure up the filter rule files in the faliban2 filter directory ‘/etc/fail2ban/filter.d directory’ for working with postfix and dovecot.

dovecot-maillog.conf.

Use: /usr/bin/fail2ban-regex /var/log/maillog /etc/fail2ban/filter.d/dovecot.conf, to set this up.

failregex = (?: Authentication failure|Aborted login|Disconnected).rip=(?:::f{4,6}:)?(?P\S),. ignoreregex = (?: Disconnected: Logged out).
failregex = pam.dovecot.(?:authentication failure).rhost=(?:::f{4,6}:)?(?P\S)

dovecot-secure.conf

To cofigure use: /usr/bin/fail2ban-regex /var/log/secure /etc/fail2ban/filter.d/dovecot.conf

failregex = (?: authentication failure).rhost=(?:::f{4,6}:)?(?P\S) failregex = pam.dovecot.(?:authentication failure).rhost=(?:::f{4,6}:)?(?P\S)

ignoreregex = dovecot-sasl.conf

failregex: regex to match the password failures messages in the logfile. The host must match a group under the name “host”. The tag “” can be used for standard IP/hostname matching and is only an alias for # (?:::f{4,6}:)?(?P[\w\-.^_]+) Values: TEXT

failregex = (?i): warning: [-._\w]+\[\]: SASL (?:LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5) 

authentication failed(: [A-Za-z0-9+/={0,2})?$ failregex = \[\]: 

SASL login authentication failed failregex = \[\]: 

SASL PLAIN authentication failed: authentication failure failregex = \[\]: SASL LOGIN authentication failed: authentication failure

[Definition]

Option: failregex: Use a regex to match the password failure messages in the log file. After that, a “host” group must match the host. Use the tag “HOST>” for standard IP/hostname matching and is an alias for # (?:::f4,6:)? (?P[\w\-.^ ]+) Values: TEXT.

failregex = reject: RCPT from (.)\[\]: 554 failregex = reject: RCPT from (.)\[\]: 550 5.1.1 reject: RCPT from (.)\[\]: 554 5.7.1

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

Disable Postfix and Dovecot

To disable the postfix and dovecot authentication after three failed attempts, add the following to /etc/fail2ban/jail.local:

[dovecot]
enabled = true
port = pop3,pop3s,imap,imaps
filter = dovecot
logpath = /var/log/mail.log
maxretry = 3

 [postfix] enabled = true port = smtp,ssmtp filter = postfix logpath = /var/log/mail.log maxretry = 3 [sasl]
enabled = true
port = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter = sasl
logpath = /var/log/mail.log
maxretry = 3

Now for the final step of fail2ban postfix dovecot; Fail2ban does not come with a Dovecot configuration, therefore generate /etc/fail2ban/filter.d/dovecot.conf:

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

# /etc/init.d/fail2ban restart

[Need assistance with similar queries? We are here to help]

Conclusion

To conclude, it is easy to set up fail2ban postfix dovecot. And, Fail2ban acts as an effective tool for managing and overviewing a wide range of malicious activities including vulnerability detection for ensuring maximum security.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF