我正在尝试在 apache 2.4、php 5.6 上使用 clamav 扫描文件(通常为 100MB+ zip),并使用 clamav-daemon 的套接字。我没有使用 PHP-FPM。 (p.s. 套接字工作正常,我可以发送 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
/编辑 尝试使用 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)"
}
最佳答案
这看起来 Moodle 正在使用 systemd PrivateTmp 的服务下运行。
许多发行版现在为 apache 和 php-fpm 等服务配置 systemd 单元,为服务提供自己的私有(private) tmp 目录(至少部分是出于安全原因)。
如果您在 systemd 单元文件中查找 apache 或 php-fpm(在 /lib/systemd/system
下查找名为 apache2.service
的文件, httpd.service
或 php-fpm.service
),您可能会在 [Service] 中看到
部分。PrivateTmp=true
行
覆盖此问题的最佳方法(如果您决定需要这样做)是在服务的相关 /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添加到网络服务器组,以便在它实际看到临时文件后授予其访问临时文件的权限,例如RHEL 上的 usermod -a -G apache clamscan
。
关于使用clamav扫描php文件上传,权限在/tmp/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47625768/