閉じる

Postfix・SpamAssassin・spamass-milter(メールサーバーでスパムチェック)

SpamAssassinを使用して、メールサーバー側でスパムチェックを行います。
メールサーバーを導入・設定済みとします。

[root@server1 ~]# dnf install -y epel-release ← EPELリポジトリをインストールします。


[root@server1 ~]# dnf -y install spamassassin spamass-milter-postfix ← SpamAssassin と spamass-milter をインストールします。

[root@server1 ~]# systemctl start spamassassin ← SpamAssassinを起動します。

[root@server1 ~]# vi /etc/mail/spamassassin/v310.pre ← v310.preを編集します。

# This is the right place to customize your installation of SpamAssassin.
#
# See 'perldoc Mail::SpamAssassin::Conf' for details of what can be
# tweaked.
#
# This file was installed during the installation of SpamAssassin 3.1.0,
# and contains plugin loading commands for the new plugins added in that
# release.  It will not be overwritten during future SpamAssassin installs,
# so you can modify it to enable some disabled-by-default plugins below,
# if you so wish.
#
# There are now multiple files read to enable plugins in the
# /etc/mail/spamassassin directory; previously only one, "init.pre" was
# read.  Now both "init.pre", "v310.pre", and any other files ending in
# ".pre" will be read.  As future releases are made, new plugins will be
# added to new files, named according to the release they're added in.
###########################################################################

# DCC - perform DCC message checks.
#
# DCC is disabled here because it is not open source.  See the DCC
# license for more details.
#
#loadplugin Mail::SpamAssassin::Plugin::DCC

# Pyzor - perform Pyzor message checks.
#
loadplugin Mail::SpamAssassin::Plugin::Pyzor

# Razor2 - perform Razor2 message checks.
#
loadplugin Mail::SpamAssassin::Plugin::Razor2

# SpamCop - perform SpamCop message reporting
#
loadplugin Mail::SpamAssassin::Plugin::SpamCop

# AntiVirus - some simple anti-virus checks, this is not a replacement
# for an anti-virus filter like Clam AntiVirus
#
#loadplugin Mail::SpamAssassin::Plugin::AntiVirus

# AWL - do auto-whitelist checks
#
#loadplugin Mail::SpamAssassin::Plugin::AWL

# AutoLearnThreshold - threshold-based discriminator for Bayes auto-learning
#
loadplugin Mail::SpamAssassin::Plugin::AutoLearnThreshold

# TextCat - language guesser
#
loadplugin Mail::SpamAssassin::Plugin::TextCat ← 行頭の#を削除してコメントを解除します。(TextCatプラグインを有効化します。)

# AccessDB - lookup from-addresses in access database
#
#loadplugin Mail::SpamAssassin::Plugin::AccessDB

# WhitelistSubject - Whitelist/Blacklist certain subject regular expressions
#
loadplugin Mail::SpamAssassin::Plugin::WhiteListSubject

###########################################################################
# experimental plugins

# DomainKeys - perform DomainKeys verification
#
# This plugin has been removed as of v3.3.0.  Use the DKIM plugin instead,
# which supports both Domain Keys and DKIM.

# MIMEHeader - apply regexp rules against MIME headers in the message
#
loadplugin Mail::SpamAssassin::Plugin::MIMEHeader

# ReplaceTags
#
loadplugin Mail::SpamAssassin::Plugin::ReplaceTags

[root@server1 ~]# vi /etc/cron.daily/spamassassin-update ← SpamAssassinの設定ファイルを毎日最新化するスクリプトを作成します。

#!/bin/bash

# SpamAssassin設定ファイル最新版ダウンロード
cd /etc/mail/spamassassin

wget -q https://github.com/kittyfreak/spamassassin_user_prefs/archive/refs/heads/main.zip
[ $? -ne 0 ] && exit

unzip main.zip >/dev/null 2>&1
[ $? -ne 0 ] && exit
rm -f main.zip

mv spamassassin_user_prefs-main/user_prefs .
rm -rf spamassassin_user_prefs-main

# 設定ファイル更新時のみSpamAssassin再起動
diff user_prefs user_prefs.org > /dev/null 2>&1
if [ $? -ne 0 ]; then
    cp user_prefs local.cf

    # スパム判定スコアを変更※デフォルト値が高くスパムメールが大量に届くため
    sed -i 's/required_score .*/required_score 8.0/g' local.cf

    # スパム判断したメールを添付形式にしないように設定
    echo "report_safe 0" >> local.cf

    # スパム判断したメールの件名に「***SPAM***」を付加するように設定 受信メールサーバーがIMAP以外の場合のみ
    lsof -i:143,993 >/dev/null 2>&1
    [ $? -ne 0 ] && echo "rewrite_header Subject ***SPAM***" >> local.cf

    # SpamAssassin再起動
    systemctl restart spamassassin > /dev/null
fi
cp user_prefs user_prefs.org

[root@server1 ~]# chmod +x /etc/cron.daily/spamassassin-update ← SpamAssassinの設定ファイルを毎日最新化するスクリプトへ実行権限を付加します。


[root@server1 ~]# /etc/cron.daily/spamassassin-update ← SpamAssassinの設定ファイルを毎日最新化するスクリプトを実行します。

[root@server1 ~]# systemctl start spamass-milter ← spamass-milterを起動します。


[root@server1 ~]# systemctl enable spamass-milter ← spamass-milterを自動的に起動するように設定します。

[root@server1 ~]# vi /etc/postfix/main.cf ← Postfixの設定ファイルを編集します。

以下のように編集します。

milter_default_action = accept
milter_protocol = 6
smtpd_milters = inet:localhost:8891,inet:localhost:8893,unix:/var/run/clamav-milter/clamav-milter.socket,unix:/run/spamass-milter/postfix/sock
non_smtpd_milters = $smtpd_milters,unix:/var/run/clamav-milter/clamav-milter.socket,unix:/run/spamass-milter/postfix/sock

[root@server1 ~]# systemctl reload postfix ← Postfixに設定を反映させます。

[root@server1 ~]# vi /etc/procmailrc ← procmailの設定ファイルを作成します。

SHELL=/bin/bash
PATH=/usr/bin:/bin
DROPPRIVS=yes
MAILDIR=$HOME/Maildir
DEFAULT=$MAILDIR/
LOGFILE=$HOME/.procmail.log # ログ出力先
VERBOSE=ON # 詳細ログ出力

# SpamAssassinにより判定されたSpam-Levelが一定値(ここでは20)以上の場合は削除
# ※必要なメールが削除されてしまう可能性があることに留意すること
:0
* ^X-Spam-Level: \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
/dev/null

[root@almalinux ~]# vi /etc/logrotate.d/procmail ← procmailログローテーション設定ファイル作成
/home/*/.procmail.log {
    missingok
    nocreate
    notifempty
}

[root@server1 ~]# vi /etc/postfix/main.cf ← Postfixの設定ファイルを編集します。

# The mailbox_command parameter specifies the optional external
# command to use instead of mailbox delivery. The command is run as
# the recipient with proper HOME, SHELL and LOGNAME environment settings.
# Exception:  delivery for root is done as $default_user.
#
# Other environment variables of interest: USER (recipient username),
# EXTENSION (address extension), DOMAIN (domain part of address),
# and LOCAL (the address localpart).
#
# Unlike other Postfix configuration parameters, the mailbox_command
# parameter is not subjected to $parameter substitutions. This is to
# make it easier to specify shell syntax (see example below).
#
# Avoid shell meta characters because they will force Postfix to run
# an expensive shell process. Procmail alone is expensive enough.
#
# IF YOU USE THIS TO DELIVER MAIL SYSTEM-WIDE, YOU MUST SET UP AN
# ALIAS THAT FORWARDS MAIL FOR ROOT TO A REAL USER.
#
#mailbox_command = /some/where/procmail
#mailbox_command = /some/where/procmail -a "$EXTENSION"
mailbox_command = /usr/bin/procmail ← 追加します。(Procmailと連携するようにします。)

[root@server1 ~]# systemctl reload postfix ← Postfixに設定を反映させます。

[root@server1 ~]# mkdir -p /etc/skel/Maildir/.Spam/{new,cur,tmp} ← 新規ユーザー追加時に、自動的にスパム専用メールボックスを作成します。


[root@server1 ~]# chmod -R 700 /etc/skel/Maildir/.Spam ← 新規ユーザー追加時に、自動的にスパム専用メールボックスを作成します。

[root@server1 ~]# vi /etc/procmailrc ← procmailの設定ファイルを編集します。

↓ 以下を最後へ追加します。
# SpamAssassinがスパム判定したメールはスパム専用メールボックス(/home/xxxx/Maildir/.Spam)へ配送
:0
*^X-Spam-Flag: YES
$HOME/Maildir/.Spam/

SpamAssassinが誤って正常メールをスパムメールと判断したり、逆にスパムメールを正常メールと判断してしまった場合、それ以降の誤認識を防止する為にSpamAssassinに学習をさせ、チェック精度を上げます。

受信トレイの既読メールを正常メールとして、Spamフォルダの既読メールをスパムメールとして、定期的にSpamAssassinに学習させるようにします。
※SpamAssassinが誤って配送したメールは、ユーザー自身の操作により正常メールなら受信トレイへ、スパムメールならSpamフォルダへ移動しておきます。
(放置しておくと正常メールをスパムメールとして学習したり、逆にスパムメールを正常メールとして学習してしまいます。)

[root@server1 ~]# dnf -y install tmpwatch ← 古いスパムメールを削除する為に、tmpwatch をインストールします。

[root@server1 ~]# vi /etc/cron.daily/spamassassin-learn ← SpamAssassinを毎日学習させる為のスクリプトを作成します。

#!/bin/bash

PATH=/usr/sbin:/usr/bin:/bin

for user in `ls /home/`
do
    # 正常メール学習
    # 正常メールをSpamAssassinに学習させる
    hammail=/home/$user/Maildir/cur
    [ -d "$hammail" ] && \
    su $user -s "/bin/bash" -c "sa-learn --ham $hammail | \
    logger -p mail.info -t 'sa-learn for $user'"
    hammail=/home/$user/Maildir/.Trash/cur
    [ -d "$hammail" ] && \
    su $user -s "/bin/bash" -c "sa-learn --ham $hammail | \
    logger -p mail.info -t 'sa-learn for $user'"

    # スパムメール
    spammail=/home/$user/Maildir/.Spam/cur

    # スパムメール学習
    if [ -d "$spammail" ]; then
        # スパムメールをSpamAssassinに学習させる
        su $user -s "/bin/bash" -c "sa-learn --spam $spammail | \
        logger -p mail.info -t 'sa-learn for $user'"

        # 受信後3日経過したスパムメールを削除
        tmpwatch -m 72 $spammail
    fi
done

[root@server1 ~]# chmod +x /etc/cron.daily/spamassassin-learn ← SpamAssassinを毎日学習させる為のスクリプトに実行権限を付加します。

スパムメール学習結果のログは/var/log/maillogに記録されます。

© 2025 Linuxで自宅サーバー(ホームサーバー)-構築・設定方法- | WordPress テーマ: CrestaProject の Annina Free