7 Effective Tips For Blocking Email Spam With Postfix SMTP Server
7 Effective Tips For Blocking Email Spam With Postfix SMTP Server
SMTP Server
linuxbabe.com/mail-server/block-email-spam-postfix
Note: If you plan to run your own mail server, I recommend using iRedmail, which
really simplifies the process of setting up a mail server. It also ships with anti-spam
rules. If you prefer to set up a mail server from scratch, then check out my mail server
tutorial series.
Characteristics of Spam
Below is what I found about email spam. These spam are easy to block.
Legitimate email servers should never have these characteristics. So here comes my 7
tips, which will block 90% of spam.
Fact: Around 93%~95% of emails in the world are rejected at the SMTP gateway, never
landed in the inbox or spam folder.
For example, the following command returns the hostname of Google’s mail server.
1/46
host 209.85.217.172
Output:
Due to the prevalence of spam, many mail servers (such as gmx.com, gmx.net,
facebook.com) require that SMTP clients have valid PTR records associated with their
IP addresses. Every mail server admin should set PTR record for their SMTP servers. If
the SMTP client has a PTR record, you can find a line in Postfix log like below.
If the SMTP client doesn’t have a PTR record, then the hostname will be identified as
unknown .
To filter out emails with no PTR records, open Postfix main configuration file.
reject_unknown_reverse_client_hostname
Example:
smtpd_sender_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unknown_reverse_client_hostname
Save and close the file. Then restart Postfix for the change to take effect.
2/46
As you can see, the HELO hostname is ip-172-30-0-149.ec2.internal , which is only
valid in AWS internal network. It has no valid A record nor MX record.
First, add the following line to require the client to provide a HELO/EHLO hostname.
smtpd_helo_required = yes
smtpd_helo_restrictions =
permit_mynetworks
permit_sasl_authenticated
Use the following line to reject clients who provide malformed HELO/EHLO hostname.
reject_invalid_helo_hostname
reject_non_fqdn_helo_hostname
To reject email when the HELO/EHLO hostname has neither DNS A record nor MX
record, use
reject_unknown_helo_hostname
Like this:
smtpd_helo_required = yes
smtpd_helo_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_invalid_helo_hostname
reject_non_fqdn_helo_hostname
reject_unknown_helo_hostname
Note that although most legitimate mail servers have valid A record for the
HELO/EHLO hostname, occasionally a legitimate mail server doesn’t meet this
requirement. You need to whitelist them with check_helo_access .
3/46
smtpd_helo_required = yes
smtpd_helo_restrictions =
permit_mynetworks
permit_sasl_authenticated
check_helo_access hash:/etc/postfix/helo_access
reject_invalid_helo_hostname
reject_non_fqdn_helo_hostname
reject_unknown_helo_hostname
optimus-webapi-prod-2.localdomain OK
va-massmail-02.rakutenmarketing.com OK
It’s likely that you don’t know which hostnames to whitelist, then simply copy the above
two lines, which is the only lines in my helo_access file. You can always add more
hostnames later. Save and close the file. Then run the following command to create the
/etc/postfix/helo_access.db file.
Tip #3: Reject Email if SMTP Client Hostname doesn’t have valid
A Record
A legitimate email server should also have a valid A record for its hostname. The IP
address returned from A record should match the IP address of email server. To filter
out emails from hosts that don’t have valid A record, edit Postfix main configuration file.
reject_unknown_reverse_client_hostname
reject_unknown_client_hostname
Example:
smtpd_sender_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unknown_reverse_client_hostname
reject_unknown_client_hostname
Save and close the file. Then restart Postfix for the change to take effect.
4/46
sudo systemctl restart postfix
Note that reject_unknown_client_hostname does not require HELO from SMTP client.
It will fetch the hostname from PTR record, then check the A record.
To filter out this kind of spam, edit Postfix main configuration file.
Add the following line in smtpd_sender_restrictions . It will reject email if the domain
name of the address supplied with the MAIL FROM command has neither MX record
nor A record.
reject_unknown_sender_domain
Example:
smtpd_sender_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unknown_sender_domain
reject_unknown_reverse_client_hostname
reject_unknown_client_hostname
Save and close the file. Then restart Postfix for the change to take effect.
Note that I placed this restriction above other reject restrictions. From my experience,
if it is below other reject restrictions, it won’t work. (Maybe this only happens on my
email server.)
5/46
Postgrey is a greylisting policy server for Postfix. Debian and Ubuntu users can install
postgrey from the default repository.
On Debian and Ubuntu, it listens on TCP port 10023 on localhost (both IPv4 and IPv6).
Next, we need to edit Postfix main configuration file to make it use the greylisting policy
server.
check_policy_service inet:127.0.0.1:10023
If you use CentOS/RHEL, you need to add the following line instead.
check_policy_service unix:/var/spool/postfix/postgrey/socket
From now on, Postgrey will reject an email if the sender triplet (sender IP address,
sender email address, recipient email address) is new. The following log message in
/var/log/mail.log shows a new sender triplet. The action “ greylist ” means this email
message was rejected.
From my experience, Chinese email spammers like to use a fake, weird-looking and
randomly generated sender address for every email, so adding these fake email
addresses to blacklist won’t stop them. On the other hand, they never try re-sending a
rejected email with the same sender address, which means greylisting can be very
effective at stopping this kind of spam.
The problem is that postgrey is not running. You need to specify 127.0.0.1 as the
listening address in /etc/default/postgrey file. So change the following line
POSTGREY_OPTS="--inet=10023"
to
POSTGREY_OPTS="--inet=127.0.0.1:10023"
7/46
Whitelist
Postgrey ships with two whitelist files ( /etc/postgrey/whitelist_clients and
/etc/postgrey/whitelist_recipients ). The former contains a list of hostnames and the
latter contains a list of recipient addresses.
By default, Google’s mail servers are whitelisted. No matter the sender is using a
@gmail.com address or other address, as long as the sender is using Google’s mail
server, Postgrey won’t reject the email. The following line in my /var/log/mail.log file
shows this.
Note: You can also see postgrey logs with this command sudo journalctl -u postgrey .
You can get these hostnames with a tool called pflogsumm , which I will discuss later in
this article. Save and close the file, the restart Postgrey.
MX @ mail.yourdomain.com 0
MX @ mail2.yourdomain.com 5
The sender will try the first mail server (with priority 0). If mail.yourdomain.com rejects
email by greylisting, then the sender would immediately try the second mail server (with
priority 5).
If the two mail server hostnames have the same IP address, then when the sender tries
the second mail server hostname, the email will be accepted immediately (if all other
checks pass) and end users will not notice email delay caused by greylisting.
Note that this requires you to set a very small delay time like 1 second in
/etc/default/postgrey (Debian & Ubuntu) or /etc/sysconfig/postgrey
(CentOS/RHEL). The delay time tells the SMTP client how many seconds to wait before
sending again. If the delay time is not small enough, then the second email delivery
would still be rejected.
Debian/Ubuntu
POSTGREY_OPTS="--inet=127.0.0.1:10023 --delay=1"
CentOS/RHEL
8/46
OSTGREY_DELAY="--delay=1"
Restart Postgrey.
Also beware that not all mail servers would immediately try the second MX host.
dbl.spamhaus.org
zen.spamhaus.org
multi.uribl.com
ivmURI
InvaluementURI
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
check_policy_service unix:private/policyd-spf,
check_policy_service inet:127.0.0.1:10023,
reject_rhsbl_helo dbl.spamhaus.org,
reject_rhsbl_reverse_client dbl.spamhaus.org,
reject_rhsbl_sender dbl.spamhaus.org,
reject_rbl_client zen.spamhaus.org
Where:
rhs stands for right hand side, i.e, the domain name.
reject_rhsbl_helo makes Postfix reject email when the client HELO or EHLO
hostname is blacklisted.
9/46
reject_rhsbl_reverse_client : reject the email when the unverified reverse client
hostname is blacklisted. Postfix will fetch the client hostname from PTR record. If
the hostname is blacklisted, reject the email.
reject_rhsbl_sender makes Postfix reject email when the MAIL FROM domain is
blacklisted.
reject_rbl_client : This is an IP-based blacklist. When the client IP address is
backlisted, reject the email.
Some spammers use Google’s mail server, so reject_rhsbl_helo is ineffective, but most
of them use their own domain names in the MAIL FROM header, so
reject_rhsbl_sender will be effective.
Create A Whitelist
Sometimes there are legitimate email servers blacklisted. You can create a whitelist so
they won’t be blocked. Create the following file.
Save and close the file. Then run the following command to create the rbl_override.db
file.
check_client_access hash:/etc/postfix/rbl_override,
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
check_policy_service unix:private/policyd-spf,
check_policy_service inet:127.0.0.1:10023,
check_client_access hash:/etc/postfix/rbl_override,
reject_rhsbl_helo dbl.spamhaus.org,
reject_rhsbl_reverse_client dbl.spamhaus.org,
reject_rhsbl_sender dbl.spamhaus.org,
reject_rbl_client zen.spamhaus.org
permit_dnswl_client list.dnswl.org=127.0.[0..255].[1..3],
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
check_policy_service unix:private/policyd-spf,
check_policy_service inet:127.0.0.1:10023,
check_client_access hash:/etc/postfix/rbl_override,
reject_rhsbl_helo dbl.spamhaus.org,
reject_rhsbl_reverse_client dbl.spamhaus.org,
reject_rhsbl_sender dbl.spamhaus.org,
permit_dnswl_client list.dnswl.org=127.0.[0..255].[1..3],
reject_rbl_client zen.spamhaus.org
permit_dnswl_client swl.spamhaus.org,
It’s impossible for an IP address to be listed in Spamhaus whitelist and blacklist at the
same time, so if you only use Spamhaus blacklist in Postfix, then it’s not necessary to
check against Spamhaus whitelist.
11/46
You might be wondering why there is no comma in the first two configuration snippets.
Well, you can separate values in Postfix configuration file with space, carriage return or
comma. If you add comma to one parameter ( smptd_recipient_restrictions as in the
above screenshot), then make sure all remaining values are separated with comma.
Use the following command to generate a report for today. (Note that on
CentOS/RHEL, the mail log file is /var/log/maillog .)
To emit “problem” reports (bounces, defers, warnings, rejects) before “normal” stats,
use --problems-first flag.
To append the email from address to each listing in the reject report, use --rej-add-
from flag.
You can add a cron job to make pflogsumm to send a report to your email address every
day.
sudo crontab -e
Add the following line, which will generate a report every day at 4:00 AM.
To receive the report via email, add the following line above all cron jobs.
MAILTO="your-email-address"
You should pay attention to the message reject detail section, where you can see for
what reason those emails are rejected and if there’s any false positives. Greylisting
rejections are safe to ignore.
If the MAILTO variable has already been set but you want Postfix log summary sent to a
different email address, you can put the following line in your Cron job.
13/46
The output of pflogsumm command is redirected to mutt , a command line mail user
agent, which will use the output as the email body and send it to the email address you
specify at the end. Of course, you need to install mutt on your Linux server.
or
This line tells Postfix to forward email only from clients in trusted networks, from
clients that have authenticated with SASL, or to domains that are configured as
authorized relay destinations. It should be already in the main configuration file after
you install Postfix.
14/46
Dec 14 09:58:37 email postfix/smtpd[22095]: connect from unknown[117.86.35.119]
Dec 14 09:58:37 email postfix/smtpd[22119]: lost connection after AUTH from
unknown[114.232.141.99]
Dec 14 09:58:37 email postfix/smtpd[22119]: disconnect from unknown[114.232.141.99]
ehlo=1 auth=0/1 commands=1/2
Dec 14 09:58:37 email postfix/smtpd[22119]: connect from unknown[180.120.191.91]
Dec 14 09:58:38 email postfix/smtpd[22095]: lost connection after AUTH from
unknown[117.86.35.119]
Dec 14 09:58:38 email postfix/smtpd[22095]: disconnect from unknown[117.86.35.119]
ehlo=1 auth=0/1 commands=1/2
Dec 14 09:58:38 email postfix/smtpd[22119]: lost connection after AUTH from
unknown[180.120.191.91]
Dec 14 09:58:38 email postfix/smtpd[22119]: disconnect from unknown[180.120.191.91]
ehlo=1 auth=0/1 commands=1/2
Dec 14 09:58:38 email postfix/smtpd[22095]: connect from unknown[49.67.68.34]
Dec 14 09:58:39 email postfix/smtpd[22106]: lost connection after AUTH from
unknown[180.120.192.199]
Dec 14 09:58:39 email postfix/smtpd[22106]: disconnect from unknown[180.120.192.199]
ehlo=1 auth=0/1 commands=1/2
Dec 14 09:58:39 email postfix/smtpd[22095]: lost connection after AUTH from
unknown[49.67.68.34]
Dec 14 09:58:39 email postfix/smtpd[22095]: disconnect from unknown[49.67.68.34] ehlo=1
auth=0/1 commands=1/2
Dec 14 09:58:39 email postfix/smtpd[22119]: connect from unknown[121.226.62.16]
Dec 14 09:58:39 email postfix/smtpd[22119]: lost connection after AUTH from
unknown[121.226.62.16]
Dec 14 09:58:39 email postfix/smtpd[22119]: disconnect from unknown[121.226.62.16]
ehlo=1 auth=0/1 commands=1/2
Dec 14 09:58:39 email postfix/smtpd[22106]: connect from unknown[58.221.55.21]
Dec 14 09:58:40 email postfix/smtpd[22106]: lost connection after AUTH from
unknown[58.221.55.21]
Dec 14 09:58:40 email postfix/smtpd[22106]: disconnect from unknown[58.221.55.21] ehlo=1
auth=0/1 commands=1/2
Dec 14 09:58:47 email postfix/smtpd[22095]: connect from unknown[121.232.65.223]
Dec 14 09:58:47 email postfix/smtpd[22095]: lost connection after AUTH from
unknown[121.232.65.223]
Dec 14 09:58:47 email postfix/smtpd[22095]: disconnect from unknown[121.232.65.223]
ehlo=1 auth=0/1 commands=1/2
Postfix is designed to run even under stressful conditions. It uses a limited amount of
memory, so such attacks are much less effective. However, I don’t want them to appear
in my mail log and we should save smtpd processes for legitimiate SMTP clients,
instead of wasting time dealing with spambots. To stop this kind of flood attack, you can
use fail2ban, which is a set of server and client programs to limit brute force
authentication attempts. Install fail2ban from default Ubuntu repository.
15/46
The fail2ban-server program included in fail2ban monitors log files and issues
ban/unban command. By default, it would ban a client’s IP address for 10 minutes if the
client failed authentication 5 times. The ban is done by adding iptables firewall rules.
You can check iptables rules by running the following command.
sudo iptables -L
To enable fail2ban on Postifx SMTP AUTH attack, add the following lines in
/etc/fail2ban/jail.local file. If the file doesn’t exist, then create this file.
[postfix-flood-attack]
enabled = true
bantime = 10m
filter = postfix-flood-attack
action = iptables-multiport[name=postfix,
port="http,https,smtp,submission,pop3,pop3s,imap,imaps,sieve", protocol=tcp]
logpath = /var/log/mail.log
You can change the bantime to something like 30m or 12h to ban the bad actor for
longer time. If you would like to whitelist your own IP address, add the following line to
tell fail2ban to ignore your IP address. Replace 12.34.56.78 with your own IP address.
Multiple IP addresses are separated by spaces.
By default, the allowed max number of failure it 5 times. After 5 failures, the client will
be banned. To specify a customized number of failures, add the following line. Change
the number to your liking.
maxretry = 4
Save and close the file. Then create the filter rule file.
In this file, we specify that if the “lost connection after AUTH from” is found, then ban
that IP address.
[Definition]
failregex = lost connection after AUTH from (.*)\[<HOST>\]
ignoreregex =
Save and close the file. Restart fail2ban the changes to take effect.
In the fail2ban log file ( /var/log/fail2ban.log ), I can find the message like below, which
indicates the IP address 114.223.221.55 has been banned because it failed
authentication 5 times.
16/46
2018-12-14 09:52:15,598 fail2ban.filter [21897]: INFO [postfix-flood-attack] Found
114.223.211.55 - 2018-12-14 09:52:15
2018-12-14 09:52:16,485 fail2ban.filter [21897]: INFO [postfix-flood-attack] Found
114.223.211.55 - 2018-12-14 09:52:16
2018-12-14 09:52:20,864 fail2ban.filter [21897]: INFO [postfix-flood-attack] Found
114.223.211.55 - 2018-12-14 09:52:20
2018-12-14 09:52:21,601 fail2ban.filter [21897]: INFO [postfix-flood-attack] Found
114.223.211.55 - 2018-12-14 09:52:21
2018-12-14 09:52:22,102 fail2ban.filter [21897]: INFO [postfix-flood-attack] Found
114.223.211.55 - 2018-12-14 09:52:22
2018-12-14 09:52:22,544 fail2ban.actions [21897]: NOTICE [postfix-flood-attack] Ban
114.223.211.55
sudo iptables -L
Output:
This indicates fail2ban has set up a iptables rule that reject connection from
195.140.231.114.broad.nt.js.dynamic.163data.com.cn , which is a hostname is used by
the spammer.
If you would like to manually block an IP address, run the following command. Replace
12.34.56.78 with the IP address you want to block.
17/46
504 5.5.2 : Helo command rejected: need fully-qualified hostname; from=
<[email protected]> to=<[email protected]> proto=ESMTP helo= (total: 1)
1 185.191.228.36
504 5.5.2 : Helo command rejected: need fully-qualified hostname; from=
<[email protected]> to=<[email protected]> proto=ESMTP helo= (total: 1)
1 185.191.228.36
504 5.5.2 : Helo command rejected: need fully-qualified hostname; from=
<[email protected]> to=<[email protected]>
proto=ESMTP helo= (total: 1)
1 185.191.228.36
504 5.5.2 : Helo command rejected: need fully-qualified hostname; from=
<[email protected]> to=<[email protected]> proto=ESMTP helo= (total:
1)
1 185.191.228.36
504 5.5.2 : Helo command rejected: need fully-qualified hostname; from=
<[email protected]> to=<[email protected]> proto=ESMTP helo= (total: 1)
1 185.191.228.36
504 5.5.2 : Helo command rejected: need fully-qualified hostname; from=
<[email protected]> to=<[email protected]> proto=ESMTP helo= (total: 1)
This spammer continues sending spam, ignoring the Postfix reject message: Helo
command rejected: need fully-qualified hostname . To stop this kind of behavior, we can
also use Fail2ban by adding the following lines in /etc/fail2ban/jail.local file.
[postfix]
enabled = true
maxretry = 3
bantime = 1h
filter = postfix
logpath = /var/log/mail.log
The [postfix] jail will use the builtin filter shipped with Fail2ban
( /etc/fail2ban/filter.d/postfix.conf ). Save and close the file. Then restart Fail2ban.
Now the spammer will have to wait 1 hour before pounding your mail server again.
Sometimes I can see the following lines in /var/log/mail.log file, which indicates that
this sender declare its hostname first. This spammer just want to pound my mail server
with endless connections, but has no intent to send any email. And the EHLO hostname
ylmf-pc is a clear indication that these connections are originated from compromised
home computers. ( ylmf is an acronym for the defunct Chinese Linux distro: 雨林木风.)
18/46
PREGREET 14 after 0.22 from [121.226.63.86]:64689: EHLO ylmf-pc\r\n
PREGREET 14 after 0.24 from [121.232.8.131]:55705: EHLO ylmf-pc\r\n
PREGREET 14 after 0.24 from [114.232.9.57]:62783: EHLO ylmf-pc\r\n
iRedMail ships with a fail2ban rule to filter this kind of malicious activities. You can see
the following line in /etc/fail2ban/filter.d/postfix.iredmail.conf file.
But I think the default bantime (1 hour) for this filter to too low. Open the
/etc/fail2ban/jail.local file and add a custom bantime parameter like below.
[postfix-iredmail]
enabled = true
max-retry = 1
bantime = 24h
filter = postfix.iredmail
logpath = /var/log/mail.log
I set the bantime value to 24 hours because the sender is clearly using compromised
home computers. Save and close the file. Restart fail2ban the changes to take effect.
And most DNS blacklists have query limit. Running your own local DNS resolver to
cache DNS records can help you stay under the query limit.
Next Step
I hope these 7 Postfix anti spam measures helped you block email spam. You may also
want to deploy a content filter such as SpamAssassin to better detect spam.
As always, if you found this post useful, then subscribe to our free newsletter to get
more tips and tricks. Take care
Run Your Own Email Server on CentOS 8/RHEL 8 - Postfix SMTP Server
19/46
7 Effective Tips for Blocking Email Spam with Postfix on CentOS/RHEL
Enable SMTPS Port 465 in Postfix SMTP Server For Email Submission
How to Set up a Backup Email Server with Postfix on Ubuntu (Complete Guide)
Vladimír
2 years ago
Reply
Nice! Thanks for ideas.
Joshua Campbell
2 years ago
Reply
Really helpful. I currently pay for web hosting and a mail server but my 3 year
contract is ending soon and have been researching methods on hosting this on an
easy to deploy Vultr instance. Thanks for the spam filter information!!
20/46
Mark
2 years ago
Reply
For several days I trying to set up and secure mail server. It’s probably okay, but I
still have questions.
1. GreyList.
In your “How to Easily Set Up a Full-Featured Mail Server on Ubuntu 18.04 with
iRedMail” you wrote: “By default, iRedMail has enabled greylisting”. What is
better: default iRedMail greylist or Postgrey? If I install Postgrey, should I disable
default iRedMail greylist?
2. Fail2ban.
In my version of iRedMail in filter.d/postfix.iredmail.conf file I have something
like similar line:
Is this the same rule or I still should add your [postfix-auth] to my jail.local?
And in my jail.local is only one jail [DEFAULT] – is this correct?
I would use Postgrey and create my custom Fail2ban jails if I set up mail
server from scratch.
21/46
deibis
1 year ago
Reply
Excelente post compa gracias por el aporte.
Aunque actualemtne tengo el siguiente problema:
alguna idea de como bloquear esas IP a traves del csf de firewall de centos
kde35
1 year ago
Reply
Hi, this is nice, but my postfix server accepts mails from local network
(192.168.1.xxx) as well as localhost,
so if I follow one of your first four tips, then it will stop sending mails from local
network, am I right?
22/46
webrunner
1 year ago
Reply
fail2ban-client status
cat /var/log/auth.log | grep ‘Failed password’ – Debian
cat /var/log/secure | grep ‘Failed password’ – Centos
webrunner
1 year ago
Reply
check_policy_service unix:private/policyd-spf – this blocks gmail and other
external mailservers, even if maillog show, that gmail is whitelisted.
In practice, SPF is more useful when you combine it with DKIM to enforce
DMARC record checking to stop spammers impersonating other person’s
domain name.
Sunil Kumar
9 months ago
Reply
What if I am using CSF ?
23/46
Breen
1 year ago
Reply
I sent email from “http://www.anonymailer.net” and I got this response in maillog
postfix/smtpd[15785]: NOQUEUE: reject: RCPT from unknown[10.230.220.57]:
450 4.7.1 Client host rejected: cannot find your reverse hostname,
[10.230.220.57]; from= to= proto=ESMTP helo=
Then, I sent email from my gmail account, I get the same (which is not expected)
postfix/smtpd[15785]: NOQUEUE: reject: RCPT from unknown[10.230.220.56]:
450 4.7.1 Client host rejected: cannot find your reverse hostname,
[10.230.220.56]; from=" to= proto=ESMTP helo="
smtpd_helo_required = yes
smtpd_helo_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_invalid_helo_hostname,
reject_non_fqdn_helo_hostname,
reject_unknown_helo_hostname
smtpd_sender_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_unknown_sender_domain,
reject_unknown_reverse_client_hostname,
reject_unknown_client_hostname
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination,
reject_rhsbl_helo dbl.spamhaus.org,
reject_rhsbl_reverse_client dbl.spamhaus.org,
reject_rhsbl_sender dbl.spamhaus.org,
reject_rbl_client zen.spamhaus.org
24/46
Jack
1 year ago
Reply
Hi, thanks for your post. Let me say my IMHO:
Guys, don’t use greylisting in your postfix config. Because of different
implementation many mail servers can attempt second connection through very
long time instead of your server advised. Your mail can be delayed for a long time,
it’s not good idea for 21 century
Alan
1 year ago
Reply
These seem quite sensible. When I implemented the HELO checks, however, I
started to get emails to my local Postmanster account with subject lines like
I assume these are notifying me of rejected messages. How can I suppress these
notification emails?
25/46
Nikolay
1 year ago
Reply
for what version of postfix is this?
I find lots of options different from my own. I use bit old version –
mail_version = 2.10.1
There are several options that are different in Postfix 2.x. You can find the
equivalent in Postfix documentation.
26/46
Pauli
12 months ago
Reply
I made it a bit different.
When in the list is more then 20 addresses in one range then I block the entire
range eg. 176.221.42.0/24.
smtpd_recipient_restrictions =
check_client_access cidr:/root/firewall/postfix_blocklist_de.list
I made some whois checking that I do not block IPs registered to well known
brands/partners and countries.
I also made whitelisting feature. Since I made this, we eliminated 99.99% of spam.
Of course I filtered from log all blocked addresses to check if there is some false
blocking and in two months is no regular email blocked.
Sorry for the English, but certainly you have a clue what have I done
Peet Verstraten
10 months ago
Reply
These tips helped me perfectly to block spammers and SMTP AUTH requests from
our Zentyal email server. Thanks a lot !
27/46
Fernando
9 months ago
Reply
Hello,
How can that be done? What happened here? Did the get the root password?
Thanks
Sunil Kumar
9 months ago
Reply
I too looking for same.
28/46
Matteo
8 months ago
Reply
Thanks for your advice, they helped me a lot!
Just a question: pflogsumm configured as you say it sends me a summary email
but graylisting warning lines are also included, can they be omitted in some way?
Matteo
8 months ago
Reply
Yes! I’ve used the command
to remove the lines containing the word “Greylisted” and the line
directly after it.
thanks!
Philips
7 months ago
Reply
For the past three years, I am running an email server but receiving a lot of spam.
With the help of the technical team, I have learned to block email spam with the
Postfix SMTP server. Meanwhile, I have signed up with EasySendy and Mailgun
for delivering email newsletters and leveraging sales process.
29/46
Mike Johnson
7 months ago
Reply
Hi,
How can postfix be configured to forward email for multiple domains while using
TLS terminated behind a F5? Issues we are running into are ehlo banner
hostname and TLS cert designation. DO I need a SAN certificate? Does actual
hostname of the mailserver need to align with SAN entry? confused?
Mike
Jesper R
7 months ago
Reply
I think you fail to properly explain the repercussions that enabling
reject_unknown_reverse_client_hostname and
reject_unknown_client_hostname can have. First of all I think it is important to
make it clear that setting these effectively makes sure that your mail server will not
accept mails from people running their own mail servers on home internet
connections where the PTR is either rarely set or can not be changed by the
customer.
I also think that it is worth mentioning that if the mail server fails for resolve the
PTR due to a DNS error, then it leads to delays in mail delivery because reject a
mail with 450 means that the sending mail server will have to retry at a late time.
Lastly I think the assumption “Their IP addresses don’t have PTR records.” is just
wrong, and not only spammers have missing PTR records, and enabling these two
setting should be discouraged as they inevitably will lead to false positives and lost
or delayed mail. You really shouldn’t recommend these kinds of over
approximations but instead push for interoperability.
30/46
Xiao Guoan (Admin)
7 months ago
Reply
First of all, you shouldn’t run a mail server with a home IP address, which
often has bad IP reputation (dynamic IP) and doesn’t have PTR record. If
you really want to run a mail server at home, you should set up SMTP relay.
There are many SMTP relay services that offer free quotas, which is enough
for people who run mail server at home.
SMTP relay services have PTR records for their servers. So if you don’t have
PTR record, use SMTP relay service.
Facebook rejects any IP address that doesn’t have PTR record. Convince
Facebook before trying to convince me not to use PTR restriction.
Gmail rejects emails sent from IPv6 that doesn’t have PTR record.
SpamRats have a blacklist for IP addresses that don’t have PTR records.
The SMTP protocol (RFC 2821) recommends that sending SMTP server
should retry failed email delivery at least 4-5 days. It’s the case for Postfix
SMTP server. And you can configure your mail server to use multiple DNS
servers to prevent DNS failure. That’s not hard to do. It’s common for mail
servers to query DNS-based blacklist (DNSBL), also known as realtime
blacklist (RBL), such as Spamhaus, to reject connections from spammers.
You can’t even send emails when your DNS doesn’t work properly. If you
worry about DNS failure, that indicates you are not qualified to run your own
email server.
31/46
Chris
7 months ago
Reply
But there is another problem with the reject_unknown_reverse_client_hostname
IP heck. IIUIC, this would reject any mail server which is on a local LAN behind a
firewall but with valid PTR for the Public IP address NAT’s to the server – even if
it has full DMARC and DKIM compliance.
Given the shortage of IPv4 public IP addresses, this is a real issue and until IPv6
becomes common, likely to be an increasing one.
Chris
7 months ago
Reply
I recently did a check using an online mail deliverability tool and spf reported the
delivery from the lan to my ‘public’ server on the lan (which has a local IP address
and not the public address) IIRC. However, I guess it could have been the mail
client not the server that was being reported – which would make sense.
I had always assumed that NATing would work the way you suggested, but that
report made me wonder.
7 months ago
Reply
No, a single mx server. The report even detailed internal passage to and from the
virus scanner, or it may have been another component … I can’t recall.
It showed a lan address -> lan address -> 127.0.0.1 -> public ip routing.
lan address -> lan address -> 127.0.0.1 -> public ip routing.
Such mail server has serious internal design flaw, but it’s not related to PTR
check (reject_unknown_reverse_client_hostname). PTR check happens in
the SMTP dialog phase and it doesn’t scan email headers.
33/46
chris
7 months ago
Reply
It is a plain iredmail install so any internal flaws are inherent in that packaging.
I am enjoying your series vet much. Linuxbabe is my goto for any guides – always
dependable. Thank you for your very thorough work.
lan address -> lan address -> 127.0.0.1 -> public ip routing
Actually iRedMail is not like that. The email flow sent from iRedMail servers
is:
Darren
6 months ago
Reply
this was a great help, cut down spam by about 90%
34/46
Alex
6 months ago
Reply
If I add a 2nd and 3rd MX records, do I need to ADD a lets encrypt certificate for
each additional domain MAIL2 and MAIL3?
And do I need to configure them in postfix as well?
Once the certificate is obtained, simply restart Nginx, Postfix and Dovecot.
Nginx, Postfix and Dovecot will now use the multi-domain certificate.
35/46
Alex
5 months ago
Reply
I think I got banned from ICLOUD.COM because of this “host
mx6.mail.icloud.com[17.178.97.79] refused to talk to me: 550 5.7.0 Blocked”
Alex
5 months ago
Reply
Thank You so much.
36/46
Alex
4 months ago
Reply
Hello there,
Thank you.
maximumwarp
3 months ago
Reply
Hello and thank you for your awesome guide.
I’m following all the steps but I have a problem:
if I insert these lines in
/etc/postfix/main.cf
37/46
policyd-spf_time_limit = 3600
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
check_policy_service unix:private/policyd-spf,
check_policy_service inet:127.0.0.1:10023,
check_client_access hash:/etc/postfix/rbl_override,
reject_rhsbl_helo dbl.spamhaus.org,
reject_rhsbl_reverse_client dbl.spamhaus.org,
reject_rhsbl_sender dbl.spamhaus.org,
permit_dnswl_client list.dnswl.org=127.0.[0..255].[1..3],
reject_rbl_client zen.spamhaus.org
maximumwarp
3 months ago
Reply
If I comment that lines everything works.
38/46
Xiao Guoan (Admin)
3 months ago
Reply
maximumwarp
3 months ago
Reply
Thank you, “open database /etc/postfix/rbl_override.db: No such file
or directory” problem solved!
Sebastiaan
3 months ago
Reply
Ever since setting this up I’ve managed to get spam sent to one of my email
addresses, sent FROM my server. The spammer didn’t just spoof my from address
but it appears the email actually processed through my server when I examine the
raw source of the email. DKIM is in there and looks to be correct. This is the raw
source of one of the emails
39/46
Return-Path:
Delivered-To: [email protected]
Received: from my-domain.com
by my-domain.com with LMTP
id DnMyOUrmtl4nJAAAz5pt0Q
(envelope-from )
for ; Sat, 09 May 2020 19:20:10 +0200
Received: from www.my-domain.com (my-domain.com [my-vps-ip])
by my-domain.com (Postfix) with ESMTPSA id D6BB21388DC
for ; Sat, 9 May 2020 19:20:10 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=my-domain.com;
s=default; t=1589044810;
bh=a3ZcB1JPs8G/6Vjk02Kw6aw+tRhBodL9r/mEDwRzQmE=;
h=Date:To:From:Reply-To:Subject:From;
b=oht/iF3jok5fo6MWKHQA3nyA1WSnoRHjaamIMM41EltsdzZNnCqbHTCegVBg3uOZg
+axnbUX3Zqu0OSOog+0VNMj1ky3MNm0vtzmJQsRA8sF+Y0G4iDtYgOwkthzEO68ktN
7fmtbZVQmvCZj7/Cd4ECa1HuakfSZaJCqq+rsvXw=
Date: Sat, 9 May 2020 17:20:10 +0000
To: [email protected]
From: Whole Latte Coffee
Reply-To: [email protected]
Subject: Subject: Hi nice website https://google.com
Message-ID:
X-Mailer: WPMailSMTP/Mailer/smtp 2.0.1
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Message Body:
Hi nice website https://google.com
I have edited my domain name to my-domain.com and edited the Reply-To email.
The IP in the received line was actually my VPS IP address. The google.com links
were all in the original email believe it or not.
I checked my logs for the id in the “Received” part in the headers as well as the
message-id and found this:
Over the past few days I have received two of these emails, this being the most
recent one. I have followed these tutorials to the letter and everything is set up
you’ve said except for the greylisting.
Sebastiaan
3 months ago
Reply
The Xmailer header right? That’s what I thought initially too. I’ve
disabled the “WP Mail SMTP” plugin for now as it seems I no longer
need it. We’ll see what happens. Thanks for the reply btw, I appreciate
it a great deal =)
42/46
Viktor
3 months ago
Reply
Hi,
is there any way to use IMSVA from Trend Micro with postfix + dovecot? I searche
dand searched but cannot find how to set it up with IMSVA.
Thank you!
Ron Jones
2 months ago
Reply
Excellent work again! Everything seems to work as it should.
I had to make a minor change to my pflogsumm crontab entry in order to make it
work for me (in order to get my morning report).
MAILTO="[email protected]"
Followed by the first line of crontab, which shows the order of cron commands
I chose 07:28 so that it would run in time to hit my mailbox right before I come
down to my office.
The big change for me (Ubuntu 20.04 server) was to call
/usr/bin/perl
43/46
saurav
2 months ago
Reply
how to stop unauthenticated users to send e-mails neither locally nor remotely?
from port:465 ssl
germs
2 months ago
Reply
Am using CentOS8 (release 8.1) and trying to implement the SMTP AUTH flood
attack as you stated. The filter is detecting the attack but firewalld is not banning
the IP addresses.
[postfix-flood-attack]
enabled = true
bantime = 1h
filter = postfix-flood-attack
action = firewallcmd-ipset
port = http,https,smtp,submission,pop3,pop3s,imap,imaps,sieve
protocol = tcp
logpath = /var/log/maillog
44/46
RS BARI
3 weeks ago
Reply
I try to this command:
smtpd_sender_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unknown_reverse_client_hostname
smtpd_helo_required = yes
smtpd_helo_restrictions =
permit_mynetworks
permit_sasl_authenticated
smtpd_helo_required = yes
smtpd_helo_restrictions =
permit_mynetworks
permit_sasl_authenticated
check_helo_access hash:/etc/postfix/helo_access
reject_invalid_helo_hostname
reject_non_fqdn_helo_hostname
reject_unknown_helo_hostname
45/46
Matthieu
27 seconds ago
Reply
Dear Xiao,
PS.: Please check the pflogsumm example, the parameter –problems-first needs to
be changed to –problems_first.
Cheers,
Matthieu
Leave a Comment
Comments with links are moderated by admin before published.
Your email address will not be published.
Use <pre> ... </pre> HTML tag to quote the output from your
terminal/console.
Please use the community ( https://community.linuxbabe.com) for questions
unrelated to this article.
I don't have time to answer every question. Making a donation would incentivize
me to spend more time answering questions.
46/46