Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
tutoriaux:install-email-server:install-email-server-part-7 [2023/01/06 16:41] – supprimée - modification externe (Unknown date) 127.0.0.1 | tutoriaux:install-email-server:install-email-server-part-7 [2024/07/06 01:17] (Version actuelle) – frater | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== Postfix and SpamAssassin Content Filter ====== | ||
+ | Specifically, | ||
+ | ===== Email Header and Body Checks with Postfix SMTP Server ===== | ||
+ | |||
+ | Postfix provides 4 simple content checking parameters. | ||
+ | |||
+ | * header_checks | ||
+ | * mime_header_checks | ||
+ | * nested_header_checks | ||
+ | * body_checks | ||
+ | |||
+ | Postfix will check all inbound emails when any of the above parameters is being used. Each parameter points to a lookup table containing regular expression patterns and actions. The patterns are compared to strings within email messages (header and body). If Postfix finds a match, the specified action is executed. Header and body checks are done by the Postfix '' | ||
+ | |||
+ | <WRAP center round tip> | ||
+ | when checking with those parameters, you have 2 main options: | ||
+ | |||
+ | |||
+ | * DISCARD : appear to accept the message, but the message will be silently discarded | ||
+ | * REJECT | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | There are mainly two types of regular expressions that can be used by Postfix. | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Postfix comes with POSIX regular expression support, but PCRE is way faster. To use PCRE in Postfix, you need to install the '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Run the following command and you will see '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | ==== Header Checks ==== | ||
+ | |||
+ | To enable header_checks in Postfix, open the main configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following line at the end of the file. | ||
+ | |||
+ | < | ||
+ | |||
+ | <WRAP round info> | ||
+ | You may use [[https:// | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then you need to create the ''/ | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | You can add regular expression checking like below. | ||
+ | |||
+ | < | ||
+ | /free mortgage quote/ | ||
+ | /repair your credit/ | ||
+ | </ | ||
+ | |||
+ | The lefthand key is a regular expression enclosed by two forward slashes. If any of the strings on the left-hand appear in any of the headers of an email message (these would most likely show up in the Subject: header), the message is rejected during the SMTP dialog. By default regular expression checking is not case-sensitive. | ||
+ | |||
+ | You can also use DISCARD, instead of REJECT. | ||
+ | |||
+ | < | ||
+ | /free mortgage quote/ | ||
+ | /repair your credit/ | ||
+ | </ | ||
+ | |||
+ | This will cause Postfix to __claim successful delivery and silently discard__ the message. DISCARD makes it look as if the message was delivered even though it was simply thrown away. | ||
+ | |||
+ | I often use DISCARD when I don’t want the spammer to know I have blocked a certain phrase for incoming email. DISCARD can also be useful to minimize the backscatter problem. If an innocent user’s email address is used as the sender address, you can claim successful delivery, so that the innocent user does not receive bounce messages. | ||
+ | |||
+ | Some stupid spammers use multiple email addresses in the '' | ||
+ | |||
+ | < | ||
+ | / | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | The above lines will check if an Gmail/ | ||
+ | |||
+ | You may also want to do the same with the '' | ||
+ | |||
+ | < | ||
+ | / | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | Some spammers use blank email address in the '' | ||
+ | |||
+ | < | ||
+ | / | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | Once you finish editing the header_checks lookup file, you need to build the index file. | ||
+ | |||
+ | <code bash> | ||
+ | sudo postmap / | ||
+ | </ | ||
+ | |||
+ | Then restart Postfix for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | ==== Body Checks ==== | ||
+ | |||
+ | In addition to header checks, Postfix can check the body of an email message. To enable '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following line at the end of the file. | ||
+ | |||
+ | < | ||
+ | |||
+ | Save and close the file. Then you need to create the ''/ | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | You can add regular expression checking like below. | ||
+ | |||
+ | < | ||
+ | /free mortgage quote/ | ||
+ | /repair your credit/ | ||
+ | </ | ||
+ | |||
+ | You can use DISCARD, instead REJECT. | ||
+ | |||
+ | < | ||
+ | /free mortgage quote/ | ||
+ | /repair your credit/ | ||
+ | </ | ||
+ | |||
+ | The patterns indicated by the '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Then restart Postfix for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | ===== SpamAssassin Content Filter ===== | ||
+ | |||
+ | The built-in content checking in Postfix is very simple. However, there is no way to whitelist individual messages that you might want to receive despite their containing phrases that trigger a rejection and you might not want to reject or discard an email message based on a single rule. For more sophisticated analysis, we need to use a dedicated content filter (such as SpamAssassin) specifically designed to detect spam. | ||
+ | |||
+ | SpamAssassin is a score-based system. It will check email message against a large set of rules, in contrast to a single rule in Postfix. Each rule adds or removes points in the message’s score. If the score is high enough (by default 5.0), the message is considered spam. | ||
+ | |||
+ | ==== Install SpamAssassin ==== | ||
+ | |||
+ | <WRAP round info> | ||
+ | If you used iRedMail to set up your mail server, then SpamAssassin is already installed alongside Amavis, which can read SpamAssassin rules. You don’t need to follow the instructions in this section. | ||
+ | </ | ||
+ | |||
+ | Run the following command to install SpamAssassin from the default Debian/ | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | During the installation, | ||
+ | |||
+ | By default, the '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Then start SpamAssassin. | ||
+ | |||
+ | <code bash> | ||
+ | ==== Integrate SpamAssassin with Postfix SMTP Server as a Milter ==== | ||
+ | |||
+ | There are several ways you can use to integrate SpamAssassin with Postfix. I prefer to use SpamAssassin via the sendmail milter interface, because it allows me to reject an email when it gets a very high score such as 8, so it will never be seen by the recipient. | ||
+ | |||
+ | Install the '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Next, edit ''/ | ||
+ | |||
+ | < | ||
+ | # Milter configuration | ||
+ | milter_default_action = accept | ||
+ | milter_protocol = 6 | ||
+ | smtpd_milters = local: | ||
+ | non_smtpd_milters = $smtpd_milters | ||
+ | </ | ||
+ | |||
+ | If you have configured [[tutoriaux: | ||
+ | |||
+ | < | ||
+ | # Milter configuration | ||
+ | milter_default_action = accept | ||
+ | milter_protocol = 6 | ||
+ | smtpd_milters = local: | ||
+ | non_smtpd_milters = $smtpd_milters | ||
+ | </ | ||
+ | |||
+ | If you haven’t configured OpenDMARC, then you should remove '' | ||
+ | |||
+ | Save and close the file. Now open the ''/ | ||
+ | |||
+ | < | ||
+ | |||
+ | Uncomment this line and change 15 to your preferred reject score such as 8. | ||
+ | |||
+ | < | ||
+ | |||
+ | If the score of a particular email is over 8, Spamassassin would reject it and you would find a message like below in the ''/ | ||
+ | |||
+ | < | ||
+ | |||
+ | If you want the sender to see a different reject text, then add the '' | ||
+ | |||
+ | < | ||
+ | |||
+ | Save and close the file. Restart Postfix and Spamass Milter for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | <WRAP round tip> | ||
+ | iRedMail users need to start the SpamAssassin service in order to use '' | ||
+ | </ | ||
+ | |||
+ | <code bash> | ||
+ | sudo systemctl start spamassassin | ||
+ | sudo systemctl enable spamassassin | ||
+ | </ | ||
+ | ==== Checking Email Header and Body with SpamAssassin ==== | ||
+ | |||
+ | SpamAssassin ships with many spam detection rules in ''/ | ||
+ | |||
+ | In the ''/ | ||
+ | |||
+ | < | ||
+ | header MISSING_HEADERS | ||
+ | describe MISSING_HEADERS | ||
+ | </ | ||
+ | |||
+ | The first line tests if the To: header exists in an email message. The second line, which is optional, explains what the first line does. The uppercase letters is the name of this test. | ||
+ | |||
+ | The following 3 lines are for testing if there’s a '' | ||
+ | |||
+ | < | ||
+ | header __HAS_DATE | ||
+ | meta MISSING_DATE | ||
+ | describe MISSING_DATE | ||
+ | </ | ||
+ | |||
+ | And these 3 lines are for testing if there’s a '' | ||
+ | |||
+ | < | ||
+ | header __HAS_FROM | ||
+ | meta MISSING_FROM | ||
+ | describe MISSING_FROM | ||
+ | </ | ||
+ | |||
+ | You might want to use the Cron job shipped with SpamAssassin to automatically update SpamAssassin’s rules on a daily basis. If so, open the ''/ | ||
+ | |||
+ | ====== Set Custom Score for Existing Rules ====== | ||
+ | |||
+ | In the '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | For example, RFC 5322 requires that every email message must have //From:// and //Date:// header fields, so I can set a very high score if either of them is missing in an email message by appending the following two lines in '' | ||
+ | |||
+ | < | ||
+ | score MISSING_FROM | ||
+ | score MISSING_DATE | ||
+ | </ | ||
+ | |||
+ | Although the To: header field is not mandatory in RFC 5322, I prefer to set a high score if it’s missing in an email message because I have never seen a legitimate email missing this header field. | ||
+ | |||
+ | < | ||
+ | score MISSING_HEADERS 3.0 | ||
+ | </ | ||
+ | |||
+ | Some spammers uses two email addresses in the //From:// header field like below. | ||
+ | |||
+ | < | ||
+ | |||
+ | I think the default score for this kind of email is low, I prefer to set it to 3.0. | ||
+ | |||
+ | < | ||
+ | |||
+ | There are spammers who send empty message with no subject and no textual parts in the body. I set the score for this kind of email to 5.0, so it will be placed to spam folder. Why read it if it’s empty? | ||
+ | |||
+ | < | ||
+ | |||
+ | And other spammers often ask you to send a read receipt, I set the score to 2.0 for this kind of email. | ||
+ | |||
+ | < | ||
+ | |||
+ | There are some spammers use different domain names in the //From:// and // | ||
+ | |||
+ | < | ||
+ | |||
+ | I also have seen some spammers using non-existent domain name in the //From:// header field. I set a 5.0 score for this type of email. | ||
+ | |||
+ | < | ||
+ | |||
+ | Last but not least, many spammers spoof the gmail.com domain in the //From:// header field. I set a 2.5 score this kind of email. | ||
+ | |||
+ | < | ||
+ | ====== Adding Your Own Rules ====== | ||
+ | |||
+ | |||
+ | You can add custom SpamAssassin rules in ''/ | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | ===== Header Rules ===== | ||
+ | |||
+ | For example, some spammers use the same email address in the From: and To: header, you can add the following lines at the end of the file to add scores to such emails. | ||
+ | |||
+ | < | ||
+ | header | ||
+ | describe FROM_SAME_AS_TO | ||
+ | score FROM_SAME_AS_TO | ||
+ | </ | ||
+ | |||
+ | Some spammers use an empty address for the Envelope From address (aka the Return Path header). Although this is legitimate for sending bounce messages, I prefer to give this kind of email a score. | ||
+ | |||
+ | < | ||
+ | header | ||
+ | describe | ||
+ | score | ||
+ | </ | ||
+ | |||
+ | If you have configured [[tutoriaux: | ||
+ | |||
+ | < | ||
+ | header | ||
+ | describe | ||
+ | score | ||
+ | </ | ||
+ | |||
+ | The above code tells SpamAssassin to check if the Authentication-Results header contains the string “dmarc=fail”. If found, increase the score by 3.0. | ||
+ | ===== Body Rules ===== | ||
+ | |||
+ | You can tell SpamAssassin to increase the score of an email if a certain phrase is found in the body. For example, many spammers use the recipient’s email address in the first body line like below. | ||
+ | |||
+ | < | ||
+ | Hi frater@nox-rhea.org | ||
+ | Hello frater@nox-rhea.org | ||
+ | Dear frater@nox-rhea.org | ||
+ | </ | ||
+ | |||
+ | I don’t want to talk with people who doesn’t bother writing my name in the first line of email. So I created a rule in SpamAssassin to filter this kind of email. | ||
+ | |||
+ | < | ||
+ | body BE_POLITE | ||
+ | describe | ||
+ | score | ||
+ | </ | ||
+ | |||
+ | Regular expression in SpamAssassin is case-sensitive by default, you can add the '' | ||
+ | ===== Add Negative Scores ===== | ||
+ | |||
+ | You can also add negative score to good emails, so there will be less false positives. For example, many of my blog readers ask me Linux questions and I don’t think spammers would include words like '' | ||
+ | |||
+ | < | ||
+ | body GOOD_EMAIL | ||
+ | describe | ||
+ | score | ||
+ | </ | ||
+ | |||
+ | If the email body contains a Linux distro’s name, then add a negative score (-4.0). | ||
+ | |||
+ | There are some common phrases that is included in legitimate bounce messages, so I can add negatives scores to these email messages. | ||
+ | |||
+ | < | ||
+ | body BOUNCE_MSG | ||
+ | describe | ||
+ | score | ||
+ | </ | ||
+ | |||
+ | Note that body rules also include the Subject as the first line of the body content. | ||
+ | ===== Meta Rules ===== | ||
+ | |||
+ | In addition to header and body rules, there’s also meta rules. Meta rules are combinations of other rules. You can create a meta rule that fires off when two or more other rules are true. For example, I occasionally receive emails saying that the sender wants to apply for a job and a resume is attached. I have never said on my website that I need to hire people. The attachment is used to spread virus. I created the following meta rule to filter this kind of email. | ||
+ | |||
+ | < | ||
+ | body __RESUME | ||
+ | meta RESUME_VIRUS | ||
+ | describe | ||
+ | score | ||
+ | </ | ||
+ | |||
+ | The first sub rule '' | ||
+ | |||
+ | < | ||
+ | rawbody | ||
+ | describe | ||
+ | </ | ||
+ | |||
+ | My meta rule '' | ||
+ | |||
+ | Now you learned how to add score if a string is found. What if you want to add score when a string doesn’t exist in the email headers? Well, you can use the '' | ||
+ | |||
+ | < | ||
+ | header __AT_IN_FROM | ||
+ | meta NO_AT_IN_FROM | ||
+ | score NO_AT_IN_FROM | ||
+ | </ | ||
+ | |||
+ | The first line checks if the @ sign exists in the From: header. The second line defines a meta rule, which fires off when '' | ||
+ | |||
+ | You can also add the following lines to check if a dot exists in the From: address. | ||
+ | |||
+ | < | ||
+ | header __DOT_IN_FROM | ||
+ | meta | ||
+ | score NO_DOT_IN_FROM | ||
+ | </ | ||
+ | ==== Whitelist ==== | ||
+ | |||
+ | You can use the '' | ||
+ | |||
+ | < | ||
+ | whitelist_from frater@nox-rhea.org | ||
+ | whitelist_from *@canonical.com | ||
+ | </ | ||
+ | |||
+ | A whitelisted sender has a '' | ||
+ | ==== Blacklist ==== | ||
+ | |||
+ | To blacklist a sender, use the blacklist_from parameter, which has the same format as '' | ||
+ | |||
+ | < | ||
+ | blacklist_from spam@example.com | ||
+ | blacklist_from *@example.org | ||
+ | </ | ||
+ | ==== Checking Syntax and Restart ==== | ||
+ | |||
+ | After saving the '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Then restart SpamAssassin for the changes to take effect. (If you use Amavis with Spamassasin as in iRedMail, you just need to restart Amavis: '' | ||
+ | |||
+ | <code bash> | ||
+ | sudo systemctl restart spamassassin | ||
+ | </ | ||
+ | ==== SpamAssassin’s Builtin Whitelist ==== | ||
+ | |||
+ | It’s worth mentioning that SpamAssassin ships with its own whitelist. There are several files under ''/ | ||
+ | ==== Move Spam into the Junk Folder ==== | ||
+ | |||
+ | <WRAP round important> | ||
+ | iRedMail has this configured out-of-the-box. | ||
+ | </ | ||
+ | |||
+ | I’m going to show you how to move spam to Junk folder with the Dovecot IMAP server and the sieve plugin. This method requires that inbound emails are delivered to the message store via the Dovecot “deliver” LDA (local delivery agent). If you can find the following text in ''/ | ||
+ | |||
+ | < | ||
+ | postfix/ | ||
+ | </ | ||
+ | |||
+ | or | ||
+ | |||
+ | < | ||
+ | delivered via dovecot service | ||
+ | </ | ||
+ | |||
+ | Run the following command install dovecot-sieve from Ubuntu software repository. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | This package installs two configuration files under ''/ | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the sieve plugin to local delivery agent (LDA). | ||
+ | |||
+ | < | ||
+ | protocol lda { | ||
+ | # Space separated list of plugins to load (default is global mail_plugins). | ||
+ | mail_plugins = $mail_plugins sieve | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Save and close the file. If you can find the '' | ||
+ | |||
+ | < | ||
+ | protocol lmtp { | ||
+ | mail_plugins = quota sieve | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Edit the ''/ | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Sieve scripts are stored under each user’s home directory. If you followed my PostfixAdmin tutorial and are using virtual mailbox domains, then you need to enable '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Save and close the file. Then open the '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Go to line 79 and add the following line, which tells Sieve to always execute the '' | ||
+ | |||
+ | < | ||
+ | sieve_before = / | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then create the sieve script. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following lines, which tells Dovecot to move any email messages with the '' | ||
+ | |||
+ | < | ||
+ | require " | ||
+ | |||
+ | if header :contains " | ||
+ | { | ||
+ | | ||
+ | stop; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Save and close the file. We can compile this script, so it will run faster. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Now there is a binary file saved as / | ||
+ | |||
+ | <code bash> | ||
+ | ==== Set Message Maximum Size ==== | ||
+ | |||
+ | By default, SpamAssassin does not check messages with attachments larger than 500KB, as indicated by the following line in the ''/ | ||
+ | |||
+ | < | ||
+ | spamc[18922]: | ||
+ | </ | ||
+ | |||
+ | The default '' | ||
+ | |||
+ | < | ||
+ | #Spamc options | ||
+ | OPTIONS=" | ||
+ | </ | ||
+ | |||
+ | The empty '' | ||
+ | |||
+ | <code bash> | ||
+ | sudo systemctl restart spamass-milter | ||
+ | </ | ||
+ | ==== How to Configure Individual User Preferences ==== | ||
+ | |||
+ | You may want to set custom rules for emails sent to a specific address on the mail server. I like this feature very much. I have a contact email address for this blog, which is only used for keeping contact with readers. **I don’t use the contact email address elsewhere**, | ||
+ | |||
+ | First, edit the SpamAssassin main configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | sudo nano / | ||
+ | </ | ||
+ | |||
+ | Add the following line to allow user rules. | ||
+ | |||
+ | <code bash> | ||
+ | allow_user_rules 1 | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Next, edit the SpamAssassin environment file. | ||
+ | |||
+ | <code bash> | ||
+ | sudo nano / | ||
+ | </ | ||
+ | |||
+ | Find the following line. | ||
+ | |||
+ | < | ||
+ | OPTIONS=" | ||
+ | </ | ||
+ | |||
+ | We need to change it to | ||
+ | |||
+ | < | ||
+ | OPTIONS=" | ||
+ | </ | ||
+ | |||
+ | Where: | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Save and close the file. Then restart SpamAssassin. | ||
+ | |||
+ | <code bash> | ||
+ | sudo systemctl restart spamassassin | ||
+ | </ | ||
+ | |||
+ | By default, spamass-milter will send only the local part of email address to SpamAssassin. We need to make it send the full email address. Edit the spamass-milter configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | sudo nano / | ||
+ | </ | ||
+ | |||
+ | Find the following line. | ||
+ | |||
+ | < | ||
+ | OPTIONS=" | ||
+ | </ | ||
+ | |||
+ | Add the following option to this line. | ||
+ | |||
+ | < | ||
+ | -e yourdomain.com | ||
+ | </ | ||
+ | |||
+ | Like this: | ||
+ | |||
+ | < | ||
+ | OPTIONS=" | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | <code bash> | ||
+ | sudo systemctl restart spamass-milter | ||
+ | </ | ||
+ | |||
+ | Now send an email from Gmail, Hotmail, etc. to your domain email address. You will find the spamassassin directory is automatically created under ''/ | ||
+ | |||
+ | <code bash> | ||
+ | cd / | ||
+ | </ | ||
+ | |||
+ | You can use a command-line text editor to create the per-user preference file here. This file __must be named__ as '' | ||
+ | |||
+ | <code bash> | ||
+ | sudo nano user_prefs | ||
+ | </ | ||
+ | |||
+ | You can add custom rules in this file just as you would do in the ''/ | ||
+ | |||
+ | For instance, I found many spammers end their email body with an unsubscribe link to let you remove future contact. I didn’t subscribe to their spam and I don’t think the unsubscribe link will remove my email address from their contact database. So I use SpamAssassin to score this kind of email. | ||
+ | |||
+ | < | ||
+ | body SUBSCRIPTION_SPAM | ||
+ | describe | ||
+ | score | ||
+ | </ | ||
+ | |||
+ | Sometimes the email body doesn’t contain the word “unsubscribe”, | ||
+ | |||
+ | < | ||
+ | header | ||
+ | describe | ||
+ | score | ||
+ | </ | ||
+ | |||
+ | I occasionally receive emails from Chinese spammers whose '' | ||
+ | |||
+ | < | ||
+ | score FROM_DOMAIN_NOVOWEL 4.0 | ||
+ | </ | ||
+ | |||
+ | Some spam emails use many images in the body but contains very little text. The default score for this kind of email is 1.9, but I prefer to set a high score for my contact email address. | ||
+ | |||
+ | < | ||
+ | score HTML_IMAGE_RATIO_02 4.0 | ||
+ | </ | ||
+ | |||
+ | I also received a spam email with my email address in the subject, so I can add a high score to it. | ||
+ | |||
+ | < | ||
+ | header | ||
+ | describe | ||
+ | score | ||
+ | </ | ||
+ | |||
+ | Some spammers use BCC (Blind Carbon Copy) to hide other recipients. I don’t want to receive such email. So I made the following rule. If my domain name is not in the To: header, add 3.0 to the email. | ||
+ | |||
+ | < | ||
+ | header __DOMAIN_IN_TO | ||
+ | meta | ||
+ | score DOMAIN_NOT_IN_TO | ||
+ | </ | ||
+ | |||
+ | After adding custom rules, close the file and run the following command to check syntax. Silent output means there’s no syntax error. | ||
+ | |||
+ | <code bash> | ||
+ | sudo spamassassin --lint | ||
+ | </ | ||
+ | |||
+ | Finally, restart SpamAssassin for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | sudo systemctl restart spamassassin | ||
+ | </ | ||
+ | |||
+ | Now you can test the user preferences by sending test emails from other email services to your own domain email address. | ||
+ | ==== Whitelisting for Specific Email Addresses ==== | ||
+ | |||
+ | Let’s say you have an email address that accepts emails from a few email addresses, and you want to block all other senders. It’s very easy to accomplish this. Go to ''/ | ||
+ | |||
+ | < | ||
+ | whitelist_from *@your-own-domain.com | ||
+ | whitelist_from someboday@gmail.com | ||
+ | </ | ||
+ | |||
+ | Next, add all domains to the blacklist. | ||
+ | |||
+ | < | ||
+ | blacklist_from * | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Restart SpamAssassin for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | sudo systemctl restart spamassassin | ||
+ | </ | ||
+ | ==== Reject or Bounce ==== | ||
+ | |||
+ | If a receiving SMTP server determines during the SMTP conversation that it will not accept the message, it rejects the message. Sometimes the SMTP server accepts a message and later discovers that it cannot be delivered, perhaps the intended recipient doesn’t exist or there is a problem in the final delivery. In this case, the SMTP server that has accepted the message bounces it back to the original sender by sending an error report, usually including the reason the original message could not be delivered. | ||
+ | |||
+ | You should not bounce spam, because the email address in the '' | ||
+ | ==== URIBL_BLOCKED ==== | ||
+ | |||
+ | By default, SpamAssassin enables URIBL rule, which checks if an email message contains links that are identified as spam by URIBL. This is a very effective anti-spam measurement. However, you might be blocked from querying URIBL. Check the raw email headers of an inbound email message, find the '' | ||
+ | |||
+ | < | ||
+ | X-Spam-Status: | ||
+ | DKIM_VALID, | ||
+ | SUBSCRIPTION_SPAM, | ||
+ | autolearn=no autolearn_force=no version=3.4.2 | ||
+ | </ | ||
+ | |||
+ | If you can find **URIBL_BLOCKED** in this header, that means you are blocked from querying URIBL. Most of the time it’s because you are not using your own local DNS resolver. You can run the following command on your mail server to test which DNS server you are using to query URIBL. | ||
+ | |||
+ | < | ||
+ | host -tTXT 2.0.0.127.multi.uribl.com | ||
+ | </ | ||
+ | |||
+ | Sample output: | ||
+ | |||
+ | < | ||
+ | 2.0.0.127.multi.uribl.com descriptive text " | ||
+ | </ | ||
+ | |||
+ | To fix this error, you need to run your own local DNS resolver on your mail server. | ||
+ | |||
+ | Once your local DNS resolver is up and running, test URIBL again. | ||
+ | |||
+ | <code bash> | ||
+ | host -tTXT 2.0.0.127.multi.uribl.com | ||
+ | </ | ||
+ | |||
+ | If you see the following output, it means you are now allowed to query URIBL. | ||
+ | |||
+ | < | ||
+ | 2.0.0.127.multi.uribl.com descriptive text " | ||
+ | </ | ||
+ | |||
+ | From here on out, inbound email messages won’t have the URIBL_BLOCKED tag in the '' | ||
+ | ==== Prevent Outgoing Spam ==== | ||
+ | |||
+ | Postfix header and body checks can also be used to prevent outgoing spam. For example, if you don’t want your email server to send emails to a particular email address, you can add the following line to ''/ | ||
+ | |||
+ | < | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | The above line tests if the '' | ||
+ | |||
+ | Save and close the file. Then run the following command to rebuild the hash table. | ||
+ | |||
+ | <code bash> | ||
+ | sudo postmap / | ||
+ | </ | ||
+ | |||
+ | Reload Postfix for the change to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | sudo systemctl reload postfix | ||
+ | </ | ||
+ | ==== Deleting Email Headers For Outgoing Emails ==== | ||
+ | |||
+ | You can use '' | ||
+ | |||
+ | For example, you might not want the recipient to know that you are using SpamAssassin on your mail server, then you can create the ''/ | ||
+ | |||
+ | <code bash> | ||
+ | sudo nano / | ||
+ | </ | ||
+ | |||
+ | And add the following lines in the file. This tells Postfix to delete the X-Spam-Status and X-Spam-Checker-Version header from the email message when sending emails. | ||
+ | |||
+ | < | ||
+ | / | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then edit the Postfix main configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | sudo nano / | ||
+ | </ | ||
+ | |||
+ | Add the following line at the end of the file. | ||
+ | |||
+ | < | ||
+ | smtp_header_checks = pcre:/ | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Next, run the following command. | ||
+ | |||
+ | <code bash> | ||
+ | sudo postmap / | ||
+ | </ | ||
+ | |||
+ | Reload Postfix for the change to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | sudo systemctl reload postfix | ||
+ | </ | ||
+ | ===== Other Tidbits ===== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | SpamAssassin 4.0 includes a HashBL plugin, which can check if a Bitcoin address in the email body has been used by scammers. And there’s also a new plugin called “Ole Macro” that can check if an email contains an Office attachment with a macro. This plugin would try to detect if the attched macro is malicious or not. | ||
+ | |||
+ | {{page> |