upload-labs靶场简介
- Upload-labs是一个使用PHP语言编写,专门用于渗透测试和CTF中遇到的各种文件上传漏洞的靶场。目前一共20关,每个关都包含着不同上传方式。
- 文件上传漏洞:用户可以越过其本身权限,向服务器上传可执行的动态脚本文件,例如木马、病毒、恶意脚本或者WebShell等。文件上传漏洞本身就是一个危害巨大的漏洞,WebShell更是将这种漏洞的利用无限扩大。
新学知识
新学html知识
- form 标签中的enctype属性:规定在向服务器发送表单数据之前如何对其进行编码(适用于 method=“post” 的情况)
- form 标签中的onsubmit属性:onsubmit只有两个值true或者false,用于对表单进行验证之后再进行提交。如果值为false,则不会提交。如果值为true,则提交。如果没有得到值,则默认为true
新学php知识
- document.getElementsByName(name)函数:查询元素的name属性,返回带有指定名称的对象的集合
- $_POST[‘var’]: 用于收集来自 method=“post” 的表单中,输入框为 name=“ver” 的值。
- $UPLOAD_ADDR :为上传到哪个地址
- $_FILES[‘myFile’][‘name’] :表示文件的名称
- $_FILES[‘myFile’][‘type’] :表示文件的 MIME 类型
- $_FILES[‘myFile’][‘size’] :已上传文件的大小(单位:字节)
- $_FILES[‘myFile’][‘tmp_name’]: 在服务器中,临时文件名,一般是系统默认。
- $_FILES[‘myFile’][‘error’] :该文件上传相关的错误代码,PHP4.2版本后增加的。
Pass-01
漏洞分析
- 上传一个php文件时,发现被禁止了,只能.jpg、.png、.gif文件
- 点击提示发现,是因为在前端,使用js对不合法文件进行检查
- 绕过js的方法有两种:一种是,直接禁用js;另一种,使用工具抓包,直接修改后缀名为.php,为.jpg/.png/.gif
源码分析
- enctype=“multipart/form-data”:向服务器发送表单数据之前,以multipart/form-data对数据进行编码。
- method=“post”:用于发送表单数据的 HTTP 方法是post。
- οnsubmit=“return checkFile()”:onsubmit只有两个值true或者false,用于对表单进行验证之后再进行提交。如果checkFile()的返回值为false,则不会提交。如果checkFile()的返回值为true,则提交。如果不返回值,则默认为true
<form enctype="multipart/form-data" method="post" onsubmit="return checkFile()">
<p>请选择要上传的图片:p>
<p>
<input class="input_file" type="file" name="upload_file">
<input class="button" type="submit" name="submit" value="上传">
p>form>
- checkFile():
1.先用变量file保存,document.getElementsByName(‘upload_file’)返回的文件名。
2.再判断该文件是否存在
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;
}
}
绕过方法
-
直接修改一句话木马病毒的后缀名:
-
在浏览器中,开启手动配置代理。HTTP配置为127.0.0.1,端口号配置为8080
-
打开Burp抓包工具,打开拦截
-
选择一句话木马,点击上传
-
在浏览器发送数据包时,用Burp捕获
-
将1.jpg修改为1.php,点击Forward,把数据包发送给服务器
-
点击新建标签页打开图像
-
我们发现一句话木马已经上传成功,复制url
-
用蚁剑链接一句话木马,获取服务器资源
Pass-02
漏洞分析
-
在浏览器上开启代理
-
打开Burp,拦截浏览器发出的数据包
-
上传含有一语句木马的1.php文件
-
发现Burp拦截了数据包,说明文件格式是由服务器判断的
-
再次上传一个.jpg文件,用Burp拦截发出的数据包
-
删除:Content-Type: image/jpeg,点击Forward发送。提示:文件类型不正确,请重新上传!由此可见,服务器根据Content-Type: image/jpeg,判断文件格式。
- 注意:Content-Type(内容类型):用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式、什么编码读取这个文件
源码分析
$is_upload = false;
$msg = null;
if (isset($_POST['submit']))
{
if (file_exists($UPLOAD_ADDR))
{
if (($_FILES['upload_file']['type'] == 'image/jpeg') || ($_FILES['upload_file']['type'] == 'image/png') || ($_FILES['upload_file']['type'] == 'image/gif')) {
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) {
$img_path = $UPLOAD_ADDR . $_FILES['upload_file']['name'];
$is_upload = true;
}
} else {
$msg = '文件类型不正确,请重新上传!';
}
} else {
$msg = $UPLOAD_ADDR.'文件夹不存在,请手工创建!';
}
}
绕过方法
-
在浏览器上开启代理
-
打开Burp,拦截浏览器发出的数据包
-
上传含有一语句木马的1.php文件
-
发现Burp拦截发出数据包
-
将Content-Type: image/jpeg修改为Content-Type: image/jpeg。点击Forward发送数据包。
-
漏洞上传成功
Pass-03
漏洞分析
- 在浏览器上开启代理,打开Burp,拦截浏览器发出的数据包。
- 上传含有一语句木马的1.php文件,提示:不允许上传.asp,.aspx,.php,.jsp后缀文件!
- 发现Burp拦截发出数据包
- 可能在服务器上使用黑名单
源码分析
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists($UPLOAD_ADDR)) {
$deny_ext = array('.asp','.aspx','.php','.jsp');
$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);
$file_ext = trim($file_ext);
if(!in_array($file_ext, $deny_ext)) {
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR. '/' . $_FILES['upload_file']['name'])) {
$img_path = $UPLOAD_ADDR .'/'. $_FILES['upload_file']['name'];
$is_upload = true;
}
} else {
$msg = '不允许上传.asp,.aspx,.php,.jsp后缀文件!';
}
} else {
$msg = $UPLOAD_ADDR . '文件夹不存在,请手工创建!';
}
}
绕过方法
- 在浏览器上开启代理,并且打开Burp,拦截浏览器发出的数据包
- 上传含有一语句木马的1.php文件
- 用Burp捕获数据包,将文件名后缀.php,修改为.php5。点击Forward,发送数据包。
- 漏洞上传成功
Pass-04
漏洞分析
- 在浏览器上开启代理,打开Burp,拦截浏览器发出的数据包。
- 上传含有一语句木马的1.php文件,提示:不允许上传.asp,.aspx,.php,.jsp后缀文件!
- Burp拦截到了数据包,由此可知,是在后端进行文件验证的。
源码分析
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists($UPLOAD_ADDR)) {
$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".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);
$file_ext = trim($file_ext);
if (!in_array($file_ext, $deny_ext)) {
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) {
$img_path = $UPLOAD_ADDR . '/' . $file_name;
$is_upload = true;
}
} else {
$msg = '此文件不允许上传';
}
} else {
$msg = $UPLOAD_ADDR . '文件夹不存在,请手工创建!';
}
}
- 源码可知,后端不会禁止后缀名为.htaccess文件
绕过方法
-
构造一个1.htaccess文件,让服务器把.jpb文件作为.php文件
-
上传含有一语句木马的1.htaccess文件
-
上传含有一语句木马的1.php文件
-
点击新建标签页打开图像
-
发现一句话木马已经上传成功
Pass-05
漏洞分析
- 上传含有一语句木马的1.php文件,提示:此文件不许上传
- 服务器上验证文件格式php源码:
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists($UPLOAD_ADDR)) {
$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".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);
$file_ext = trim($file_ext);
if (!in_array($file_ext, $deny_ext)) {
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) {
$img_path = $UPLOAD_ADDR . '/' . $file_name;
$is_upload = true;
}
} else {
$msg = '此文件不允许上传';
}
} else {
$msg = $UPLOAD_ADDR . '文件夹不存在,请手工创建!';
}
}
- 通过分析源码,发现后端不会把文件名后缀转化为小写,而黑名单上的文件名后缀全都有小写字母组成。我们可以吧大小写混用组成文件名后缀,从而绕过黑名单。
绕过方法
- 构造含有一句话木马的2.Php文件
- 上传2.Php文件
- 文件漏洞上传成功
Pass-06
漏洞分析
- 上传含有一语句木马的1.php文件,提示:此文件不许上传
- 服务器上验证文件格式php源码:
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists($UPLOAD_ADDR)) {
$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".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);
if (!in_array($file_ext, $deny_ext)) {
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) {
$img_path = $UPLOAD_ADDR . '/' . $file_name;
$is_upload = true;
}
} else {
$msg = '此文件不允许上传';
}
} else {
$msg = $UPLOAD_ADDR . '文件夹不存在,请手工创建!';
}
}
- 通过分析源码,发现后端不会把文件名后缀最后的空格给删除。我们可以构造发出的数据包,在后缀名上添加一个空格,从而绕过黑名单。
绕过方法
- 在浏览器上开启代理,并且打开Burp,拦截浏览器发出的数据包
- 上传含有一语句木马的1.php文件
- 用Burp捕获数据包,将文件名后缀".php",修改为".php "。点击Forward,发送数据包。
- 漏洞上传成功
Pass-07
漏洞分析
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists($UPLOAD_ADDR)) {
$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".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);
$file_ext = trim($file_ext);
if (!in_array($file_ext, $deny_ext)) {
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) {
$img_path = $UPLOAD_ADDR . '/' . $file_name;
$is_upload = true;
}
} else {
$msg = '此文件不允许上传';
}
} else {
$msg = $UPLOAD_ADDR . '文件夹不存在,请手工创建!';
}
}
- 通过分析源码,发现后端不会把文件名后缀最后 . 给删除。我们可以构造发出的数据包,在后缀名上添加一个点,从而绕过黑名单
绕过方法
- 在浏览器上开启代理,并且打开Burp,拦截浏览器发出的数据包
- 上传含有一语句木马的1.php文件
- 用Burp捕获数据包,将文件名后缀".php",修改为".php."。点击Forward,发送数据包。
- 漏洞上传成功
Pass-08
漏洞分析
- 上传含有一语句木马的1.php文件,提示:此文件不许上传
- 服务器上验证文件格式php源码:
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists($UPLOAD_ADDR)) {
$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".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)) {
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) {
$img_path = $UPLOAD_ADDR . '/' . $file_name;
$is_upload = true;
}
} else {
$msg = '此文件不允许上传';
}
} else {
$msg = $UPLOAD_ADDR . '文件夹不存在,请手工创建!';
}
}
- 通过分析源码,发现后端不会把文件名后缀最后 :: D A T A 给删除。我们可以构造发出的数据包,在后缀名上添加 : : DATA给删除。我们可以构造发出的数据包,在后缀名上添加:: DATA给删除。我们可以构造发出的数据包,在后缀名上添加::DATA,从而绕过黑名单。
绕过方法
- 在浏览器上开启代理,并且打开Burp,拦截浏览器发出的数据包
- 上传含有一语句木马的1.php文件
- 用Burp捕获数据包,将文件名后缀".php",修改为".php::$DATA"。点击Forward,发送数据包。
- 漏洞上传成功
Pass-09
漏洞分析
- 上传含有一语句木马的1.php文件,提示:此文件不许上传
- 服务器上验证文件格式php源码:
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists($UPLOAD_ADDR)) {
$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".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);
$file_ext = trim($file_ext);
if (!in_array($file_ext, $deny_ext)) {
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) {
$img_path = $UPLOAD_ADDR . '/' . $file_name;
$is_upload = true;
}
} else {
$msg = '此文件不允许上传';
}
} else {
$msg = $UPLOAD_ADDR . '文件夹不存在,请手工创建!';
}
}
绕过方法
- 在浏览器上开启代理,并且打开Burp,拦截浏览器发出的数据包
- 上传含有一语句木马的1.php文件
- 用Burp捕获数据包,将文件名后缀".php",修改为".php. . "。点击Forward,发送数据包。
4. 漏洞上传成功
Pass-10
漏洞分析
- 上传含有一语句木马的1.php文件
-
点击新建标签页打开图像
-
我们发现,文件的后缀被删了,无法解析。
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists($UPLOAD_ADDR)) {
$deny_ext = array("php","php5","php4","php3","php2","html","htm","phtml","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);
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $file_name)) {
$img_path = $UPLOAD_ADDR . '/' .$file_name;
$is_upload = true;
}
} else {
$msg = $UPLOAD_ADDR . '文件夹不存在,请手工创建!';
}
}
- 通过分析源码,我们可以构造发出的数据包,使后缀为**.pphphp**,从而绕过黑名单
绕过方法
- 在浏览器上开启代理,并且打开Burp,拦截浏览器发出的数据包
- 上传含有一语句木马的1.php文件
- 用Burp捕获数据包,将文件名后缀".php",修改为".pphphp."。点击Forward,发送数据包。
- 漏洞上传成功