PHP安全上传系统
所以我有一个文件上传系统,它应该只接受$AllowedExt中的文件类型,但是有人一直用一个名为angle.jpg.php的文件破坏这个系统。我认为他已经改变了mime类型,但我仍然想知道他是如何违反扩展检查的。 我们将不胜感激。提前谢谢PHP安全上传系统,php,file,upload,mime-types,Php,File,Upload,Mime Types,所以我有一个文件上传系统,它应该只接受$AllowedExt中的文件类型,但是有人一直用一个名为angle.jpg.php的文件破坏这个系统。我认为他已经改变了mime类型,但我仍然想知道他是如何违反扩展检查的。 我们将不胜感激。提前谢谢 <?php $allowedExts = array("gif", "jpeg", "jpg", "png", "bmp", "doc", "pages", "docx", "pdf","ppt","pptx","xls","xlsx");
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png", "bmp", "doc", "pages", "docx", "pdf","ppt","pptx","xls","xlsx");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$extension2 = pathinfo($target_file,PATHINFO_EXTENSION);
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/x-iwork-pages-sffpages")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/vnd.ms-powerpoint")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.presentationml.presentation")
|| ($_FILES["file"]["type"] == "application/excel")
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel")
|| ($_FILES["file"]["type"] == "application/x-excel")
|| ($_FILES["file"]["type"] == "application/x-msexcel")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
&& in_array($extension, $allowedExts) && in_array($extension2, $allowedExts) && getimagesize($_FILES["file"]["name"])!=='FALSE')
{
// Upload file
}
else {
// Do not upload file
}
因为没有人添加更多有用的提示。
我决定自己去找他们。
我在各种网站上找到了一些很好的提示
这里有两个:
您的逻辑有缺陷,因为扩展的条件仅链接到最后一个mime类型条件,因为&
的优先级高于|
。您需要在所有这些| |
链接的条件周围放置另一组圆括号。请尝试通过放置$_FILES[“file”][“type”]来最小化逻辑条件将所有这些非图像mime类型与getimagesize
组合在一起也没有多大意义–我怀疑如果让getimagesize
检查excel文档,它将返回除false
之外的任何内容。那么我需要查看文件内容吗?删除所有这些内容,并用数组和在数组中($realMimeType,$arrayofallowedmetypes,true)