Description
XPath is a type of query language that describes how to locate specific elements (including attributes, processing instructions, etc.) in an XML docu ment. Since it is a query language, XPath is somewhat similar to Structured Query Language (SQL), however, XPath is different in that it can be used to reference almost any part of an XML docu ment without access control restrictions. In SQL, a "user" (which is a term undefined in the XPath/XML context) may be restricted to certain databases, tables, columns, or queries. Using an XPATH Injection attack, an attacker is able to modify the XPATH query to perform an action of his choosing.
Blind XPath Injection attacks can be used to extract data from an application that embeds user supplied data in an unsafe way. When input is not properly sanitized, an attacker can supply valid XPath code that is executed. This type of attack is used in situations where the attacker has no knowledge about the structure of the XML docu ment, or perhaps error message are suppressed, and is only able to pull once piece of information at a time by asking true/false questions(booleanized queries), much like Blind SQL Injection.
Examples
The attacker may mount a successful attack using two methods: Boolenization and XML Crawling. By adding to the XPath syntax, the attacker uses additional expressions (replacing what the attacker entered in the place of the injection).
Boolenization
Using the "Boolenization" method the attacker may find out if the given XPath expression is True or False. Let's assume that the aim of the attacker is to log in to an account in a web application. A Successful log in would return "True" and failed log in attempt would return "False". Only a small portion of the information is targeted via the analyzed character or number. When the attacker focuses on a string he may reveal it in its entirety by checking every single character within the class/range of characters this string belongs to.
Using a string-length(S) function, where S is a string, the attacker may find out the length of this string. With the appropriate number of substring(S,N,1) function iterations, where S is a previously mentioned string, N is a start character, and "1" is a next character counting from N character, the attacker is able to enumerate the whole string.
Function:
To get to know the XML docu ment structure the attacker may use:
C#:
then the attacker should inject the following code:
The XPath syntax may remind you of common SQL Injection attacks but the attacker must consider that this language disallows commenting out the rest of expresssion. To omit this limitation the attacker should use OR expressions to vo id all expressions, which may disrupt the attack.
Because of Boolenization the number of queries, even within a small XML docu ment, may be very high (thousands, houndred of thousands and more). That is why this attack is not conducted manually. Knowing a few basic XPath functions, the attacker is able to write an application in a short time which will rebuild the structure of the docu ment and will fill it with data by itself.
Source: OWASP
XPath is a type of query language that describes how to locate specific elements (including attributes, processing instructions, etc.) in an XML docu ment. Since it is a query language, XPath is somewhat similar to Structured Query Language (SQL), however, XPath is different in that it can be used to reference almost any part of an XML docu ment without access control restrictions. In SQL, a "user" (which is a term undefined in the XPath/XML context) may be restricted to certain databases, tables, columns, or queries. Using an XPATH Injection attack, an attacker is able to modify the XPATH query to perform an action of his choosing.
Blind XPath Injection attacks can be used to extract data from an application that embeds user supplied data in an unsafe way. When input is not properly sanitized, an attacker can supply valid XPath code that is executed. This type of attack is used in situations where the attacker has no knowledge about the structure of the XML docu ment, or perhaps error message are suppressed, and is only able to pull once piece of information at a time by asking true/false questions(booleanized queries), much like Blind SQL Injection.
Examples
The attacker may mount a successful attack using two methods: Boolenization and XML Crawling. By adding to the XPath syntax, the attacker uses additional expressions (replacing what the attacker entered in the place of the injection).
Boolenization
Using the "Boolenization" method the attacker may find out if the given XPath expression is True or False. Let's assume that the aim of the attacker is to log in to an account in a web application. A Successful log in would return "True" and failed log in attempt would return "False". Only a small portion of the information is targeted via the analyzed character or number. When the attacker focuses on a string he may reveal it in its entirety by checking every single character within the class/range of characters this string belongs to.
Using a string-length(S) function, where S is a string, the attacker may find out the length of this string. With the appropriate number of substring(S,N,1) function iterations, where S is a previously mentioned string, N is a start character, and "1" is a next character counting from N character, the attacker is able to enumerate the whole string.
Kod:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<user>
<login>admin</login>
<password>test</password>
<realname>SuperUser</realname>
</user>
<user>
<login>rezos</login>
<password>rezos123</password>
<realname>Simple User</realname>
</user>
</data>
- string.stringlength(//user[position()=1]/child::node()[position()=2]) returns the length of the second string of the first user (8),
- substring((//user[position()=1]/child::node()[position()=2),1,1) returns the first character of this user ('r').
To get to know the XML docu ment structure the attacker may use:
- count(expression)
Kod:count(//user/child::node()
- stringlength(string)
Kod:string-length(//user[position()=1]/child::node()[position()=2])=6
- substring(string, number, number)
Kod:substring((//user[position()=1]/child::node()[position()=2]),1,1)="a"
C#:
Kod:
String FindUser;
FindUser = "//user[login/text()='" + Request("Username") + "' And
password/text()='" + Request("Password") + "']";
Kod:
Username: ' or substring((//user[position()=1]/child::node()[position()=2]),1,1)="a" or ''='
Because of Boolenization the number of queries, even within a small XML docu ment, may be very high (thousands, houndred of thousands and more). That is why this attack is not conducted manually. Knowing a few basic XPath functions, the attacker is able to write an application in a short time which will rebuild the structure of the docu ment and will fill it with data by itself.
Source: OWASP