CentOS 7 Clam AntiVirusインストール

2015年1月4日修正
clamscanでスキャンすると時間が結構かかってたので、より高速に動作するclamdscanでウイルススキャンするように変更。また、除外ディレクトリーも新たに設定。
※clamdscanはスキャンして、変更の無いファイルは2度目以降のスキャンをスキップするので高速である。

■Clam AntiVirusをRepoforgeリポジトリからインストール
yumでインストールする場合、Repoforgeリポジトリを追加しておく必要がある

[root@server1 ~]# yum -y install clamd
読み込んだプラグイン:fastestmirror, langpacks, priorities
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * centosplus: ftp.nara.wide.ad.jp
 * epel: mirrors.ustc.edu.cn
 * extras: ftp.nara.wide.ad.jp
 * rpmforge: ftp.neowiz.com
 * updates: ftp.nara.wide.ad.jp
77 packages excluded due to repository priority protections
依存性の解決をしています
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> トランザクションの確認を実行しています。
---> パッケージ clamd.x86_64 0:0.98.4-1.el7.rf を インストール
--> 依存性解決を終了しました。

依存性を解決しました

====================================================================================================
 Package            アーキテクチャー    バージョン                      リポジトリー           容量
====================================================================================================
インストール中:
 clamd              x86_64              0.98.4-1.el7.rf                 rpmforge              158 k

トランザクションの要約
====================================================================================================
インストール  1 パッケージ

総ダウンロード容量: 158 k
インストール容量: 680 k
Downloading packages:
clamd-0.98.4-1.el7.rf.x86_64.rpm                                             | 158 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : clamd-0.98.4-1.el7.rf.x86_64                                        1/1
  検証中                  : clamd-0.98.4-1.el7.rf.x86_64                                        1/1

インストール:
  clamd.x86_64 0:0.98.4-1.el7.rf

完了しました!

■設定ファイル変更

[root@server1 ~]# vi /etc/clamd.conf

#除外ディレクトリーを指定
# Don't scan files and directories matching regex
# This directive can be used multiple times
# Default: scan all
#ExcludePath ^/proc/
#ExcludePath ^/sys/
#------追加ここから------
ExcludePath ^/proc/
ExcludePath ^/sys/
ExcludePath ^/backup/
ExcludePath ^/home/share/
#--------ここまで--------

#ルートで動作するように変更
User clamav
↓
#User clamav

#壊れたファイルは誤検出が多いため無効
DetectBrokenExecutables yes
↓
#DetectBrokenExecutables yes

#圧縮ファイルのスキャンは無効
# ClamAV can scan within archives and compressed files.
# Default: yes
ScanArchive yes
 ↓
ScanArchive no

※どのディレクトリを除外ディレクトリーに指定するかは以下を参照。
CentOSの各ディレクトリに格納されているファイルについて

bin    CentOSに最初から有るコマンド群
boot   ブートに関するファイル
dev    デバイスに関するファイル
etc    コンフィグファイル
home   root以外のユーザーディレクトリ
lib    コマンドを実行するためのファイル
lib64  コマンドを実行するための64ビット用ファイル
media  CDやDVDのマウントポイント
mnt    ファイルシステムのマウントポイント
opt    パッケージのインストール先
proc   プロセスやカーネルに関するファイル
root   rootユーザーのディレクトリ
run    プロセスのデータ /var/runは/runのシンボリックリンク
sbin   管理者が使用するコマンド
srv    システムのサービスデータ
sys    ドライバ関連のファイル
tmp    テンポラリファイル
usr    ユーザーが共通で使うプログラム群。
     ※/usr/share/applicationsがWindowsのコントロールパネルに相当
var    ログやApacheのデフォルトのドキュメントルートなど

■Clam AntiVirusの起動

clamdの起動
[root@server1 ~]# systemctl start clamd

clamdの自動起動設定
[root@server1 ~]# systemctl enable clamd
clamd.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig clamd on
The unit files have no [Install] section. They are not meant to be enabled
using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
   .wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
   a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
   D-Bus, udev, scripted systemctl call, ...).

■「freshclam」を使用してVirusDBをアップデート

ウィルス定義ファイルの更新機能を有効化
[root@server1 ~]# sed -i 's/Example/#Example/g' /etc/freshclam.conf

VirusDBをアップデート
[root@server1 ~]# freshclam
ClamAV update process started at Sun Jan  4 12:47:36 2015
WARNING: Your ClamAV installation is OUTDATED!
WARNING: Local version: 0.98.4 Recommended version: 0.98.5
DON'T PANIC! Read http://www.clamav.net/support/faq
main.cld is up to date (version: 55, sigs: 2424225, f-level: 60, builder: neo)
Downloading daily-19879.cdiff [100%]
daily.cld updated (version: 19879, sigs: 1301911, f-level: 63, builder: neo)
bytecode.cld is up to date (version: 244, sigs: 44, f-level: 63, builder: dgoddard)
Database updated (3726180 signatures) from db.local.clamav.net (IP: 218.44.253.75)
Clamd successfully notified about the update.

※clamAVのバージョンが古いというメッセージが出るが、特に問題ないので放置。

■VirusDB自動アップデートスクリプトの作成
yumでインストールすると自動で毎日アップデートしてくれるが、何らかの理由でファイルが無いという場合には以下のファイルを作成。

[root@server1 ~]# vi /etc/cron.daily/freshclam
#!/bin/sh

### A simple update script for the clamav virus database.
### This could as well be replaced by a SysV script.

### fix log file if needed
LOG_FILE="/var/log/clamav/freshclam.log"
if [ ! -f "$LOG_FILE" ]; then
    touch "$LOG_FILE"
    chmod 644 "$LOG_FILE"
    chown clamav.clamav "$LOG_FILE"
fi

/usr/bin/freshclam \
    --quiet \
    --datadir="/var/clamav" \
    --log="$LOG_FILE" \
    --daemon-notify="/etc/clamd.conf"

■clamscanでウイルススキャン
※/rootディレクトリをスキャンしてウイルスが見つかった場合だけ表示、削除する。

[root@server1 ~]# clamscan /root --infected --remove --recursive

----------- SCAN SUMMARY -----------
Known viruses: 3720154
Engine version: 0.98.4
Scanned directories: 1456
Scanned files: 3455
Infected files: 0
Data scanned: 139.04 MB
Data read: 3429.55 MB (ratio 0.04:1)
Time: 36.553 sec (0 m 36 s)

■より高速に動作するclamdscanでウイルススキャン
※clamdscanはウイルススキャンして、変更の無いファイルは2度目以降のウイルススキャンをスキップするので高速である。

[root@server1 ~]# clamdscan /root
/root: OK

----------- SCAN SUMMARY -----------
Infected files: 0
Time: 21.567 sec (0 m 21 s)

■clamdscanの定期自動実行設定(毎日実行するので/etc/cron.dailyに保存)

[root@server1 ~]# vi /etc/cron.daily/clamdscan
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 
CLAMSCANTMP=`mktemp`
 
clamdscan / --remove > $CLAMSCANTMP 2>&1
 
[ ! -z "$(grep FOUND$ $CLAMSCANTMP)" ] && \
grep FOUND$ $CLAMSCANTMP | mail -s "Virus Found" hoge@example.com
 
[ -z "$(grep FOUND$ $CLAMSCANTMP)" ] && \
echo "clamdscan normal end" | mail -s "Virus Not Found" hoge@example.com
 
rm -f $CLAMSCANTMP

■Clam AntiVirus定期自動実行スクリプトに実行権限付加

[root@server1 ~]# chmod +x /etc/cron.daily/clamdscan

■ウィルススキャン確認

[root@server1 ~]# sh /etc/cron.daily/clamdscan

ウイルススキャンが終了すると、問題なければ以下の様なメールが届く。
Virus Not Found

clamdscan normal end

■再起動するとclamdが起動しないというトラブル発生

[root@server1 ~]# systemctl status clamd
clamd.service - SYSV: Clam AntiVirus Daemon is a TCP/IP or socket protocol server.
   Loaded: loaded (/etc/rc.d/init.d/clamd)
   Active: failed (Result: exit-code) since 日 2014-07-20 11:26:16 JST; 2min 7s ago
  Process: 21056 ExecStart=/etc/rc.d/init.d/clamd start (code=exited, status=1/FAILURE)

 7月 20 12:09:17 server1.yokensaka.com systemd[1]: Starting SYSV: Clam AntiVirus Daemon is a T.....
 7月 20 12:09:17 server1.yokensaka.com clamd[1034]: clamd daemon 0.98.4 (OS: linux-gnu, ARCH: ...4)
 7月 20 12:09:17 server1.yokensaka.com clamd[1034]: Log file size limited to 4294967295 bytes.
 7月 20 12:09:17 server1.yokensaka.com clamd[1034]: Reading databases from /var/clamav
 7月 20 12:09:17 server1.yokensaka.com clamd[1034]: Not loading PUA signatures.
 7月 20 12:09:17 server1.yokensaka.com clamd[1034]: Bytecode: Security mode set to "TrustSigned".
 7月 20 12:09:33 server1.yokensaka.com clamd[1034]: Loaded 3502516 signatures.
 7月 20 12:09:34 server1.yokensaka.com clamd[1034]: TCP: Bound to address 127.0.0.1 on port 3310
 7月 20 12:09:34 server1.yokensaka.com clamd[1034]: TCP: Setting connection queue length to 30
 7月 20 12:09:34 server1.yokensaka.com clamd[1034]: LOCAL: Socket file /var/run/clamav/clamd.s...ry
 7月 20 12:09:34 server1.yokensaka.com clamd[1034]: Can't unlink the socket file /var/run/clam...ck
 7月 20 12:09:34 server1.yokensaka.com clamd[1021]: Starting Clam AntiVirus Daemon: ERROR: LOC...ry
 7月 20 12:09:34 server1.yokensaka.com clamd[1021]: ERROR: Can't unlink the socket file /var/r...ck
 7月 20 12:09:34 server1.yokensaka.com clamd[1021]: [失敗]
 7月 20 12:09:34 server1.yokensaka.com systemd[1]: clamd.service: control process exited, code...=1
 7月 20 12:09:34 server1.yokensaka.com systemd[1]: Failed to start SYSV: Clam AntiVirus Daemon.....
 7月 20 12:09:34 server1.yokensaka.com systemd[1]: Unit clamd.service entered failed state.
Hint: Some lines were ellipsized, use -l to show in full.

ソケットファイルが見つからないというエラーで clamd が起動しなくなる。原因はcentos7では/ver/runがtempfsになったので再起動後にはディレクトリ等全て消えてしまう。なのでソケットファイルが見つからないというエラーになるのではと思う。

■手動でソケット用ディレクトリーを作成してclamdを起動させてみる

[root@server1 ~]# mkdir /var/run/clamav
[root@server1 ~]# chown -R clamav:clamav /var/run/clamav
[root@server1 ~]# chmod -R 700 /var/run/clamav
[root@server1 ~]# systemctl start clamd
[root@server1 ~]# systemctl status clamd
clamd.service - SYSV: Clam AntiVirus Daemon is a TCP/IP or socket protocol server.
   Loaded: loaded (/etc/rc.d/init.d/clamd)
   Active: active (running) since 日 2014-07-20 12:02:43 JST; 7s ago
  Process: 2583 ExecStart=/etc/rc.d/init.d/clamd start (code=exited, status=0/SUCCESS)
 Main PID: 2586 (clamd)
   CGroup: /system.slice/clamd.service
           └─2586 clamd

 7月 20 12:02:42 server1.yokensaka.com clamd[2586]: ELF support enabled.
 7月 20 12:02:42 server1.yokensaka.com clamd[2586]: Detection of broken executables enabled.
 7月 20 12:02:42 server1.yokensaka.com clamd[2586]: Mail files support enabled.
 7月 20 12:02:42 server1.yokensaka.com clamd[2586]: OLE2 support enabled.
 7月 20 12:02:42 server1.yokensaka.com clamd[2586]: PDF support enabled.
 7月 20 12:02:42 server1.yokensaka.com clamd[2586]: SWF support enabled.
 7月 20 12:02:42 server1.yokensaka.com clamd[2586]: HTML support enabled.
 7月 20 12:02:42 server1.yokensaka.com clamd[2586]: Self checking every 600 seconds.
 7月 20 12:02:43 server1.yokensaka.com clamd[2583]: Starting Clam AntiVirus Daemon: [  OK  ]
 7月 20 12:02:43 server1.yokensaka.com systemd[1]: Started SYSV: Clam AntiVirus Daemon is a TCP/IP or socket protocol server..

起動できた

再起動後にディレクトリーを作成するように設定ファイルを編集(赤字の部分を追記)

[root@server1 ~]# vi /etc/init.d/clamd
#!/bin/sh
#
# Startup script for the Clam AntiVirus Daemon
#
# chkconfig: 2345 61 39
# description: Clam AntiVirus Daemon is a TCP/IP or socket protocol \
#              server.
# processname: clamd
# pidfile: /var/run/clamav/clamd.pid
# config: /etc/clamav.conf

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

# Source networking configuration.
. /etc/sysconfig/network

[ -x /usr/sbin/clamd ] || exit 0

start(){
        echo -n "Starting Clam AntiVirus Daemon: "
        #再起動後にclamavのdirを作成
        mkdir /var/run/clamav > /dev/null 2>&1
        schown clamav:clamav /var/run/clamav > /dev/null 2>&1
        daemon clamd
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/clamd
        return $RETVAL
}

stop() {
        echo -n "Stopping Clam AntiVirus Daemon: "
        killproc clamd
        rm -f /var/clamav/clamd.socket
        rm -f /var/run/clamav/clamav.pid
        RETVAL=$?
        echo
        ### heres the fix... we gotta remove the stale files on restart
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/clamd
        return $RETVAL
}

restart() {
        stop
        start
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status clamd
        RETVAL=$?
        ;;
  restart|reload)
        restart
        ;;
  condrestart)
        [ -e /var/lock/subsys/clamd ] && $0 restart
        RETVAL=$?
        ;;
  *)
        echo "Usage: clamd {start|stop|status|restart|reload|condrestart}"
        exit 1
esac

exit $RETVAL

2015/1/2追記
以前はclamdの設定ファイルを編集していたが、/etc/tmpfiles.d/にclamd.confを追記することで、OS起動時に/var/run/clamavディレクトリを作成できるように変更

[root@server1 ~]# vi /etc/tmpfiles.d/clamd.conf
d /var/run/clamav 0755 root root - ← 追加
[root@server1 ~]# systemd-tmpfiles --create clamd.conf

systemdをリロード
[root@server1 ~]# systemctl daemon-reload
clamdを再起動
[root@server1 ~]# systemctl restart clamd

■確認

[root@server1 ~]# reboot

[root@server1 ~]# systemctl status clamd
clamd.service - SYSV: Clam AntiVirus Daemon is a TCP/IP or socket protocol server.
   Loaded: loaded (/etc/rc.d/init.d/clamd)
   Active: active (running) since 金 2015-01-02 16:24:11 JST; 30s ago
  Process: 1005 ExecStart=/etc/rc.d/init.d/clamd start (code=exited, status=0/SUCCESS)
 Main PID: 1480 (clamd)
   CGroup: /system.slice/clamd.service
           └─1480 clamd

 1月 02 16:24:10 server1.yokensaka.com clamd[1480]: ELF support enabled.
 1月 02 16:24:10 server1.yokensaka.com clamd[1480]: Detection of broken executables enabled.
 1月 02 16:24:10 server1.yokensaka.com clamd[1480]: Mail files support enabled.
 1月 02 16:24:10 server1.yokensaka.com clamd[1480]: OLE2 support enabled.
 1月 02 16:24:10 server1.yokensaka.com clamd[1480]: PDF support enabled.
 1月 02 16:24:10 server1.yokensaka.com clamd[1480]: SWF support enabled.
 1月 02 16:24:10 server1.yokensaka.com clamd[1480]: HTML support enabled.
 1月 02 16:24:10 server1.yokensaka.com clamd[1480]: Self checking every 600 seconds.
 1月 02 16:24:11 server1.yokensaka.com clamd[1005]: Starting Clam AntiVirus Daemon: [  OK  ]
 1月 02 16:24:11 server1.yokensaka.com systemd[1]: Started SYSV: Clam AntiVirus Daemon is a TC.....
Hint: Some lines were ellipsized, use -l to show in full.

自動起動できた。

■epel.repoでclamを除外 2015年2月14日追記
Clam AntiVirusはRepoforgeリポジトリのものを使っているが、Repoforgeリポジトリとepelリポジトリの両方を有効にしていると、Clam AntiVirusのyum updateでエラーになり、全てのupdateが出来なくなるので、epel.repoでclamを除外しておく。

[root@server1 ~]# vi /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1 ← 1 に設定
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
exclude=clam* ← clamはRepoforgeのものを使うのでepelのclamは除外