1

我正在尝试在 apache 2.4、php 5.6 上使用 clamav 扫描文件(通常为 100MB+ zip),并使用 clamav-daemon 的套接字。我没有使用 PHP-FPM。(ps套接字工作,我可以发送一个PING并得到一个PONG)。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (isset($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name'])) {

    $socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
    if(socket_connect($socket, '/var/run/clamav/clamd.ctl')) {
        $result = "";
        $file = $_FILES['file']['tmp_name'];
        socket_send($socket, "SCAN $file", strlen($file) + 5, 0);
        socket_recv($socket, $result, 20000, 0);
        var_dump($result);
    }
    socket_close($socket);
}
?>

<form method="post" enctype="multipart/form-data"><input type="file" name="file"><input type="submit"></form>

上传文件会产生:

string(65) "/tmp/phpxYBjyS: lstat() failed: No such file or directory. ERROR "

在我看来,这似乎是一个权限错误(即使 /tmp 是drwxrwxrwt 9 root root 4096 Dec 4 13:10 tmp)。但我可能是错的。我不能轻易查看文件,因为在 php 进程完成后,文件被删除。

clamdscan,在命令行,当然工作(例如/tmp/virus.txt: Eicar-Test-Signature FOUND)。

/etc/clamav/clamd.conf的仍然是默认值。它看起来像这样:

LocalSocket /var/run/clamav/clamd.ctl
FixStaleSocket true
LocalSocketGroup clamav
LocalSocketMode 666
# TemporaryDirectory is not set to its default /tmp here to make overriding
# the default with environment variables TMPDIR/TMP/TEMP possible
User clamav
AllowSupplementaryGroups true
ScanMail true
ScanArchive true
ArchiveBlockEncrypted false
MaxDirectoryRecursion 15
FollowDirectorySymlinks false
FollowFileSymlinks false
ReadTimeout 180
MaxThreads 12
MaxConnectionQueueLength 15
LogSyslog false
LogRotate true
LogFacility LOG_LOCAL6
LogClean false
LogVerbose false
DatabaseDirectory /var/lib/clamav
OfficialDatabaseOnly false
SelfCheck 3600
Foreground false
Debug false
ScanPE true
MaxEmbeddedPE 10M
ScanOLE2 true
ScanPDF true
ScanHTML true
MaxHTMLNormalize 10M
MaxHTMLNoTags 2M
MaxScriptNormalize 5M
MaxZipTypeRcg 1M
ScanSWF true
DetectBrokenExecutables false
ExitOnOOM false
LeaveTemporaryFiles false
AlgorithmicDetection true
ScanELF true
IdleTimeout 30
CrossFilesystems true
PhishingSignatures true
PhishingScanURLs true
PhishingAlwaysBlockSSLMismatch false
PhishingAlwaysBlockCloak false
PartitionIntersection false
DetectPUA false
ScanPartialMessages false
HeuristicScanPrecedence false
StructuredDataDetection false
CommandReadTimeout 5
SendBufTimeout 200
MaxQueue 100
ExtendedDetectionInfo true
OLE2BlockMacros false
ScanOnAccess false
AllowAllMatchScan true
ForceToDisk false
DisableCertCheck false
DisableCache false
MaxScanSize 100M
MaxFileSize 25M
MaxRecursion 16
MaxFiles 10000
MaxPartitions 50
MaxIconsPE 100
PCREMatchLimit 10000
PCRERecMatchLimit 5000
PCREMaxFileSize 25M
ScanXMLDOCS true
ScanHWP3 true
MaxRecHWP3 16
StatsEnabled false
StatsPEDisabled true
StatsHostID auto
StatsTimeout 10
StreamMaxLength 25M
LogFile /var/log/clamav/clamav.log
LogTime true
LogFileUnlock false
LogFileMaxSize 0
Bytecode true
BytecodeSecurity TrustSigned
BytecodeTimeout 60000

/Edit 尝试使用exec而不是套接字。

if (isset($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
  $path = escapeshellarg($_FILES['file']['tmp_name']);
  $code = -1;
  $result = '';
  exec('clamdscan ' . $path, $result, $code);
  if ($code !== 0) {
    var_dump($result);
  }
}

这也会产生类似的错误

array(6) {
[0]=>
string(64) "/tmp/php2hQTE8: lstat() failed: No such file or directory. ERROR"
[1]=>
string(0) ""
[2]=>
string(36) "----------- SCAN SUMMARY -----------"
[3]=>
string(17) "Infected files: 0"
[4]=>
string(15) "Total errors: 1"
[5]=>
string(25) "Time: 0.000 sec (0 m 0 s)"
}
4

2 回答 2

0

这看起来像 Moodle 在使用 systemd PrivateTmp 的服务下运行。

许多发行版现在为 apache 和 php-fpm 等服务配置 systemd 单元,为服务提供自己的私有 tmp 目录(至少部分出于安全原因)。

如果您查看 apache 或 php-fpm 的 systemd 单元文件(在下面查找/lib/systemd/system名为apache2.servicehttpd.service或的文件php-fpm.service),您可能会看到该PrivateTmp=true部分中的行[Service]

如果这是您决定需要做的,最好的覆盖它的方法是/etc/systemd/systemd/在服务的相关子目录中创建一个覆盖文件。您可以使用systemctl edit相关服务执行此操作,例如,systemctl edit php-fpm.service如果在 RHEL 上的 php-fpm 下运行。apache2 的服务名称会有所不同,具体取决于您所在的发行版。

覆盖文件应仅包含:

# A comment to explain why you're doing this
[Service]
PrivateTmp=false

您可能需要将 clamd 正在运行的用户添加到 webserver 组,以便在它可以实际看到临时文件时授予它访问临时文件的权限,例如usermod -a -G apache clamscan在 RHEL 上。

于 2021-01-25T04:06:04.813 回答
0

尝试--fdpass作为选项传递给 clamdscan。

我有一个类似的问题,可以用 clamscan 找到文件,但不能用 clamdscan 找到文件,它对我有用。

--fdpass 将文件描述符权限传递给 clamd。如果 clamd 以不同的用户身份运行,这很有用,因为它比将文件流式传输到 clamd 更快。仅当通过本地(unix)套接字连接到 clamd 时才可用。

来源:https ://linux.die.net/man/1/clamdscan

于 2022-02-16T14:57:04.070 回答