首先,对于序列化和反序列化还不太理解的话看看以下链接: PHP中序列化与反序列化 JAVA中的序列化与反序列化 简单来说: 在PHP中 序列化 将变量或对象转换成字符串的过程。 反序列化 将字符串转换成变量或对象的过程。
就是对数据处理的两种互逆过程 下面以一道题目来进行讲解反序列化中的字符串逃逸 题目链接:bugku-newphp 打开后页面展示源码
<?php
header("Content-type: text/html; charset=utf-8");
highlight_file(__FILE__);
class evil{
public $hint;
public function __construct($hint){
$this->hint = $hint;
}
public function __destruct(){
if($this->hint==="hint.php")
@$this->hint = base64_encode(file_get_contents($this->hint));
var_dump($this->hint);
}
function __wakeup() {
if ($this->hint != "╭(●`?′●)╯") {
$this->hint = "╰(●’?’●)╮";
}
}
}
class User
{
public $username;
public $password;
public function __construct($username, $password){
$this->username = $username;
$this->password = $password;
}
}
function write($data){
global $tmp;
$data = str_replace(chr(0).'*'.chr(0), '\0\0\0', $data);
$tmp = $data;
}
function read(){
global $tmp;
$data = $tmp;
$r = str_replace('\0\0\0', chr(0).'*'.chr(0), $data);
return $r;
}
$tmp = "test";
$username = $_POST['username'];
$password = $_POST['password'];
$a = serialize(new User($username, $password));
if(preg_match('/flag/is',$a))
die("NoNoNo!");
unserialize(read(write($a)));
通过初步的代码审计我们可以得知
evil类里$this->hint指向文件触发file_get_contents函数读取文件内容 提示有个hint.php 所以我们要构造触发这个evil类 因为最后会数据进行反序列化 所以对于我们传入的数据就得序列化 来看看需要传入什么  payload
O:4:"evil":1:{s:4:"hint";s:8:"hint.php";}
 这里还有一个wakeup函数需要绕过 (因为反序列化有优先调用wakeup函数)
小知识点:当序列化对象个数与后面数据不匹配时 会跳过反序列化,也就绕过了wakeup函数
O:4:"evil":2:{s:4:"hint";s:8:"hint.php";}
所以我们再将将evil后面的数字改的更大就行了,比如1改成2 (绕过wakeup只需对象属性个数值改得比真实对象大)
User类中,有read()方法和write() 方法  这两个方法都是经过处理后才进行反序列化的 漏洞也就是从这里产生的 序列化的字符串在经过过滤函数不正确的处理而导致对象注入,目前看到都是因为过滤函数放在了serialize函数之后,要是放在序列化之前应该就不会产生这个问题 相关 用User类触发 
得到  现在的问题就是 反序列化时字符长度固定的情况下,我们要怎么注入想要的属性? 我们真正需要的说后面的数据 所以这一段想要替换:";s:8:“password”;s:41:" 用python简单计算字符串长度  长度为23 根据之前的read和write函数会对字符串进行替换 chr(0)*chr(0)s是三个字符,而\0\0\0\是六个字符 所以 每增加一组的\0\0\0就会吞掉三个字符,即多出三个位置给后面。 那么我们可以在password的值上再加一个任意字符,即可凑齐24个,那么\0\0\0就是八组,完成3的倍数要求
那这样我们就可以尝试注入了  得到base64编码后的源码 解码  按照提示访问index.cgi 关于这里为什么存在ssrf我也不是太了解  参考师傅解题 
到此解题完毕 题外 对于刚才的“吞并”字符串操作 我后面还有再试了其他长度的 为了验证下正确性,将username里的字符变成2倍,然后通过前面要替换为23位 就用24x2得到的48位减去23位得到25位 为了避免覆盖payload我们就增添25位 记住,这里是6->3,\0的2位替换成1位的* 所以计算的时候要注意,都是以被替换成后来计算的  照样打得通 参考链接
|