πŸ“„βš™οΈ XML Entity Expansion (XEE): Unveiling the Hidden Vulnerability in Your Systems πŸ”’

Aditya Pandey
3 min readDec 12, 2024

--

XML Entity Expansion (XEE) is a type of attack that exploits XML processors that parse XML documents containing entity references. These entity references are placeholders that allow developers to define reusable content or external references within an XML document. However, when XML processors expand these entities, they can inadvertently trigger unintended consequences, leading to security vulnerabilities.

Description:

In an XEE attack, a malicious actor manipulates the XML input to include external entities that reference resources such as local files, URLs, or even network services. When the XML document is parsed by the server’s XML processor, it may inadvertently access or disclose sensitive information, execute arbitrary code, or perform unintended actions.

How it’s Done by Malicious Actors:
1. Crafting Malicious XML: The attacker crafts a malicious XML document containing entity references to external resources.

2. Submitting the XML: The attacker submits the malicious XML document to the target application or server that processes XML input.

3. Exploiting Vulnerabilities: The XML processor parses the document, expands the entities, and inadvertently accesses or executes the referenced external resources, leading to various security risks.

Countermeasures:

1. Disable External Entity Expansion

Description
Disable the expansion of external entities in the XML parser settings to prevent XEE attacks.

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class SecureXMLParser {
public static void main(String[] args) {
try {
// Disable external entity expansion
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

// Use the secure parser for further XML processing
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
}

2. Input Validation and Whitelisting

Description:
Validate and sanitize XML input to ensure that only known, safe entities are allowed, and reject any inputs containing external entity references.Code Snippet (Python - Using defusedxml library):

from defusedxml.ElementTree import parse

def parse_secure_xml(xml_string):
try:
# Parse XML with defusedxml library which disables external entities by default
tree = parse(xml_string)
# Process the parsed XML tree
return tree
except Exception as e:
print("Error parsing XML:", e)

3. Use a Secure XML Parser:

Description:
Utilize a secure XML parser library that inherently mitigates XEE vulnerabilities by disabling external entity expansion and enforcing strict parsing rules.

Code Snippet (Python - Using lxml library):

from lxml import etree

def parse_secure_xml(xml_string):
try:
# Parse XML with lxml library which disables external entities by default
parser = etree.XMLParser(resolve_entities=False)
tree = etree.fromstring(xml_string, parser=parser)
# Process the parsed XML tree
return tree
except etree.XMLSyntaxError as e:
print("Error parsing XML:", e)

4. XML Schema Validation:

Description
Validate XML documents against an XML schema (XSD) to ensure that they conform to the expected structure and do not contain unexpected entities.

Code Snippet (Java - Using javax.xml.validation.Validator):

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

public class XMLValidator {
public static void main(String[] args) {
try {
// Load XML schema
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File("schema.xsd"));

// Create a validator with the schema
Validator validator = schema.newValidator();

// Validate XML document
validator.validate(new StreamSource(new File("document.xml")));
System.out.println("XML is valid.");
} catch (SAXException | IOException e) {
System.out.println("XML is not valid: " + e.getMessage());
}
}
}

Summary

XML Entity Expansion (XEE) attacks exploit XML processors by including external entity references in XML documents, leading to security vulnerabilities. To mitigate XEE attacks, developers should disable external entity expansion in XML parsers and implement input validation and whitelisting to ensure that only safe inputs are processed. Additionally, using secure XML processing libraries and keeping software dependencies up-to-date can further enhance protection against XEE attacks.

--

--

Aditya Pandey
Aditya Pandey

Written by Aditya Pandey

Cyber security researcher | GNU / Linux Modder | GameDev

No responses yet