美文网首页CTF-Web安全
Upload-labs通关笔记(更新中)

Upload-labs通关笔记(更新中)

作者: 简言之_ | 来源:发表于2019-07-11 10:27 被阅读2次

upload-labs包含漏洞类型分类

image
如何判断上传漏洞类型?
image
上传的过程
image

Pass-01(前端JS绕过)

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;
    }
}

方法一:前端检测。js的检测只能位于client,可以禁用js,在浏览器设置中修改。或者直接改掉这里的 checkFile()

image
修改之后就可以直接上传.php文件,上传之后复制图像地址就可以得到上传路径了
image
image
方法二:上传1.png直接抓包,修改后缀为php就可以绕过上传
image
得到路径/upload/1.php,连接菜刀,得到shell
image

Pass-02(MIME绕过)

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

本节对数据包的MIME(content-type)进行了限定,只允许 image/jpeg、image/png、image/gif 图片内容数据传输。操作和第一节方法二一样。

上传1.png直接抓包,修改后缀为php就可以绕过上传


image

得到路径/upload/1.php,连接菜刀,得到shell


image

Pass-03(上传特殊可解析后缀绕过php4、phtml)

$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);//去除字符串::$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 = '不允许上传.asp,.aspx,.php,.jsp后缀文件!';
        }
    } else {
        $msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
    }
}

查看源码,发现是设置了文件后缀名黑名单,禁止上传后缀名为.php文件,这里利用php2、php3、php4、php5、phps、phtml一样会解析,直接修改后缀名为phps上传。
复制图像地址

image
得到上传路径
image
常见扩展名绕过
asp:asa,cer,cdx
aspx:ashx,asmx,ascx
php:php2、php3、php4、php5、phps、phtml
jsp:jspx,jspf

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,文件内容如下,意思就是在upload目录下匹配1.jpg的文件并以php文件执行

<FilesMatch "1.jpg">
SetHandler application/x-httpd-php
</FilesMatch>   

上传一个.htaccess

image
上传1.jpg,应为重写了文件解析规则,1.jpg将会被以php文件执行
image
然后直接连接菜刀
image
getshell
image
.htaccess攻击总结
有的时候由于各种名单的原因,可能我们不能上传任何php文件,而且还没有其他地方来解析成php,咋办?如果你能上传.htaccess文件的话,那么就很好办了。
建一个.htaccess 文件,里面的内容如下
<FilesMatch "1.jpg">
SetHandler application/x-httpd-php
</FilesMatch>

这个时候就上传一个文件名字是1.jpg的文件,然后里面是一句话木马,1.jpg就会被当成1.php执行,就能成功连接菜刀

Pass-05(后缀大小写绕过)

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

Pass-04与Pass-05代码对比

image
对比之后发现黑名单多了一个.htaccess
并且没有将文件后缀转小写的代码了
于是这里显然可以用大小写绕过,例如 .Php .phP
image

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-05与Pass-06代码对比

image
发现删去了将文件名前后去空格的操作 所以可以利用6.php(空格)
image

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

Pass-06与Pass-07代码对比

image
对比发现没有去处文件末尾的点的操作了
于是利用7.php.
image

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

[图片上传失败...(image-b1aca6-1562811998688)]
对比发现这里删掉了::$DATA的限制
::DATA备用流存在于每个文件,因此它可以是访问任何文件的替代方法 所以使用`8.php::DATA`

image
Windows :: DATA备用数据流漏洞:
https://www.owasp.org/index.php/Windows_::DATA_alternate_data_stream

Pass-09( 加点 空格 配合绕过)

参考:
https://cloud.tencent.com/developer/article/1377897
http://www.bubuko.com/infodetail-2944836.html
https://blog.csdn.net/u011377996/article/details/86776198
http://www.she1don.cn/index.php/archives/38.html

相关文章

  • Upload-labs通关笔记(更新中)

    upload-labs包含漏洞类型分类 Pass-01(前端JS绕过) 方法一:前端检测。js的检测只能位于cli...

  • upload-labs 通关笔记

    注入脑图 假设上传图片text.jpg,用burpsuite抓包,将文件名改为text.jpgaaaa。如果上传成...

  • upload-labs通关

    pass-01 尝试上传一个php,发现提示不行。 前端js拦截了,先将php文件后缀改为允许的格式,比如jpg,...

  • upload-labs-21关通关笔记

    项目地址 https://github.com/c0ny1/upload-labs/ 通关记录 Pass-01-前...

  • upload-labs通关手册

    序 文件上传靶场,搭建了很长时间了,一直没做,刚好最近闲来无事。学习一下 靶场环境 因为环境的不同导致上传的绕过方...

  • 文件上传漏洞靶场--c0ny1 / upload-labs

    c0ny1/upload-labs c0ny1/upload-labs是一个文件上传漏洞靶场,地址:https:/...

  • upload-labs

    title: upload-labsdate: 2019-04-17 09:20:52tags:- 文件上传cat...

  • upload-labs

    文件上传的目的是通过上传.php文件,从而植入木马,然后通过菜刀进行连接,最终get shell 0x01 Pas...

  • CMOOC 19991

    2021年6月7日,注册。笔记陆续更新中……

  • upload-labs(Pass02)

    本次搭建upload-labs靶机进行File Upload测试 测试项目:Pass02[MIME check] ...

网友评论

    本文标题:Upload-labs通关笔记(更新中)

    本文链接:https://www.haomeiwen.com/subject/nzhrkctx.html