Killing That Spam With Postgrey And Postfix

Want to support HowtoForge? Become a subscriber!
 
Submitted by erk (Contact Author) (Forums) on Wed, 2006-06-28 14:26. :: Anti-Spam/Virus | Postfix

Greylisting


Ever heard of greylisting?

There are numerous ways to prevent spam from reaching your inbox, the most popular is probably SpamAssassin. Greylisting will not replace spam filtering software like SA but it will serve as a powerful first hurdle for spam thus reducing the ammount of spam entering the system at all. It should rather be seen as an addition to all the other anti spam features of Postfix.

Apart from the obvious benefit of reducing the ammount of spam in your inbox it will give your virus scanner and SpamAssassing less work to do. Both spam filtering and virus scanning is a CPU intesive business and stopping spam in the door will potentially save you money due to reduced hardware requirements.

Greylisting in short means that when someone wants to deliver a mail to your mailserver it will simply reply “Please come back later�. That is something all RFC compliant mailservers do and when they do come back the mail is accepted. Most spammers and spam software are not compliant and not patient enough to try again. You will be surprised to see how effective this is. Anyway, follow the links below to really learn about it. There are as always pros and cons so do your homework before you put it on a production server.

Greylisting.org
Greylisting on Wikipedia

There are several implementations of greylisting and it can be done with most any server. I will show you how to do it on a Debian Sarge with Postfix running as MTA with Postgrey.

Install Postgrey

I assume you have a working Postfix installation.

If you use apt-get to install Postgrey the requirements will be handled automagically. However, if you install from source you will have to check it manually.

Requirements for Postgrey are:
- Perl >= 5.6.0
- Net::Server (Perl Module)
- IO::Multiplex (Perl Module)
- BerkeleyDB (Perl Module)
- Berkeley DB >= 4.1 (Library)

First we install the Postgrey package with :

apt-get install postgrey

This will install the Postgrey server on your machine. You might want to change some settings. The most annoying thing with greylisting is that it impose a delay on the delivery of mails. If you find the 5 minute delay that is default to be a little too long you can set it to 1 minute. You can change this by editing /etc/default/postgrey. Change the default :

POSTGREY_OPTS="--inet=127.0.0.1:60000"

to

POSTGREY_OPTS="--inet=127.0.0.1:60000 --delay=60"

However I would suggest changing the defaults only after you have verified that everything works as expected. It is also worth noticing that a shorter delay will reduce the efficiency of the greylisting. Increasing the delay to an hour will be harder on spam but also harder on your users patience. :)

Now start the postgrey policyserver with :

/etc/init.d/postgrey start

The Postgrey policy service should now be up and running on port 60000.

Now let’s configure Postfix to use Postgrey.

Configure Postfix

The Postfix configuration files are located in /etc/postfix. Edit /etc/postfix/main.cf and add check_policy_service inet:127.0.0.1:60000 to the smtpd_recipient_restrictions. It should look something like this :

smtpd_recipient_restrictions = permit_sasl_authenticated,
           permit_mynetworks,
           reject_unauth_destination,
           check_policy_service inet:127.0.0.1:60000

Now all we have to do is to reload the Postfix configuration with :

postfix reload

Simple and nice. Sit back and enjoy the absence of spam :)

//Erk


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by ksga (registered user) on Fri, 2013-11-29 18:10.

I'm using postgrey 1.34 and were having issues getting postgrey working.

smtpd[7976]: warning: connect to 127.0.0.1:10023: Connection refused

 it seems postgrey now uses ipv6 as default - so change the line 

 POSTGREY_OPTS="--inet=127.0.0.1:60000"

to: 

 POSTGREY_OPTS="--inet=60000"

and everything is fine.. 

Submitted by Jay (not registered) on Sat, 2011-02-19 14:27.
On my box the default port was not 60000. Run `ps aux|grep postgrey` after you start it and it will show the port on the command line.
Submitted by IzFazt (not registered) on Thu, 2010-11-11 03:29.

used yast2 to install on opensuse 11, had to edit /etc/sysconfig/postgrey

then commented out

#POSTGREY_CONN_OPTIONS="--unix=/var/spool/postfix/postgrey/socket"

and uncommented 

POSTGREY_CONN_OPTIONS="--inet=127.0.0.1:10031"

then changed port from default 10031 to 60000 in this line

after that it worked by restarting postgrey

Submitted by mymaestro (not registered) on Tue, 2010-07-27 22:51.

This howto threw me off and never worked. Turns out newer postgrey is different and/or RedHat configures it differently.

Following the CentOS howto makes everything work again.

http://wiki.centos.org/HowTos/postgrey

Submitted by Anonymous (not registered) on Thu, 2010-11-25 13:31.
Yeah the author should probably say that this guide it's for debian/ubuntu only.
Submitted by Anonymous (not registered) on Thu, 2006-07-06 19:50.
The setup for postfix-gld is just as easy and you can manage the lists directly through mysql. Tutorial to setup postfix-gld: http://wyae.de/docs/greylisting/
Submitted by wuj (registered user) on Sat, 2006-07-08 20:17.

Good tutorial for postfix-gld!

There are a fairly large number of greylisting softwares and they are not very well known so I'm glad to see tips and opinions about them in the comments.

One of the deciding factors for going with Postgrey for me was acually that I did not want any SQL database server. I suppose Sqlgrey with SQLite would have been an option but I like to keep to stuff that is in Debian stable for production use.

//Erk
Submitted by Anonymous (not registered) on Sun, 2006-07-02 19:40.

Interesting timeing. I just got done installing sqlgrey on my Sarge box.  This has more knobs to tweak than postgrey seems to, and overall should scale better.  The downside is that it's not packaged for Debian.  :-( Just RH and gentoo.)

 http://sourceforge.net/projects/sqlgrey/