最近在练习文件上传,所以记录一下自己练习的过程,既能帮助自己以后复习,同时也能帮到初学者。

主要用到的工具是Burpsuite。首先我们应该明白上传文件的目的是什么,通过上传文件将web后门上传并被成功解析。

Pass-01

首先查看源码



通过源码可以看到,第一关是在本地前段进行验证,使用的是前端JS脚本,所以呢,你让它不起作用不就完了,前端我们是可以修改的,在前端JS函数中加上.php文件,然后上传PHP文件。(具体怎么修改,直接查看源代码,然后修改,然后保存本地,再然后点击访问,然后上传即可)

Pass-02

源码:



这个其实也很简单,我们发现它只判断content-type,那所以只要burpsuite抓包修改content-type的类型就可以成功绕过,然后上传。



上传成功如图:

Pass-03

步骤一样,第一步先查看源码



通过源代码分析,我们能够看出来是黑名单过滤,这里有很多绕过方式,抓包修改后缀为php5、php3、phtml、pht等可解析的文件后缀即可

Pass-04

查看源码:

$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文件,里面写上SetHandler application/x-httpd-php,之后所有的文件都会以PHP文件进行解析,那就正常上传图片,然后访问即可

此处用到知识点:

.htaccess文件 -服务

通过.htaccess文件修改解析规则

条件:必须为apache搭建平台

Pass-05

这一关和上一关几乎一模一样,但是没有进行大小写过滤,所以,大小写修改后缀名,然后成功上传。



上传结果如图:

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);//去除字符串::$DATA 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 . '文件夹不存在,请手工创建!';
}
}

仍然是过滤了很多后缀名,这个时候我们可以在后面加后缀名后面加空格进行绕过



结果如图所示:



成功上传!

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 . '文件夹不存在,请手工创建!';
}
}

第7关我们发现多了一个函数,就是首位去空,发现加空格以不再有效。那就没有别的了吗?

当然不是了,出了空格还有很多可以加,它虽然过滤了空格,但是没有过滤“.”!所以,我们可以在后缀名后面加“.”即可绕过上传,正所谓,条条大路通罗马,这里我们也可以看出来,黑名单的弊端!





成功

Pass-08

源码

$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 . '文件夹不存在,请手工创建!';
}
}

这里我们发现,大小写不行,加点不行,加空格也不行,但是没有对::\(DATA进行过滤,所以在后缀名中加::\)DATA进行绕过

知识点:

文件名+::\(DATA - 服务
必须是windows, 必须是php,
php在window的时候如果文件名+"::\)DATA"会把::\(DATA之后的数据当成文件流处理,不会检测后缀名.且保持"::\)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 . '文件夹不存在,请手工创建!';
}
}

这里,在后缀后加上 点+空格+点 即可成功绕过,



pass-10

这里的步骤和之前的一样,重复的我就不再写了,浪费时间,还是先看源码,这是第一步,通过源码去分析它是黑名单过滤还是白名单,然后看它漏掉了哪项。

第10关采用双写绕过,后缀名为 .phphpp 即可成功上传,结果一样,我就不再一一截图了。

Pass-11

查看源码,我们发现11关是白名单判断,因此可以采用%00截断绕过

但是有环境限制: php版本要小于5.3.4,5.3.4及以上

magic_quotes_gpc需要为OFF状态

如果你在做实验时发现一直上传错误,请看你是否满足我上面说的两个环境限制,如果不满足就会上传错误,如果已经设置好,则可以正常上传,得到正确结果。

Pass-12

这一关和11关一样,只是这个是POST提交,上面那个是GET提交,用的方法是一样的!

这里就不再过多阐述

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 = "上传出错!";
}
}
}

13关往后与之前不一样,13关之后的是上传图片马。

这里我们看到,它只判断前两个字节,所以自己搞一个图片,然后编辑,将一句话木马写进去,然后上传即可,访问即可得到。



图片上面是乱码,但下面就是被php解析的函数。

Pass-14

跟13关一样。

getimagesize判断图片内置函数可以参考官方文档http://php.net/manual/zh/function.getimagesize.php

Pass-15

这关需要开启php_exif模块,通过php_exif来判断函数类型,具体可以看官方文档http://php.net/manual/zh/function.exif-imagetype.php,接下来还是上传图片马

Pass-16

源码我就不贴了,占地,自己在自己的靶场可以看到。

本关综合判断了后缀名、content-type,以及利用imagecreatefromgif判断是否为gif图片,最后对图片再做了一次二次渲染重新创建,所以在上传图片马的时候需要寻找二次渲染之后不会被修改的地方。之后的操作与之前的关卡一样

Pass-17

本关考察的是条件竞争

通过白名单检测后缀名,符合就rename改名,不符合就unlink删除文件

可以用burp来发包不断访问,先写一个php写入文件的1.php然后在不断访问这文件就可以看到我们上传的东西了,而且在访问这文件就可以生成另外一个shell了(通过马生成的)

Pass-18

//index.php
$is_upload = false;
$msg = null;
if (isset($_POST['submit']))
{
require_once("./myupload.php");
$imgFileName =time();
$u = new MyUpload($_FILES['upload_file']['name'], $_FILES['upload_file']['tmp_name'], $_FILES['upload_file']['size'],$imgFileName);
$status_code = $u->upload(UPLOAD_PATH);
switch ($status_code) {
case 1:
$is_upload = true;
$img_path = $u->cls_upload_dir . $u->cls_file_rename_to;
break;
case 2:
$msg = '文件已经被上传,但没有重命名。';
break;
case -1:
$msg = '这个文件不能上传到服务器的临时文件存储目录。';
break;
case -2:
$msg = '上传失败,上传目录不可写。';
break;
case -3:
$msg = '上传失败,无法上传该类型文件。';
break;
case -4:
$msg = '上传失败,上传的文件过大。';
break;
case -5:
$msg = '上传失败,服务器已经存在相同名称文件。';
break;
case -6:
$msg = '文件无法上传,文件不能复制到目标目录。';
break;
default:
$msg = '未知错误!';
break;
}
} //myupload.php
class MyUpload{
......
......
......
var $cls_arr_ext_accepted = array(
".doc", ".xls", ".txt", ".pdf", ".gif", ".jpg", ".zip", ".rar", ".7z",".ppt",
".html", ".xml", ".tiff", ".jpeg", ".png" ); ......
......
......
/** upload()
**
** Method to upload the file.
** This is the only method to call outside the class.
** @para String name of directory we upload to
** @returns void
**/
function upload( $dir ){ $ret = $this->isUploadedFile(); if( $ret != 1 ){
return $this->resultUpload( $ret );
} $ret = $this->setDir( $dir );
if( $ret != 1 ){
return $this->resultUpload( $ret );
} $ret = $this->checkExtension();
if( $ret != 1 ){
return $this->resultUpload( $ret );
} $ret = $this->checkSize();
if( $ret != 1 ){
return $this->resultUpload( $ret );
} // if flag to check if the file exists is set to 1 if( $this->cls_file_exists == 1 ){ $ret = $this->checkFileExists();
if( $ret != 1 ){
return $this->resultUpload( $ret );
}
} // if we are here, we are ready to move the file to destination $ret = $this->move();
if( $ret != 1 ){
return $this->resultUpload( $ret );
} // check if we need to rename the file if( $this->cls_rename_file == 1 ){
$ret = $this->renameFile();
if( $ret != 1 ){
return $this->resultUpload( $ret );
}
} // if we are here, everything worked as planned :) return $this->resultUpload( "SUCCESS" ); }
......
......
......
};

和上一关几乎一模一样

Pass-19

$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 = $_POST['save_name'];
$file_ext = pathinfo($file_name,PATHINFO_EXTENSION); 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 . '文件夹不存在,请手工创建!';
}
}

本关考察CVE-2015-2348 move_uploaded_file() 00截断,上传webshell,同时自定义保存名称,直接保存为php是不行的,查看源码发现move_uploaded_file()函数中的img_path是由post参数save_name控制的,因此可以在save_name利用00截断绕过

Pass-20

$is_upload = false;
$msg = null;
if(!empty($_FILES['upload_file'])){
//检查MIME
$allow_type = array('image/jpeg','image/png','image/gif');
if(!in_array($_FILES['upload_file']['type'],$allow_type)){
$msg = "禁止上传该类型文件!";
}else{
//检查文件名
$file = empty($_POST['save_name']) ? $_FILES['upload_file']['name'] : $_POST['save_name'];
if (!is_array($file)) {
$file = explode('.', strtolower($file));
} $ext = end($file);
$allow_suffix = array('jpg','png','gif');
if (!in_array($ext, $allow_suffix)) {
$msg = "禁止上传该后缀文件!";
}else{
$file_name = reset($file) . '.' . $file[count($file) - 1];
$temp_file = $_FILES['upload_file']['tmp_name'];
$img_path = UPLOAD_PATH . '/' .$file_name;
if (move_uploaded_file($temp_file, $img_path)) {
$msg = "文件上传成功!";
$is_upload = true;
} else {
$msg = "文件上传失败!";
}
}
}
}else{
$msg = "请选择要上传的文件!";
}

通过查看源码可以发现:

\(file_name经过reset(\)file) . '.' . \(file[count(\)file) - 1];处理。

如果上传的是数组的话,会跳过\(file = explode('.', strtolower(\)file));。

并且后缀有白名单过滤:

\(ext = end(\)file);

$allow_suffix = array('jpg','png','gif');

而最终的文件名后缀取的是\(file[count(\)file) - 1],因此我们可以让$file为数组。

\(file[0]为smi1e.php/,也就是reset(\)file),然后再令$file[2]为白名单中的jpg。

此时end(\(file)等于jpg,\)file[count($file) - 1]为空。

而 \(file_name = reset(\)file) . '.' . \(file[count(\)file) - 1];,也就是smi1e.php/.,最终move_uploaded_file会忽略掉/.,最终上传smi1e.php。

总结:

以上就是自己的一些笔记和经验,但是,试想一下,如果没有源代码,你又能通过几关呢?

upload-labs通关手册的更多相关文章

  1. SUCTF 2019 Upload labs 2 踩坑记录

    SUCTF 2019 Upload labs 2 踩坑记录 题目地址 : https://github.com/team-su/SUCTF-2019/tree/master/Web/Upload La ...

  2. Upload - Labs (下)

    Pass - 11: 1.查看源代码,发现进行了一次对后缀名替换成空格,因此考虑双写绕过, 2.上传成功, 关键代码: $is_upload = false; $msg = null; if (iss ...

  3. VulnHub-[DC-8-9]-系列通关手册

    DC8-通关手册 DC-8是另一个专门构建的易受攻击的实验室,目的是在渗透测试领域积累经验. 这个挑战有点复杂,既是实际挑战,又是关于在Linux上安装和配置的两因素身份验证是否可以阻止Linux服务 ...

  4. Upload - Labs (上)

    Pass - 01: 1.尝试上传一个php文件:aaa.php,发现只允许上传某些图片类型,用bp抓包,发现http请求都没通过burp就弹出了不允许上传的提示框,这表明验证点在前端,而不在服务端 ...

  5. C# 通关手册(持续更新......)

    String 常用静态方法 string.Compare(string str1,string str2,bool ignoreCase) 按照字典顺序比较字符串 当str1 > str2时,返 ...

  6. Upload-labs通关指南(上) 1-10

    Upload-labs 所有文章和随笔(随笔将不于csdn更新)将于我的个人博客(移动端暂未适配)第一时间更新. 一些网站存在文件上传接口,一旦存在这个接口就有可能存在漏洞. 文件上传漏洞的逻辑是将一 ...

  7. 商城项目(ssm+dubbo+nginx+mysql统合项目)总结(4)

    我不会在这里贴代码和详细步骤什么的,我觉得就算我把它贴出来,你们照着步骤做还是会出很多问题,我推荐你们去看一下黑马的这个视频,我个人感觉很不错,一步一步走下来可以学到很多东西.另外,视频和相关文档的话 ...

  8. Java NIO 概览

    Java面试通关手册(Java学习指南) Github地址:https://github.com/Snailclimb/Java_Guide 一 NIO简介 Java NIO 是 java 1.4 之 ...

  9. Java NIO之Selector(选择器)

    历史回顾: Java NIO 概览 Java NIO 之 Buffer(缓冲区) Java NIO 之 Channel(通道) 其他高赞文章: 面试中关于Redis的问题看这篇就够了 一文轻松搞懂re ...

随机推荐

  1. tomcat8调优

    a. tomcat的运行原理: 1. Tomcat是运行在JVM中的一个进程.它定义为[中间件],顾名思义,是一个在Java项目与JVM之间的中间容器. 2. Web项目的本质,是一大堆的资源文件和方 ...

  2. Java面试题:抽象类和接口的区别

    1.abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系.但是,一个类却可以实现多个interface. 2.在abstract class 中可以有自己 ...

  3. 使用RSS订阅喜欢的微博博主

    获取uid方法:进入博主微博主页,使用控制台执行 /uid=(\d+)/. exec(document.querySelector('.opt_box .btn_bed').getAttribute( ...

  4. LeetCode--Jewels and Stones && Range Sum of BST (Easy)

    771. Jewels and Stones (Easy)# You're given strings J representing the types of stones that are jewe ...

  5. HBase Filter 过滤器之FamilyFilter详解

    前言:本文详细介绍了 HBase FamilyFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.FamilyFilter 基于列族进行过滤,在工作中涉及 ...

  6. 永磁同步电机 spmsm 和 ipmsm 的区别总结

    layout: post tags: [motor control] comments: true 永磁同步电机的分类 永磁同步电机根据转子上永磁体的位置不同,可以分为: 表贴式永磁同步电机--S-P ...

  7. 网站主机技术+linux教程

    一.Linux 云服务器 自己安装服务器还是麻烦了些,现在一般都推荐大家使用云服务器,比较方便,价格也不贵. 目前市场上的云服务器很多,这边比较下腾讯云跟阿里云的服务器优惠活动,现在看来腾讯云性价比会 ...

  8. Git使用教程之SSH连接方式配置(二)

    什么是GitHub?这个网站就是提供Git仓库托管服务的. 什么是SSH Key?你的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,大白话理解就是这两个仓库如果要进行远程同步,则我们需 ...

  9. 一个排查了大半天儿的问题,差点又让 MyBatis 背锅

    我是风筝,公众号「古时的风筝」,一个不只有技术的技术公众号,一个在程序圈混迹多年,主业 Java,另外 Python.React 也玩儿的 6 的斜杠开发者. Spring Cloud 系列文章已经完 ...

  10. Linux系统中如何升级pip

    问题提出:在Linux系统下安装python的logging库时提示以下信息 经过一番折腾,定位在pip版本过低和setuptools版本过低上 一.Linux下更新包 sudo python3 -m ...