Sim*_*mon 43 php security upload file-upload image
我不知道这是否会发生,但我会尝试一下.
过去一小时,我研究了图像上传安全性.我了解到有很多功能可以测试上传.
在我的项目中,我需要安全上传图片.也可能有相当大的数量,并且可能需要大量带宽,因此购买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;
}
Run Code Online (Sandbox Code Playgroud)形式:
<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>
Run Code Online (Sandbox Code Playgroud)所以,我要问的是通过发布代码片段来帮助我(以及其他所有人)使这个图像上传脚本变得超级安全.或者通过共享/创建添加了所有片段的完整脚本.
ice*_*cub 82
当您开始处理安全图像上载脚本时,需要考虑很多事情.现在我没有接近这方面的专家,但我曾被要求在过去开发一次.我要经历整个过程,所以你可以跟进.为此,我将从一个非常基本的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>
Run Code Online (Sandbox Code Playgroud)
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.";
}
?>
Run Code Online (Sandbox Code Playgroud)
第一个问题:文件类型
攻击者不必使用您网站上的表单将文件上传到您的服务器.POST请求可以通过多种方式拦截.想想浏览器插件,代理,Perl脚本.无论我们如何努力,我们都无法阻止攻击者试图上传他本不应该的东西.因此,我们所有的安全性都必须在服务器端完成.
第一个问题是文件类型.在上面的脚本中,攻击者可以上传他想要的任何内容,例如php脚本,并按照直接链接执行它.因此,为了防止这种情况,我们实施内容类型验证:
<?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.";
}
?>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这还不够.正如我之前提到的,攻击者可以完全控制请求.没有什么能阻止他/她修改请求标题,只是将内容类型更改为"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.";
}
?>
Run Code Online (Sandbox Code Playgroud)
我们还没有到那里.大多数图像文件类型允许添加文本注释.同样,没有什么能阻止攻击者添加一些PHP代码作为评论.GD库将此评估为完全有效的图像.PHP解释器将完全忽略该图像并在注释中运行php代码.确实,它取决于php配置哪些文件扩展名由php解释器处理,哪些不是,但由于有许多开发人员由于使用VPS而无法控制此配置,我们无法假设php解释器不会处理图像.这就是为什么添加文件扩展名白名单也不够安全.
解决方案是将图像存储在攻击者无法直接访问文件的位置.这可能位于文档根目录之外,也可能位于受.htaccess文件保护的目录中:
order deny,allow
deny from all
allow from 127.0.0.1
Run Code Online (Sandbox Code Playgroud)
编辑:在与其他一些PHP程序员交谈之后,我强烈建议使用文档根目录之外的文件夹,因为htaccess并不总是可靠的.
我们仍然需要用户或任何其他访问者能够查看图像.所以我们将使用php为它们检索图像:
<?php
$uploaddir = 'uploads/';
$name = $_GET['name']; // Assuming the file name is in the URL for this example
readfile($uploaddir.$name);
?>
Run Code Online (Sandbox Code Playgroud)
第二个问题:本地文件包含攻击
虽然我们的脚本现在相当安全,但我们不能假设服务器没有其他漏洞.常见的安全漏洞称为本地文件包含.为了解释这一点,我需要添加一个示例代码:
<?php
if(isset($_COOKIE['lang'])) {
$lang = $_COOKIE['lang'];
} elseif (isset($_GET['lang'])) {
$lang = $_GET['lang'];
} else {
$lang = 'english';
}
include("language/$lang.php");
?>
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我们谈论的是一个多语言网站.站点语言不是被认为是"高风险"信息的东西.我们尝试通过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;
Run Code Online (Sandbox Code Playgroud)
<?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!");
}
}
?>
Run Code Online (Sandbox Code Playgroud)
所以现在我们已经完成了以下工作:
我们仍然需要能够向访问者显示图像.我们只需使用数据库的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']);
?>
Run Code Online (Sandbox Code Playgroud)
由于此脚本,访问者将能够查看图像或使用其原始文件名下载它.但是,(s)他无法直接访问您服务器上的文件,也无法欺骗您的服务器访问他/她的文件,因为他无法知道它是哪个文件.(S)他不能强行上传目录,因为它根本不允许任何人访问除服务器本身以外的目录.
这就是我的安全图像上传脚本.
我想补充一点,我没有在此脚本中包含最大文件大小,但您应该可以轻松地自己完成.
ImageUpload类
由于这个脚本的高要求,我已经书面方式的ImageUpload类,应该让所有的你安全地处理你的网站访问者上传的图片更容易了很多.该类可以同时处理单个和多个文件,并为您提供显示,下载和删除图像等附加功能.
由于代码只是大到这里发布,你可以在这里从MEGA下载这个类:
只需阅读README.txt并按照说明操作即可.
开源
我们的Github配置文件现在也可以使用Image Secure类项目.这样,其他人(你?)可以为项目做出贡献,并使这个人成为一个很棒的图书馆.(目前有问题.请使用上面的下载,直到修复).
使用PHP上传文件既简单又安全。我建议学习以下内容:
fileinfo
资源要使用PHP上传文件,您有两种方法:PUT
和POST
。要将POST
方法与HTML结合使用,您需要enctype
在表单上启用以下代码:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
Run Code Online (Sandbox Code Playgroud)
然后在您的PHP中,您需要使用以下命令获取上传的文件$_FILES
:
$_FILES['file']
Run Code Online (Sandbox Code Playgroud)
然后,您需要使用以下命令将文件从temp(“ upload”)中移出move_uploaded_file
:
if (move_uploaded_file($_FILES['file']['tmp_name'], YOUR_PATH)) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
上传文件后,您需要检查文件的扩展名。最好的方法是这样使用pathinfo
:
$extension = pathinfo($_FILES['file']['tmp_name'], PATHINFO_EXTENSION);
Run Code Online (Sandbox Code Playgroud)
但是扩展名并不安全,因为您可以上传具有扩展名.jpg
但具有mimetype 的文件,text/php
这是后门。因此,我建议finfo_open
像这样检查真实的模仿类型:
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $_FILES['file']['tmp_name']);
Run Code Online (Sandbox Code Playgroud)
并且不要使用它,$_FILES['file']['type']
因为有时,您可能会收到不同的浏览器和客户端操作系统,
application/octet-stream
并且这种模仿类型不是您上传的文件的真实模仿类型。
我认为您可以在这种情况下安全地上传文件。
对不起,我的英语,再见!
归档时间: |
|
查看次数: |
46366 次 |
最近记录: |