跳转至

File inclusion vulnerability

文件包含漏洞

描述

文件包含漏洞是一种通常影响依赖脚本运行时的编程语言的 Web 漏洞。当应用程序使用用户控制的参数构建可执行代码的路径时,就会出现此漏洞。这种构造允许攻击者指定在运行时执行哪个文件。

与路径遍历攻击(其中对文件系统的未授权访问允许对文件的只读访问)不同,文件包含漏洞允许在运行时包含和执行代码。成功利用此漏洞可能会导致远程代码执行、未经授权的文件访问和敏感信息泄漏。

文件包含漏洞可以分为两大类:

  • Local File Inclusion (LFI):在这种情况下,攻击者仅限于 Web 服务器上存在的本地文件。根据编程语言和配置,有可能使用特殊方案(如 php://)将此攻击升级为远程代码执行。
  • Remote File Inclusion (RFI):虽然在现代 Web 应用程序中不是很常见,但如果存在,此漏洞可能允许通过包含恶意代码来实现远程代码执行。
<?php

$page = $_GET['page'];
include($page . '.php');

?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

String page = request.getParameter("page");
include(page + ".jsp");

%>
<!DOCTYPE html>
<html>
<head>
<title>Test file</title>
</head>
<body>
<!--#include file="$USER_LANGUAGE"-->
</body>
</html>

建议

为了解决文件包含漏洞,请考虑以下建议:

  • 避免来自用户输入的动态文件包含: 除非必要,否则避免使用用户控制的输入调用 include()require() 或类似函数。
  • 输入白名单: 使用白名单只允许已知和预期的输入值。
  • 输入验证和清理: 清理用户输入中的特殊字符(例如 ../),这些字符可能允许路径遍历。
  • 使用文件映射数组:创建一个为允许的文件分配唯一索引的数组。每个索引对应于应用程序中的特定文件。
<?php

// Get the requested page from the query string
$page = isset($_GET['page']) ? $_GET['page'] : 'home';

// Define a whitelist of allowed pages
$allowed_pages = ['home', 'about', 'contact'];

// Check if the requested page is in the whitelist
if (in_array($page, $allowed_pages)) {
// Include the valid page
include($page . '.php');
} else {
// Redirect to a default page or display an error message
include('error.php');
}

?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>

<%
// Get the requested page from the query string
String page = request.getParameter("page");

// Define a whitelist of allowed pages
List<String> allowedPages = List.of("home", "about", "contact");

// Check if the requested page is in the whitelist
if (allowedPages.contains(page)) {
    // Include the valid page
    %><jsp:include page="<%= page %>.jsp" /><%
} else {
    // Redirect to a default page or display an error message
    response.sendRedirect("error.jsp");
}
%>
<!DOCTYPE html>
<html>
<head>
<title>Test file</title>
</head>
<body>
<!--#if expr='"$USER_LANGUAGE" =~ /^(en|fr|es)$/ -->
   <!--#include file="/path/to/allowed_languages/$USER_LANGUAGE"-->
<!--#else -->
   <p>Invalid language selection.</p>
<!--#endif -->
</body>
</html>

链接

标准

  • CWE_TOP_25:
    • CWE_22
  • GDPR:
    • ART_5
    • ART_32
  • PCI_STANDARDS:
    • REQ_6_5
    • REQ_11_3
  • SOC2_CONTROLS:
    • CC_2_1
    • CC_4_1
    • CC_6_1
    • CC_7_1
    • CC_7_2
    • CC_7_4
    • CC_7_5
  • HIPAA_CONTROLS:
    • SECURITY212
    • SECURITY213
    • SECURITY255