harumaki.net

インフラ屋の覚書や、ラーメン食べある記とか。

infra MAIL postfix

postfixウィルス対策[ClamAV]

投稿日:2006年3月24日

Last Updated on 2013年7月30日 by かんりにん

[pukiwiki]

#topicpath
&lastmod; testuser

#contents

*postfixウィルス対策
タイトルはpostfixadmin/ウィルス対策となっているが
実際にはpostfixがフィルタリングルールに沿ってウィルススキャンへメールを渡す。

**インストール

過去のインストールは[[こちら>インフラ/社内/サーバー/メール/Clam AntiVirus/インストール(RPM版)]]を参照のこと。
※postfix+MySQL関連の設定は完了しているものとする。
最新版のインストールログは以下のとおり。

***zlib
以前clamav-0.86をインストールした際にバージョン1.2.1にアップデートしたので
今回は特にアップデートの必要なし。インストール状況だけ確認する。

[root@example SPECS]# rpm -qa | grep zlib
zlib-1.2.1.2-2
zlib-devel-1.2.1.2-2
[root@example SPECS]#

***automake16
gmpのパッケージコンパイルにautomake16が必要になるので
事前にインストール。
# cd ../SRPMS/
[root@example SRPMS]# wget ftp://rpmfind.net/linux/redhat/enterprise/4/en/os/i386/SRPMS/automake16-1.6.3-5.src.rpm
[root@example SRPMS]# rpm -Uvh automake16-1.6.3-5.src.rpm
警告: automake16-1.6.3-5.src.rpm: V3 DSA signature: NOKEY, key ID db42a60e
1:automake16 ########################################### [100%]
[root@example SRPMS]#
[root@example SRPMS]# cd ../SPECS/
[root@example SPECS]# ll automake16.spec
-rw-rw-r– 1 root root 6140 9月 29 2004 automake16.spec
[root@example SPECS]#
[root@example SPECS]# rpmbuild -bb automake16.spec
コンパイルが完了すると以下のパッケージが作成される
[root@example SPECS]# ll ../RPMS/noarch/
合計 248
-rw-r–r– 1 root root 247916 3月 23 18:07 automake16-1.6.3-5.noarch.rpm
[root@example SPECS]#
そのままインストール
[root@example noarch]# rpm -ivh automake16-1.6.3-5.noarch.rpm
Preparing… ########################################### [100%]
1:automake16 ########################################### [100%]
[root@example noarch]#

***GMP
パターンファイル実行時にVirusDBの電子署名を
検証するためにインストール。
オプション無しでサックリとインストールしてしまってOK。

[root@example SRPMS]# wget ftp://rpmfind.net/linux/redhat/enterprise/4/en/os/i386/SRPMS/gmp-4.1.4-3.src.rpm
[root@example SRPMS]# rpm -uvh gmp-4.1.4-3.src.rpm
-uvh: unknown option
[root@example SRPMS]# rpm -Uvh gmp-4.1.4-3.src.rpm
警告: gmp-4.1.4-3.src.rpm: V3 DSA signature: NOKEY, key ID db42a60e
1:gmp ########################################### [100%]
[root@example SRPMS]#
[root@example noarch]# cd ../../SPECS/
[root@example SPECS]# rpmbuild -bb gmp.spec
完了するすると以下のパッケージが出来る。
[root@example SPECS]# ll ../RPMS/i386/ | grep gmp
-rw-r–r– 1 root root 550302 3月 23 18:27 gmp-4.1.4-3.i386.rpm
-rw-r–r– 1 root root 517478 3月 23 18:27 gmp-debuginfo-4.1.4-3.i386.rpm
-rw-r–r– 1 root root 412666 3月 23 18:27 gmp-devel-4.1.4-3.i386.rpm
これらはそのままインストールしてOK

***clamAV
現在インストールしている0.86に脆弱性がみつかったため
最新版0.88へアップデート。

[root@example SRPMS]# wget http://crash.fce.vutbr.cz/crash-hat/4/clamav/clamav-0.88-1.src.rpm
# rpm -Uvh clamav-0.88-1.src.rpm
警告: clamav-0.88-1.src.rpm: V3 DSA signature: NOKEY, key ID 6cdf2cc1
1:clamav ########################################### [100%]
[root@example SRPMS]#
[root@example SRPMS]# cd ../SPECS/
[root@example SRPMS]# rpmbuild -bb clamav.spec
で、インストール。
[root@example i386]# rpm -Uvh clamav-*
警告: clamav-0.88-1.i386.rpm: V3 DSA signature: NOKEY, key ID 6cdf2cc1
Preparing… ########################################### [100%]
1:clamav 警告: /etc/freshclam.conf created as /etc/freshclam.conf.rpmnew
########################################### [ 20%]
2:clamav-server ########################################### [ 40%]
3:clamav-db 警告: /var/lib/clamav/daily.cvd created as /var/lib/clamav/daily.cvd.rpmnew
########################################### [ 60%]
警告: /var/lib/clamav/main.cvd created as /var/lib/clamav/main.cvd.rpmnew
4:clamav-devel ########################################### [ 80%]
5:clamav-milter ########################################### [100%]

***clamsmtp
MySQL管理にするとprocmailではダメで、postfixからメールを受け取るため
こちらをインストールする必要がある。用途としてはpostfixとclamavの橋渡し。
流れとしては
postfix
↓ clamavへのソケットを指定する
clamsmtp
↓ ウィルススキャンを実行するため呼び出す
clamav
↓ スキャン結果で問題なければヘッダーを追加してpostfixへメールを戻す
postfix

・本家サイト
http://memberwebs.com/nielsen/software/clamsmtp/

[root@example SRPMS]# wget http://www.inet.lt/clamsmtp/clamsmtp-1.6-1.src.rpm
[root@example SRPMS]# rpm -Uvh clamsmtp-1.6-1.src.rpm
1:clamsmtp ########################################### [100%]
[root@example SRPMS]# cd ../SPECS/
[root@example SPECS]# ll clamsmtp.spec
-rw-r–r– 1 root root 2341 11月 5 07:29 clamsmtp.spec
[root@example SPECS]#
[root@example i386]# rpm -ivh clamsmtp-*
Preparing… ########################################### [100%]
1:clamsmtp-debuginfo ########################################### [ 50%]
2:clamsmtp ########################################### [100%]
[root@example i386]#

**設定

***clamsmtp

設定はパッケージ内のREADMEにあった
http://memberwebs.com/nielsen/software/clamsmtp/postfix.html
を参照。

-clamsmtp
–/etc/clamsmtpd.conf
設定内容は以下のとおり。
ClamAddressのパラメータはclamdのソケットを指定するが
デフォルトのままでは認識できない。
clamd.sockのようにソケットまで指定すること。
29c29
< #Listen: 0.0.0.0:10025 --- > Listen: 0.0.0.0:10025
32c32
< #ClamAddress: /var/run/clamav/clamd --- > ClamAddress: /var/run/clamav/clamd.sock
35c35
< #Header: X-Virus-Scanned: ClamAV using ClamSMTP --- > Header: X-Virus-Scanned: ClamAV using ClamSMTP
50c50
< #User: clamav --- > User: clamav
-そのほかの設定
clamsmtpd.confでいろいろな動作を指定できる。
:TempDirectory|フィルタリング実行時のメールファイルの一時保存先
デフォルトは/tmp
:Bounce|ウィルスメールの送信コマンド指定
デフォルトはdrop
:Quarantine|ウィルスメールをサーバ上に残すかどうかの指定
デフォルトはoff
※ファイルはメールファイルの状態で残される。
:VirusAction|ウィルスメールのフィルタリング時に使用するスクリプトを指定
デフォルトは未指定

***postfix
ローカル内でclamsmtpd→clamdへとメールファイルを渡すため
main.cfとmaster.cfへそれぞれ設定を追加。
-/etc/postfix/main.cf
643a644,647
> # Virus Scan Action
> content_filter = scan:127.0.0.1:10025
> receive_override_options = no_address_mappings
>
-/etc/postfix/master.cf
一番最下部へ追加。
79a80,93
> # Virus Scan method
> #
> scan unix – – n – 16 smtp
> -o smtp_send_xforward_command=yes
>
> 127.0.0.1:10026 inet n – n – 16 smtpd
> -o content_filter=
> -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks
> -o smtpd_helo_restrictions=
> -o smtpd_client_restrictions=
> -o smtpd_sender_restrictions=
> -o smtpd_recipient_restrictions=permit_mynetworks,reject
> -o mynetworks_style=host
> -o smtpd_authorized_xforward_hosts=127.0.0.0/8

**動作テスト
ここまで設定したところで各デーモンを起動。

# /etc/init.d/clamd start
# /etc/init.d/clamsmtpd start
# /etc/init.d/postfix restart

試しにテストメールとウィルスメールを添付してみる。

***送信テスト
-テストメール
maillogにclamsmtpdを呼び出していることが確認できる。
status=CLEANとなっている。
Mar 24 13:34:56 example postfix/smtpd[7040]: connect from unknown[10.0.0.11]
Mar 24 13:34:56 example postfix/smtpd[7040]: C29C9FC124: client=unknown[10.0.0.11]
Mar 24 13:34:56 example postfix/cleanup[7047]: C29C9FC124: message-id=<20060324133519.4A3A.testuser@foo.com>
Mar 24 13:34:56 example postfix/qmgr[6950]: C29C9FC124: from=, size=1247, nrcpt=1
(queue active)
Mar 24 13:34:56 example postfix/smtpd[7040]: disconnect from unknown[10.0.0.11]
Mar 24 13:34:56 example clamsmtpd: 100006: accepted connection from: 127.0.0.1
Mar 24 13:34:56 example postfix/smtpd[7050]: connect from example[127.0.0.1]
Mar 24 13:34:56 example postfix/smtpd[7050]: D1844FC128: client=unknown[10.0.0.11]
Mar 24 13:34:56 example postfix/cleanup[7047]: D1844FC128: message-id=<20060324133519.4A3A.testuser@foo.com>
Mar 24 13:34:56 example postfix/qmgr[6950]: D1844FC128: from=, size=1465, nrcpt=1 (queue active)
Mar 24 13:34:56 example clamsmtpd: 100006: from=testuser@foo.com, to=testuser2@example.com, status=CLEAN
Mar 24 13:34:56 example postfix/smtp[7048]: C29C9FC124: to=, relay=127.0.0.1[127.0.0.1],
delay=0, status=sent (250 Ok: queued as D1844FC128)
Mar 24 13:34:56 example postfix/qmgr[6950]: C29C9FC124: removed
Mar 24 13:34:56 example postfix/smtpd[7050]: disconnect from example[127.0.0.1]
Mar 24 13:34:56 example postfix/virtual[7058]: D1844FC128: to=, relay=virtual, delay=0,
status=sent (delivered to maildir)
Mar 24 13:34:56 example postfix/qmgr[6950]: D1844FC128: removed
–ヘッダー情報
X-Virus-Scanned: ClamAV using ClamSMTP
が付加される。
-ウィルスメール
ウィルスメールの場合はstatus=VIRUS:<ウィルス名>と表示される。
下記ではstatus=VIRUS:Worm.SomeFool.Gen-1と表示されている。
Mar 24 13:36:20 example postfix/smtpd[7040]: connect from unknown[10.0.0.11]
Mar 24 13:36:20 example postfix/smtpd[7040]: 3A200FC124: client=unknown[10.0.0.11]
Mar 24 13:36:20 example postfix/cleanup[7047]: 3A200FC124: message-id=<20060324133643.4A3D.testuser@foo.com>
Mar 24 13:36:20 example postfix/qmgr[6950]: 3A200FC124: from=, size=26499, nrcpt=1 (queue active)
Mar 24 13:36:20 example postfix/smtpd[7040]: disconnect from unknown[10.0.0.11]
Mar 24 13:36:20 example clamsmtpd: 100008: accepted connection from: 127.0.0.1
Mar 24 13:36:20 example postfix/smtpd[7050]: connect from example[127.0.0.1]
Mar 24 13:36:20 example postfix/smtpd[7050]: 55C34FC128: client=unknown[10.0.0.11]
Mar 24 13:36:20 example clamsmtpd: 100008: from=testuser@foo.com, to=testuser2@example.com,
status=VIRUS:Worm.SomeFool.Gen-1
Mar 24 13:36:20 example postfix/smtp[7048]: 3A200FC124: to=, relay=127.0.0.1[127.0.0.1],
delay=0, status=sent (250 Virus Detected; Discarded Email)
Mar 24 13:36:20 example postfix/qmgr[6950]: 3A200FC124: removed
Mar 24 13:36:20 example postfix/smtpd[7050]: disconnect from example[127.0.0.1]
またstatus=sentとなっているが (250 Virus Detected; Discarded Email)と表示され
実際にはクライアントへは配信されず、ローカル内で処理される。

デフォルトのアクションでは検知したウィルスメールは自動で削除されるが
clamsmtpのパッケージ内にサンプルスクリプトが含まれており
ウィルス検知時のアクションを指定できる。

***ウィルス検知時のアクションの設定

clamsmtpdのパッケージに含まれるスクリプトの雛形は以下のとおり。
(本家サイトからもダウンロードできる)
/usr/share/doc/clamsmtp-1.6/scripts/clamsmtpd.sh
/usr/share/doc/clamsmtp-1.6/scripts/virus_action.sh
また/etc/clamsmtpd.confでスクリプトの置き場所を指定できる。
スクリプトの置き場所はひとまず/usr/local/binとして動作を検証。

スクリプトサンプルの抜粋。
/usr/share/doc/clamsmtp-1.6/scripts/virus_action.sh
変数fileでログの出力先を指定、dirでメールファイルの移動先を指定。
-clamsmtp
–/usr/local/bin/virus_action.sh
#!/bin/sh
file=”/path/to/virus.log”
dir=”/path/to/quarantine/”

exec 1>>$file
exec 2>>$file

# Add some fun log lines to the log file

echo “——————————————————-”
echo Sender $SENDER
echo Recipients $RECIPIENTS
echo Virus $VIRUS
echo “——————————————————-”

# Move the virus file to another directory
# This only works if Quarantine is enabled

if [ -n “$EMAIL” ]; then
mv “$EMAIL” “$dir”
fi

設定変更してテストしてみる。
サンプルスクリプトを/usr/local/binへ移動し、権限を変更し、編集。
[root@example etc]# cd /usr/share/doc/clamsmtp-1.6/scripts
[root@example scripts]# cp virus_action.sh /usr/local/bin/
[root@example scripts]# cd /usr/local/bin/
[root@example bin]# chmod 755 virus_action.sh
[root@example bin]# chown clamav:clamav virus_action.sh
[root@example bin]# vi virus_action.sh
-変更箇所
ログ出力先とメールファイルの保存先を変更。
26,27c26,29
< file="/path/to/virus.log" < dir="/path/to/quarantine/" --- > #file=”/path/to/virus.log”
> file=”/var/log/clamav/virus.log”
> #dir=”/path/to/quarantine/”
> dir=”/home/filtered/virus/”
またウィルス保存先ディレクトリの権限を変更する
ファイルのフィルタリングを実行するユーザーはclamav。
[root@example bin]# chmod -R 700 /home/filtered/virus/
[root@example bin]# chown -R clamav:clamav /home/filtered/virus/

続いて/etc/clamsmtpd.confを編集。
スクリプトを指定する。
[root@example bin]# vi /etc/clamsmtpd.conf
-ウィルスメールを残す設定
#Quarantine: off

Quarantine: on
-スクリプトをインクルードする
#VirusAction: /path/to/some/script.sh

VirusAction: /usr/local/bin/virus_action.sh
またログファイルを作成しておく。
[root@example etc]# cd /var/log/clamav/
[root@example clamav]# touch virus.log

編集後、デーモンを再起動して動作を確認。
[root@example etc]# /etc/init.d/clamsmtpd restart
Stopping ClamSmtpd: [ OK ]
Starting ClamSmtpd: [ OK ]
[root@example etc]#

***フィルタリング動作確認
試しにウィルスメールを転送したところ、
”quarantined virus file as: /tmp//virus.dTvXqZ”とログが出力され、
メールファイルが残されていることが確認できる。
Mar 24 16:53:04 example postfix/smtpd[11663]: initializing the server-side TLS engine
Mar 24 16:53:04 example postfix/smtpd[11663]: connect from unknown[10.0.0.11]
Mar 24 16:53:04 example postfix/trivial-rewrite[11666]: warning: do not list domain example.com
in BOTH mydestination and virtual_mailbox_domains
Mar 24 16:53:04 example postfix/smtpd[11663]: D0292FC102: client=unknown[10.0.0.11]
Mar 24 16:53:04 example postfix/cleanup[11670]: D0292FC102: message-id=<20060324165326.4ABF.testuser@foo.com>
Mar 24 16:53:04 example postfix/qmgr[10925]: D0292FC102: from=, size=26499, nrcpt=1 (queue active)
Mar 24 16:53:04 example postfix/smtpd[11663]: disconnect from unknown[10.0.0.11]
Mar 24 16:53:04 example clamsmtpd: 100005: accepted connection from: 127.0.0.1
Mar 24 16:53:05 example postfix/smtpd[11673]: initializing the server-side TLS engine
Mar 24 16:53:05 example postfix/smtpd[11673]: connect from example[127.0.0.1]
Mar 24 16:53:05 example postfix/trivial-rewrite[11666]: warning: do not list domain example.com in BOTH
mydestination and virtual_mailbox_domains
Mar 24 16:53:05 example postfix/smtpd[11673]: 0F7DCFC112: client=unknown[10.0.0.11]
Mar 24 16:53:05 example clamsmtpd: 100005: quarantined virus file as: /tmp//virus.dTvXqZ
Mar 24 16:53:05 example clamsmtpd: 100005: from=testuser@foo.com, to=testuser2@example.com,
status=VIRUS:Worm.SomeFool.Gen-1
Mar 24 16:53:05 example postfix/smtp[11671]: D0292FC102: to=, relay=127.0.0.1[127.0.0.1],
delay=1, status=sent (250 Virus Detected; Discarded Email)
Mar 24 16:53:05 example postfix/qmgr[10925]: D0292FC102: removed
Mar 24 16:53:05 example postfix/smtpd[11673]: disconnect from example[127.0.0.1]

また保存先にメールファイルがmvされていることが確認できる。
[root@example etc]# ll /home/filtered/virus/ | grep virus.dTvXqZ
-rw——- 1 clamav clamav 26499 3月 24 16:53 virus.dTvXqZ
[root@example etc]#
※ファイルはメールファイルの状態で残される。

**運用
***デーモンの自動起動
OSboot時に起動する順番としては
+clamav
+clamsmtp
+postfix
となると思われる。
そのほかのauthlib等を含めた順番は別項で検証。
***パターンファイルの更新
dailyでパターンファイルを更新。cronで実行。
必要に応じてログをadmin宛へ送信するのもオッケー。
-cron例
0 3 * * * /usr/sbin/freshclam | mail -s “Freshclam Result” admin@foo.com
***フィルタリングしたウィルスメールの確認/レポート
フィルタリングを実行した際対象となったウィルスメールの
サブジェクトやウィルス種別をログファイルに落とす。
ログの出力については上記のvirus_action.shの項目を参照。
こちらも必要に応じてログをadminのエイリアスなどへ送信してもオッケー。
※スクリプト内で指定可能
***ウィルスメールの処理
サーバに残しておいても仕方が無いのでレポートを確認したら削除?
***ログのローテート
clamavはインストール時に/etc/logrotate.dへ
clamdとfreshclamというファイルを置くので、デフォルトのままなら放置オッケー。
但し出力先を変更する場合は要編集。
またclamsmtpの出力ログのローテートも追加する必要がある。
これはclamdに追加ないしコピってclamsmtp用のrotateファイルを作成しても可。

[/pukiwiki]

-infra, MAIL, postfix
-, ,

執筆者:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


関連記事

nagiosgraph​/2.詳細設定

  [pukiwiki] nagiosgraphの詳細設定。一部編集中です(スイマセン) 不明点は都度INSTALLドキュメントを参照すること。 http://nagiosgraph.svn.sour …

no image

[Juniper]SRX ssh公開鍵認証でログイン

LinuxサーバーへdailyでSRXのコンフィグをバックアップするべく、tftpサーバーを設定しようと思ったら どうやらtftpらしきものがないので、sshでリモートコマンドを使ってコンフィグをとっ …

nagiosgraph/1.install

  [pukiwiki] *nagiosgraphインストールログ [#x0215ac2] nagiosgraphの導入ログ。 参考資料:nagiosgraph本家サイト http://nagiosg …

no image

net-snmp snmpd.logの設定を追加、変更

[pukiwiki] 某VPSサービスで借りた仮想サーバーでsnmpdを動かしたところ、ログがどこにも出ていなかったのでログ出力設定を追加。 たまたまだと思いつつ、設定状況は環境によって様々だと思うの …

no image

monitインストール[RHEL3]

[pukiwiki]   システムマネージメントツールmonitのインストール 監視ツールの発展版といえる、デーモン監視と異常時の再起動などを行う便利ツール。 既にnagiosやzabbixをインスト …