Friday, February 19, 2010

Postfix Backup MX eMail Server Anti-Spam Configuration

SkyHi @ Friday, February 19, 2010

According to RFC2821 the lowest-numbered records are the most preferred MX for domain. So I've a target Postfix backup server to keep the messages in a queue waiting for the primary server to become available. This ensures that if my primary MX goes down I do not loss any emails. However, spammers are connecting to my backup MX to avoid anti spam filters that are running on the primary MX server. This also hides their real IP from my primary MX. How do I configure anti-spam for my backup RHEL / CentOS 5.3 based Postfix mx server?

This is well known issue. Make sure your backup MX runs the same config in terms of spam rejection as your primary server. Try the following to improve backup eMail server anti spam configuration.

If the backup MX acts as a store-and-forward mail server

Consider the following example:	86400	IN	MX 	10 86400 IN MX 20 email handled by two email servers. is your backup server. Open and append the following restrictions on

Only allow your own domain to accept email

Use relay_domains to relay email for two domain called and Also, set lookup tables with all valid addresses in the domains that match $relay_domains i.e. only accept email for valid email address.
# vi /etc/postfix/
Modify settings as follows:

relay_domains =,, $mydestination<br />relay_recipient_maps = hash:/etc/postfix/relay_recipients

Create /etc/postfix/relay_recipients to accept email for,, and so on..   OK<br />   OK<br />    OK

Save and close the file. Finally, update your db:
# postmap hash:/etc/postfix/relay_recipients

Anti spam via RBL

Now, add following lines to check spammer IP address using RBLs. Reject all email if they do not have a valid hostname or proper email address:

smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks,<br />  reject_non_fqdn_hostname,<br />  reject_non_fqdn_sender,<br />  reject_non_fqdn_recipient,<br />  reject_unauth_destination,<br />  reject_unauth_pipelining,<br />  reject_invalid_hostname,<br />  reject_rbl_client<br /># helo required<br />smtpd_helo_required = yes<br /># disable vrfy command<br />disable_vrfy_command = yes<br /><br />smtpd_data_restrictions =<br />            reject_unauth_pipelining,<br />            permit<br />

Save and close the file. Restart / reload postfix:
# service postfix reload
There are other anti UCE settings, see Postfix anti UCE cheat sheet for more information.

Nolisting mx A entry

Spammers email software does not retry higher-priority MX records. So all you have to do is create a non-existent primary mail server and a working secondary mail server, attempts to contact the primary mail server will always fail. This technique uses a non-existent primary mail server, which is compatible with all correctly configured mail servers such as Sendmail, MS-Exchange, Postfix, Qmail, Exim etc. Create BIND dns configuration as follows:	86400	IN	MX 	10<br />	86400	IN 	MX 	20<br />	86400	IN 	MX 	30<br />	86400	IN 	MX 	40


  • - Runs your actual primary MX with anti spam and anti virus configurations.
  • - Your backup mx server with anti spam / virus and act as store and forward server for
  • and are nolist MX servers. They can either be dead (or point to non existing IP) or you can run SMTP on port 25 that always returns 4xx error so that legitimate MTA to retry on a lower numbered MX server. nolist MX servers can also used to get more information about spammers to blacklist them. Google for "spam filtering services that offer free nolist servers" specifically for botnet data harvesting.

Greylisting Backup MX

Postfix can be configured to temporarily reject any email from a sender it does not recognize. If the mail is legitimate, the originating server will try again and the email is accepted. If the mail is from a spammer it will probably not be retried since a spammer goes through thousands of email addresses and cannot afford the time delay to retry. See how to configure postfix greylist policy server.

Spamassassin+Amavis+Clamd For Backup MX Server

Spamassassin is open source mail filter, to identify spam using a wide range of heuristic tests on mail headers and body text. You can install Spamassassin spam checking on your backup server. Emails found to be Spam (with higher spam score) will be drop out before reaching your primary email server. You can also use Clamav / Amavis to scan email and drop or forward infected emails. Install spamassassin, clamd and amavisd-new using yum or apt-get commands (turn on EPEL repo under RHEL / CentOS to install the following packages):
# yum install clamav-server amavisd-new spamassassin

  • clamav-server : Clam Antivirus scanner server
  • amavisd-new : amavisd-new is a high-performance and reliable interface between Postfix and virus scanners, and/or
    Mail::SpamAssassin Perl module.
  • spamassassin : Spam filter for email which can be invoked from mail delivery agents or in our case via amavisd-new

Once done, add as the following to your /etc/postfix/


Save and close the file. Open /etc/postfix/ and add the following settings:

smtp-amavis unix - - n - 2 smtp<br />  -o smtp_data_done_timeout=2400<br />  -o smtp_send_xforward_command=yes<br />  -o disable_dns_lookups=yes<br />  -o max_use=20<br /> inet n - n - - smtpd<br />  -o content_filter=<br />  -o local_recipient_maps=<br />  -o relay_recipient_maps=<br />  -o smtpd_restriction_classes=<br />  -o smtpd_delay_reject=no<br />  -o smtpd_client_restrictions=permit_mynetworks,reject<br />  -o smtpd_helo_restrictions=<br />  -o smtpd_sender_restrictions=<br />  -o smtpd_recipient_restrictions=permit_mynetworks,reject<br />  -o mynetworks_style=host<br />  -o mynetworks=<br />  -o strict_rfc821_envelopes=yes<br />  -o smtpd_error_sleep_time=0<br />  -o smtpd_soft_error_limit=1001<br />  -o smtpd_hard_error_limit=1000<br />  -o smtpd_client_connection_count_limit=0<br />  -o smtpd_client_connection_rate_limit=0<br />  -o receive_override_options=no_header_body_checks,no_unknown_recipient_checks,no_address_mappings

Save and close the file. Also, update /etc/amavisd/amavisd.conf with required settings.

$daemon_user  = 'amavis';     # (no default;  customary: vscan or amavis), -u<br />$daemon_group = 'amavis';     # (no default;  customary: vscan or amavis), -g<br />$mydomain = '';   # a convenient default for other settings<br />$log_level = 1;              # verbosity 0..5, -d<br />$DO_SYSLOG = 1;              # log via syslogd (preferred<br />$inet_socket_port = 10024;   # listen on this local TCP port(s) (see $protocol)<br />$sa_tag_level_deflt  = -999;  # add spam info headers if at, or above that level<br />$sa_tag2_level_deflt = 6.31; # add 'spam detected' headers at that level<br />$sa_kill_level_deflt = 6.31; # triggers spam evasive actions<br />$sa_dsn_cutoff_level = 10;   # spam level beyond which a DSN is not sent<br />$virus_admin               = 'postmaster\';                    # notifications recip.<br />$mailfrom_notify_admin     = 'postmaster\';                    # notifications sender<br />$mailfrom_notify_recip     = 'postmaster\';                    # notifications sender<br />$mailfrom_notify_spamadmin = 'postmaster\';                    # notifications sender<br />$mailfrom_to_quarantine = 'postmaster\'; # null return path; uses original sender if undef<br />$sa_spam_subject_tag = '***SPAM*** ';<br />$myhostname = '';  # must be a fully-qualified domain name!<br />$notify_method  = 'smtp:[]:10025';<br />$forward_method = 'smtp:[]:10025';  # set to undef with milter!<br /># add your server public ip, private ip,<br />@inet_acl = qw( 127/8;<br />

Save and close the file. Update spamassassin settings in /var/spool/amavisd/:
# usermod -s /bin/bash amavis
# passwd amavis
# su - amavis
$ razor-admin -discover
$ razor-admin -create
$ razor-admin -register -l -pass=somePassword
$ cd .spamassassin
$ cp /usr/share/spamassassin/user_prefs.template user_prefs
$ exit
# usermod -s /sbin/nologin amavis

Update /etc/clamd.d/amavisd.conf as follows:

# Use system logger.<br />LogSyslog yes<br /><br /># Specify the type of syslog messages - please refer to 'man syslog'<br /># for facility names.<br />LogFacility LOG_MAIL<br /><br /># This option allows you to save a process identifier of the listening<br /># daemon (main thread).<br />PidFile /var/run/amavisd/<br /><br /># Remove stale socket after unclean shutdown.<br /># Default: disabled<br />FixStaleSocket yes<br /><br /># Run as a selected user (clamd must be started by root).<br />User amavis<br /><br /># Path to a local socket file the daemon will listen on.<br />LocalSocket /var/spool/amavisd/clamd.sock

Update /etc/mail/spamassassin/ as follows:

required_hits 6.31<br />report_safe 1<br />rewrite_subject         0<br /># Enable the Bayes system<br />use_bayes               1<br /># Enable Bayes auto-learning<br />auto_learn              1<br />

Save and close the file. Finally, restart postfix and other services:
# service clamd.amavisd start
# service amavisd start
# service postfix restart

Turn services on boot:
# chkconfig clamd.amavisd on
# chkconfig amavisd on
# chkconfig postfix on

Now, check your /var/log/maillog for any errors or details:
# netstat -tulpn -A inet| egrep ':25|:1002?'
# tail -f /var/log/maillog

Above configuration will open the following ports on server:

  1. 10024 - Amavisd
  2. 10025 - Amavisd will communicate back the results to Postfix
  3. 25 - SMTP Port

A note about same priority mx servers

You can point the mail servers, all with the same priority. It offers the following benfits:

  • Load balancing
  • Centralized user mail managment via LDAP or MySQL / PGSQL
  • Centralized virus scanning
  • Centralized Spam scanning

Sample dns records:	86400	IN	MX 	10<br />	86400	IN 	MX 	10<br />	86400	IN 	MX 	10<br />; imap server<br />imap		86400	IN 	A<br />; pop3 server - can be CNAME too<br />pop3		86400	IN 	A<br />

You may need additional servers inside your lan:

  • MySQL/OpenLDAP ( - Store user name, email, mailbox and other information.
  • Central anti virus server ( - Used by all your mx servers for scanning using TCP/IP. You can also do the same for spam scanning using TCP/IP.

Each mx server can use centralized anti spam and anti-virus server. Once scanned Postfix can deliver final mail which can be retrieved using POP3 / IMAP server.

Further Readings / References :

This FAQ assumed that you have working Postfix primary and backup server. It only covered anti spam related topics. For further details refer the following urls and respective man pages: