CentOS

2006/4/17 01:54 PM 更新

ClamAV

外部から送られてくるメールのウィルスチェックをおこなうためオープンソースのアンチウィルスソフトであるClamAVを導入する。
またClamAVはqmailと連動して動作するようにする。

ClamAVを動作させるためには以下のプログラムが必要になるので事前にインストールしておく(全てRPMパッケージ名で記載)。
 ・zlib
 ・zlib-devel
 ・gcc
 ・bzip2
 ・bzip2-devel
 ・gmp-devel

ClamAVの公式サイトよりClamAVの最新版のソースファイルを入手しインストールする。
$ wget http://jaist.dl.sourceforge.net/sourceforge/clamav/clamav-0.87.tar.gz
$ tar xvzf clamav-0.87.tar.gz
$ cd clamav-0.87
$ ./configure --prefix=/usr/local/clamav --disable-clamuko
$ make
# make install
ClamAVを起動する専用ユーザを作成する。
# /usr/sbin/groupadd -g 20007 clamav
# /usr/sbin/useradd -u 20007 -g 20007 -s /sbin/nologin -d /dev/null clamav
ClamAVで使用するディレクトリを作成する。
# mkdir /var/clamd
# chown -R qscand:clamav /var/clamd/
# chmod -R 770 /var/clamd/

# mkdir /var/run/clamd
# chown -R qscand:qscand /var/run/clamd/
# chmod -R 755 /var/run/clamd/
ClamAVの設定ファイルを編集。
  /usr/local/clamav/etc/clamd.conf
##
●Exampleをコメントアウトにする。
# Comment or remove the line below.
#Example

●シスログを有効にする。
# Use system logger (can work together with LogFile).
# Default: disabled
LogSyslog

# Specify the type of syslog messages - please refer to 'man syslog'
# for facility names.
# Default: LOG_LOCAL6
#LogFacility LOG_MAIL

●PIDファイルの作成場所を指定する。
# This option allows you to save a process identifier of the listening
# daemon (main thread).
# Default: disabled
#PidFile /var/run/clamd.pid
PidFile /var/run/clamd/clamd.pid

●テンポラリディレクトリを指定する。
# Optional path to the global temporary directory.
# Default: system specific (usually /tmp or /var/tmp).
#TemporaryDirectory /var/tmp
TemporaryDirectory /var/clamd

●ClamAVのパターンファイル保存場所を指定する。
# Path to the database directory.
# Default: hardcoded (depends on installation options)
#DatabaseDirectory /var/lib/clamav
DatabaseDirectory /usr/local/clamav/share/clamav

●ローカルソケットを指定する。
# Path to a local socket file the daemon will listen on.
# Default: disabled
#LocalSocket /tmp/clamd
LocalSocket /var/clamd/clamd

●ClamAVの起動ユーザを指定する。(qmailと連動させるためqscandとしている)
# Run as a selected user (clamd must be started by root).
# Default: disabled
#User clamav
User qscand


# By default clamd uses scan options recommended by libclamav. This option
# disables recommended options and allows you to enable selected ones below.
# DO NOT TOUCH IT unless you know what you are doing.
# Default: disabled
DisableDefaultScanOptions

●メールをスキャン対象とする。
# Enable internal e-mail scanner.
# Default: enabled
ScanMail
ソースに含まれているサンプルを基に起動スクリプトを作成する。
# cp $SOURCE/contrib/init/RedHat/clamd /etc/rc.d/init.d/clamd
  /etc/rc.d/init.d/clamd
#! /bin/bash
#
# crond   Start/Stop the clam antivirus daemon.
#
# chkconfig: 2345 70 41
# description: clamd is a standard Linux/UNIX program that scans for Viruses.
# processname: clamd
# config: /usr/local/etc/clamd.conf
# pidfile: /var/lock/subsys/clamd

# Source function library.
. /etc/init.d/functions

RETVAL=0

# See how we were called.

prog="clamd"
progdir="/usr/local/sbin"

# Source configuration
if [ -f /etc/sysconfig/$prog ] ; then
        . /etc/sysconfig/$prog
fi

start() {
        echo -n $"Starting $prog: "
        daemon $progdir/$prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/clamd
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        # Would be better to send QUIT first, then killproc if that fails
        killproc $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/clamd
        return $RETVAL
}

rhstatus() {
        status clamd
}

restart() {
        stop
        start
}

reload() {
        echo -n $"Reloading clam daemon configuration: "
        killproc clamd -HUP
        retval=$?
        echo
        return $RETVAL
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  status)
        rhstatus
        ;;
  condrestart)
        [ -f /var/lock/subsys/clamd ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|reload|restart|condrestart}"
        exit 1
esac

exit $?
起動スクリプトを登録する。
# /sbin/chkconfig --add clamd
# /sbin/chkconfig --list
clamd 0:オフ 1:オフ 2:オン 3:オン 4:オン 5:オン 6:オフ
clamAVを起動する。
# /sbin/service clamd start
clamdを起動中:                                             [  OK  ]

# ps -aux
qscand   29460  0.0  3.5 10212 8908 ?        S    07:57   0:00 /usr/local/clamav/sbin/clamd
サーバ稼働時のログ。
# tail -f /var/log/messages
Sep 25 19:12:47 boris clamd[7653]: Daemon started.
Sep 25 19:12:47 boris clamd[7653]: clamd daemon 0.87 (OS: linux-gnu, ARCH: i386, CPU: i686)
Sep 25 19:12:47 boris clamd[7653]: Log file size limited to 1048576 bytes.
Sep 25 19:12:47 boris clamd[7653]: Running as user qscand (UID 214, GID 214)
Sep 25 19:12:47 boris clamd[7653]: Reading databases from /usr/local/clamav/share/clamav
Sep 25 19:12:50 boris clamd[7653]: Protecting against 40192 viruses.
Sep 25 19:12:50 boris clamd[7654]: Unix socket file /var/clamd/clamd
Sep 25 19:12:50 boris clamd[7654]: Setting connection queue length to 15
Sep 25 19:12:50 boris clamd[7654]: RECOMMENDED OPTIONS DISABLED.
Sep 25 19:12:50 boris clamd[7654]: Archive: Archived file size limit set to 10485760 bytes.
Sep 25 19:12:50 boris clamd[7654]: Archive: Recursion level limit set to 8.
Sep 25 19:12:50 boris clamd[7654]: Archive: Files limit set to 1000.
Sep 25 19:12:50 boris clamd[7654]: Archive: Compression ratio limit disabled.
Sep 25 19:12:50 boris clamd[7654]: Archive support enabled.
Sep 25 19:12:50 boris clamd[7654]: Archive: RAR support enabled.
Sep 25 19:12:50 boris clamd[7654]: Portable Executable support disabled.
Sep 25 19:12:50 boris clamd[7654]: Mail files support enabled.
Sep 25 19:12:50 boris clamd[7654]: OLE2 support disabled.
Sep 25 19:12:50 boris clamd[7654]: HTML support disabled.
Sep 25 19:12:50 boris clamd[7654]: Self checking every 1800 seconds.
ウィルスは次から次へと新しいタイプのものが登場するのでアンチウィルス側も新しいウィルスに対応する必要がある。
ClamAVもパターンファイルが定期的にリリースされるので自動的にアップデートするような仕組みを構築する。
  /usr/local/clamav/etc/freshclam.conf(変更点のみ)
@@ -6,7 +6,7 @@

-Example
+#Example

 # Path to the database directory.
 # WARNING: It must match clamd.conf's directive!
@@ -23,7 +23,7 @@

 # Use system logger (can work together with UpdateLogFile).
 # Default: disabled
-#LogSyslog
+LogSyslog

 # Specify the type of syslog messages - please refer to 'man syslog'
 # for facility names.
@@ -52,7 +52,7 @@
 # Uncomment the following line and replace XY with your country
 # code. See http://www.iana.org/cctld/cctld-whois.htm for the full list.
 # Default: There is no default, which results in an error when running freshclam
-#DatabaseMirror db.XY.clamav.net
+DatabaseMirror db.jp.clamav.net
動作確認のために手動でアップデートを実行する。
# /usr/local/clamav/bin/freshclam -v
Current working dir is /usr/local/clamav/share/clamav
Max retries == 3
ClamAV update process started at Sun Oct 9 01:53:08 2005
Querying current.cvd.clamav.net
TTL: 900
Software version from DNS: 0.87
main.cvd version from DNS: 34
main.cvd is up to date (version: 34, sigs: 39625, f-level: 5, builder: tkojm)
daily.cvd version from DNS: 1123
Retrieving http://db.jp.clamav.net/daily.cvd
Downloading daily.cvd [*]
daily.cvd updated (version: 1123, sigs: 882, f-level: 6, builder: arnaud)
Database updated (40507 signatures) from db.jp.clamav.net (IP: 59.87.0.36)
Freeing option list...done
定期的にアップデートを実行するためにcronに登録
  /etc/cron.hourly/freshclam.cron
#/bin/sh

/usr/local/clamav/bin/freshclam --quiet
exit 0
ファイルに実行権限をつける。
# chmod 700 /etc/cron.hourly/freshclam.cron
cron動作時のログ。
# tail -f /var/log/clamd.log
Oct 9 01:53:08 boris freshclam[23842]: Daemon started.
Oct 9 01:53:08 boris freshclam[23842]: ClamAV update process started at Sun Oct 9 01:53:08 2005
Oct 9 01:53:08 boris freshclam[23842]: main.cvd is up to date (version: 34, sigs: 39625, f-level: 5, builder: tkojm)
Oct 9 01:53:10 boris freshclam[23842]: daily.cvd updated (version: 1123, sigs: 882, f-level: 6, builder: arnaud)
Oct 9 01:53:10 boris freshclam[23842]: Database updated (40507 signatures) from db.jp.clamav.net (IP: 59.87.0.36)

あとはメール受信時にウィルスを検知し駆除されるかを確認する。