Young Leaves

CentOS 8にmaldetect を導入しClamAV と連携させマルウェアの検疫を行う方法

今回はCentOS 8 にmaldetect を導入しセキュリティスキャンツールのClamAV と連携しマルウェアの検疫を行う方法について説明します。本記事は2020年6月5日現在の記事であり、現在ではOS のサポートが切れているため、手順などに違いがある可能性があるため、そのあたりを考慮の上確認をお願いします。

実施環境

OS

CentOS 8 最小限のインストール

maldetect

1.6.4

maldetectとは

maldetect

maldetectはGNU GPLv2ライセンスの下で開発されたLinux用のマルウェアをスキャンし検疫するソフトウェアです。Windows等で使用されるウィルスバスターやカスペルスキーといったウィルス対策ソフトのLinux版のイメージです。最新のマルウェアに対応するためのシグネチャの更新やマルウェア検知時のメール通知等、Linuxサーバのセキュリティ対策に必要な機能があるため、セキュリティを高めるためにも導入しておきたいソフトウェアです。

事前準備

初めにmaldetectのソースファイルをダウンロード、展開するためにwgetとtarをインストールします。

# wget、tarをインストールする。
dnf -y install wget tar

maldetectのインストール

次にmaldetectのソースファイルを公式サイトからダウンロードし展開します。

# maldetectのソースファイルをダウンロードし展開する
cd /usr/local/src
wget https://www.rfxn.com/downloads/maldetect-current.tar.gz
tar zxf maldetect-current.tar.gz

ソースファイルのダウンロード後、フォルダ内のinstall.sh を実行します。インストール用のシェルスクリプト実行後は自動でインストールを行ってくれます。

# install.shを実行しmaldetectをインストールする
cd maldetect-1.6.4/
./install.sh

ClamAVのインストール

今回はClamAVと連携を行うため、ClamAVをインストールします。ClamAVはepelリポジトリからインストールするため、epelリポジトリを先にインストールします。

# epelリポジトリをインストールしClamAVとinotify-toolsをインストールする 
dnf -y install epel-release
dnf -y install clamav clamav-devel clamav-update inotify-tools

ClamAVのインストール後、ClamAVをアップデートします。

# ClamAVをアップデートする
freshclam

conf.maldetの設定

maldetect、ClamAVのインストール後、maldetectの設定をします。maldetectをソースインストールした場合の設定ファイルは/usr/local/maldetect/conf.maldetとなるため、conf.maldetを編集します。

# maldetectの設定ファイルを編集する。
vi /usr/local/maldetect/conf.maldet

■conf.maldet
------------------------------------------------------------
# E-mailアラートを有効にする
email_alert="1"

# アラートを送信するメールアドレスを設定する
email_addr="hogehoge@example.com"

# maldetectのcron.dailyを有効にする
cron_daily_scan="1"

# ClamAVスキャンを有効にする
scan_clamscan="1"

# 検疫を有効にする
quarantine_hits="1"

# マルウェア部分の文字列を削除する
quarantine_clean="1"

# マルウェアの検出されたユーザーを無効にする
quarantine_suspend_user="1"

# マルウェアの検出されたユーザーの無効時間を設定する
quarantine_suspend_user_minuid="500"
------------------------------------------------------------

サンプルウィルスのダウンロード

今回はmaldetectで意図的にマルウェアを検出させるため、eicarよりサンプルウィルスをダウンロードします。

# eicarからサンプルウィルスをダウンロードする
cd /tmp
wget https://secure.eicar.org/eicar.com

もし、rootユーザーでサンプルウィルスをダウンロードした場合、conf.maldetのデフォルトではrootユーザーのファイルを無視する設定となっているため、ファイルの所有者等をテスト用の一般ユーザーに変更しておきます。今回はtestuserという一般ユーザーに変更します。

# サンプルウィルスの所有者を一般ユーザーに変更する
chown testuser.testuser eicar.com

今回はconf.maldetにてマルウェアを検出したユーザーを一時的に無効とする設定にしているため、マルウェア検出の確認を行う際は無効となっても問題ないユーザーで行ってください。

maldetectの起動、アップデート、スキャン

設定が一通り完了したらmaldetectを起動します。maldetect起動後、コマンドおよびシグネチャファイルのアップデートをします。

# maldetectを起動する
systemctl start maldet

# maldetectのコマンドをアップデートする
maldet -d

# maldetectのシグネチャファイルをアップデートする
maldet -u

maldetectの起動、アップデートが完了したら、先程サンプルウィルスをダウンロードした/tmpファイルに対してスキャンを行います。

# /tmpに対しmaldetectでスキャンする
maldet -a /tmp
# /tmpに対しmaldetectでスキャンする
maldet -a /tmp

Linux Malware Detect v1.6.4
            (C) 2002-2019, R-fx Networks <proj@rfxn.com>
            (C) 2019, Ryan MacDonald <ryan@rfxn.com>
This program may be freely redistributed under the terms of the GNU GPL v2

maldet(13880): {scan} signatures loaded: 17030 (14210 MD5 | 2035 HEX | 785 YARA | 0 USER) 
maldet(13880): {scan} building file list for /tmp, this might take awhile... 
maldet(13880): {scan} setting nice scheduler priorities for all operations: cpunice 19 , ionice 6
maldet(13880): {scan} file list completed in 0s, found 2 files...
maldet(13880): {scan} found clamav binary at /usr/bin/clamscan, using clamav scanner engine... 
maldet(13880): {scan} scan of /tmp (2 files) in progress...
maldet(13880): {clean} could not find clean rule for hit EICAR.TEST or file /usr/local/maldetect/quarantine/eicar.com.138411663 no longer exists. 
maldet(13880): {scan} processing scan results for hits: 1 hits 0 cleaned 
maldet(13880): {scan} scan completed on /tmp: files 2, malware hits 1,  cleaned hits 0, time 17s 
maldet(13880): {scan} scan report saved, to view run: maldet --report 200601-2208.13880
</ryan@rfxn.com></proj@rfxn.com>

malware hitsの部分がマルウェアを検出した数となります。今回はサンプルウィルスのeicar.com をダウンロードしているため1件検出しています。

スキャンレポートの確認

スキャンレポートを確認するためには「scan report saved, to view run:」の後に記載されているコマンドを実行します。

# maldetectのスキャンレポートを表示する
maldet --report 200601-2208.13880
# maldetectのスキャンレポートを表示する
maldet --report 200601-2208.13880

------------------------------------------------------------
HOST:      centos8-test
SCAN ID:   200601-2208.13880
STARTED:    6月  1 2020 22:08:21 +0900
COMPLETED:  6月  1 2020 22:08:38 +0900
ELAPSED:   17s [find: 0s]

PATH:          /tmp
TOTAL FILES:   2
TOTAL HITS:    1
TOTAL CLEANED: 0

FILE HIT LIST:
{HEX}EICAR.TEST.3 : /tmp/eicar.com =&gt; /usr/local/maldetect/quarantine/eicar.com.138411663 
===============================================
Linux Malware Detect v1.6.4 < proj@rfxn.com >
------------------------------------------------------------

FILE HIT LISTの部分より、eicar.com が検疫されたことを確認できます。

検疫されたファイルの確認

検疫されたファイルは/usr/local/maldetect/quarantine 配下に移動します。移動されたファイルを確認すると、権限が無効化されていることが確認できます。

# 検疫されたeicar.comを確認する
ls -la /usr/local/maldetect/quarantine/
# 検疫されたeicar.comを確認する
ls -la /usr/local/maldetect/quarantine/

----------.  1 root root   68  6月  1 22:08 eicar.com.138411663
-rw-r--r--.  1 root root  187  6月  1 22:08 eicar.com.138411663.info