完整的安全图像上传脚本

新手上路,请多包涵

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

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

在我的项目中,我需要确保上传图片的安全。它也可能非常大,并且可能需要大量带宽,因此购买 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

这是其中的很大一部分,但还不是全部。 (如果您知道更多可以帮助上传更安全的信息,请分享。)

这就是我们现在得到的

  • 主要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;
  }

  • 和形式:
   <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>

所以,我要求的是通过发布代码片段来帮助我(和其他所有人)使这个图像上传脚本变得超级安全。或者通过共享/创建一个添加了所有片段的完整脚本。

原文由 Simon 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

当您开始编写安全图像上传脚本时,需要考虑很多事情。现在我离这方面的专家还差得很远,但过去我曾被要求开发一次。我将介绍我在这里经历的整个过程,以便您可以跟进。为此,我将从一个非常基本的 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 请求获取访问者的首选语言,并根据它包含所需的文件。现在考虑当攻击者输入以下 url 时会发生什么:

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 类,它应该可以让你们所有人更容易安全地处理网站访问者上传的图像。该类可以同时处理单个文件和多个文件,并为您提供显示、下载和删除图像等附加功能。

由于代码过于庞大,无法在此处发布,您可以从 MEGA 下载该课程:

下载 ImageUpload 类

只需阅读 README.txt 并按照说明进行操作。

走向开源

Image Secure 课程项目现在也可以在我的 Github 个人资料中找到。这样其他人(你?)可以为该项目做出贡献,并使其成为每个人的优秀图书馆。

原文由 icecub 发布,翻译遵循 CC BY-SA 4.0 许可协议

用 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 从 temp(“upload”) 移动文件:

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

上传文件后,您需要检查文件的扩展名。最好的方法是使用 pathinfo 像这样:

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

但是扩展名并不安全,因为您可以上传扩展名为 .jpg 但 mimetype text/php 的文件,这是一个后门。因此,我建议使用 finfo_open 检查真正的 mimetype,如下所示:

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

并且不要使用 $_FILES['file']['type'] 因为有时,根据您的浏览器和客户端操作系统,您可能会收到 application/octet-stream 并且此 mimetype 不是您上传文件的真正 mimetype。

我认为您可以在这种情况下安全地上传文件。

对不起我的英语,再见!

原文由 Jorge Olaf 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题