# 文件包含

## 介绍

文件包含漏洞包括：本地文件包含、远程文件包含、目录遍历

## 原因

后端代码对服务器接受的请求没有经过严格的清理或验证，当用户输入可以逃过后端验证的请求时，该请求被相关的函数所处理，从而导致漏洞发生

## 危害

* 如果是本地文件包含，用户很有可能会去读取敏感数据，或者利用某种方式去获取 SHELL
* 如果是远程文件包含，用户可能会选择获取 SHELL、XSS、DDOS、获取敏感数据

## 攻击

### 1. 目录遍历

`攻击者可以查看到网站的目录结构，从而可以定位和访问到应用程序根目录之外的文件`

### 2. 本地文件包含

`配合文件上传的相关知识进行利用`

这类漏洞通常是开发人员缺乏安全意识导致的，比如说 PHP 中的 include、require、include\_once、require\_once 等函数，通常会导致易受攻击的 WEB 应用程序

举个例子：

常见的 WEB 应用程序中通常会设置多个版本并且放在同一个目录中：

```shell
|- language
	|- zh.php
	|- en.php
```

网站的后端处理代码为：

```php
<?PHP 
	include($_GET["lang"]);
?>
```

正常情况下，我们使用的请求为 <http://webapp.thm/index.php?lang=EN.php> 但是当我们将请求改为 <http://webapp.thm/index.php?lang=../../../etc/passwd> 时就会导致 passwd 文件读取

### 3. 远程文件包含

远程文件包含 (RFI) 是一种将远程文件包含到易受攻击的应用程序中的技术。与 LFI 一样，RFI 发生在不正确地清理用户输入时，允许攻击者将外部 URL 注入include函数。RFI 的一项要求是`allow_url_fopen`选项需要打开

> 远程文件包含的危害远高于本地文件包含，因为远程攻击漏洞运行攻击者在服务器上获取远程命令执行，进而造成
>
> * 敏感信息泄露
> * XSS 攻击
> * DDOS

外部服务器必须与应用程序服务器通信才能成功进行 RFI 攻击，攻击者在其服务器上托管恶意文件。然后通过HTTP请求将恶意文件注入到include函数中，恶意文件的内容在易受攻击的应用服务器上执行。

![img](https://img-blog.csdnimg.cn/img_convert/2c39b2b22391819e2f552ca503e97016.png)

## 防御

* 将系统服务更新为最新版本
* 关闭一些配置如 PHP 的 `allow_url_fopen`
* 设置 WAF
* 必要情况下对用户的所有输入都要经过验证
* 对文件名和位置设置白名单和黑名单


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jtz.gitbook.io/web-security/web-shen-tou-ce-shi/wen-jian-bao-han.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
