{"id":545,"date":"2009-01-10T20:45:57","date_gmt":"2009-01-10T12:45:57","guid":{"rendered":"http:\/\/www.liangliang.org.cn\/blog\/?p=545"},"modified":"2009-01-10T20:45:57","modified_gmt":"2009-01-10T12:45:57","slug":"how-to-filter-spam-with-spamassassin-and-postfix-in-debian","status":"publish","type":"post","link":"https:\/\/www.liangliang.org.cn\/?p=545","title":{"rendered":"How to filter spam with Spamassassin and Postfix in Debian"},"content":{"rendered":"<p>Posted by Admin on April 1st, 2008<\/p>\n<p>If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!<\/p>\n<p>We have already discussed how to install mail server with postfix+Devcot+SASL+Squirrel Mail in this article we will see how to add spam filter for postfix mail server.<\/p>\n<p>Apache SpamAssassin is an extensible email filter that is used to identify spam. Once identified, the mail can then be optionally tagged as spam for later filtering. It provides a command line tool to perform filtering, a client-server system to filter large volumes of mail, and Mail::SpamAssassin, a set of Perl modules allowing Apache SpamAssassin to be used in a wide variety of email systems.<\/p>\n<p>Install Spamassassin in Debian<\/p>\n<p>#apt-get install spamassassin spamc<\/p>\n<p>spamassassin package can also be integrated into a Mail Transport Agent such as postfix.<\/p>\n<p>Preparation<\/p>\n<p>By default Spamassassin will run as root users when you install from debian repository and is not started to avoid that, we are going to create a specific user and group for spamassassin.<\/p>\n<p>#groupadd -g 5001 spamd<\/p>\n<p>#useradd -u 5001 -g spamd -s \/sbin\/nologin -d \/var\/lib\/spamassassin spamd<\/p>\n<p>#mkdir \/var\/lib\/spamassassin<\/p>\n<p>#chown spamd:spamd \/var\/lib\/spamassassin<\/p>\n<p>we need to change some settings in \/etc\/default\/spamassassin and make sure you get the following values<\/p>\n<p>ENABLED=1<br \/>\nSAHOME=\u201d\/var\/lib\/spamassassin\/\u201d<br \/>\nOPTIONS=\"--create-prefs --max-children 5 --username spamd --helper-home-dir ${SAHOME} -s ${SAHOME}spamd.log\"<br \/>\nPIDFILE=\u201d${SAHOME}spamd.pid\u201d<\/p>\n<p>We are going to run spamd daemon as user spamd and make it use its own home dir (\/var\/lib\/spamassassin\/) and is going to output its logs in \/var\/lib\/spamassassin\/spamd.log<\/p>\n<p>spamassassin Configuration<\/p>\n<p>we need to give spamassassin some rules. The default settings are quite fine, but you might tweak them up a bit. So let\u2019s edit \/etc\/spamassassin\/local.cf and make it looks like that<\/p>\n<p>#vi \/etc\/spamassassin\/local.cf<\/p>\n<p>Modify this file looks like below<\/p>\n<p>rewrite_header Subject [***** SPAM _SCORE_ *****]<br \/>\nrequired_score 2.0<br \/>\n#to be able to use _SCORE_ we need report_safe set to 0<br \/>\n#If this option is set to 0, incoming spam is only modified by adding some \u201cX-Spam-\u201d headers and no changes will be made to the body.<br \/>\nreport_safe 0<\/p>\n<p># Enable the Bayes system<br \/>\nuse_bayes 1<br \/>\nuse_bayes_rules 1<br \/>\n# Enable Bayes auto-learning<br \/>\nbayes_auto_learn 1<\/p>\n<p># Enable or disable network checks<br \/>\nskip_rbl_checks 0<br \/>\nuse_razor2 0<br \/>\nuse_dcc 0<br \/>\nuse_pyzor 0<\/p>\n<p>we set spamassassin\u2019 spamd default settings to rewrite email subject to [***** SPAM _SCORE_ *****], where _SCORE_ is the score attributed to the email by spamassassin after running different tests, only if the actual score is greater or equal to 2.0. So email with a score lower than 2 won\u2019t be modified.<\/p>\n<p>To be able to use the _SCORE_ in the rewrite_header directive, we need to set report_safe to 0.<\/p>\n<p>In the next section, we tell spamassassin to use bayes classifier and to improve itself by auto-learning from the messages it will analyse.<\/p>\n<p>In the last section, we disable collaborative network such as pyzor, razor2 and dcc. Those collaborative network keep an up-to-date catalogue of know mail checksum to be recognized as spam. Those might be interresting to use, but I\u2019m not going to use them here as I found it took long enough to spamassassin to deal with spams only using it rules.<\/p>\n<p>Restart spamassassin using the following command<\/p>\n<p>#\/etc\/init.d\/spamassassin start<\/p>\n<p>Configuring Postfix call Spamassassin<\/p>\n<p>spamassassin will be invoked only once postfix has finished with the email.<\/p>\n<p>To tell postfix to use spamassassin, we are going to edit \/etc\/postfix\/master.cf<\/p>\n<p>#vi \/etc\/postfix\/master.cf<\/p>\n<p>Change the following line<\/p>\n<p>smtp inet n - - - - smtpd<\/p>\n<p>to<\/p>\n<p>smtp inet n - - - - smtpd<br \/>\n-o content_filter=spamassassin<\/p>\n<p>and then, at the end of master.cf file add the following lines<\/p>\n<p>spamassassin unix - n n - - pipe<\/p>\n<p>user=spamd argv=\/usr\/bin\/spamc -f -e<br \/>\n\/usr\/sbin\/sendmail -oi -f ${sender} ${recipient}<\/p>\n<p>Save and exit the file<\/p>\n<p>That\u2019s it our spam filter is setted up, we need to reload postfix settings and everything should be ready.<\/p>\n<p>#\/etc\/init.d\/postfix reload<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Posted by Admin on April 1st, 2008 If you&#8217;re new here, you may want to subscribe to my RSS feed. Tha&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-545","post","type-post","status-publish","format-standard","hentry","category-debian"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=545"}],"version-history":[{"count":0,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/545\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}