Filtering spam with Postfix

Effective ways to reduce unwelcome mail

Focus | Advanced

By Kirk Strauser

Online on: 2005-03-07

If you are responsible for maintaining an internet-connected mail-server, then you have, no doubt, come to hate spam and the waste of resources which comes with it. When I first decided to lock down my own mail-server, I found many resources that helped in dealing with these unwanted messages. Each of them contained a trick or two, however very few of them were presented in the context of running a real server, and none of them demonstrated an entire filtering framework. In the end I created my own set of rules from the bits and pieces I found, and months of experimentation and fine-tuning resulted in the system detailed in this article.

This article will show you how to configure a Postfix mail-server in order to reject the wide majority of unwanted incoming “junk email”, whether they contain unsolicited commercial email (UCE), viruses, or worms. Although my examples are specific to Postfix, the concepts are generic and can be applied to any system that can be configured at this level of detail.

For a real world example, I’ll use my server’s configuration details:

My server’s configuration details
Hostname Kanga.honeypot.net
Public address 208.162.254.122
Postfix configuration path /usr/local/etc/postfix

Goals

This configuration was written with two primary rules in mind:

  1. Safety is important above all else. That is, I would much rather incorrectly allow an unwanted message through my system than reject a legitimate message.
  2. The system had to scale well. A perfect system that uses all of my server’s processing power at moderate workloads is not useful.

It’s easy to experiment with Postfix, and it can be customized to your level of comfort

To these ends, several checks—that may have reduced the number of “false negatives” (messages that should have been rejected but were not) at the cost of increasing the number of “false positives” (legitimate messages that were incorrectly rejected)—were avoided. The more resource-intensive tests were moved toward the end of the configuration. This way, the quick checks at the beginning eliminated the majority of UCE so that the longer tests had a relatively small amount of traffic to examine.

HELO restrictions

When a client system connects to a mail-server, it’s required to identity itself using the SMTP HELO command. Many viruses and spammers skip this step altogether, either by sending impossibly invalid information, or lying and saying that they are one of your own trusted systems—in the hopes that they will be granted undeserved access. The first step in the filtering pipeline, then, is to reject connections from clients that are severely mis-configured or that are deliberately attempting to circumvent your security. Given their simplicity, these tests are far more effective than might be imagined, and they were implemented in my main.cf file with this settings block:

1  smtpd_delay_reject = yes
2  smtpd_helo_required = yes
3  smtpd_helo_restrictions =
4     permit_mynetworks,
5     check_helo_access 
           hash:/usr/local/etc/postfix/helo_access,
6     reject_non_fqdn_hostname,
7     reject_invalid_hostname,
8     permit

Given their simplicity, these tests are far more effective than might be imagined

Line 1 is a fix for certain broken (but popular) clients, and is required in able to use HELO filtering at all. The second line rejects mail from any system that fails to identify itself. Line 4 tells Postfix to accept connections from any machines on my local network. The next line references an external hash table that contains a set of black- and whitelisted entries; mine looks like this:

woozle.honeypot.net     OK
honeypot.net            REJECT You are not me. Shoo!
208.162.254.122         REJECT You are not me. Shoo!

The first line in the table explicitly allows connections from my laptop so that I can send mail when connected through an external network. At this point Postfix has already been told to accept connections from all of my local machines and my short list of remote machines, so any other computer in the world that identifies itself as one of my systems is lying. Since I’m not particularly interested in mail from deceptive systems, those connections were flatly rejected.

Lines 6 through 8 complete this stage by rejecting connections from hosts that identify themselves in blatantly incorrect ways, such as “MAILSERVER” and “HOST@192.168!aol.com”.

Some administrators also use the reject_unknown_hostname option to ignore servers whose hostnames can’t be resolved, but in my experience this causes too many false positives from legitimate systems with transient DNS problems or other harmless issues.

You can test the effect of these rules, without activating them on a live system, by using the warn_if_reject option to cause Postfix to send debugging information to your maillog without actually processing them. For example, line 6 could be replaced with:

       warn_if_reject,
       reject_non_fqdn_hostname,

This way the results can be observed without the risk of inadvertently getting false positives.

Tags: postfix, spam

License

(C) Kirk Strauser 2007

This article is made available under the "Attribution-Sharealike" Creative Commons License 2.5 available from http://creativecommons.org/licenses/by-sa/2.5/.

Biography

Kirk Strauser: Kirk Strauser has a BSc in Computer Science from Missouri State University. He works as a network application developer for The Day Companies, and runs a small consulting firm that specializes in network monitoring and email filtering for a wide array of clients. He has released several programs under free software licenses, and is active on several free software support mailing lists and community websites.

Anonymous visitor's picture

reject_unauth_pipelining

15 smtpd_recipient_restrictions =
16 reject_unauth_pipelining,

From: http://www.postfix.org/postconf.5.html
Note: reject_unauth_pipelining is not useful outside smtpd_data_restrictions when 1) the client uses ESMTP (EHLO instead of HELO) and 2) with "smtpd_delay_reject = yes" (the default). The use of reject_unauth_pipelining in the other restriction contexts is therefore not recommended.

Anonymous visitor's picture

reject_non_fqdn_hostname

In combination with Outlook the "reject_non_fqdn_hostname" in the helo restrictions will prevent your mail from the local network to be sent, even with "permit_mynetworks" enabled.
I have not found a workaround for this problem, but to disable the fqdn requirement.

Anonymous visitor's picture

Re: reject_non_fqdn_hostname

I had the same problem with Outlook -- I believe it uses a substring of the machine name for the HELO command. Since we don't have too many Outlook clients, it was pretty easy to add the individual machines to my helo_access file. I grabbed the HELO identification from the Outlook error message that popped up when a user tried to send a message (this is also available in the log file). Then I put an entry in helo_access as follows:

machine_name OK

Anonymous visitor's picture

Solved

I got the solution to my problem. Somebody pointed out that my main.cf was not correctly configured. permit_mynetworks was not working because I set mynetworks = 10.0.0.0/32 instead of 10.0.0.0/8.
Have a look here this thread.

Ramakk's picture

helo_access for Outlook clients

Hi have tried the helo_access file as well. But, I simply have not been able to get Outlook to work.

Is there a specific place one puts the helo_access lookup within main.cf?

Thanks in advance.
Rama

Anonymous visitor's picture

Outlook the "reject_non_fqdn_hostname"

FYI, it works if you use "SMTP authentication" which is probably a good idea anyway.

Anonymous visitor's picture

workaround for outlook

a better workaround: dont use outlook (=

Anonymous visitor's picture

relays.ordb.org

Should remove line 25:

reject_rbl_client relays.ordb.org,

ordb.org no longer provides any services.

You end up with a bunch of messages similar to this in your mail logs:

warning: x.x.x.x.relays.ordb.org: RBL lookup error: Host or domain name not found. Name service error for name=x.x.x.x.relays.ordb.org type=A: Host not found, try again

Anonymous visitor's picture

relays.ordb.org

It was announced a little while ago that ordb.org was no longer going
to be maintained and it is now no longer available. That is why you
are seeing the errors.

It you are using ordb.org as an RBL, remove it.

Anonymous visitor's picture

RBLs in general

Are the other two (RBLs) any decent at all?

from my experience (most recently with www.tqmcube.com) is that RBLs blacklist a whole lot of legitmate e-mail servers and thus cause WAY too many false positives.

so far the HELO,valid sender domain and greylisting is working wonders. I have dropped spam making it through the e-mail server by 80-90%. my bayesian filter (I use spamprobe) takes care of the rest, and since I put those additions into my e-mail server *knocks on wood* I haven't gotten any spam into my inbox. yeah!

and my end users of mailing list run are ecstatic as well.

but RBLs, the mission statements sound good, but in practice, I don't see them actually living up to them.

Anonymous visitor's picture

Greylisting

The greylist configuration in smtpd_recipient_restrictions is missing

Anonymous visitor's picture

No, it's not. The following

No, it's not. The following line runs postgray.

check_policy_service inet:127.0.0.1:10023

Justin Timbaker's picture

I have a great alternative

Some of this stuff is really interesting, but a bit beyond me at some points. I do have a great service that takes care of alot of this stuff. It's a company out of LA and they did wonders for my Mailing.

Cheers,

Justin

Rubi Jan's picture

Really?

So, what is it?

Rubi

Anonymous visitor's picture

Postfix Client Problem

Dear

I'm a system engineer at WirelessIP. My current mailserver is using Postfix. Recently i face with my client complaint about sending slow with large attachment from client side to my server. So Do u have any idea to solve this ? or to make it faster than usual.

Thanks and Good luck

Best Regards,

Chantha
Email:chantha@wirelessip.com.kh

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options


[TheTop10Everything] [Zoeshire] [FSDaily RSS] [Illiterarty]