扫描 PHP 上传的病毒

发布于 2024-07-15 18:41:11 字数 487 浏览 8 评论 0原文

我目前正在使用以下代码来扫描作为申请表的一部分上传的文件:

$safe_path = escapeshellarg($dir . $file);
$command = '/usr/bin/clamscan --stdout ' . $safe_path;
$out = '';
$int = -1;
exec($command, $out, $int);

if ($int == 0) {
    // all good;
} else {
    // VIRUS!;
}

它可以工作,但速度很慢。 任何人有任何建议a)。 加快速度 b). 总体改进脚本(例如,我不完全清楚 exec() 与 system() 等的好处)?

如果速度无法提高,那么我理想情况下希望显示某种临时“请耐心等待您的文件正在被扫描”消息,但我也不知道如何处理。

编辑:抱歉,应该说扫描需要在当时完成,因为如果没有有效(即无病毒)文件,相关应用程序将不会被接受。

I'm currently using the following code to scan files that have been uploaded as part of an application form:

$safe_path = escapeshellarg($dir . $file);
$command = '/usr/bin/clamscan --stdout ' . $safe_path;
$out = '';
$int = -1;
exec($command, $out, $int);

if ($int == 0) {
    // all good;
} else {
    // VIRUS!;
}

It works, but is slow. Anyone got any suggestions that would a). speed things up and b). improve the script generally (for instance, I'm not entirely clear on the benefits of exec() vs system(), etc)?

If the speed can't be improved then I'd ideally like to display some kind of interim "Please be patient your files are being scanned" message, but am not sure how to go about that either.

EDIT: Sorry, should have said the scan needs to be done at the time as the application in question won't be accepted without valid (i.e virus-free) files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情深如许 2024-07-22 18:41:11

使用 clamdscan 代替 clamscan。 Clamdscan 使用始终运行的内置守护程序,无需每次都加载病毒表(如 clamscan 那样)。

Use clamdscan instead of clamscan. Clamdscan uses the built-in daemon that's running all the time and doesn't have to load the virus tables each time (as clamscan does).

念三年u 2024-07-22 18:41:11

如果您不需要立即向用户显示结果,您可以将文件添加到数据库表中以便稍后扫描。

然后,您可以派生一个新进程来扫描并更新表中的结果。 你在这里有一个很好的例子: http:// robert.accettura.com/blog/2006/09/14/asynchronous-processing-with-php/

如果你绝对需要在同一个请求中显示结果,那么你可以完全按照我之前所说的那样做,但输出一个通过 AJAX 请求结果的临时页面; 扫描结束后,将用户重定向到结果页面。

如果您不想使用 JavaScript,那么一个简单的元刷新标记就可以解决问题。

If you don't need to display the results to the user instantly, you could add the file to a Database table for scanning later.

Then, you could fork a new process to scan and update the results in the table. You have a good example here: http://robert.accettura.com/blog/2006/09/14/asynchronous-processing-with-php/.

If you absolutely need to display the results within the same request, then you could do it exactly as I said before but outputting a temp page requesting the results via AJAX; once the scan is over, redirect the user to the results page.

If you don't want to use JavaScript, then a simple meta refresh tag would do the trick.

梦初启 2024-07-22 18:41:11

设置一个单独的应用程序,最好是在不同的盒子上,您可以在其中批量扫描。 该框可以将其状态更新到数据库中,您的前端服务可以在数据库中读取并向用户报告。

Set up a seperate application, ideally on a different box where you can batch these scans. That box can update it's status into the database where your frontend service can read and report back to the user.

山田美奈子 2024-07-22 18:41:11

为了保持最新:现在有一个 PHP 库正在使用与 ClamAV 的套接字连接,并通过 PHP 快速初始化文件扫描。

https://github.com/jonjomckay/quahog

请务必使用正确的权限,因为扫描会由 clamav 用户而不是 www-data 完成。

To keep this up-to-date: There is a PHP library now that is using a socket connection to ClamAV and initialize a file scan over PHP pretty fast.

https://github.com/jonjomckay/quahog

Be sure to use the correct permissions, because the scan will be done by the clamav user and not www-data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文