XPath Injection

Summary

XPath is a language that has been designed and developed primarily to address parts of an XML document. In XPath injection testing, we test if it is possible to inject XPath syntax into a request interpreted by the application, allowing an attacker to execute user-controlled XPath queries. When successfully exploited, this vulnerability may allow an attacker to bypass authentication mechanisms or access information without proper authorization.

Web applications heavily use databases to store and access the data they need for their operations. Historically, relational databases have been by far the most common technology for data storage, but, in the last years, we are witnessing an increasing popularity for databases that organize data using the XML language. Just like relational databases are accessed via SQL language, XML databases use XPath as their standard query language.

Since, from a conceptual point of view, XPath is very similar to SQL in its purpose and applications, an interesting result is that XPath injection attacks follow the same logic as SQL Injection attacks. In some aspects, XPath is even more powerful than standard SQL, as its whole power is already present in its specifications, whereas a large number of the techniques that can be used in a SQL Injection attack depend on the characteristics of the SQL dialect used by the target database. This means that XPath injection attacks can be much more adaptable and ubiquitous. Another advantage of an XPath injection attack is that, unlike SQL, no ACLs are enforced, as our query can access every part of the XML document

Basic Syntax

An attack technique known as XPath Injection is utilized to take advantage of applications that form XPath (XML Path Language) queries based on user input to query or navigate XML documents.

Nodes Described

Expressions are used to select various nodes in an XML document. These expressions and their descriptions are summarized below:

  • nodename: All nodes with the name "nodename" are selected.

  • /: Selection is made from the root node.

  • //: Nodes matching the selection from the current node are selected, regardless of their location in the document.

  • .: The current node is selected.

  • ..: The parent of the current node is selected.

  • @: Attributes are selected.

XPath Examples

Examples of path expressions and their results include:

  • bookstore: All nodes named "bookstore" are selected.

  • /bookstore: The root element bookstore is selected. It's noted that an absolute path to an element is represented by a path starting with a slash (/).

  • bookstore/book: All book elements that are children of bookstore are selected.

  • //book: All book elements in the document are selected, irrespective of their location.

  • bookstore//book: All book elements that are descendants of the bookstore element are selected, no matter their position under the bookstore element.

  • //@lang: All attributes named lang are selected.

Utilization of Predicates

Predicates are used to refine selections:

  • /bookstore/book[1]: The first book element child of the bookstore element is selected. A workaround for IE versions 5 to 9, which index the first node as [0], is setting the SelectionLanguage to XPath through JavaScript.

  • /bookstore/book[last()]: The last book element child of the bookstore element is selected.

  • /bookstore/book[last()-1]: The penultimate book element child of the bookstore element is selected.

  • /bookstore/book[position()<3]: The first two book elements children of the bookstore element are selected.

  • //title[@lang]: All title elements with a lang attribute are selected.

  • //title[@lang='en']: All title elements with a "lang" attribute value of "en" are selected.

  • /bookstore/book[price>35.00]: All book elements of the bookstore with a price greater than 35.00 are selected.

  • /bookstore/book[price>35.00]/title: All title elements of the book elements of the bookstore with a price greater than 35.00 are selected.

Handling of Unknown Nodes

Wildcards are employed for matching unknown nodes:

  • *: Matches any element node.

  • @*: Matches any attribute node.

  • node(): Matches any node of any kind.

Further examples include:

  • /bookstore/*: Selects all the child element nodes of the bookstore element.

  • //*: Selects all elements in the document.

  • //title[@*]: Selects all title elements with at least one attribute of any kind.

Example

Access the information

Identify & stealing the schema

Authentication Bypass

Example of queries:

OR bypass in user and password (same value in both)

Abusing null injection

Double OR in Username or in password (is valid with only 1 vulnerable field)

IMPORTANT: Notice that the "and" is the first operation made.

String extraction

The output contains strings and the user can manipulate the values to search:

Blind Explotation

Get length of a value and extract it by comparisons:

Python Example

Read file

OOB Exploitation

How to Test

The XPath attack pattern was first published by Amit Klein [1] and is very similar to the usual SQL Injection. In order to get a first grasp of the problem, let's imagine a login page that manages the authentication to an application in which the user must enter his/her username and password. Let's assume that our database is represented by the following XML file:

An XPath query that returns the account whose username is "gandalf" and the password is "!c3" would be the following:

If the application does not properly filter user input, the tester will be able to inject XPath code and interfere with the query result. For instance, the tester could input the following values:

Looks quite familiar, doesn't it? Using these parameters, the query becomes:

As in a common SQL Injection attack, we have created a query that always evaluates to true, which means that the application will authenticate the user even if a username or a password have not been provided. And as in a common SQL Injection attack, with XPath injection, the first step is to insert a single quote (') in the field to be tested, introducing a syntax error in the query, and to check whether the application returns an error message.

If there is no knowledge about the XML data internal details and if the application does not provide useful error messages that help us reconstruct its internal logic, it is possible to perform a Blind XPath Injection attack, whose goal is to reconstruct the whole data structure. The technique is similar to inference based SQL Injection, as the approach is to inject code that creates a query that returns one bit of information. Blind XPath Injection is explained in more detail by Amit Klein in the referenced paper.

Last updated