﻿# 設定 Postfix + SpamAssassin 有效阻擋非法的垃圾信來源

這陣子因為[2010年世界盃足球賽](http://zh.wikipedia.org/zh-tw/2010%E5%B9%B4%E4%B8%96%E7%95%8C%E7%9B%83%E8%B6%B3%E7%90%83%E8%B3%BD)與[端午節](http://zh.wikipedia.org/zh-tw/%E7%AB%AF%E5%8D%88%E7%AF%80)的關係，公司收到的垃圾信特別的多，我公司才十幾人但光是一個早上被 [SpamAssassin](http://spamassassin.apache.org/) 偵測到的垃圾信就有 3,000 多封垃圾郵件，由於這些垃圾郵件難免會有誤判的時候，所以通常這些 Spam 的郵件都會流下，並由專人定期查看，但看到過多的垃圾信通常就是一次全部刪除，也失去定期查看的意義。昨天將 [Postfix](http://www.postfix.org/) + [SpamAssassin](http://spamassassin.apache.org/) 調整過一番之後，今天早上的垃圾郵件已經降到 300 封了，效果非常驚人。

基本的 Mail Server 環境建立可參考 [Ubuntu Documentation > Ubuntu 10.04 > Ubuntu Server Guide > Email Services > Mail Filtering](https://help.ubuntu.com/10.04/serverguide/C/mail-filtering.html) 文章，但除此之外要更有效的阻擋垃圾信就是本文章的重點，以下是這次的設定筆記：

**絕招一：設定 Postfix 地址驗證**

編輯 Postfix 的 /etc/postfix/main.cf 設定檔，並加上以下設定：

-   [smtpd\_client\_restrictions](http://www.postfix.org/postconf.5.html#smtpd_client_restrictions) 設定來源 IP 是否被列入黑名單
-   [smtpd\_sender\_restrictions](http://www.postfix.org/postconf.5.html#smtpd_sender_restrictions) 設定會額外驗證來源端的寄件者是否合法有效
-   [unverified\_sender\_reject\_code](http://www.postfix.org/postconf.5.html#unverified_sender_reject_code) 設定驗證失敗的寄件者收到 550 (嚴重錯誤) 狀態，叫他不要在寄了!
-   [address\_verify\_map](http://www.postfix.org/postconf.5.html#address_verify_map) 設定儲存地址驗證狀態的 Lookup Table

```csharp
smtpd_client_restrictions = hash:/etc/postfix/access,
     reject_rbl_client bl.spamcop.net,
     reject_rhsbl_client dsn.rfc-ignorant.org

smtpd_sender_restrictions =
    permit_mynetworks
    check_sender_access hash:/etc/postfix/sender_access
    reject_unknown_sender_domain
    warn_if_reject reject_unverified_sender

unverified_sender_reject_code = 550

# Postfix 2.6 and later.
# unverified_sender_reject_reason = Address verification failed

# Default setting for Postfix 2.7 and later.
# Note 1: Be sure to read the "Caching" section below!
# Note 2: Avoid hash files here. Use btree instead.
address_verify_map = btree:/var/lib/postfix/verify
```

由於上述 [smtpd\_client\_restrictions](http://www.postfix.org/postconf.5.html#smtpd_client_restrictions) 與 [smtpd\_sender\_restrictions](http://www.postfix.org/postconf.5.html#smtpd_sender_restrictions) 有個別指定 hash 檔，如果檔案不存在就要額外建立才不會在 /var/log 下產生一些錯誤記錄：

```
touch /etc/postfix/access
postmap /etc/postfix/access

touch /etc/postfix/sender_access
postmap /etc/postfix/sender_access
```

設定完必須重新載入 Postfix 設定，指令如下：

```
/etc/init.d/postfix reload
```

**絕招二：設定 SpamAssassin 黑名單**

有些**專業的**散佈垃圾郵件廠商 (大部分不專業) 總是有辦法穿越 SpamAssassin 的保護，對於這些寄件者我們可以透過設定黑名單的方式來加以阻擋

編輯 SpamAssassin 的 /etc/mail/spamassassin/local.cf 設定檔，加入特定寄件者進**黑名單**：

```
blacklist_from  *@123greetings.com
blacklist_from  schedarzc13@pisem.net
```

**注意**：經實測發現，我的 **Ubuntu 8.04 LTS** 在設定 /etc/mail/spamassassin/local.cf 黑名單的參數時，參數鍵與參數值的中間要以 <TAB> 符號做分隔才會生效，用 <SPACE> 空白字元分隔會完全沒有效果!

加入黑名單後必須重新啟動 SpamAssassin 服務，指令如下：

```
/etc/init.d/spamassassin restart
```

兩個簡單的設定卻有大大的效果，值得一試阿！

**補充說明**

Postfix 的 [smtpd\_sender\_restrictions](http://www.postfix.org/postconf.5.html#smtpd_sender_restrictions) 只能驗證 **MAIL FROM** 的郵件地址，也就是 **Sender 地址**，這跟你用 Outlook 上面顯示的「**寄件者地址**」是不一樣的，這點可能要瞭解 [SMTP](http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol) 的人才清楚。

許多散發垃圾信的廠商經常會用固定的**寄件者地址**，但卻用亂數的 **Sender 地址**，這種情況就算你在 Postfix 的 /etc/postfix/sender\_access 設定黑名單就會抓不到，必須靠 SpamAssassin 才行。

SpamAssassin 的黑名單設定會驗證 MIME Header 中的多個欄位，只要分析比對到符合的信箱該郵件就會被 SpamAssassin 過濾掉，其分析的欄位有包括以下四種：

-   Envelope-Sender
-   Resent-Sender
-   X-Envelope-From
-   From

如果你要分析不止以上欄位，你也可以設定 envelope\_sender\_header 新增 MIME Header 的名稱。

**相關連結**

-   [Postfix Address Verification Howto](http://www.postfix.org/ADDRESS_VERIFICATION_README.html)
-   [ManualWhitelist - Spamassassin Wiki](http://wiki.apache.org/spamassassin/ManualWhitelist)
-   [SpamAssassin cookbook](http://johnbokma.com/spam/spamassassin-cookbook.html)
-   [Mail::SpamAssassin::Conf - SpamAssassin configuration file](http://spamassassin.apache.org/full/3.1.x/doc/Mail_SpamAssassin_Conf.html)
-   [Tagging Spam E-Mail with SpamAssassin™](http://www.lehigh.edu/helpdesk/faq/qa/spamassassin.shtml)
-   [amavisd-new documentation bits and pieces](http://www.ijs.si/software/amavisd/amavisd-new-docs.html)
-   [簡易 Mail Server 架設 -- Postfix 設定](https://linux.vbird.org/linux_server/centos5/0380mail-centos5.php)
-   增加postfix追蹤黑名單的資料庫
