* 1所需套件
* 2简介
* 3设定SpamAssassin
* 4设定SpamAssassin和Postfix搭配使用
* 4.1 spamd/spamd模式(一般作法)
* 4.2利用Amavisd-new来呼叫SpamAssassin(建议做法)
* 5测试SpamAssassin
* 6其它
所需套件 spamassassin (sa)
简介
SpamAssassin
(SA)是利用Perl来进行文字分析以达到过滤垃圾邮件之目的。它的判断方式是藉由评分方式-若这封邮件符合某种特征,则加以评分。若总得分高于某项标准,则判定为垃圾邮件。
为了应用于高负载之服务器上,它也提供了spamc/spamd这组以Client/Server为架构之程式,如此可以有效降低SpamAssassin对系统资源的需求。但Debian预设上并不会启用这个服务。
而我们还可以替而使用Amavisd-new来呼叫SpamAssassin,也就是让Amavisd-new肩负扫毒及过滤垃圾邮件的重责。而在本人的系统中,这个方法比起使用spamc/spamd的做法快上了非常多,所以建议采用这种作法。
设定SpamAssassin 修改/etc/mail/spamassassin/local.cf如下,这是SpamAssassin的配置文件:
(详细设定请参考man Mail::SpamAssassin::Conf)
(修改/etc/mail/spamassassin/local.cf后会即时生效,用不着重新启动SpamAssassin。)
#
This is the right place to customize your installation of SpamAssassin.
#
# See 'perldoc Mail::SpamAssassin::Conf' for details of what can be
#
tweaked.
#
##################################################
#########################
#
# rewrite_subject 0
# report_safe 1
# trusted_networks 212.17.35.
#得分多少以上就会被判定为垃圾邮件
required_hits
5.0
#绝不会被判定为垃圾邮件之列表,即白名单
#请自行加以增删修改!
whitelist_from
tetralet@localhost
whitelist_from *@virtual.com
score USER_IN_BLACKLIST 20.00
score USER_IN_WHITELIST -1200.0
#在垃圾邮件上之标题上加上注记
rewrite_subject 1
#要如何处理垃圾邮件。因为邮件还会经过防毒程式的处理,所以必须设定为0。
#
0:将资讯写入邮件表头。
# 1:将垃圾邮件转为附件。
# 2:将垃圾邮件转为纯文字附件。
report_safe 0
#在处理垃圾邮件时,在邮件档头是否不加注详细原因?
use_terse_report 0
rewrite_header Subject ****SPAM****
#是否使用Bayesian
use_bayes 1
# Bayesian之资料库位置
bayes_path
/var/lib/amavis/.spamassassin/bayes
#是否启用Bayesian自动学习功能?
auto_learn
1
#略过RBLs之检查
skip_rbl_checks 0
#是否使用Razor version 2
use_razor2 1
#是否使用DCC (Distributed Checksum Clearinghouse)
use_dcc 1
#是否使用Pyzor
use_pyzor 1
#是否在邮件表头中加入DCC资讯
dcc_add_header 1
#是否能够进行DNS查询。直接设定为yes将有效加快amavis的启动速度。
#设定值:(yes|no|test)
dns_available yes
#检查是否为由内部网路所寄出的信件。
#若是,则永远不判断为垃圾邮件。 (评分减50分)
#其中的example.net请替换成您的Domain Name...
header
LOCAL_RCVD Received =~ /.*\(\S+\.example\.net\s+\[.*\]\)/
describe
LOCAL_RCVD Received from local machine
score LOCAL_RCVD -50
#一些自订的规则
body WINDOWOPEN /window\.open\(/i
describe WINDOWOPEN JavaScript.
Windows.Open
score WINDOWOPEN 3.5
#垃圾邮件计分规则。
## Optional Score
Increases
score DCC_CHECK 4.000
score RAZOR2_CHECK 2.500
score
BAYES_99 4.300
score BAYES_90 3.500
score BAYES_80 3.000
# Disabled scores
#防止中文主旨和中文收件者误判,建议再加上下列几行
score HEADER_8BITS 0
score HTML_COMMENT_8BITS 0
score SUBJ_FULL_OF_8BITS 0
score UPPERCASE_25_50 0
score
UPPERCASE_50_75 0
score UPPERCASE_75_100 0
# local domain from but ip
not match #网域和ip不符,疑似为垃圾信件
header __FROM_TEATIME Received =~ /from 4wei.us/i
header __FROM_TEATIME_IP Received =~ /\[88\.88\.88\.20\]/
meta
FROM_TEATIME_BUT_IP_ERROR (__FROM_TEATIME)
describe
FROM_TEATIME_BUT_IP_ERROR From 4wei.us but ip not match
score
FROM_TEATIME_BUT_IP_ERROR 8
设定SpamAssassin和Postfix搭配使用
Postfix与外部过滤程序的通讯有两种方式:基于命令的过滤(即以下所说的"一般做法"),以及基于守护进程的过滤(即以下的"建议做法")。
对于前者,Postfix每处理一封邮件,过滤命令就被启动一次,并且过滤程序还必须调用一次Sendmail,将完成过滤的邮件送回Postfix,所以运算成本比较高。
相对于实时地启动外部过滤程序,基于守护进程的过滤显然在I/O与CPU使用方面比较经济,不必每处理一封邮件就多创建一个新的进程,并且基于守护进程的过滤与Postfix之间的交互架构也比较先进,双方使用标准的SMTP或LMTP协议交换邮件。
=== spamd/spamd模式(一般做法)===
修改/etc/default/spamassassin,将其中的ENABLED=0改为ENABLED=1,以启动spamd:
#
/etc/default/spamd.conf
# Duncan Findlay
# WARNING read README.spamd
before using. THERE ARE SECURITY RISKS!
# Change to one to enable spamd
ENABLED = 1
# Options
# See man spamd for possible options. The -d option is
automatically added.
OPTIONS="-c -m 10 -a -H"
# Set nice level of
spamd
#NICE="--nicelevel 15"
注:也可以在此使用-u参数来指定启用SpamAssassin
Daemon的帐号, 但由于SpamAssassin Daemon会su成Client帐号来过滤垃圾邮件, 因而并无此必要...
修改/etc/postfix/master.cf如下,告知Postfix使用SpamAssassin来扫描邮件内容:(其中的spamassassin字串可以自订)
(上略)
# =================================================
=========================
# service type private unpriv chroot wakeup
maxproc command + args
# (yes) (yes) (yes) (never) (100)
#
================================================= =========================
smtp inet n - n - - smtpd -v
-o
content_filter=spamassassin
(下略)
并再修改/etc/postfix/master.cf,加入以下内容:
(在此是设定spamassassin的内容。详细参数请参考man
spamc及man sendmail之说明。)
注意,一般是以nobody身份来启动SpamAssassin,但在此例中是以amavis的身份来呼叫SpamAssassin的!
spamassassin unix - n n - - pipe
flags=Rq user=amavis argv=/usr/bin/spamc -d 127.0.0.1 -f -p 783 -t 30 -e
/usr/sbin/sendmail -oi -f ${sender} -- ${recipient}
因为,若以nobody身份来启动SpamAssassin时,往往会因为nobody的Home
Directory不存在,因而在/var/log/mail.log中将出现类似以下的错误讯息:
Oct 30 13:21:47 qemu
spamd[788]: info: setuid to nobody succeeded
Oct 30 13:21:47 qemu
spamd[788]: Creating default_prefs [/nonexistent/.spamassassin/user_prefs]
Oct 30 13:21:47 qemu spamd[788]: Cannot write to
/nonexistent/.spamassassin/user_prefs: No such file or directory
Oct 30
13:21:47 qemu spamd[788]: Couldn't create readable default_prefs for
[/nonexistent/.spamassassin/user_prefs]
所以改由以amavis身份来启动SpamAssassin。
修改完毕后,使用以下指令来启动SpamAssassin并重新启动Postfix:
/etc/init.d/spamassassin start
/etc/init.d/postfix restart
在预设上,spamd会监听783这个Port。
另外,SpamAssassin可以使用sa-learn指令来学习新的垃圾邮件规则,请参考AntiSpam with Bayesian
Classifier一章之说明。
===利用Amavisd-new来呼叫SpamAssassin(建议做法)===
我们可以替而使用Amavisd-new来呼叫SpamAssassin,也就是让Amavisd-new肩负扫毒及过滤垃圾邮件的重责。而在本人的系统中,这个方法比起使用spamc/spamd的做法快上了非常多,所以建议采用这种作法。
请修改/etc/amavis/amavisd.conf如下:
(注:/etc/amavis/amavisd.conf的内容很大,以下只节录必须修改的项目)
#在@bypass_spam_checks_acl = qw( . );前加上注解,让Amavisd-new启动SpamAssassin
# Any setting can be changed with a new assignment, so make sure
# you
do not unintentionally override these settings further down!
#
@bypass_spam_checks_acl = qw( . ); # No default dependency
#不要移去SpamAssassin所加入的Headers,但似乎是没有作用?
#$remove_existing_spam_headers = 0; # leave existing X-Spam* headers alone
$remove_existing_spam_headers = 0; # remove existing spam headers if
# spam scanning is enabled
(default)
#让SpamAssassin可以和外部连线
# $sa_local_tests_only is
passed to Mail::SpamAssassin::new as a value
# of the option
local_tests_only. See Mail::SpamAssassin man page.
# If set to 1, SA tests
are restricted to local tests only, ie no tests
# that require internet
access will be performed.
#
$sa_local_tests_only = 0; # (default: false)
#启用自动的White List
$sa_auto_whitelist = 1; # turn on AWL (default:
false)
#设定White List
#注意:在/etc/spamassassin/local.cf所设定的将失去效用!
map { $whitelist_sender{lc($_)}=1 } (qw(
tetralet@example.net
));
#或指定为某个档案。注意:这些档案必须存在,否则Amavisd-new将无法启动!
read_hash(\%whitelist_sender,
'/var/lib/amavis/whitelist');
#指定黑名单
read_hash(\%blacklist_sender,
'/var/lib/amavis/blacklist');
#若收件人名列其中,则不进行垃圾邮件检测
read_hash(\%spam_lovers, '/var/lib/amavis/spam_lovers');
#超过某个特定大小的邮件可以不经过SpamAssassin的扫瞄
$sa_mail_body_size_limit =
150*1024; # don't waste time
#设定评分规则。
# default values, can be overridden by
more specific lookups, eg SQL
#超过这个标准者,就在邮件标头加入Spam资讯
$sa_tag_level_deflt = 4.0; # add spam info headers if at, or above that
level
#超过这个标准者,就直接视为垃圾邮件
$sa_tag2_level_deflt = 6.3; # add 'spam
detected' headers at that level
#超过这个标准者,就直接将信件备份后删除。
$sa_kill_level_deflt = 10 ; # triggers spam evasive actions
# at or above that level: bounce/reject/drop,
# quarantine, and adding mail address extension
#启用SpamAssassin的Debug。在正式上线时请记得去掉!
# Turn on SpamAssassin debugging
(output to STDERR, use with 'amavisd debug')
$sa_debug = 1; # defaults to
false
然后重新启动Amavisd-new即可。
若因任何原因而无法启动Amavisd-new,可以使用以下指令来进行Debug:
amavisd-new debug-sa
测试SpamAssassin在安装SpamAssassin时,也同时会附上测试用的Spam范例档。您可以用以下指令来测试SpamAssassin是否能够正确运作了:
spamassassin -t <
/usr/share/doc/spamassassin/examples/sample-spam.txt
寄封测试用的垃圾邮件吧:
#
telnet 127.0.0.1 25
Connected to 127.0.0.1.
Escape character is '^]'.
220 qemu ESMTP Postfix (Debian/GNU)
MAIL FROM:
250 Ok
RCPT TO:
250 Ok
DATA
354 End data
with .
Subject: Test spam mail
(GTUBE)
XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
.
250 Ok: queued as 0C3E9C906B
quit
221 Bye
Connection
closed by foreign host.
看看这封邮件是否真的被判定为垃圾邮件了:
From
tetralet@mail.org Fri Oct 22 16:50:46 2004
Return-Path:
X-Original-To: tetralet@virtual.com
Delivered-To: tetralet@virtual.com
Received: by qemu (Postfix, from
userid 65534)
id 7ACB8C9073; Fri, 22 Oct 2004 16:50:46 +0800 (CST)
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by qemu (Postfix) with SMTP id 9D7C0C9063
for
; Fri, 22 Oct 2004 16:50:18 +0800 (CST)
Subject:
*****SPAM***** Test spam mail (GTUBE)
Message-Id:
<20041022085018.9D7C0C9063@qemu>
Date: Fri, 22 Oct 2004 16:50:18 +0800
(CST)
From: tetralet@mail.org
To: undisclosed-recipients: ;
X-Spam-DCC: :
X-Spam-Flag: YES
X-Spam-Checker-Version: SpamAssassin
2.64 (2004-01-11) ">)
*
-100 USER_IN_WHITELIST From: address is in the user's white-list
*
3.3 MSGID_FROM_MTA_SHORT Message-Id was added by a relay
XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
如果一切没问题,那就大功告成啰∼∼
----------
其它
另外还有一个名为Spamstats的Perl
Script可以用来分析/var/log/mail.log里的Spam资讯。不妨试用看看!