前言:文件上传漏洞有很多种绕过技巧,这次就通过upload-labs进行学习
第一关
上传有限制,只让上传JPEG或PNG格式的图片,就先尝试一下抓包修改上传格式的方法看看是否可行
先将一句话木马PHP文件后缀名改为PNG格式,上传拦截抓包
拦截请求包后,将1.png
改为1.php
,再发包,用菜刀进行连接,在此之前需要知道上传的文件上传到哪个目录下,查看源码查出图片上传路径
路径也知道了,就用菜刀进行连接
连接成功
观察一下第一关的源码
function checkFile() {
var file = document.getElementsByName('upload_file')[0].value;
if (file == null || file == "") {
alert("请选择要上传的文件!");
return false;
}
//定义允许上传的文件类型
var allow_ext = ".jpg|.png|.gif";
//提取上传文件的类型
var ext_name = file.substring(file.lastIndexOf("."));
//判断上传文件类型是否允许上传
if (allow_ext.indexOf(ext_name + "|") == -1) {
var errMsg = "该文件不允许上传,请上传" + allow_ext + "类型的文件,当前文件类型为:" + ext_name;
alert(errMsg);
return false;
}
}
通过观察只是限制了在上传时的类型,所以中途拦截抓包改格式的方法是完成可行的。
方法:改包绕过上传
第二关
做第二关时发现沿用第一关的方法也是可行的,不过应该不会再考同一个点了
就查看一下源码
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists(UPLOAD_PATH)) {
if (($_FILES['upload_file']['type'] == 'image/jpeg') || ($_FILES['upload_file']['type'] == 'image/png') || ($_FILES['upload_file']['type'] == 'image/gif')) {
$temp_file = $_FILES['upload_file']['tmp_name'];
$img_path = UPLOAD_PATH . '/' . $_FILES['upload_file']['name']
if (move_uploaded_file($temp_file, $img_path)) {
$is_upload = true;
} else {
$msg = '上传出错!';
}
} else {
$msg = '文件类型不正确,请重新上传!';
}
} else {
$msg = UPLOAD_PATH.'文件夹不存在,请手工创建!';
}
}
发现这一段代码判断content-type,那就可以通过修改content-type进行绕过:
if (($_FILES['upload_file']['type'] == 'image/jpeg') || ($_FILES['upload_file']['type'] == 'image/png') || ($_FILES['upload_file']['type'] == 'image/gif'))
抓包将application/octet-stream
修改为image/png
或image/gif
都可以
连接成功
方法:修改content-type进行绕过
第三关
上传PHP文件时,发现有这样的提示
应该是源码中限制了这些文件的后缀名,查看大师傅们的博客发现还可以用
phtml,php3,php4, php5, pht
这些后缀名进行绕过,就来尝试一下,发现连接不上去,查了一下才知道原来前提是apache的httpd.conf中有如下配置代码
AddType application/x-httpd-php .php .phtml .phps .php5 .pht
我这里使用的是phpstudy+windows,即使添加了也不管用,查了大师傅的博客才知道是由于配置原因是解析不了php5等等这些后缀的,所以复现不了,可以在虚拟机中复现这关。
不过也知道了这一关是采用拓展名绕过
查看一下源码
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists(UPLOAD_PATH)) {
$deny_ext = array('.asp','.aspx','.php','.jsp');
$file_name = trim($_FILES['upload_file']['name']);//trim() 函数移除字符串两侧的空白字符或其他预定义字符
$file_name = deldot($file_name);//删除文件名末尾的点
$file_ext = strrchr($file_name, '.');//strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符。
$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)) {
//in_array() 函数搜索数组中是否存在指定的值
$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 = '不允许上传.asp,.aspx,.php,.jsp后缀文件!';
}
} else {
$msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
}