Bobcares

How to setup Fail2ban to avoid Postfix SASL attack ?

by | Dec 6, 2018

Email spamming can ruin the IP reputation of any server!

But, what can we do to defend against spammers ?

Here, the solution is to block common spamming methods like brute-force password attacks, dictionary attacks, etc.

Fail2ban is a commonly used tool to block brute-force attacks in mail servers like Postfix.

But, configuration errors can cause Fail2ban to let through malicious connections.

That’s why, we help server owners to properly setup Fail2ban as part of our Support Services for Web Hosts.

Today, we’ll see how to setup Fail2ban Postfix SASL configuration and the common failure points.

 

Why to use Fail2ban for Postfix SASL login failure?

Fail2ban is a software that scans log files and bans IP addresses that do malicious activities. Postfix servers often use Simple Authentication and Security Layer (SASL) as a method for user authentication and data security.

Now, when this authentication fails, the log files at /var/log/mailog will have entries like this :

Aug 31 22:23:52 hostxyz postfix/smtpd[38697]: warning: unknown[192.168.xx.xx]: SASL LOGIN authentication failed: authentication failure
Aug 31 22:23:52 hostxyz postfix/smtpd[38697]: lost connection after AUTH from unknown[192.168.xx.xx]

Luckily, Fail2Ban can reduce the rate of such incorrect authentications attempts. For this, we need to configure Fail2Ban to update firewall rules to reject such IP addresses for a specified amount of time.

In addition to this, Fail2ban allows to create email notification too. And, when there is a possible attack, Fail2ban immediately alerts the server owner via email.

 

How to setup Fail2ban Postfix SASL configuration

Now, let’s have a look on how we can configure Fail2ban to block SASL login failed attempts in Postfix.

The basic configuration file of Fail2ban is available at /etc/fail2ban/local.conf. However, to make customization we need to use a local config file called /etc/fail2ban/jail.local

To configure Fail2ban for Postfix SASL, our Support Engineers add the following section in the jail.local file.

[sasl]
enabled  = true
port     = smtp
filter   = postfix-sasl
logpath  = /var/log/mail.log
maxretry = 5

 

Additionally, we need to configure Fail2Ban filter for postfix authentication failures in /etc/fail2ban/filter.d/postfix-sasl.conf.

A working postfix-sasl.conf contains the following details:

# Fail2Ban filter for postfix authentication failures
[INCLUDES]
before = common.conf
[Definition]
_daemon = postfix/smtpd
failregex = ^%(__prefix_line)swarning: [-._\w]+\[<HOST>\]: SASL (?:LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5) authentication failed(: [ A-Za-z0-9+/]*={0,2})?\s*$

 

In control panels like Plesk, we can configure Fail2ban for Postfix from the panel itself.

To add a Jail, click on Tools & Settings >> IP address banning >> Jails >> Add Jail.

The graphical interface will look as shown in the picture.

fail2ban_postfix_sasl

Top failure points in Fail2ban Postfix SASL and fixes

From our experience in managing servers, we often see Fail2ban misbehaving and not blocking the IP addresses. The reasons differ each time.

Now, let’s have a look at these frequent failure points and their fixes.

 

1. IPs not blocked in firewall

In some servers, although Fali2ban triggers the firewall rules, the IP addresses are not blocked. Here, when we check the Iptables rules, we see all rules added properly.

This happens because Fail2ban rules act on the new connections. So, the existing connections are still able to use the Postfix server. That’s why, our Support Engineers set up firewall rules to rate-limit connections to the mail ports. As a result, we can avoid such attack attempts.

Similarly, we tweak the settings like “smtpdclientconnectionratelimit” in Postfix configuration file to rate-limit the connections.

 

2. Wrong time zone

Likewise, there was another incident where Fail2ban ran into problems.

On checking manually, everything looked alright. But, the IP addresses were not blocked again.

We further checked and found that the time zone of the mail log file was different than the server timezone. Thus, Fail2ban was not processing the log file correctly.

In order to fix it, our Support Engineers corrected the timezone of the mail log. And, Fail2ban started to work correctly.

 

3. Customization in wrong file

Again, often Fail2ban Postfix SASL configuration can go wrong, if the server owner make changes in the wrong file.

Recently, in a VPS server, customer reported problems with Fail2ban. On a detailed check, our Support Engineers could not see the modifications in the main file “/etc/fail2ban/local.conf”.

This happened because of a recent update of Fail2ban on the server. As a matter of fact, a fail2ban update will replace all the changes added in “.conf” files.

Here, we replicated all the customization in the /etc/fail2ban/jail.local file and that fixed the problem.

[Running into problems with Fail2ban Postfix SASL configuration? Our Expert engineers can fix it for you.]

Conclusion

Fail2ban comes really handy to avoid incorrect login attempts in Postfix mail servers. It can easily block the fraud IP addresses. Today, we’ve seen the major problems with Fail2ban Postfix SASL configuration and how our Support Engineers fix them.

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.

SEE SERVER ADMIN PLANS

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

8 Comments

  1. Typonaut

    Thanks for this, helped debug a problematic configuration.

    Reply
  2. Typonaut

    You might also want to make the filter case insensitive:

    …SASL (?i:LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5)…

    Although a server will use the forms given, an attacker may use others, ie: ‘Login’, ‘login’, etc.

    Reply
    • Sijin George

      Glad to know that blog was of help. Thank you for the tip on making filter case insensitive.

      Reply
  3. Coenraad

    In the current (July 2020) iteration of Debian, there is a default postfix.conf file shipped that contains SASL failures plus several others, so it seems only the jail file is needed.

    Reply
  4. Coenraad

    In fact, the jail file only needs to contain [postfix-sasl] and enabled=true. grep postfix /etc/fail2ban -R will show more.

    Reply
    • Sijin George

      Hello Coenraad,
      Thank you for your input.

      Reply
  5. Eugene van der Merwe

    Just did a fresh installation of Kopano on a fresh Ubuntu 20.04. Everything is stock, including Fail2ban.

    I had to configure Fail2ban for Kopano Gateway and Z-Push but now I see actually the script hackers are targetting Postfix SASL. Googled how to do it, and found your article.

    This is one of the best articles about Fail2ban I have seen. I love your simple example and then the variation of showing how it’s done in Plesk.

    The part I mostly agree with is this:

    > From our experience in managing servers, we often see Fail2ban misbehaving and not blocking the IP addresses. The reasons differ each time.

    That is so totally true. It’s always something slightly different.

    Thanks so much! A truly inspirational article.

    @Coenraad, a fresh Ubuntu 20.04 stock Fail2ban installation doesn’t have the SASL jail.

    Reply
    • ShuaBlue

      Indeed it does have the SASL jail. And he is correct, all you have to do is enable it.

      Reply

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