简体   繁体   English

ZipArchive类PHP安全问题

[英]ZipArchive class PHP security issue

I have the following PHP code 我有以下PHP代码

// Check if the upload is setted
if
(
    isset($_FILES['file']['name']) && !empty($_FILES['file']['name']) && 
    isset($_FILES['file']['type']) && !empty($_FILES['file']['type']) &&
    isset($_FILES['file']['size']) && !empty($_FILES['file']['size'])
)
{
    $UploadIsSetted = true;
    $UploadIsBad = false;

    $UploadExtension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

    // Check if the upload is good
    require "../xdata/php/website_config/website.php";
    $RandomFoo = rand(1000999999,9999999999);

    if (($_FILES["file"]["size"] < ($MaxAvatarPictureSize*1000000)))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            $UploadIsBad = true;
            $hrefs->item(0)->setAttribute("Error","true");
            $hrefs->item(0)->setAttribute("SomethingWrong","true");
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],"../upload/tmp/".$RandomFoo.".file");    
        }
    }
    else
    {
        // The file is too big
        $UploadIsBad = true;
        $hrefs->item(0)->setAttribute("Error","true");
        $hrefs->item(0)->setAttribute("UploadTooBig","true");
    }
}
else
{
    $UploadIsSetted = false;
}

$ZipFile = new ZipArchive;
$ZipFile->open('../upload/tmp/'.$LastFilename.'.zip',ZIPARCHIVE::CREATE);
$ZipFile->addFile('../upload/tmp/'.$RandomFoo.'.file',$RandomFoo.".".$UploadExtension);
$ZipFile->close();

now my big concern is that user can upload anything so how can i prevent : 现在我最关心的是用户可以上传任何东西,所以我该如何防止:

  • uploading 2GB 3GB files 上传2GB 3GB文件
  • floading 加载
  • uploading some kind of twisted exploit that would eventually alter my server security 上传某种扭曲的漏洞,最终会改变我的服务器安全性
  • buffer overflow 缓冲区溢出
  • filenames that have arbitrary code injections 具有任意代码注入的文件名

i mean, how secure is this script? 我的意思是,此脚本的安全性如何?

i'm running windows for now, i will switch to linux 我现在正在运行Windows,我将切换到linux

Four your other questions: 您的其他四个问题:

floading 加载

That's the complex part. 那是复杂的部分。 Let me google you some ideas: 让我用谷歌搜索一些想法:

uploading some kind of twisted exploit that would eventually alter my server security 上传某种扭曲的漏洞,最终会改变我的服务器安全性

Use a commandline virus scanner ( f-prot or clamav ) to scan uploaded files. 使用命令行病毒扫描程序( f-protclamav )扫描上载的文件。 You might use a naive regex scanner in PHP itself (probe for HTMLish content in image files, eg), but that's not a factual security feature; 您可能在PHP本身中使用了一个幼稚的正则表达式扫描仪(例如,对图像文件中的HTMLish内容进行了探测),但这并不是事实安全功能; don't reinvent the wheel. 不要重新发明轮子。

buffer overflow 缓冲区溢出

PHP in general is not susceptible to buffer overflows. 通常,PHP不容易受到缓冲区溢出的影响。

Okay, joking. 好吧开玩笑 But you can't do anything in userland about it. 但是您不能在用户领域做任何事情。 But pushing strings around isn't much of a problem. 但是向周围推弦并不是什么大问题。 That's pretty reliable and unexploitable in scripting languages, as long as you know how to escape what in which context. 只要您知道如何在哪种情况下转义,这在脚本语言中是相当可靠且无法解释的。

filenames that have arbitrary code injections 具有任意代码注入的文件名

At the very leat you should most always use basename() to avoid path traversal exploits. 千篇一律的是,您应该始终使用basename()来避免路径遍历漏洞。 If you want to keep user-specified filenames, a regex whitelist is in order. 如果要保留用户指定的文件名,则按顺序排列正则表达式白名单。 =preg_replace('/[^\\w\\s.]/', '', $fn) as crude example. =preg_replace('/[^\\w\\s.]/', '', $fn)作为粗略示例。

Your line if (($_FILES["file"]["size"] < ($MaxAvatarPictureSize*1000000))) already limits the size of file acceptable to $MaxAvatarPictureSize megabytes. if (($_FILES["file"]["size"] < ($MaxAvatarPictureSize*1000000)))已经将可接受的文件大小限制为$MaxAvatarPictureSize兆字节。 Though $MaxAvatarPictureSize doesn't appear to be set in the code you provided. 虽然$MaxAvatarPictureSize似乎没有在您提供的代码中设置。 My guess that should be 1 or 2 max. 我的猜测应该是1或2个最大值。 Also not set is $LastFilename and probably some others too. 同样没有设置的是$LastFilename ,可能还有其他一些。

Also place an if($UploadIsBad === false) { /* do zipping */ } around the Zipping part to avoid zipping up files which are too large or otherwise invalid. 另外,在压缩部分周围放置一个if($UploadIsBad === false) { /* do zipping */ }以避免压缩太大或无效的文件。

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

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