スパム対策(taRgrey)

「taRgrey」はS25R、tarpitting、greylistingの3つのスパム判定手法を組み合わせたものです。
これだけでもかなりのスパムを弾く事が出来ますが、Yahooメール等のフリーメールや固定IPから送信されたスパムは弾くことが出来ません。
その為、「taRgrey」を1次フィルタとして用い、「SpamAssassin」を2次フィルタとして用いる事により、ほとんどのスパムを弾くことが出来ます。
「SpamAssassin」の設定方法はこちらにあります。
なお、「SMTPサーバ(Postfix)の設定」「Postfix」+「Cyrus-SASL」の設定「Berkeley DB」のインストールは終わっているものとします。

「Postfix」のインストール

「taRgrey」では怪しい送信元に対しtarpitting(応答の遅延)を行いますが、応答待ちですぐに処理が終了しない為、smtpdプロセスが増加します。
そこで「Postfix」にパッチを当てて、遅延している間に相手が接続を切ったら、こちらのsmtpdもすぐに終了するよう、パッチを当てます。

Solaris% cd /usr/local/src/postfix/postfix-2.5.1 <= 展開したディレクトリへ移動
Solaris% gmake tidy <= 以前に「Postfix」をコンパイルした場合
Solaris% wget http://k2net.hakuba.jp/pub/postfix-sleep.patch <= パッチのダウンロード
Solaris% gpatch -p1 < postfix-sleep.patch <= パッチの適用
Solaris% rm -f src/smtpd/smtpd_check.c\~ <= パッチを適用した元のファイルを削除
Solaris% rm -f postfix-sleep.patch <= パッチファイルの削除
Solaris% gmake makefiles \
CCARGS="-DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/local/include/sasl \
-DUSE_TLS -I/usr/sfw/include \
-DHAS_DB -I/usr/local/BerkeleyDB.4.4/include" \
AUXLIBS="-L/usr/local/lib -lsasl2 -L/usr/sfw/lib -lssl -lcrypto \
-L/usr/local/BerkeleyDB.4.4/lib -ldb -R/usr/local/BerkeleyDB.4.4/lib" |& tee makefile.log

Solaris% gmake |& tee make.log <= コンパイル
Solaris% su <= スーパーユーザになる
Password: <= パスワードの入力
Solaris# gmake install |& tee make-install.log <= インストール

Please specify the prefix for installed file names. Specify this ONLY
if you are building ready-to-install packages for distribution to other
machines.
install_root: [/] <= 空Enter

Please specify a directory for scratch files while installing Postfix.
You must have write permission in this directory.
tempdir: [/usr/local/src/postfix/postfix-2.5.1] <= 空Enter

Please specify the final destination directory for installed Postfix
configuration files.
config_directory: [/etc/postfix] <= 空Enter

Please specify the final destination directory for installed Postfix
administrative commands. This directory should be in the command search
path of adminstrative users.
command_directory: [/usr/sbin] <= 空Enter

Please specify the final destination directory for installed Postfix
daemon programs. This directory should not be in the command search
path of any users.
daemon_directory: [/usr/libexec/postfix] <= 空Enter

Please specify the final destination directory for Postfix-writable
data files such as caches or random numbers. This directory should not
be shared with non-Postfix software.
data_directory: [/var/lib/postfix] <= 空Enter

Please specify the destination directory for the Postfix HTML files.
Specify "no" if you do not want to install these files.
html_directory: [no] <= 空Enter

Please specify the owner of the Postfix queue. Specify an account with
numerical user ID and group ID values that are not used by any other
accounts on the system.
mail_owner: [postfix] <= 空Enter

Please specify the final destination pathname for the installed Postfix
mailq command. This is the Sendmail-compatible mail queue listing
command.
mailq_path: [/usr/bin/mailq] <= 空Enter

Please specify the destination directory for the Postfix on-line manual
pages. You can no longer specify "no" here.
manpage_directory: [/usr/local/man] <= 空Enter

Please specify the final destination pathname for the installed Postfix
newaliases command. This is the Sendmail-compatible command to build
alias databases for the Postfix local delivery agent.
newaliases_path: [/usr/bin/newaliases] <= 空Enter

Please specify the final destination directory for Postfix queues.
queue_directory: [/var/spool/postfix] <= 空Enter

Please specify the destination directory for the Postfix README files.
Specify "no" if you do not want to install these files.
readme_directory: [no] <= 空Enter

Please specify the final destination pathname for the installed Postfix
sendmail command. This is the Sendmail-compatible mail posting
interface.
sendmail_path: [/usr/lib/sendmail] <= 空Enter

Please specify the group for mail submission and for queue management
commands. Specify a group name with a numerical group ID that is not
shared with other accounts, not even with the Postfix mail_owner
account. You can no longer specify "no" here.
setgid_group: [postdrop] <= 空Enter
「Postgrey」のインストール

perlモジュールのインストール

「Postgrey」に必要なperlモジュールをインストールします。
必要なモジュールは下記の通りです。
インストールは「CPAN」を利用すると簡単です。
「CPAN」の利用方法はこちらを参照して下さい。
・Net::Server
・IO::Multiplex
・BerkeleyDB

ただし、BerkeleyDBは「CPAN」でインストールすると、パスが違っている為エラーになります。
そこで、以下のようにしてインストールします。
なお、こちらの方法で「BerkeleyDB」をインストールしたものとしていますので、 ファイルのダウンロード先と展開作業までは、リンク先と同じとしています。

Solaris% cd /usr/local/src/db/db-4.4.20/perl/BerkeleyDB
Solaris% setenv BERKELEYDB_INCLUDE /usr/local/BerkeleyDB.4.4/include
Solaris% setenv BERKELEYDB_LIB /usr/local/BerkeleyDB.4.4/lib
Solaris% perl Makefile.PL |& tee make-pl.log
Solaris% make |& tee make-berkeleydb.log
Solaris% make test
Solaris% su
Password:
Solaris# make install |& tee make-install.log

「Postgrey」のインストール

「taRgrey」を利用する為に「Postgrey」にパッチを当ててインストールします。

Solaris# wget http://postgrey.schweikert.ch/pub/postgrey-1.31.tar.gz <= 「Postgrey」のダウンロード
Solaris# gunzip -c postgrey-1.31.tar.gz | tar xf - <= ダウンロードしたファイルの展開
Solaris# cd postgrey-1.31 <= 展開したディレクトリに移動
Solaris# wget http://k2net.hakuba.jp/pub/targrey-0.31-postgrey-1.31.patch <= パッチのダウンロード
Solaris# patch -p0 < targrey-0.31-postgrey-1.31.patch <= パッチの適用
Solaris# cp postgrey /usr/local/sbin/ <= postgreyファイルのコピー
Solaris# cp postgrey_whitelist_clients /etc/postfix/ <= 設定ファイルのコピー
Solaris# cp postgrey_whitelist_recipients /etc/postfix/ <= 設定ファイルのコピー
Solaris# cd ../
Solaris# rm -rf postgrey-1.31* <= ダウンロードしたファイルの削除
「Postfix」の設定

main.cfの設定

当サイトでは、元々記述している部分を1行で記述していますが、ここでは複数行に分けています。

Solaris# vi /etc/postfix/main.cf <= 設定ファイルの編集
smtpd_recipient_restrictions =
        permit_mynetworks
        permit_sasl_authenticated
        reject_unauth_destination
以下を追加
        check_client_access    hash:$config_directory/whitelist_client
        check_client_access    regexp:$config_directory/permit_client_nots25r
        check_policy_service   inet:60000
        permit

smtpd_data_restrictions =
        permit_mynetworks
        permit_sasl_authenticated
        reject_unauth_destination
        check_client_access    hash:$config_directory/whitelist_client
        check_client_access    regexp:$config_directory/permit_client_nots25r
        check_policy_service   inet:60000
        permit
ここまで

「Postgrey」関係の設定

設定ファイルをダウンロードし、指定のディレクトリに配置します。

Solaris# wget http://k2net.hakuba.jp/spam/postfix.conf.2.tar.gz <= 設定ファイルのダウンロード
Solaris# gunzip -c postfix.conf.2.tar.gz | tar xf - <= ダウンロードしたファイルの展開
Solaris# cp postfix.conf.2/whitelist_client /etc/postfix/ <= 設定ファイルのコピー
Solaris# cp postfix.conf.2/permit_client_nots25r /etc/postfix/ <= 設定ファイルのコピー
Solaris# rm -rf postfix.conf.2 <= 展開したディレクトリの削除
Solaris# rm -f postfix.conf.2.tar.gz <= ダウンロードしたファイルの削除
Solaris# postmap /etc/postfix/whitelist_client <= whitelist_clientファイルのDB化
「Postfix」、「Postgrey」の起動

「Postgrey」起動の準備

「Postgrey」用のユーザの作成等を行います。

Solaris# mkdir /var/spool/postfix/postgrey <= 「Postgrey」が利用するDB格納ディレクトリの作成
Solaris# mkdir /var/run/postgrey <= 「Postgrey」のPID格納ディレクトリを作成
Solaris# groupadd -g 10003 postgrey <= 「Postgrey」用グループの作成
Solaris# useradd -u 10003 -g postgrey -d /var/spool/postfix/postgrey -s /bin/false postgrey <= 「Postgrey」用ユーザの作成
Solaris# chown postgrey:postgrey /var/spool/postfix/postgrey <= ディレクトリのオーナの変更
Solaris# chown postgrey:postgrey /var/run/postgrey <= ディレクトリのオーナの変更

「Postgrey」の起動

「Postgrey」の起動時に「taRgrey」が有効になるように起動スクリプトを作成し、Solaris起動時に自動的に起動するようにします。
ここでは、125秒の遅延を設定し、最初に送信されてから1時間(3600秒)以上開けて、 再送を2回以上されたときのみメールを受け取るように設定しています。
また、上記の設定と合わせ、ポリシーサーバの待ち受けポートを60000に指定しています。

Solaris# vi /etc/init.d/postgrey <= 起動スクリプトの作成
#!/sbin/sh

PIDDIR='/var/run/postgrey'
PIDFILE='/var/run/postgrey/postgrey.pid'
DBDIR='/var/spool/postfix/postgrey'
USER=postgrey
GROUP=postgrey
HOST='127.0.0.1'
PORT=60000
TARPIT=125
RETRY=2
DELAY=3600

PID=`/usr/bin/ps -aef | /usr/bin/grep postgrey | /usr/bin/grep perl | /usr/bin/grep -v grep | /usr/bin/awk '{print $2}'`

case "$1" in
'start')
        if [ -f /usr/local/sbin/postgrey ]; then
            if [ $PID ]; then
                echo "Postgrey has started"
            else
                if [ ! -d $PIDDIR ]; then
                    /usr/bin/mkdir -p $PIDDIR
                    /usr/bin/chown $USER:$GROUP $PIDDIR
                fi
                cd /usr/local/sbin
                ./postgrey --pidfile=${PIDFILE} \
                           --dbdir=${DBDIR} \
                           --user=${USER} \
                           --group=${GROUP} \
                           --daemonize \
                           --inet=${HOST}:${PORT} \
                           --tarpit=${TARPIT} \
                           --targrey \
                           --retry-count=${RETRY} \
                           --delay=${DELAY}
                echo "Running: Postgrey"
            fi
        fi
        ;;

'stop')
        if [ $PID ]; then
                kill -KILL $PID
                /usr/bin/rm -f ${PIDFILE}
                echo "Stopping: Postgrey"
        else
                echo "Postgrey has stopped"
        fi
        ;;

'restart')
        $0 stop
        sleep 3
        $0 start
        ;;

'status')
        if [ $PID ]; then
                echo "Postgrey starts"
        else
                echo "Postgrey stopped"
        fi
        ;;

*)
        echo "Usage: $0 { start | stop | restart | status }"
        exit 1
        ;;
esac

exit 0


Solaris# chmod 744 /etc/init.d/postgrey <= 起動スクリプトファイルに実行権限付加
Solaris# ln /etc/init.d/postgrey /etc/rc2.d/S86postgrey <= ランレベル2で起動する
Solaris# ln /etc/init.d/postgrey /etc/rc3.d/S86postgrey <= ランレベル3で起動する
Solaris# ln /etc/init.d/postgrey /etc/rc0.d/K38postgrey <= ランレベル0で停止する

作成した起動スクリプトを利用して起動します。

Solaris# /etc/init.d/postgrey start

「Postfix」の再起動

Solaris# /etc/init.d/postfix restart
▲ページのトップへ