XXE

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"?>

XML DTD (Document Type Description)

Email.dtd

<!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)>
]>

Retrieving Email.dtd

XML Entities

External XML Entities

You can use SYSTEM or PUBLIC


Exploitation

Identifying

  1. Note which elements are being displayed, so we know which elements to inject into

  2. Try to display an entity (declaration is placed outside <root>)

Reading Sensitive Files

Note: In java, sometimes we can get directory listing when we input a directory instead of a file

Note: If the file we are referencing to contains XML chars (<, >, &), it might not display due to error so we need to encode it

Remote Code Execution

Note: encode <space> so we won't' break the XML. Also avoid using |, >, and {

Other XXE Attacks

DoS

Note: Entity self referencing doesn't work on modern web servers (i.e. apache)

Advance File Disclosure

php filter alternative. CDATA will make XML treat as raw data so special characters won't be a problem

Note: &joined; will not work here since XML forbids joining of internal dtd and external dtd. Thats why use XML parameter entities %. This will make xml treat all of them as external

Final Exploit

Note: In some modern web servers, we may not be able to read some files (like index.php), as the web server would be preventing a DOS attack caused by file/entity self-reference (i.e., XML entity reference loop)

Error-Based XXE

Not as reliable as previous methods

  • certain characters might break it

  • content length limitation

Identifying

  • Pass a malformed xml data <roo> instead of <root>

  • or use a &nonExistentEntity;

Exploitation

Assuming $nonExistentEntity; (or you can use a bad character in the reference file or a bad URI) throws an error:

Blind XXE (Out-of-band exfiltration)

xxe.dtd

PHP Server

Payload

Automated OOB Exfiltration

XXEinjectorarrow-up-right

Prevention

  • Avoid using outdated components

  • Using Safe XML Configurations

    • Disable referencing custom Document Type Definitions (DTDs)

    • Disable referencing External XML Entities

    • Disable Parameter Entity processing

    • Disable support for XInclude

    • Prevent Entity Reference Loops

  • Add proper exception handlings for error-based XXE

  • Others suggest avoid using XML data (SOAP API), instead use JSON or YAML

  • WAF

Last updated