Zimbra Custom SpamAssassin Rules: 5 Easy Steps, 2 Mistakes

Zimbra custom SpamAssassin rules let you add your own spam tests to the filter that ships with Zimbra: a rule for a word in the subject, a pattern in the body, or a change of the thresholds that decide what goes to Junk. This guide keeps the working commands from the original post (Zimbra 8.6 and later, run on the Zimbra MTA) and adds what the Zimbra Administrator Guide says about scores and percentages, because that is where most custom rules go wrong.

The most important fact is this: Zimbra uses a percentage value for "spaminess" based on a SpamAssassin score of 20 as 100%. Messages tagged between 33% and 75% are considered spam and delivered to the user's Junk folder; messages above 75% are always spam and are discarded. Keep this in mind for every score you write in a Zimbra custom SpamAssassin rule.

How Zimbra custom SpamAssassin rules fit in

Zimbra uses SpamAssassin to identify spam, with Amavis running the checks on the MTA. Your own rules are plain SpamAssassin rule files in the local rules directory of Zimbra, and the Amavis configuration template decides which mail is checked at all. The three places used in this guide are:

  • zmlocalconfig keys on the MTA, to keep the rule updates and rule compilation working.
  • zmprov attributes for the subject tag and the tag and kill percentages.
  • /opt/zimbra/data/spamassassin/localrules/ for your rule file, and /opt/zimbra/conf/amavisd.conf.in for the Amavis policy.

All Zimbra custom SpamAssassin rules in this guide run on the MTA, so run the commands as the zimbra user on the MTA node (su - zimbra) unless a step says to edit a file.

Step 1: enable updates before you add Zimbra custom SpamAssassin rules

These local configuration keys switch on the automatic update, restart and compilation of the rules. Restart Amavis and the MTA afterwards so that the filter reads the new settings:

su - zimbra
zmlocalconfig -e antispam_enable_rule_updates=true
zmlocalconfig -e antispam_enable_restarts=true
zmlocalconfig -e antispam_enable_rule_compilation=true
zmamavisdctl restart
zmmtactl restart

The Administrator Guide lists zmamavisdctl and zmmtactl as the tools to start, stop and restart the Amavis daemon and the MTA, and says that SpamAssassin's sa-update tool, installed in /opt/zimbra/common/bin, updates the rules from the SpamAssassin organization.

Step 2: check and set the subject tag and the percentages

Read the three attributes first, then change them. zimbraSpamSubjectTag is the text added to the subject of tagged mail, zimbraSpamTagPercent the percentage above which a message is tagged as spam and zimbraSpamKillPercent the percentage above which it is discarded:

zmprov gcf zimbraSpamSubjectTag
zmprov gcf zimbraSpamTagPercent
zmprov gcf zimbraSpamKillPercent

zmprov mcf zimbraSpamSubjectTag ***SPAM***
zmprov mcf zimbraSpamKillPercent 50
zmprov mcf zimbraSpamTagPercent 25

With the percentages above, tagging starts at 25% (a score of 5) and the kill level is 50% (a score of 10). The guide says that you can also change the percentages and the subject prefix in the Administration Console under Home, Configure, Global Settings, AS/AV, Spam checking Settings.

Step 3: write your first of the Zimbra custom SpamAssassin rules

Create a rule file in the local rules directory. The example marks every message whose subject contains the word OH_YEAH:

vim /opt/zimbra/data/spamassassin/localrules/huupv.cf
zimbra custom spamassassin rules: a header rule for the subject OH_YEAH in huupv.cf
A Zimbra custom SpamAssassin rule file with header, describe and score lines.

Every one of the Zimbra custom SpamAssassin rules has three lines. header defines the test (the Subject header matches the pattern, ignoring case), describe is a short text that appears in reports and score is the number added to the message score when the test matches. The syntax is the standard SpamAssassin one, described in the Mail::SpamAssassin::Conf documentation.

Now do the arithmetic. The score is 40.0. In Zimbra's scale 20 is 100%, so this rule alone brings a message to 200%, far above the 75% (or the 50% set in step 2) at which messages are discarded. A message with OH_YEAH in the subject is not delivered to the Junk folder but dropped. For a test rule that is fine; for production use a small score, for example 5, so that the rule only adds to the evidence.

Step 4: Zimbra custom SpamAssassin rules for URLs in the body

A body rule works on the text of the message. The original post shows a rule that matches links that start with http or https:

body      HUU_RULE    m{http(s)?://(?:[a-z]+[0-9]+\.)*}
describe  HUU_RULE    http and https contains as spam
score     HUU_RULE    40

Use it only as a pattern example. The expression matches every http or https link, and with a score of 40 (200%) every newsletter, ticket notification or shared document link in your mail would be treated as spam and discarded. Lower the score and make the expression more specific, for example a known bad domain, before you use a Zimbra custom SpamAssassin rule like this on a real server.

Step 5: spam-check internal mail with your Zimbra custom SpamAssassin rules

By default Zimbra does not spam-check mail that originates from your own users. The policy bank ORIGINATING in the Amavis template controls it. First back up the file, then edit it:

cp /opt/zimbra/conf/amavisd.conf.in /opt/zimbra/conf/amavisd.conf.in.bk
vim /opt/zimbra/conf/amavisd.conf.in

In the $policy_bank{'ORIGINATING'} block the post changes one line so that internal mail is checked:

bypass_spam_checks_maps   => [0],   # spam-check internal mail

Leave the other lines of the block as they are, including the DKIM forward and the disclaimer settings. Restart Amavis with zmamavisdctl restart after the edit. Internal mail is then scored by your Zimbra custom SpamAssassin rules too, so a harsh rule like the two examples above can discard messages between your own users.

Test your Zimbra custom SpamAssassin rules

  1. Restart the services (zmamavisdctl restart, zmmtactl restart) after every rule change.
  2. Send a test message from an outside address with the trigger word in the subject, and check whether it arrives, lands in Junk or is discarded; compare with the percentages above.
  3. Start with a low score and raise it only when the rule proves reliable. Remember that a message that is discarded leaves no copy for the user.
  4. Keep the files with your Zimbra custom SpamAssassin rules in version control or at least a copy, because an update of Zimbra should not be the first time you notice a missing file.

FAQ

What is the default percentage for spam in Zimbra?

According to the Administrator Guide, a SpamAssassin score of 20 equals 100%, messages from 33% to 75% go to the Junk folder and messages above 75% are discarded. Your server may use other values if somebody changed them; read them with zmprov gcf.

Where can I find more examples of Zimbra custom SpamAssassin rules?

The SpamAssassin configuration reference documents every rule type, such as header, body, uri and meta. For more on whitelists and blacklists see the posts on checking Zimbra account status and locking a Zimbra account.

How does Zimbra learn from users?

Messages that users move to or from the Junk folder are copied to training mailboxes, and the Zimbra tool zmtrainsa retrieves them to train the filter, as the Administrator Guide explains. That is separate from your own rules.

References

In short: enable the rule updates, set sensible percentages, put small-score rules in localrules, and always compare your score with Zimbra's 20 = 100% scale before you rely on Zimbra custom SpamAssassin rules.

Comments

Popular posts from this blog

How to Play Minecraft Bedrock Edition on Linux: A Comprehensive Guide for Tech Professionals

Linux desktop frozen? My 5 go-to tricks to try - before forcing a hard reboot

The Ultimate Guide: How to Set Up DXVK in Wine on Linux for Enhanced Gaming Performance