4. XXE - Overview, and prevention

XML data taken from user controlled input without sanitization or safe parsing - Uses XML features to perform malicious actions

XXE vulns mega dangerous to webapp+backend -- disclosing sensitive files, shutting backend down top10 sec risk according to OWASP

XML - Extensible Markup Language

Common markup language similar to HTML/SGML

Each element denoted by a tag - first ele called root element + other eles called child elements.

example XML doc representing email doc structure:

<?xml version="1.0" encoding="UTF-8"?>
<email>
  <date>01-01-2022</date>
  <time>10:00 am UTC</time>
  <sender>john@inlanefreight.com</sender>
  <recipients>
    <to>HR@inlanefreight.com</to>
    <cc>
        <to>billing@inlanefreight.com</to>
        <to>payslips@inlanefreight.com</to>
    </cc>
  </recipients>
  <body>
  Hello,
      Kindly share with me the invoice for the payment made on January 1, 2022.
  Regards,
  John
  </body> 
</email>

The above example shows some of the key elements of an XML document, like:

Key Definition Example
Tag The keys of an XML document, usually wrapped with (</>) characters. <date>
Entity XML variables, usually wrapped with (&/;) characters. &lt;
Element The root element or any of its child elements, and its value is stored in between a start-tag and an end-tag. <date>01-01-2022</date>
Attribute Optional specifications for any element that are stored in the tags, which may be used by the XML parser. version="1.0"/encoding="UTF-8"
Declaration Usually the first line of an XML document, and defines the XML version and encoding to use when parsing it. <?xml version="1.0" encoding="UTF-8"?>

Some chars used part of doc structure  like <, >, &, or ". if we need to use them in an XML document - replace them with their corresponding entity references, for example &lt; , &gt; , &amp; and &quot; -

Comments are allowed with <!-- and --> (like HTML)

XML DTD (Document Type Definition)

Allows validation of xml docs against pre-defined doc structure. Can be defined in doc itself; or an external file.

Example DTD for the above email structure:

<!DOCTYPE email [
  <!ELEMENT email (date, time, sender, recipients, body)>
  <!ELEMENT recipients (to, cc?)>
  <!ELEMENT cc (to*)>
  <!ELEMENT date (#PCDATA)>
  <!ELEMENT time (#PCDATA)>
  <!ELEMENT sender (#PCDATA)>
  <!ELEMENT to  (#PCDATA)>
  <!ELEMENT body (#PCDATA)>
]>

DTD declaring root email ele with ELEMENT type declaration + denoting its' child elements. After which each child ele is also declared, and some of those child eles also have children, while other may only contain raw data (see #PCDATA)

This DTD can be placed in the XML document itself right after XML Declaration in the first line. Otherwise - stored in external file, and referenced in the xml doc with SYSTEM

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE email SYSTEM "email.dtd">

It's also possible to reference a DTD via URL:

<!DOCTYPE email SYSTEM "http://inlanefreight.com/email.dtd">

relatively similar to how HTML documents define/reference JS / CSS scripts.

XML Entities

Possible to define custom entities (XML variables) in XML DTDs , to allow refactoring of variables + reduce repetitive data. This is done with ENTITY k/w followed by entity name+value.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE email [
  <!ENTITY company "Inlane Freight">
]>

Once entity defined - referenced later with &[entity]; i.e. above: &company;

When entity is referenced it is replaced with its' value by the XML parser. We can reference EXTERNAL XML ENTITIES with the SYSTEM keyword followed by the external entities' path:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE email [
  <!ENTITY company SYSTEM "http://localhost/company.txt">
  <!ENTITY signature SYSTEM "file:///var/www/html/signature.txt">
]>

PUBLIC k/w can be used instead of SYSTEM for loading ext. resources, which is used with publicly declared entities and standards such as language code lang="en" - in this module we're using SYSTEM but we should be able to use either in most cases

This works similar to internal xml entities defined in docs. When we ref. ext. entity (i.e. &signature; ), parser will replace entity with its value stored in the external file.

When the XML file is parsed server-side, in cases like SOAP (XML) APIs or web forms, then an entity can reference a file stored on the backend which may eventually be disclosed to us when we reference the entity.

XXE Prevention

While other input validation web vulnerabilities are usually prevented through secure coding practices (e.g., XSS, IDOR, SQLi, OS Injection), this is not entirely necessary to prevent XXE vulnerabilities. This is because XML input is usually not handled manually by the web developers but by the built-in XML libraries instead.

For example, PHP's libxml_disable_entity_loader function is deprecated since it allows a developer to enable external entities in an unsafe manner, which leads to XXE vulnerabilities.

Common code editors i.e. vscode will highlight this function as deprecated and warn us against using it.

Detailed report of vulnerable XML libraries with recommendations on updating them to use safe functions. https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#php

In addition to updating XML libraries we shold also update components that parse XML input

Any document / file processor that may perform XML parsing like SVG image processors or PDF document processors may also be vulnerable to XXE vulns and should be updated.

These issues are not exclusive to XML libraries. The same applies to all other web components like outdated Node modules. Common package managers and code editors will also notify webdevs on the use of outdated components + suggest alternatives.

Using latest XML libraries and webdev components greatly help reduce various webvulns.

Safe XML Configurations

Other than using the latest XML libraries, certain XML configurations for web applications can help reduce the possibility of XXE exploitation. These include:

We also saw error-based XXE exploitation - so we should always have proper exception handling in our web applications and ALWAYS disable displaying runtime errors

Such configurations should be another layer of protetion if we miss updating some xml libraries and should also prevent xxe exploits - however, still using vuln libs is a no-no

Recommend using other formats such as JSON or YAML

WAFs are another layer of defense against XXE. Never fully rely on WAFs - they're bypassable.