繁体   English   中英

使用 PHP 在文件上传时动态运行 ClamAV 的 clamscan

[英]Dynamically running ClamAV's clamscan on file uploads with PHP

堆,

我想使用 clam 防病毒软件的 clamscan 工具扫描通过我的 php 上传脚本上传的每个文件。 我想我已经写了一个很好的剧本,但我想超越你们。

所以假设我发送给这个 php 上传脚本的文件名为“uploadedfile”,下面的代码有意义吗?

<?php

$safe_path = escapeshellarg('/tmp/' . $_FILES['uploadedfile']['tmp_name']);
$command = 'clamscan ' . $safe_path;
$out = '';
$int = -1;
exec($command, $out, $int);

if ($int == 0) {
   // all good, code goes here uploads file as normal IE move to
permanent directory etc;
} else {
   unlink('/tmp/' . $_FILES['uploadedfile']['tmp_name']);
header(Location: http://www.domain.com/uploadform.php?error=your-file-was-infected-pal);
}

?>

此外,clamscan 会找到 php shell 以及传统的旧恶意软件吗?

谢谢!

更新 - 找到答案

我回答了我自己的问题,但没有正式这样做的声誉。 这是答案:

对于后来者。 我已经使用 EICAR 测试病毒文件http://eicar.org/86-0-Intended-use.html测试了这个脚本,经过一些调整后它可以工作了。 返回变量 $int 告诉您文件是否安全。 如果 $int 为 0,则未发现病毒,如果 $int 为 1,则发现病毒。 但是,我必须进行一些更改才能使脚本正常工作(我将 $safe_path 变量更新为正确的),这是工作脚本:

<?php

$safe_path = escapeshellarg($_FILES['uploadedfile']['tmp_name']);
$command = 'clamscan ' . $safe_path;
$out = '';
$int = -1;
exec($command, $out, $int);

if ($int == 0) {
   // all good, code goes here uploads file as normal IE move to
permanent directory etc;
} else {
  //whatever you need to do if a virus is found.
}

?>

请注意,如果您的服务器运行 clamav 守护程序 (clamd),则可以使用 clamdscan 而不是建议的 clamscan,这种用法会更快,因为使用已由 clamd 加载的病毒签名。

小心点。 如果您的 clamscan 已过时,您将在输出中获得反馈:

它看起来像这样:

LibClamAV Warning: ***********************************************************
LibClamAV Warning: ***  This version of the ClamAV engine is outdated.     ***
LibClamAV Warning: *** DON'T PANIC! Read http://www.clamav.net/support/faq ***
LibClamAV Warning: ***********************************************************

同样取决于 clamscan 的版本,“结果”可能如下所示(您需要相应地解析它):

[filename]: OK

----------- SCAN SUMMARY -----------
Known viruses: x
Engine version: x.x.x
Scanned directories: 0
Scanned files: 1
Infected files: 0
Data scanned: x.xx MB
Data read: x.xx MB (ratio 0.00:1)
Time: x.xx sec (0 m x s)

尝试使用 clamdscan 运行它时,我在权限方面遇到了很多麻烦。 我在这里找到了权限问题的解决方案: https : //wiki.archlinux.org/index.php/ClamAV

这改变了这一行:

$command = 'clamscan ' . $safe_path;

到:

$command = 'clamdscan  --fdpass ' . $safe_path;

似乎成功通过了一个好的文件并标记了一个 EICAR 文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM