Classic in-band XXE file disclosure
Classic XXE file disclosure is the textbook XML External Entity attack: you declare an external entity that points at a local file with the file:// scheme, reference it inside an element the application reflects in its response, and the vulnerable parser resolves the entity and returns the file contents.
- Target formats
- Plain XMLSOAPREST XML bodies
- Affected parsers
- Java DOM/SAXPHP libxml (legacy).NET XmlDocument (legacy)Python lxml/sax
- Impact
- Local file disclosureConfig/secret theftSource code read
How it works
An XML document can declare entities in its Document Type Definition (DTD). An external general entity such as <!ENTITY xxe SYSTEM "file:///etc/passwd"> tells the parser to fetch an external resource and substitute its contents wherever &xxe; appears.
When the application parses attacker-controlled XML with a parser that resolves external entities, and then reflects any parsed value back in a response, the file contents are returned to the attacker. This is the in-band (classic) case: the data comes straight back, no out-of-band channel needed.
Which parser configuration makes it work
The parser must resolve external general entities and the application must echo a parsed value back to the client. This is the default in older libxml (<2.9), legacy .NET XmlDocument/XmlTextReader, and any Java factory left at defaults without disallow-doctype-decl.
Representative payloads
Replace callback hosts and target paths with your own authorized test values.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
<!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<foo>&xxe;</foo>Replace <foo> with whichever element the application reflects.
<!DOCTYPE foo [
<!ENTITY xxe SYSTEM "file:///C:/Windows/win.ini">
]>
<foo>&xxe;</foo><!DOCTYPE foo [
<!ENTITY xxe SYSTEM "file:///etc/">
]>
<foo>&xxe;</foo>Some Java versions return a directory listing for a file:// directory URL.
Variants
Reflected into an existing field
Rather than adding a new element, inject the entity reference into a value the application already returns (a name, search term or error echo) so the parsed file contents surface in the normal response.
Detection
From the tester side, submit a benign external entity that points at a resource you control and watch for the callback, or request a file that is always present (file:///etc/hostname) and look for its contents in the response. From the defender side, inspect XML request bodies for <!DOCTYPE and <!ENTITY ... SYSTEM declarations, and alert on application processes opening unexpected files (/etc/passwd, private keys) right after an XML request.
- Inbound XML containing <!DOCTYPE and SYSTEM/PUBLIC keywords
- Application host making file:// or outbound reads during XML parsing
- Response bodies that contain file contents such as root:x:0:0
Remediation
Disable DOCTYPE/DTD processing in the XML parser — this removes the entire attack class. In most stacks that means one factory feature: set disallow-doctype-decl to true (Java/Xerces), DtdProcessing = Prohibit and XmlResolver = null (.NET), or parse with defusedxml / an lxml parser configured with resolve_entities=False, no_network=True, load_dtd=False (Python). If some legitimate input genuinely needs a DTD, instead disable external general and parameter entities and external DTD loading, and never echo raw parsed values back to the client.
Fix checklist
- Set the parser to reject documents that contain a DOCTYPE declaration
- If DTDs must be allowed, disable external general + parameter entities and external DTD loading
- Null the entity/URL resolver so file:// and http:// are never fetched
- Validate against an allow-list schema after parsing, not the raw entity output
FAQ
- Why does the file read sometimes fail silently?
- Files with XML metacharacters (< or &) break the enclosing document, so the parser errors instead of returning the data. Use a PHP base64 filter or an out-of-band/error-based channel for those files.