Mailserver: Configuring Filters – Part 5

Welcome to Part 5 of the Mailserver Configuration series in which we will cover Mail Filters. We will be using SpamAssassin to protect our users from Spam, and ClamAV to protect them from Viruses.

I feel like I should mention this right away: These Filters will NOT stop everything, they just help reduce the amount of spam and viruses the users will receive.

If you haven’t seen the previous parts of the mailserver series, you can find an Index of the sections at the end of this post or on Part 1 of the series. A lot of this series has been fairly backward-compatible with CentOS 6, however there are some significant differences in the configuration of the filters between CentOS 7 and CentOS 6.

Configure Repositories

There are 3 packages that we need to install to configure the filters on our mailserver. The first one being Amavis which is like the manager that scans the emails using the “tools” we configure. ClamAV and SpamAssassin are the “tools” that it will use. To get these three packages we need to add the EPEL (Extra Packages for Enterprise Linux) repository to the server. For CentOS 7 you can use the following commands:
(If for some reason you are following this guide with CentOS 6 and this doesn’t work you can find instructions to add the repo here.)

yum install epel-release

You can verify it was installed successfully by running:

yum repolist

Installing Filters

To install Amavis you need to run the following command:

yum install amavisd-new

This will automatically install Spamassassin but not ClamAV use the following to install ClamAV:

yum install clamav clamav-server clamav-server-systemd

Configuration

Now we should have everything we need to setup filters on our mailserver. SpamAssassin doesn’t require any configuration, Amavis takes care of that by default. We just need to configure Amavis and ClamAV.

Amavis

Before Amavis can start scanning emails, it needs to be told what tools are available to it. To define the “tools” we edit “/etc/amavisd/amavisd.conf”:

nano /etc/amavisd/amavisd.conf

The first thing we need to do is verify that Virus and Spam filtering is not disabled. Toward the top of the configuration file, you will see three lines under “Commonly Adjusted Settings:”. They should be commented out be default, this is good. Specifically make sure that @bypass_virus_checks_maps and @bypass_spam_checks_maps are both commented out or else Amavis wont even try to scan your emails. After verifying those lines, move on to find the $mydomain line. Set that to the appropriate domain.tld for your server.

$mydomain = 'domain.com';

If you want to receive notifications when spam is detected, you should find and set the following lines: (they are set to undefined by default)

Now find the http://www.clamav.net/ section and verify that it is not commented out, it should look like this:

### http://www.clamav.net/
['ClamAV-clamd',
\&ask_daemon, ["CONTSCAN {}\n", "/var/run/clamd.amavisd/clamd.sock"],
qr/\bOK$/m, qr/\bFOUND$/m,
qr/^.*?: (?!Infected Archive)(.*) FOUND$/m ],
# NOTE: run clamd under the same user as amavisd - or run it under its own
# uid such as clamav, add user clamav to the amavis group, and then add
# AllowSupplementaryGroups to clamd.conf;
# NOTE: match socket name (LocalSocket) in clamav.conf to the socket name in
# this entry; when running chrooted one may prefer a socket under $MYHOME.

It is important that you pay attention to the “/var/run/clamd.amavisd/clamd.sock” portion. This is the socket where Amavis is going to look for ClamAV. To verify that ClamAV is configured to run using that socket use this command:

cat /etc/clamd.d/amavisd.conf | grep LocalSocket

The path after “Local Socket” should match the path in “/etc/amavisd/amavisd.conf” if it doesn’t, adjust the path in amavisd.conf. This should complete the ClamAV configuration, Amavis takes care of the rest provided it can access the socket.

ClamAV

First were going to copy a configuration template for ClamAV and edit it to work with how we just configured Amavis:

Make Postfix use Amavis

At this point Amavis should be configured and ready to start scanning emails, we haven’t started the service yet but we will get to that. Now lets configure Postfix to send messages over to Amavis for scanning. To give you a rough idea of how this works; an email will come into Postifx, Postfix will transfer it to Amavis for scanning, and then Amavis sends the message back to Postfix where it continues along its path. We need to edit master.cf to make Postfix send messages to Amavis:

nano /etc/postfix/master.cf

Add the following lines to the end of the file to tell Postfix to give the mail to Amavis for scanning:

Now the email has left Postfix, and Amavis scans it but we need a way to get the email back into Postfix, so we add another SMTP daemon that only Amavis can use, and lets run it on another port like: 10025. We don’t want anyone else to be able to use this SMTP server though, so we will restrict it to only accept mail from the local host. Add the following lines to the end of master.cf to make Postfix accept the mail back from Amavis:

That should be all the modifications we need to make to master.cf so lets add a couple lines to main.cf. This line just tells Postfix that there is a content filter available (amavis) and where to find it.

Now Postfix is configured to use Amavis, so we should be ready to start the filtering daemons:

systemctl start spamassassin
systemctl start amavisd

Then configure them to start at boot time:

systemctl enable spamassassin
systemctl enable amavisd

And finally, restart Postfix:

systemctl restart postfix

Now if you take a look at “/var/log/maillog” you should see a bunch of “Found decoder” messages for amavis, then a “Found secondary av scanner ClamAV-clamscan” then the usual stopping and starting Postfix logs. Your system should be set to scan emails now, lets test it.

Testing

During testing, I like to use telnet, so lets install that now:

yum install telnet -y

Verify SMTP:

Now lets first verify that Amavis is listening for connections:

telnet localhost 10024

You should see a line saying “220 [127.0.0.1] ESMTP amavisd-new service ready”, respond with:

ehlo localhost

It should respond with a bunch of lines starting with “250-” if you get these, Amavis should be working.

You can disconnect with:

quit

Now lets do the same thing on port 10025 to make sure Postifix can accept the mail after Amavis is done scanning it:

telnet localhost 10025
ehlo localhost

You should get a bunch of “250-” lines again. If you got the lines, Postfix is configured to receive from Amavis.

Test the Filters

We know that both Amavis and Postifx are able to talk to each other, now we have to see if they communicate like they are supposed to. For this we will use mailx again, the strings we are going to be sending are safe strings that are configured in SpamAssassin and ClamAV for testing to verify the filters are working. Use the “mail” command to send yourself an email containing the test string:

mail yourtestingemail@somedomain.com

Set whatever subject you want, and include the following in the body:

X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*

Then use CTRL+D to send it. If you check “/var/log/maillog” you should see a line similar to: “Blocked INFECTED” this means ClamAV is working. Lets test SpamAssassin now, send yourself another email, only this don’t use the string above, use this one:

XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X

This time you should see a line similar to: “Blocked SPAM” that means SpamAssassin is working. If both of these tests were successful, you should now have Anti-Virus and Anti-Spam protection for your server. We should make sure the definitions stay up to date though.

Automatic Updates

Viruses and Spam are changing constantly, so we need to make sure that our server’s filters always have the latest definitions so they can effectively fight unwanted emails.

SpamAssassin

SpamAssassin automatically includes a cron job, so we just need to verify that it is configured to run. To do that we just need to check a file to make sure the line isn’t commented out:

Conclusion

In this section we covered the installation of mail filters. There will still be another section at the end of the series on configuring blacklists sender verification. For now there is enough Spam and Virus protection that we can move on to setting up a way to get the email off the server. Up until this point our mailserver can send and receive email, but we don’t have any way to download and read that email. This is where Dovecot comes in. Dovecot provides IMAP and POP3 capabilities to our server. In the next section we will cover installing Dovecot and configuring it to use our PostgreSQL database we setup in Part 3 for authentication.

If you have missed any of the previous Sections, you can use the links below to go back to them: