本文已参与「新人创作礼」活动,一起开启掘金创作之路。
0x00 前言
之前做反序列化的时候都是先创建新对象后在外部赋值,有点蠢的
所以在碰到protected,private类型的变量时会束手无策,只能看有没有__set()来赋值
这次看了dl们的wp才意识到直接在类内部赋值就完了
反正这不会影响你调试的时候用到的序列化结果
知识盲点
php7.1以上的版本对属性类型不敏感
所以将属性改为public绕过即可(震惊…
0x01 审计
<?php
include("flag.php");
highlight_file(__FILE__);
class FileHandler {
protected $op;
protected $filename;
protected $content;
function __construct() {
$op = "1";
$filename = "/tmp/tmpfile";
$content = "Hello World!";
$this->process();
}
public function process() {
if($this->op == "1") {
$this->write();
} else if($this->op == "2") {
$res = $this->read();
$this->output($res);
} else {
$this->output("Bad Hacker!");
}
}
private function write() {
if(isset($this->filename) && isset($this->content)) {
if(strlen((string)$this->content) > 100) {
$this->output("Too long!");
die();
}
$res = file_put_contents($this->filename, $this->content);
if($res) $this->output("Successful!");
else $this->output("Failed!");
} else {
$this->output("Failed!");
}
}
private function read() {
$res = "";
if(isset($this->filename)) {
$res = file_get_contents($this->filename);
}
return $res;
}
private function output($s) {
echo "[Result]: <br>";
echo $s;
}
function __destruct() {
if($this->op === "2")
$this->op = "1";
$this->content = "";
$this->process();
}
}
function is_valid($s) {
for($i = 0; $i < strlen($s); $i++)
if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
return false;
return true;
}
if(isset($_GET{'str'})) {
$str = (string)$_GET['str'];
if(is_valid($str)) {
$obj = unserialize($str);
}
}
讲一下大致思路:
目的是读取flag.php 毋庸置疑
反序列化传入之后观察入口
反序列化直接绕过__construct()
- 1.只能通过__destruct()开始调用–>process()
- 2.process中有write和read
显然我们需要read,也就是op需要为2
a.
function __destruct() {
if($this->op === "2")
$this->op = "1";
$this->content = "";
$this->process();
}
b.
public function process() {
if($this->op == "1") {
$this->write();
} else if($this->op == "2") {
$res = $this->read();
$this->output($res);
} else {
$this->output("Bad Hacker!");
}
}
两次校验 一次强等于 一次弱等于
显然要read -->op必须弱等于2,并且不能强等于字符串2(否则会被重置为1)
那么op=2 (int)即可
- 3.还有一层valid校验
function is_valid($s) {
for($i = 0; $i < strlen($s); $i++)
if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
return false;
return true;
}
对传入序列化对象的合法性 需满足ascii值在32~125之间
关于protected与private序列化时的区别
忘记的可以回顾一下dl的blog
大致就是
private声明的会在前面加上:\0*\0 记入长度(三个字符)
protected会加上:\0私有字段类的类名\0
这里的 \0 表示 ASCII 码为 0 的字符(不可见字符),而不是 \0 组合!! (这里表示ascii为0的字符也就是 空 )
%00通过url解码后也为空
所以也可以用它来代替,但是这样就不满足ascii值在32-125之间
此处就涉及到前言中的7.1以上版本对属性类型的不敏感
直接public声明绕过就行
总结:
public op=2;
public filename=‘flag.php’;(伪协议也行)
public content=‘1’;(无所谓,满足valid即可)
done!
0x02 rethink
越无知的人越自满
一回家就摆烂确实不像话
多练多学