Rejecting viral email at SMTP time with exim4
Posted by Steve on Mon 16 May 2005 at 22:55
If you're using the exim4 mail server you can reject mails which have viral content at SMTP time - meaning they aren't delivered and you don't have to worry about sending bounce messages to the often-faked "From" address.
To make use of the virus checking you need :
- ClamAV antivirus software.
- The exim4-daemon-heavy package installed.
The exim4-daemon-heavy package has additional options compared to the exim4-daemon-light, including ACL checks which we'll use to validate the message body of incoming emails with the virus scanner.
To start with we need to install the virus scanner, running the following should install a scanner along with a tool to keep your definitions up to date:
apt-get install clamav-daemon
The clamav-daemon package will pull in clamav-freshclam package which will keep the virus definitions up to date - logging its update checks and results to the file /var/log/clamav/freshclam.log.
Once the package has been installed you should check that it's setup properly for use with the exim4 package.
You should examine the file /etc/clamav/clamd.conf file and make sure the following two lines are present:
User clamav AllowSupplementaryGroups
(These are included by default).
Now that the scanner is setup we need to do two things:
- Tell exim4 how to connect to the scanning deamon.
- Force exim4 to reject incoming mails which are flagged as viruses by the scanner.
If you're using the split configuration of Exim4 you should add the following content to /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs:
# Specify the virus scanner to use av_scanner = clamd:/var/run/clamav/clamd.ctl
Then we need to edit the exim ACL check - inside the directory /etc/exim4/conf.d/acl there are several files which contain ACLs which you can use to reject mails.
The file we will need to look at is called 40_exim4-config_check_data - this is used to check the body of incoming messages. ("data" here means the data that is sent as part of a message body with the SMTP command "data").
The file that you'll be looking at reads like this by default:
# 40_exim4-config_check_data acl_check_data: # Deny unless the address list headers are syntactically correct. # # This is disabled by default because it might reject legitimate mail. # If you want your system to insist on syntactically valid address # headers, you might want to enable the following lines. # deny message = Message headers fail syntax check # !acl = acl_whitelist_local_deny # !verify = header_syntax # require that there is a verifiable sender address in at least # one of the "Sender:", "Reply-To:", or "From:" header lines. # deny message = No verifiable sender address in message headers # !acl = acl_whitelist_local_deny # !verify = header_sender # accept otherwise accept
To this we need to add some new directives just before the "accept otherwise" lines.
Insert the following:
# Reject messages that have serious MIME errors.
# This calls the demime condition again, but it
# will return cached results.
deny message = Serious MIME defect detected ($demime_reason)
demime = *
condition = ${if >{$demime_errorlevel}{2}{1}{0}}
#
# Reject file extensions used by worms.
#
deny message = This domain has a policy of not accepting certain types \
of attachments in mail as they may contain a virus. \
\
This mail has a file with a .$found_extension attachment and \
is not accepted. \
\
If you have a legitimate need to send this attachment, send it \
in a compressed archive, and it will then be forwarded to the \
recipient.
demime = vbs:bat:pif:scr
.ifdef TEERGRUBE
delay = TEERGRUBE
.endif
# Reject messages containing malware.
deny message = This message contains a virus ($malware_name) and has been rejected
malware = *
Once you've made this addition then you can restart the server:
/etc/init.d/exim4 restart
You can test it's working correctly by sending a message from an outside machine and verifying that it is bounced without being delivered to your local user.
For this purpose the Eicar test virus is ideal, it is not a real virus at all! Instead it is a pattern that legitimate virus scanners add to their databases so they can be tested.
If you include an attatchment wiht your mail which has the following test scring as an attachment it should be identified as infected with a virus:
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
The test string actually is a real executable on DOS systems! It's a great example of pure ASCII shellcode - a program who's instructions come entirely from the printable ASCII range.
If you wish to run it you can save it to a file, named test.com - just note that the third character is the number zero, not the letter O...
Managed AntiSpam
Fully managed filtering of your incoming email.
[ Parent | Reply to this comment ]
There's no excuse for backscattering (bouncing messages to envelope sender addresses), because most bouncing mail these days is either spam or virus laden. Backscatter needlessly annoys innocent systems and users to no good end. If you want to reject mail based upon whatever criteria you choose, do it during the SMTP transaction!
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
I'm curious as to why you dislike exim so much?
I've used sendmail, postfix, qmail and exim at various times (different systems/jobs/contract work, etc).
Each of them have pro's and con's but the only one I actively dislike using nowadays is sendmail.
Steve
-- Steve.org.uk
[ Parent | Reply to this comment ]
Maybe I'm just too darn simple minded. Once I abandoned sendmail, I decided to keep the MTA as simple as possible -- never mind that I had to endure qmail's quirks and limitations (including backscatter) for far too long...
[ Parent | Reply to this comment ]
It took me 3 full days to "get in" Exim, but now I can setup almost any behavior in less than an hour.
And remember : want a simple MTA ? Use SSMTP :)
[ Parent | Reply to this comment ]
Refer to the introduction Exim ACL configuration for more information.
[ Parent | Reply to this comment ]
I apologize for not being more careful. I didn't mean to imply that exim was responsible for backscattering.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Great article but can you tell me why I'm getting the following error message:
zorb:~# /etc/init.d/exim4 restart
Restarting MTA: 2005-07-03 15:35:07 Exim configuration error in line 333 of /var/lib/exim4/config.autogenerated.tmp:
error in ACL: unknown ACL condition/modifier in "and is not accepted. If you have a legitimate need to send this attachment,"
Invalid new configfile /var/lib/exim4/config.autogenerated.tmp
not installing /var/lib/exim4/config.autogenerated.tmp to
/var/lib/exim4/config.autogenerated
zorb:~#
Could you post the correct version of:
40_exim4-config_check_data
Great site and regards - John
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
Take a look at line 333 of the file which is named there - maybe there's a comment wrap, or other issue.
For reference here's my complete 40_exim4-config_check_data.
Steve
-- Steve.org.uk
[ Parent | Reply to this comment ]
zorb:~# /etc/init.d/exim4 restart
Restarting MTA: exim4.
compassweb:~#
Prior to installing 'Clam' the mail was being delivered fine. However the system is now refusing to deliver mail to any address :-( and the Eicar virus test doesn't seem to work. Guess I must have screwed up the Clam installation somewhere. It's at times like these that one wishes Debian had a 'rollback' facility!
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
What do the exim logfiles show?
You should see messages in /var/log/exim4, the rejectlog, or mainlog might contain useful diagnostics.
Steve
-- Steve.org.uk
[ Parent | Reply to this comment ]
I suppose the real question should be how to backup Exim prior to tinkering with the installation?? Is it possible to rollback to a previous working backup insallation of Exim?
I have a friend that ran into similar problems with ClamAV and Qmail on a virtual server running Gentoo. Now he manages the mail with rbl smtp and spamassassin only.
Steve, it would be great if you could offer an Exim version of Qmail Rocks or perhaps even individual articles on setting up Squirrel Mail and Spamassassin on Exim 4??
Kind regards
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
Backing up exim is a simple matter of preserving pristine copies of your configuration files before making changes, either:
cp -R /etc/exim /etc/exim.safe
Or:
cp -R /etc/exim4 /etc/exim4-safe
should be suitable for exim3 and exim4. Restoring these backups and then restarting the exim server, via the init script should be sufficient to rollback.
(Although personally I'd never make changes to a live server, without replicating the setup on a test machine, or a desktop system first - that allows me to make large changes without too many broken upgrades, and potential lost mail).
I'm happy to add more Exim pieces to the pending list of articles to write, but my time is severely limitted at the moment, so I wouldn't expect much more exim coverage at the moment.
Steve
-- Steve.org.uk
[ Parent | Reply to this comment ]
Good luck,
Tim
[ Parent | Reply to this comment ]
Thanks for the useful help in configuring clamav for exim4. I use Debian Sarge (r3.1). It's a clean installation, not an upgraded Woody. The solution in this article raises a problem related to access rights on Sarge systems. Just configure exim4, then try to send an e-mail with an attachment. The mail will be rejected temporarily (quoted from /var/log/exim4/mainlog):
2005-08-14 19:13:11 1E4M2o-0003CG-W6 malware acl condition: clamd: ClamAV returned /var/spool/exim4/scan/1E4M2o-0003CG-W6: Access denied. ERROR
2005-08-14 19:13:11 1E4M2o-0003CG-W6 H=(sirius) [192.168.0.136] F= temporarily rejected after DATA
I tracked this down. Exim places files to scan under: /var/spool/exim4/scan/
But clamd has no right to access those files, since:
s2:/var/spool# ls -la | grep exim
drwxr-x--- 6 Debian-exim Debian-exim 4096 2005-08-14 19:13 exim4
s2:/var/spool# cd exim4
s2:/var/spool/exim4# ls -la|grep scan
drwxr-x--- 2 Debian-exim Debian-exim 4096 2005-08-14 19:13 scan
Clamd is run as user clamav,
s2:/var/log/exim4# ps -Af|grep clamd
clamav 12700 1 0 19:46 ? 00:00:00 /usr/sbin/clamd
so I've added clamav to the Debian-exim group, then restarted exim4. This solved the problem for mails sent after restarting exim4.
The virus filtering has been verified by attaching a file containing the standard Eicar-Test-Signature. Mainlog entries:
2005-08-14 20:29:34 1E4NEk-0003Lu-3y H=(sirius) [192.168.0.136] F= rejected after DATA: A levele [Eicar-Test-Signa
ture] kartekony programot tartalmaz. Kerem virusirtas utan kuldje ujra!
The error message is in Hungarian (only ASCII characters used, no accented ones).
Note: It seems that Outlook cannot handle the reject response from the SMTP server (exim4). Raises error code 0x800CCC69 and reports some random characters as the reponse of the SMTP server. Anybody knows why?
I hope this helps to configure ClamAV for Exim4.
Greetings, Ferenczi Viktor - Hungary
[ Parent | Reply to this comment ]
malware acl condition: clamd: ClamAV \
returned /var/spool/exim4/scan/1EqAmc-0005Be-UI: \
Access denied. ERROR
Unfortunately, in my case this wasn't going to solve anything. This is because I've got exim4 and clamd running on different machines. I'm running woody with exim-daemon-heavy 4.34.9 (a backport; the machine can't be upgraded any further for now) on the one and sarge with clamav 0.87.1 on the other.
For Exim on the woody box I have the following configuration:
- in ./acl/40_exim4-config_check_data:
malware = *
- in ./main/02_exim4-config_options:
av_scanner = clamd:192.168.242.11 3310
What I wanted to do was have the Exim contact clamd on the other box and stream its data there to have it checked. It's supposed to do this by contacting clamd on port 3310, issuing the 'STREAM' command after which clamd returns 'PORT <number>', establishing a concurrent connecting to that port number, stream the data there, and then receive the results on the 3310 connection. I tested this manually and it works just fine as far as I can tell.
However, this is not what Exim (Exiscan) does. Apparently, Exiscan can only issue the clamd 'SCAN' command and doesn't know about 'STREAM'. After 'SCAN', it sends a file name, '/var/spool/exim4/scan/<message_id>', for clamd to scan. Clamd on the other machine tries to access this file, finds nothing and assumes it's an acl problem. That's the reason for the error message.
The way I solved this problem was to export the mail server's /var/spool/exim4/scan directory to the same location on the clamd machine's file system and by making sure that clamd had sufficient access to it. It's not the most elegant solution, but it works.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
av_scanner = clamd:/var/run/clamav/clamd.ctl
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
This will only work if you have the "heavy" version of exim4 installed:
apt-get install exim4-daemon-heavy
The "light" version will of course complain. I recently setup a new server using this recipe and all works fine, so I'm guessing that something is different on your system, or you didn't follow things 100% to the letter..
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
(Jeesh, I can't believe I had light installed.. What a cad!)
[ Parent | Reply to this comment ]
After I added clamav to the Debian-exim group and restarted the deamon it works as expected.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Just tried this process on my sarge box and came across an issue.
I installed clamav (used the debian-volatile source) and freshclam updates fine. I also installed exim-daemon-heavy and that seems fine too.
Only when I changed 40_exim4-config_check_data as per your suggestion I get mails bouncing (when I test via telnet) with an error 451, local problem. Of course, I then don't get the mail.
Amy ideas what could be the cause? I see nothing in mainlog or rejectlog.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
clamd: unable to connect to UNIX socket /var/run/clamav/clamd.ctl
but the file exists. I see both exim and clam start as S20 under rc2.d - just wondering if maybe clamav isn't fully started when exim4 starts? I could try changing the numbers - but - I don't want to keep bouncing a live web/mail server - just wondering if any others have seen this? The clam headers appear to be correctly present in my mail.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
This seems like a better place for it than main/01_exim4-config_listmacrosdefs, because it's not a macro.
Otherwise, this page is very helpful.
[ Parent | Reply to this comment ]
http://shellcode.org/Shellcode/linux/ascii/
Larry.
[ Parent | Reply to this comment ]