LoginSignup
14
18

More than 5 years have passed since last update.

アップロードされたファイルにウイルスチェックを行う

Posted at

LAMP環境において、ユーザがアップロードしたファイルにウイルスチェックを施す方法です。
PHP5.6 + Cent OS 6で検証しています。

サーバーにアンチウイルスソフトをインストールする

今回は、Linux用のオープンソースのアンチウイルスソフトである「Clam AntiVirus」を使用します。

参考 :
インストール方法はこちらの記事を参照しました
AWSのEC2インスタンスで稼働するAmazon Linuxでは、設定方法が異なります

[root@centos ~]# yum install clamd

# ウイルス定義ファイル更新設定ファイル編集
[root@centos ~]# vi /etc/freshclam.conf

# Send the RELOAD command to clamd.
# Default: no
#NotifyClamd /path/to/clamd.conf
NotifyClamd /etc/clamd.conf
↑ 追加(ウイルス定義ファイル更新をclamdに通知する)

# ウイルス定義ファイル最新化
[root@centos ~]# freshclam

アップロードされたファイルを検証する

$command = 'clamscan '.$filePath;

$out = '';
$int = -1;
exec($command, $out, $int);

if ($int == 0) {
    // ウイルスが検知されなかった場合

} else {
    // ウイルスが検知された場合、または実行に失敗した場合
    throw new \RuntimeException(implode(PHP_EOL, $out));
}

ウイルスのテストファイル

ウイルススキャンが有効に機能しているかどうかを検証するためには、EICARテストファイルと呼ばれる、実際には被害をもたらさないダミーのウイルスファイルを利用します。

  1. EICARテストファイルのダウンロードページにアクセスします。
  2. 任意のパターンのテストファイルをダウンロードし、実際に作成したプログラムにアップロードします。
14
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
18