文章目录
- Upload-labs 1~15 通关详细教程
- Pass-01-前端js验证
- Pass-02-后端MIME验证
- Pass-03-黑名单验证
- Pass-04-黑名单验证.htaccess
- Pass-05-文件后缀名大小写绕过
- Pass-06-文件后缀名空格绕过
- Pass-07-文件后缀名点绕过
- Pass-08-文件后缀名::$DATA绕过
- Pass-09-点+空格+点+空格绕过
- Pass-10-文件后缀名双写绕过
- Pass-11-GET型00截断
- Pass-12-POST型00截断
- Pass-13-图片木马绕过
- Pass-14-getimagesize()检查绕过
- Pass-15-exif_imagetype()检测绕过
Upload-labs 1~15 通关详细教程
靶场地址Upload-labs。
还有俺写的另一篇博客作为参考任意文件上传。
Pass-01-前端js验证
上传一个php的一句话木马文件
文件内容如下:
这里显示上传的文件类型只能是jpg,png,gif类型的
查看源码
我们可以修改文件后缀名为png。
使用BurpSuite抓取数据包,然后再将文件的后缀名修改为php。
使用中国蚁剑进行连接。
http://192.168.188.183/upload/wuhu.php
Pass-02-后端MIME验证
后端 MIME 验证是一种常用的安全机制,用于验证接收到的文件的MIME 类型(Multipurpose Internet Mail Extensions)。MIME 类型是通过 HTTP 协议将文件的特定类型信息传输给客户端的一种标准方法。后端 MIME 验证是在服务器端对接收到的文件进行验证,确保其声明的 MIME 类型与实际文件类型相匹配。
后端 MIME 验证的目的是防止恶意用户绕过前端限制,通过伪造文件的扩展名或修改文件内容来上传和执行恶意文件。通过验证文件的真实 MIME 类型,可以减少安全风险,有效防止不合法的文件被上传和处理。
在后端 MIME 验证中,服务器会解析上传的文件,并根据文件的内容或文件头部信息判断其实际的 MIME 类型。然后,服务器会将解析得到的 MIME 类型与客户端提交的 MIME 类型进行比较,如果两者不一致,则表示可能存在风险,服务器可以拒绝接受或进一步处理该文件。
后端 MIME 验证通常作为文件上传功能的一部分,结合其他安全措施如文件大小限制、文件类型白名单等,以增强系统的安全性。
回到靶场,上传一个php的一句话木马文件。
页面报错文件类型不正确,需要重新上传。
查看源码
我们在上传文件的时候使用BurpSuite抓取数据包,来修改里面的Content-Type值从而绕过MIME 验证。
上传成功
然后就可以使用蚁剑进行连接了
Pass-03-黑名单验证
查看源码,发现这关设置了文件后缀名的黑名单。
但是这里的黑名单规则不严谨,在某些特定环境中某些特殊后缀仍会被当作php文件解析 php、php2、php3、php4、php5、php6、php7、pht、phtm、phtml。
这里用.php6文件后缀名,直接上传一个名为wuhu.php6的文件
发现直接上传成功。
Pass-04-黑名单验证.htaccess
查看源码,黑名单中的过滤的内容更多了。
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {if (file_exists(UPLOAD_PATH)) {$deny_ext = array(".php",".php5",".php4",".php3",".php2","php1",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2","pHp1",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf");$file_name = trim($_FILES['upload_file']['name']);$file_name = deldot($file_name);//删除文件名末尾的点$file_ext = strrchr($file_name, '.');$file_ext = strtolower($file_ext); //转换为小写$file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA$file_ext = trim($file_ext); //首尾去空if (!in_array($file_ext, $deny_ext)) {$temp_file = $_FILES['upload_file']['tmp_name'];$img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;if (move_uploaded_file($temp_file, $img_path)) {$is_upload = true;} else {$msg = '上传出错!';}} else {$msg = '此文件不允许上传!';}} else {$msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';}
}
这里使用 .htaccess 后缀名的文件。
.htaccess 是一个用于 Apache Web 服务器的配置文件,它是一种特殊的文本文件,使用 “.htaccess” 作为文件名的后缀。该文件通常用于在特定目录下改变 Apache 服务器的行为以及对网站进行配置。
.htaccess 文件的作用是允许网站管理员在特定目录中覆盖默认的服务器设置,以灵活地控制网站的行为和功能。它可以包含一系列指令,用于自定义访问权限、重定向URL、启用或禁用模块、修改请求处理规则等。
常见的用途和功能:
- URL 重写:通过 RewriteRule 指令可以修改 URL 地址的结构,使得对外暴露的 URL 更加友好和易读,同时也方便实现重定向和 URL 路由功能。
- 访问控制:通过使用指令如 Require 和 Allow/Deny,可以限制特定用户或 IP 地址的访问权限,保护敏感目录或文件。
- 自定义错误页面:通过 ErrorDocument 指令,可以针对不同的 HTTP 错误代码自定义相应的错误页面。
- 缓存控制:通过使用指令如 ExpiresByType,可以设置不同类型文件的缓存过期时间,以提升网站性能和加快页面加载速度。
- MIME 类型配置:通过 AddType 或 AddHandler 指令,可以自定义文件扩展名与 MIME 类型之间的映射关系。
- 重写规则:使用 RewriteRule 和 RewriteCond 指令,可以实现灵活的 URL 重写和重定向规则,对请求进行修改或转发。
上传文件的内容
<FilesMatch "jpg"> SetHandler application/x-httpd-php
</FilesMatch>
说明:如果文件里面有一个后缀为.jpg的文件,就会被解析成.php。
先上传**.htaccess**文件,然后上传phpinfo.jpg文件。
Pass-05-文件后缀名大小写绕过
查看源码,发现本关的黑名单中过滤了.htaccess文件,所以这关无法使用.htaccess来进行绕过。
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {if (file_exists(UPLOAD_PATH)) {$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");$file_name = trim($_FILES['upload_file']['name']);$file_name = deldot($file_name);//删除文件名末尾的点$file_ext = strrchr($file_name, '.');$file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA$file_ext = trim($file_ext); //首尾去空if (!in_array($file_ext, $deny_ext)) {$temp_file = $_FILES['upload_file']['tmp_name'];$img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;if (move_uploaded_file($temp_file, $img_path)) {$is_upload = true;} else {$msg = '上传出错!';}} else {$msg = '此文件类型不允许上传!';}} else {$msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';}
}
观察发现源码中缺少了strtolower函数,说明这里可以尝试大小写绕过。
$file_ext = strtolower($file_ext); //转换为小写
上传**.Php**后缀名的文件
Pass-06-文件后缀名空格绕过
查看源码
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {if (file_exists(UPLOAD_PATH)) {$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");$file_name = $_FILES['upload_file']['name'];$file_name = deldot($file_name);//删除文件名末尾的点$file_ext = strrchr($file_name, '.');$file_ext = strtolower($file_ext); //转换为小写$file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATAif (!in_array($file_ext, $deny_ext)) {$temp_file = $_FILES['upload_file']['tmp_name'];$img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;if (move_uploaded_file($temp_file,$img_path)) {$is_upload = true;} else {$msg = '上传出错!';}} else {$msg = '此文件不允许上传';}} else {$msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';}
}
观察发现源码中缺少了trim函数(去除字符串首尾处的空白字符),说明这里可以尝试空格绕过。
$file_ext = trim($file_ext); //首尾去空
上传php文件,然后抓取数据包,在文件后缀名后面加一个空格。
上传成功
Pass-07-文件后缀名点绕过
查看源码
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {if (file_exists(UPLOAD_PATH)) {$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");$file_name = trim($_FILES['upload_file']['name']);$file_ext = strrchr($file_name, '.');$file_ext = strtolower($file_ext); //转换为小写$file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA$file_ext = trim($file_ext); //首尾去空if (!in_array($file_ext, $deny_ext)) {$temp_file = $_FILES['upload_file']['tmp_name'];$img_path = UPLOAD_PATH.'/'.$file_name;if (move_uploaded_file($temp_file, $img_path)) {$is_upload = true;} else {$msg = '上传出错!';}} else {$msg = '此文件类型不允许上传!';}} else {$msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';}
}
观察源码发现缺少deldot,删除文件名末尾的点
$file_name = deldot($file_name); //删除文件名末尾的点
上传php文件,然后抓取数据包,在文件后缀名后面加一个点。
利用windows特性,会自动去掉后缀名中最后的”.”
,可在后缀名中加”.”
绕过。
上传成功
访问上传的php文件
Pass-08-文件后缀名::$DATA绕过
查看源码:
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {if (file_exists(UPLOAD_PATH)) {$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");$file_name = trim($_FILES['upload_file']['name']);$file_name = deldot($file_name);//删除文件名末尾的点$file_ext = strrchr($file_name, '.');$file_ext = strtolower($file_ext); //转换为小写$file_ext = trim($file_ext); //首尾去空if (!in_array($file_ext, $deny_ext)) {$temp_file = $_FILES['upload_file']['tmp_name'];$img_path = UPLOAD_PATH.'/'.date("YmdHis").rand(1000,9999).$file_ext;if (move_uploaded_file($temp_file, $img_path)) {$is_upload = true;} else {$msg = '上传出错!';}} else {$msg = '此文件类型不允许上传!';}} else {$msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';}
}
观察源码发现缺少了去除字符串str_ireplace。
$file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA
说明:
- 在 Windows 操作系统中,::$DATA 是一种用于隐藏文件的特殊附加数据流(Alternate Data Stream)。它是一种扩展文件属性,可以将额外的数据附加到一个文件中,而不会影响文件的主要内容。
- 附加数据流基于文件名和冒号之间的特殊语法,例如:
file.txt::$DATA
。附加数据流可以储存任意类型的数据,例如文本、二进制文件或其他文件。这使得 .::$DATA 可以被用于隐藏文件内容、存储元数据或其他需要与主文件相关但不希望直接显示给用户的信息。
上传php文件,然后抓取数据包,在文件后缀名后面加上::$DATA即可。
上传成功
Pass-09-点+空格+点+空格绕过
查看源码:
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {if (file_exists(UPLOAD_PATH)) {$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");$file_name = trim($_FILES['upload_file']['name']);$file_name = deldot($file_name);//删除文件名末尾的点$file_ext = strrchr($file_name, '.');$file_ext = strtolower($file_ext); //转换为小写$file_ext = str_ireplace('::$DATA', '', $file_ext);//去除字符串::$DATA$file_ext = trim($file_ext); //首尾去空if (!in_array($file_ext, $deny_ext)) {$temp_file = $_FILES['upload_file']['tmp_name'];$img_path = UPLOAD_PATH.'/'.$file_name;if (move_uploaded_file($temp_file, $img_path)) {$is_upload = true;} else {$msg = '上传出错!';}} else {$msg = '此文件类型不允许上传!';}} else {$msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';}
}
观察源码发现代码先是去除文件名前后的空格,再去除文件名末尾的.
,再通过strrchar
函数来寻找.
来确认文件名的后缀,但是最后保存文件的时候没有重命名而使用的原始的文件名,本关没有循环验证,说明转换大小写去除空格什么的它只验证一次。
上传php文件,然后抓取数据包,在文件后缀名后面加上.php. .
即可。也就是点+空格+点+空格
绕过。
上传成功
Pass-10-文件后缀名双写绕过
查看源码:
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {if (file_exists(UPLOAD_PATH)) {$deny_ext = array("php","php5","php4","php3","php2","html","htm","phtml","pht","jsp","jspa","jspx","jsw","jsv","jspf","jtml","asp","aspx","asa","asax","ascx","ashx","asmx","cer","swf","htaccess");$file_name = trim($_FILES['upload_file']['name']);$file_name = str_ireplace($deny_ext,"", $file_name);$temp_file = $_FILES['upload_file']['tmp_name'];$img_path = UPLOAD_PATH.'/'.$file_name; if (move_uploaded_file($temp_file, $img_path)) {$is_upload = true;} else {$msg = '上传出错!';}} else {$msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';}
}
上传一个文件查看,发现将违规的文件后缀名替换为空了
那么我们可以使用文件后缀名双写来绕过
上传成功
页面访问
Pass-11-GET型00截断
查看源码:
$is_upload = false;
$msg = null;
if(isset($_POST['submit'])){$ext_arr = array('jpg','png','gif');$file_ext = substr($_FILES['upload_file']['name'],strrpos($_FILES['upload_file']['name'],".")+1);if(in_array($file_ext,$ext_arr)){$temp_file = $_FILES['upload_file']['tmp_name'];$img_path = $_GET['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext;if(move_uploaded_file($temp_file,$img_path)){$is_upload = true;} else {$msg = '上传出错!';}} else{$msg = "只允许上传.jpg|.png|.gif类型文件!";}
}
观察源码发现是白名单上传,只允许上传白名单中的后缀名。
关键代码如下:
$img_path = $_GET['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext;
这个$img_path变量是直接进行拼接的,所以可以使用%00截断绕过。
%00截断的条件:php版本要小于5.3.4 修改php.ini配置文件中的magic_quotes_gpc为OFF状态。
上传PHP文件,使用bp抓包,然后修改save_path的值
说明:GET请求提交的内容会被自动进行URL编码。
上传成功
页面访问
补充:
00截断(Null Byte Injection)是一种针对Web应用程序的安全漏洞,它利用输入验证不充分的情况下,以空字节(\x00或%00)作为终止字符来截断字符序列。这种漏洞通常出现在不正确地处理用户输入的情况下,尤其是在字符串拼接或文件路径处理等场景中。
当应用程序没有正确处理输入数据时,攻击者可以通过插入空字节来绕过输入验证。空字节可以用来截断字符串,导致应用程序对输入数据的处理产生意外结果,进而造成安全问题。
0x00,%00,/00这三类阶截断都是属于同种原理。在url中%00表示ascll码中的0 ,而ascii中0作为特殊字符保留,表示字符串结束,所以当url中出现%00时就会认为读取已结束。
例如:在1.php文件名改为1.php%00.jpg会被解析为1.php,欺骗应用程序认为这是受支持的php文件类型,从而绕过文件类型验证,上传shell。
Pass-12-POST型00截断
查看源码:
$is_upload = false;
$msg = null;
if(isset($_POST['submit'])){$ext_arr = array('jpg','png','gif');$file_ext = substr($_FILES['upload_file']['name'],strrpos($_FILES['upload_file']['name'],".")+1);if(in_array($file_ext,$ext_arr)){$temp_file = $_FILES['upload_file']['tmp_name'];$img_path = $_POST['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext;if(move_uploaded_file($temp_file,$img_path)){$is_upload = true;} else {$msg = "上传失败";}} else {$msg = "只允许上传.jpg|.png|.gif类型文件!";}
}
同样的白名单策略,与上一关不同的是,这次换成了POST方式传参,使用00截断。
上传php木马,使用bp抓取数据包,修改数据包内容
但是这里不会像GET方式那样会对%00进行解码,所以要对%00进行URL编码。
上传成功
页面访问
Pass-13-图片木马绕过
查看源码
function getReailFileType($filename){$file = fopen($filename, "rb");$bin = fread($file, 2); //只读2字节fclose($file);$strInfo = @unpack("C2chars", $bin); $typeCode = intval($strInfo['chars1'].$strInfo['chars2']); $fileType = ''; switch($typeCode){ case 255216: $fileType = 'jpg';break;case 13780: $fileType = 'png';break; case 7173: $fileType = 'gif';break;default: $fileType = 'unknown';} return $fileType;
}$is_upload = false;
$msg = null;
if(isset($_POST['submit'])){$temp_file = $_FILES['upload_file']['tmp_name'];$file_type = getReailFileType($temp_file);if($file_type == 'unknown'){$msg = "文件未知,上传失败!";}else{$img_path = UPLOAD_PATH."/".rand(10, 99).date("YmdHis").".".$file_type;if(move_uploaded_file($temp_file,$img_path)){$is_upload = true;} else {$msg = "上传出错!";}}
}
使用cmd命令:
copy imgName/b + phpName/a newImgName
上传图片木马
结合文件包含漏洞执行图片木马,利用靶场提供的include.php文件进行漏洞测试。
浏览器访问路径
http://192.168.188.183/include.php?file=./upload/6720230902142414.png
蚁剑进行连接
Pass-14-getimagesize()检查绕过
查看源码
function isImage($filename){$types = '.jpeg|.png|.gif';if(file_exists($filename)){$info = getimagesize($filename);$ext = image_type_to_extension($info[2]);if(stripos($types,$ext)>=0){return $ext;}else{return false;}}else{return false;}
}$is_upload = false;
$msg = null;
if(isset($_POST['submit'])){$temp_file = $_FILES['upload_file']['tmp_name'];$res = isImage($temp_file);if(!$res){$msg = "文件未知,上传失败!";}else{$img_path = UPLOAD_PATH."/".rand(10, 99).date("YmdHis").$res;if(move_uploaded_file($temp_file,$img_path)){$is_upload = true;} else {$msg = "上传出错!";}}
}
观察源码发现,getimagesize()函数,该函数会对文件内容进行检测,但是只检测文件头部。
上传php,使用bp抓取数据包,修改数据包中的内容,在文件内容的头部添加”GIF89a“。
上传成功
结合文件包含漏洞执行图片木马,利用靶场提供的include.php文件进行漏洞测试。
浏览器访问路径:
http://192.168.188.183/include.php?file=./upload/9920230902150500.gif
Pass-15-exif_imagetype()检测绕过
查看源码:
function isImage($filename){//需要开启php_exif模块$image_type = exif_imagetype($filename);switch ($image_type) {case IMAGETYPE_GIF:return "gif";break;case IMAGETYPE_JPEG:return "jpg";break;case IMAGETYPE_PNG:return "png";break; default:return false;break;}
}$is_upload = false;
$msg = null;
if(isset($_POST['submit'])){$temp_file = $_FILES['upload_file']['tmp_name'];$res = isImage($temp_file);if(!$res){$msg = "文件未知,上传失败!";}else{$img_path = UPLOAD_PATH."/".rand(10, 99).date("YmdHis").".".$res;if(move_uploaded_file($temp_file,$img_path)){$is_upload = true;} else {$msg = "上传出错!";}}
}
观察源码发现有一个exif_imagetype()函数,该函数是判断一个图形的类型。
绕过方式采用图片木马:
结合文件包含漏洞执行图片木马,利用靶场提供的include.php文件进行漏洞测试。
浏览器访问:
蚁剑进行连接即可。
也可以使用Pass-14的方式进行绕过。