完整的安全图片上传脚本

62

我不知道这是否会发生,但我会尝试。

在过去的一个小时里,我研究了图像上传安全性。我了解到有很多功能可以测试上传。

在我的项目中,我需要确保上传的图像是安全的。此外,可能会有大量图像,并且可能需要大量的带宽,因此购买API不是一个选择。

因此,我决定获得一个完整的PHP脚本,用于真正安全的图像上传。我还认为它将对许多人有所帮助,因为很难找到真正安全的上传脚本。但我不是php专家,因此添加一些功能对我来说真的是个头疼的问题,所以我会请求这个社区的帮助,创建一个真正安全的图像上传完整脚本。

这段文字的意思是:

这里有一些非常棒的关于图片上传安全检查列表的主题(然而,它们只是告诉我们需要做什么,而不是如何做到这一点,正如我所说,我不是PHP大师,所以我不能独自完成所有操作): PHP 图片上传安全检查列表 https://security.stackexchange.com/questions/32852/risks-of-a-php-image-upload-form

总的来说,它们告诉我们安全上传图片需要以下步骤(我将引用以上页面的内容):

使用.httaccess禁止在上传文件夹内运行PHP。 如果文件名包含字符串“php”,则不允许上传。 仅允许扩展名为jpg,jpeg,gif和png的文件上传。 仅允许上传图像文件类型。 禁止两个文件类型的图像上传。 更改图像名称。将其上传到子目录而不是根目录。 此外: 重新使用GD(或Imagick)处理图像并保存处理后的图像。其他都只是对黑客来说无聊的乐趣。 正如rr所指出的那样,对于任何上传,请使用move_uploaded_file()。 顺便说一下,您需要非常严格地限制上传文件夹。这些地方是许多漏洞的黑暗角落之一。这适用于任何类型的上传和任何编程语言/服务器。请参考https://www.owasp.org/index.php/Unrestricted_File_Upload。 级别1:检查扩展名(扩展名文件以...结尾)。 级别2:检查MIME类型($file_info = getimagesize($_FILES ['image_file']; $file_mime = $file_info ['mime'];)。 级别3:阅读前100个字节,并检查它们是否具有以下范围的任何字节:ASCII 0-8、12-31(十进制)。 级别4:检查标题中的魔术数字(文件的前10-20个字节)。您可以从此处找到一些文件头字节:http://en.wikipedia.org/wiki/Magic_number_%28programming%29#Examples。 您可能还希望在$_FILES ['my_files'] ['tmp_name']上运行“is_uploaded_file”。请参阅http://php.net/manual/en/function.is-uploaded-file.php。
这是其中的一部分,但还不是全部。(如果您知道更多有助于使上传更安全的信息,请分享。) 目前我们拥有的是:
  • Main PHP:

    function uploadFile ($file_field = null, $check_image = false, $random_name = false) {
    
    //Config Section    
    //Set file upload path
    $path = 'uploads/'; //with trailing slash
    //Set max file size in bytes
    $max_size = 1000000;
    //Set default file extension whitelist
    $whitelist_ext = array('jpeg','jpg','png','gif');
    //Set default file type whitelist
    $whitelist_type = array('image/jpeg', 'image/jpg', 'image/png','image/gif');
    
    //The Validation
    // Create an array to hold any output
    $out = array('error'=>null);
    
    if (!$file_field) {
      $out['error'][] = "Please specify a valid form field name";           
    }
    
    if (!$path) {
      $out['error'][] = "Please specify a valid upload path";               
    }
    
    if (count($out['error'])>0) {
      return $out;
    }
    
    //Make sure that there is a file
    if((!empty($_FILES[$file_field])) && ($_FILES[$file_field]['error'] == 0)) {
    
    // Get filename
    $file_info = pathinfo($_FILES[$file_field]['name']);
    $name = $file_info['filename'];
    $ext = $file_info['extension'];
    
    //Check file has the right extension           
    if (!in_array($ext, $whitelist_ext)) {
      $out['error'][] = "Invalid file Extension";
    }
    
    //Check that the file is of the right type
    if (!in_array($_FILES[$file_field]["type"], $whitelist_type)) {
      $out['error'][] = "Invalid file Type";
    }
    
    //Check that the file is not too big
    if ($_FILES[$file_field]["size"] > $max_size) {
      $out['error'][] = "File is too big";
    }
    
    //If $check image is set as true
    if ($check_image) {
      if (!getimagesize($_FILES[$file_field]['tmp_name'])) {
        $out['error'][] = "Uploaded file is not a valid image";
      }
    }
    
    //Create full filename including path
    if ($random_name) {
      // Generate random filename
      $tmp = str_replace(array('.',' '), array('',''), microtime());
    
      if (!$tmp || $tmp == '') {
        $out['error'][] = "File must have a name";
      }     
      $newname = $tmp.'.'.$ext;                                
    } else {
        $newname = $name.'.'.$ext;
    }
    
    //Check if file already exists on server
    if (file_exists($path.$newname)) {
      $out['error'][] = "A file with this name already exists";
    }
    
    if (count($out['error'])>0) {
      //The file has not correctly validated
      return $out;
    } 
    
    if (move_uploaded_file($_FILES[$file_field]['tmp_name'], $path.$newname)) {
      //Success
      $out['filepath'] = $path;
      $out['filename'] = $newname;
      return $out;
    } else {
      $out['error'][] = "Server Error!";
    }
    
     } else {
      $out['error'][] = "No file uploaded";
      return $out;
     }      
    }
    
    
    if (isset($_POST['submit'])) {
     $file = uploadFile('file', true, true);
     if (is_array($file['error'])) {
      $message = '';
      foreach ($file['error'] as $msg) {
      $message .= '<p>'.$msg.'</p>';    
     }
    } else {
     $message = "File uploaded successfully".$newname;
    }
     echo $message;
    }
    
  • And the form:

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <input name="file" type="file" id="imagee" />
    <input name="submit" type="submit" value="Upload" />
    </form>
    
所以,我想要的是通过发布代码片段来帮助我(以及其他人)使这个图像上传脚本变得超级安全。 或者分享/创建一个包含所有代码片段的完整脚本。

1
由于我在这个答案上不断获得赞,让我解释一下为什么您的问题会被踩:Stack Overflow是一个帮助您解决代码问题的地方。它不是寻求改进工作代码的地方(Code Review是该网站),也不是寻求或请求教程的地方。仅仅因为(如您所见),需要写半本书才能给出一个好的、适当的答案。浏览量之所以高,只是因为您在上面放置了悬赏。并不是因为每个人都需要这个 :) - icecub
5
我相信仍有人在寻找这个答案,他们会很高兴你已经回答了。 - Simon
是的,似乎许多人在其他问题中链接到它,因为许多人没有意识到他们的上传脚本是不安全的。虽然我没想到它会得到那么多赞,但这种情况今天已经很少见了。 - icecub
5
谢谢你的问题,西蒙。我投了你的票。我在网上找了很久才找到你在问题中已经整理好的信息。 - Altimus Prime
2
很遗憾,<?php echo $_SERVER['PHP_SELF']; ?> 不是完全安全的,容易受到跨站脚本攻击。 - Raymond Nijland
显示剩余2条评论
4个回答

118

当您开始编写安全的图像上传脚本时,有许多事情要考虑。现在我远非这方面的专家,但过去曾被要求开发过一次。我将逐步介绍我经历的整个过程,以便您可以跟随。为此,我将从一个非常基本的HTML表单和处理文件的PHP脚本开始。

HTML表单:

<form name="upload" action="upload.php" method="POST" enctype="multipart/form-data">
    Select image to upload: <input type="file" name="image">
    <input type="submit" name="upload" value="upload">
</form>

PHP文件:

<?php
$uploaddir = 'uploads/';

$uploadfile = $uploaddir . basename($_FILES['image']['name']);

if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
    echo "Image succesfully uploaded.";
} else {
    echo "Image uploading failed.";
}
?> 

第一个问题:文件类型
攻击者不必使用您网站上的表单来上传文件到您的服务器。 POST请求可以以多种方式被拦截。考虑浏览器插件、代理、Perl脚本等。无论我们如何努力,都无法阻止攻击者尝试上传他们不应该上传的东西。因此,我们所有的安全措施都必须在服务器端完成。

第一个问题是文件类型。在上面的脚本中,攻击者可以上传任何他们想要的文件,例如php脚本,并通过直接链接执行它。因此,为了防止这种情况发生,我们实现了Content-type验证

<?php
if($_FILES['image']['type'] != "image/png") {
    echo "Only PNG images are allowed!";
    exit;
}

$uploaddir = 'uploads/';

$uploadfile = $uploaddir . basename($_FILES['image']['name']);

if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
    echo "Image succesfully uploaded.";
} else {
    echo "Image uploading failed.";
}
?>

很不幸,这还不够。正如我之前提到的那样,攻击者完全控制请求。没有任何东西可以防止他/她修改请求头并将内容类型简单地更改为"image/png"。因此,除了仅依赖于Content-type标头外,还最好验证上传文件的内容。这就是php GD库发挥作用的地方。使用 getimagesize(),我们将使用GD库处理图像。如果它不是图像,则会失败,因此整个上传也将失败:

<?php
$verifyimg = getimagesize($_FILES['image']['tmp_name']);

if($verifyimg['mime'] != 'image/png') {
    echo "Only PNG images are allowed!";
    exit;
}

$uploaddir = 'uploads/';

$uploadfile = $uploaddir . basename($_FILES['image']['name']);

if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
    echo "Image succesfully uploaded.";
} else {
    echo "Image uploading failed.";
}
?>

虽然如此,我们还没有到那一步。大多数图像文件类型允许添加文本注释。同样,没有任何东西能阻止攻击者在注释中添加一些PHP代码。GD库将评估这个注释作为完全有效的图像。PHP解释器会完全忽略图像并运行注释中的php代码。尽管 php 解释器处理哪些文件扩展名取决于 PHP 配置,但由于有很多开发人员使用 VPS 而无法控制此配置,我们不能假设 PHP 解释器不会处理图像。这就是为什么添加文件扩展名白名单也不够安全的原因。

解决方案是将图像存储在攻击者无法直接访问文件的位置。这可以在文档根目录之外或受 .htaccess 文件保护的目录中完成:

order deny,allow
deny from all
allow from 127.0.0.1

编辑:与其他PHP程序员交谈后,我强烈建议使用文档根目录之外的文件夹,因为htaccess并不总是可靠的。

但我们仍然需要让用户或其他访问者能够查看图像。因此,我们将使用PHP为他们检索图像:

<?php
$uploaddir = 'uploads/';
$name = $_GET['name']; // Assuming the file name is in the URL for this example
readfile($uploaddir.$name);
?>

第二个问题:本地文件包含攻击
虽然我们的脚本现在相当安全,但我们不能假设服务器没有其他漏洞。一种常见的安全漏洞称为本地文件包含。为了解释这一点,我需要添加一个示例代码:

<?php
if(isset($_COOKIE['lang'])) {
   $lang = $_COOKIE['lang'];
} elseif (isset($_GET['lang'])) {
   $lang = $_GET['lang'];
} else {
   $lang = 'english';
}

include("language/$lang.php");
?>

这个例子讲述的是一个多语言网站。网站语言不被视为“高风险”信息。我们尝试通过cookie或GET请求获取访问者的首选语言,并根据此包含所需的文件。现在考虑一下当攻击者输入以下网址时会发生什么:

www.example.com/index.php?lang=../uploads/my_evil_image.jpg

PHP将包含攻击者上传的文件,绕过了他们无法直接访问该文件的限制,我们回到了原点。

解决这个问题的方法是确保用户不知道服务器上的文件名。相反,我们将使用数据库来更改文件名甚至扩展名以进行跟踪:

CREATE TABLE `uploads` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(64) NOT NULL,
    `original_name` VARCHAR(64) NOT NULL,
    `mime_type` VARCHAR(20) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
<?php

if(!empty($_POST['upload']) && !empty($_FILES['image']) && $_FILES['image']['error'] == 0)) {

    $uploaddir = 'uploads/';

    /* Generates random filename and extension */
    function tempnam_sfx($path, $suffix){
        do {
            $file = $path."/".mt_rand().$suffix;
            $fp = @fopen($file, 'x');
        }
        while(!$fp);

        fclose($fp);
        return $file;
    }

    /* Process image with GD library */
    $verifyimg = getimagesize($_FILES['image']['tmp_name']);

    /* Make sure the MIME type is an image */
    $pattern = "#^(image/)[^\s\n<]+$#i";

    if(!preg_match($pattern, $verifyimg['mime']){
        die("Only image files are allowed!");
    }

    /* Rename both the image and the extension */
    $uploadfile = tempnam_sfx($uploaddir, ".tmp");

    /* Upload the file to a secure directory with the new name and extension */
    if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {

        /* Setup a database connection with PDO */
        $dbhost = "localhost";
        $dbuser = "";
        $dbpass = "";
        $dbname = "";
        
        // Set DSN
        $dsn = 'mysql:host='.$dbhost.';dbname='.$dbname;

        // Set options
        $options = array(
            PDO::ATTR_PERSISTENT    => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        );

        try {
            $db = new PDO($dsn, $dbuser, $dbpass, $options);
        }
        catch(PDOException $e){
            die("Error!: " . $e->getMessage());
        }

        /* Setup query */
        $query = 'INSERT INTO uploads (name, original_name, mime_type) VALUES (:name, :oriname, :mime)';

        /* Prepare query */
        $db->prepare($query);

        /* Bind parameters */
        $db->bindParam(':name', basename($uploadfile));
        $db->bindParam(':oriname', basename($_FILES['image']['name']));
        $db->bindParam(':mime', $_FILES['image']['type']);

        /* Execute query */
        try {
            $db->execute();
        }
        catch(PDOException $e){
            // Remove the uploaded file
            unlink($uploadfile);

            die("Error!: " . $e->getMessage());
        }
    } else {
        die("Image upload failed!");
    }
}
?>

现在我们已经完成了以下操作:

  • 创建了一个安全的存储图片的位置
  • 使用GD库处理了图像
  • 检查了图像的MIME类型
  • 重命名了文件名并更改了扩展名
  • 将新文件名和原始文件名都保存在我们的数据库中
  • 还将MIME类型保存在我们的数据库中

我们仍然需要能够向访客显示图像。我们只需使用数据库的id列即可实现:

<?php

$uploaddir = 'uploads/';
$id = 1;

/* Setup a database connection with PDO */
$dbhost = "localhost";
$dbuser = "";
$dbpass = "";
$dbname = "";

// Set DSN
$dsn = 'mysql:host='.$dbhost.';dbname='.$dbname;

// Set options
$options = array(
    PDO::ATTR_PERSISTENT    => true,
    PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
);

try {
    $db = new PDO($dsn, $dbuser, $dbpass, $options);
}
catch(PDOException $e){
    die("Error!: " . $e->getMessage());
}

/* Setup query */
$query = 'SELECT name, original_name, mime_type FROM uploads WHERE id=:id';

/* Prepare query */
$db->prepare($query);

/* Bind parameters */
$db->bindParam(':id', $id);

/* Execute query */
try {
    $db->execute();
    $result = $db->fetch(PDO::FETCH_ASSOC);
}
catch(PDOException $e){
    die("Error!: " . $e->getMessage());
}

/* Get the original filename */
$newfile = $result['original_name'];

/* Send headers and file to visitor */
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($newfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($uploaddir.$result['name']));
header("Content-Type: " . $result['mime_type']);
readfile($uploaddir.$result['name']);
?>

感谢这个脚本,访客可以查看图像或使用其原始文件名进行下载。但是,他们无法直接访问您服务器上的文件,也无法欺骗您的服务器为他/她访问该文件,因为他们无法知道哪个文件是哪个。他们也无法通过暴力破解上传目录,因为除了服务器本身,它根本不允许任何人访问该目录。

这就是我的安全图片上传脚本的全部内容。

我想补充一下,我没有在此脚本中包括最大文件大小,但是您应该很容易自己实现这一点。

ImageUpload 类
由于这个脚本的高需求,我编写了一个 ImageUpload 类,这应该会让所有人更轻松地安全处理网站访客上传的图像。该类可以同时处理单个和多个文件,并为您提供附加功能,如显示、下载和删除图像。

由于代码太多而无法在此处发布,您可以从 MEGA 下载该类:

下载 ImageUpload 类

只需阅读 README.txt 并按照说明操作即可。

转为开源
Image Secure 类项目现在也可以在我的 Github 个人资料中获取。这样,其他人(您?)就可以为该项目做出贡献,并将其变成一个伟大的库供所有人使用。


8
getimagesize() 函数明确指出,不应将此函数用于验证图像是否为图像。请使用专门的解决方案,如 Fileinfo 扩展来检查给定文件是否为有效图像。请参考:http://php.net/manual/en/function.getimagesize.php。 - Hugo Zonderland
这个能支持多文件上传吗?比如说我在一个表单提交中上传了3张图片? - Lachie
1
@Foobarer 你是直接下载的还是从Github上拉取的?因为Github上的版本目前有bug。我请了别人帮忙,但他搞砸了,所以我正在处理这个问题。 - icecub
1
@Foobarer 是的,只需在图像元素的src属性值中使用“image.php”文件。像这样:<img src='image.php?id=1'>。那应该可以工作。 - icecub
1
@pileup 请将有关您特定情况的问题发送至自述文件中FAQ部分中找到的电子邮件地址。这样,这里的评论列表就会保持清晰。至于您的问题:请确保您下载的是来自Mega而不是Github的版本。我曾经有另一个人帮我处理它,但他搞砸了那个版本。我还没有找到时间修复Github上的版本,所以它无法正常工作。 - icecub
显示剩余9条评论

8
在PHP中上传文件既简单又安全。我建议学习以下内容:

在PHP中上传文件有两种方法:PUTPOST。 要在HTML中使用POST方法,需要在表单中启用enctype,如下所示:

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

然后在 PHP 中,您需要使用 $_FILES 获取已上传的文件,例如:

$_FILES['file']

然后你需要使用 move_uploaded_file 将文件从临时目录("upload")移动到目标目录:

if (move_uploaded_file($_FILES['file']['tmp_name'], YOUR_PATH)) {
   // ...
}

在你上传完文件后,需要检查文件的扩展名。最好的方法是使用 pathinfo 函数,如下所示:

$extension = pathinfo($_FILES['file']['tmp_name'], PATHINFO_EXTENSION);

但是这个扩展并不安全,因为你可以上传一个拓展名为.jpg但MIME类型为text/php的文件,这是一个后门。 所以,我建议使用finfo_open来检查真正的MIME类型,像这样:

$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $_FILES['file']['tmp_name']);

不要使用$_FILES['file']['type'],因为有时候,根据你的浏览器和客户端操作系统,你可能会收到application/octet-stream,而这个MIME类型并不是你上传文件的真实MIME类型。

我认为你可以使用这种方案安全地上传文件。

对不起,我的英语不好,再见!


你好,感谢你的回答。然而,我认为它并没有完全回答我的问题。我有一个包含你提到的所有函数的脚本。然而,正如我们在这里所看到的http://security.stackexchange.com/questions/32852/risks-of-a-php-image-upload-form,它并不能使脚本完全安全。我希望有人能够给出(不需要编写代码,也许有人已经有了)前面回答中提到的所有函数,这将使图像上传脚本尽可能安全。无论如何,再次感谢你回答这个问题的努力。 - Simon
@Simon 在图像部分,存储和显示时有很多需要考虑的事情。我希望在这个平台上会有很多人帮助你解决存储问题。但是关于显示部分,我建议你阅读一下这个链接:http://php.net/manual/en/book.imagick.php - Lokesh Pandey

3

这里有一个提示。不要依赖于[type]元素,它太不可靠了。相反,检查文件头本身以查看文件类型实际上是什么。像这样:

 <?php


 // open the file and check header

 $tempfile = $FILES['tmp_name'];
                  
 if (!($handle = fopen($tempfile, 'rb')))
 {
   echo 'open file failed';
   fclose($handle);
   exit;

 }else{
  
        $hdr = fread($handle, 12); //should grab first 12 of header
        fclose($handle);                                     
    
 
        //now check the header results
        $subheaderpre = substr($hdr, 0, 12);
        $subheader = trim($subheaderpre);
     
        //get hex value to check png 
        $getbytes = substr($subheader, 0, 8);
        $hxval = bin2hex($getbytes);
     
     
         if ((substr($subheader, 0, 4) == "\xff\xd8\xff\xe0") && (substr($subheader, 6, 5) == "JFIF\x00"))
         {

           //passed jpg test
 
         }elseif($hxval == "89504e470d0a1a" || substr($subheader, 0, 8) == "\x89PNG\x0d\x0a\x1a\x0a")
           {

              //passed png test 
                
           }else{
              
                 //fail both 

                 echo 'Sorry but image failed to validate, try another image';
                 exit;                            
                      
                 }//close else elseif else
              
    }//close else ! $handle 

0

以下代码对我很有效,谢谢

      function gen_uid($l=5){
           return substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 10, $l);
      }
      $mime_type = mime_content_type($_FILES['imglink']['tmp_name']);
      $allowed_file_types = ['image/png', 'image/jpeg'];
      if (! in_array($mime_type, $allowed_file_types)) {
          // File type is NOT allowed.
        echo "Only PNG and JPG images are allowed!";
        exit;
      }
        $uploaddir = '../uploads/images/';
    
        $name = pathinfo($_FILES['imglink']['name'], PATHINFO_FILENAME);
        $ext  = pathinfo($_FILES['imglink']['name'], PATHINFO_EXTENSION);
    
        $newname = gen_uid(rand(0,30));
        $uploadfile = $uploaddir.$newname.".webp";
        if (move_uploaded_file($_FILES['imglink']['tmp_name'], $uploadfile)) {
            $imglink = basename($uploadfile);
        } else {
            echo "Image upload failed!";
            die();
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接