About Me

Showing posts with label XPath Injection. Show all posts
Showing posts with label XPath Injection. Show all posts

Friday, 13 April 2012

XPath - Predicates

XPath - Predicates

We have learned how to select elements and attributes in an XML document, but we haven't learned how to be eliminate unwanted items. This lesson will teach you how to impose restrictions in your XPath expressions using predicates.


We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - Being Choosy with Predicates

Imagine that we wanted to select all the products from lemonade2.xml that had an amount greater than 15. With the current knowledge you have, this would be impossible and would require programming outside of XPath expression to solve this problem.
The closest we could get would be to select all the products:

XPath Expression:

inventory/*/*
However, with the use of XPath predicates, the problem of selecting only those with an amount greater than 15 is easy to conquer.
A predicate is similar to an If/Then statement. If our predicate is TRUE, then the element will be selected. If the predicate is FALSE, it will be excluded. What would be our predicate for this example?
Well, we want to select those amount elements that are greater than 15, so in English, our predicate would be:
  • predicate: greater than 15
Now, we just need to know how to convert this English into an XPath predicate!

XPath - Predicates are in Brackets [ & ]

An XPath predicate is contained within square brackets [], and comes after the parent element of what will be tested!
Because we want to test each product's amount element, we might create a separate predicate for each product: lemonade, pop and chips.
To start off simply, let's just do lemonade first using an absolute path.

Predicate XPath Expression:

inventory/drink/lemonade[amount>15]
Notice that the form of a predicate is:
  • parent[child someTestHere]
If we wanted to do this predicate on all the products we would use the wildcard feature to select inventories children and then select drink and snack children.

Predicate XPath Expression:

inventory/*/*[amount>15]
Question: Which products will be selected?
Answer: lemonade and chips are selected because they both have amount elements greater than 15.

XPath - Predicates with Attributes

Besides testing the values of elements, you can also use predicates to check the values of attributes. The form pretty much the same as before, except the attribute belongs to the element before the predicate.
  • element[@element'sAttribute someTestHere]
Our XML document contains an attribute supplier which describes where the item originated from. With a predicate, we could find all the products that were purchased from the store.

Predicate XPath Expression:

inventory/*/*[@supplier='store']
This XPath expression would select the elements pop and chips since they were from the store.

XPath - Predicate Current Element .

Sometimes you don't want to select the child element or attribute, but rather, the current element. XPath lets you refer to the current element with the period ".", when placed within the predicate.
If we wanted to select the amount of pop where the amount was less than 20, we would need to use a period.

Predicate XPath Expression:

inventory/drink/pop/amount[. < 20]
Whenever you need to select an element and use a predicate on that same element you will need to use a period "." to access that element's value.

XPath - Vertical Bar | (Pipe)

XPath - Vertical Bar | (Pipe)

XPath makes use of the character "|", which we will be referring to as pipe from here on out. The pipe character is a way of combining two or more expressions into one. Depending on where you are using XPath, this ability to combine multiple expressions into one may be useful.


This lesson will teach you how to use XPath's combine feature, "|".
We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - Combining Two Expressions with |

When you need to select multiple things with one expression, chances are you will have to use the pipe character. In our lemonade2.xml document, we might want to select the children of chips and the children of pop. To do this, we would use the pipe character "|".

Combining XPath Expression:

inventory/snack/chips/* | inventory/drink/pop/*
This expression combines two expressions into one and will select every element that matches either expression.

XPath - Combining Multiple Expressions with |

Although you probably won't use this feature very often, it is possible to combine as many expressions as you want with the pipe character. Here is an example that selects all of the children elements of chips, using |.

XPath - Wildcard *

XPath - Wildcard *

A wildcard is a special character used in programming to include everything in your selection. Up to this point we have been specifying elements by their name, but with the use of the wildcard * we can actually select more than one element at a time.

This lesson will teach you how to use XPath's wildcard, * .
We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - Match Anything with *

The wildcard is another great way to save time. Often, you will want to select every child element of certain element, which would normally mean a great deal of typing. However, the wildcard can select every child element at once because it automatically matches everything possible! If you play cards, you can think of the wildcard as being the joker of the deck.
If we wanted to select each child element of chips, then we would normally have to do something like...

XPath Expression:

inventory/snack/chips/price
inventory/snack/chips/amount
inventory/snack/chips/calories
However, the asterisk character * matches everything and those three expressions could be replaced with one that selects all the children of chips using a wilcard.

Wildcard XPath Expression:

inventory/snack/chips/*

XPath - Advanced Wildcard Usage

Imagine that we wanted to select every child element of each of our products: lemonade, pop and chips. Using normal XPath expressions, it would take a huge amount of expressions, which we have labored to show you.

XPath Expression:

inventory/drink/lemonade/price
inventory/drink/lemonade/amount
inventory/drink/pop/price
inventory/drink/pop/amount
inventory/snack/chips/price
inventory/snack/chips/amount
inventory/snack/chips/calories
However, with the wildcard we could instead decide to select every child of inventory: drink and snack. Then select every child of those two elements: lemonade, pop and chips. And finally we could select each child of those three elements: price x 3, amount x 3 and calories x 1.

Wildcard XPath Expression:

inventory/*/*/*
This may be a little confusing to look at, so we have broken it down for you.
  1. We specified the root element inventory
  2. We then selected every child of inventory with a wildcard: inventory/*
  3. The elements currently selected were drink and snack
  4. We selected all the children of snack and chips with another wildcard: inventory/*/*
  5. The elements currently selected were lemonade, pop and chips
  6. We selected all the children of lemonade, pop and chips with another wildcard: inventory/*/*/*

XPath - Parent ..

XPath - Parent ..

This lesson will teach you how to select the parent of an element in your XPath expressions.

We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - Selecting a Parent with ..

Normally in an XPath expression, you have a string of elements separated by a slash. However, if you put two periods ".." where an element would normally go you can select the parent element. The element to the left of the double period will have its parent selected.
For example, if we wanted to select all of the product elements, lemonade, pop, and chips we would have to do a few expressions.

Normal XPath Expression:

inventory/drink/lemonade
inventory/drink/pop
inventory/snack/chips
However, we could instead select the parent element of amount to get the same three product elements. This is because lemonade, pop and chips all have a child amount.

Parent XPath Expression:

amount/..
This expression uses a relative path location and selects all of the amount elements in our XML document. It then uses the parent sequence ".." to select the parent element of each amount element. In our XML document, there are three amount elements, and their parents are lemonade, pop and chips!

XPath - Descendants "//"

XPath - Descendants "//"

As you know, the slash "/" is used to separate parent from child in our XPath expressions. However, when you put two slashes together "//", it does something completely different. Two slashes are used to select the descendants of an element.


This lesson will teach you how to advance your XPath knowledge and save time with the descendant double-slash "//".
We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - Choosing Descendants

Although relative and absolute paths will let you select anything you want in your XML document, they can sometimes be quite time-consuming. If you wanted to select each price element that was a descendant of drink, you would have to write two absolute XPath expressions.

Absolute XPath Expression:

inventory/drink/lemonade/price
inventory/drink/pop/price
Or you could use the relative XPath expressions.

Relative XPath Expression:

drink/lemonade/price
drink/pop/price
Although this isn't that much work, imagine if you had fifty different types of drinks, each with their own price. Not only that, but you would have to write additional XPath expressions as more drinks were added and taken away from the XML document!
The "//" sequence will save you a lot of time. If you wanted to select every price element that has a drink ancestor, this expression would get the job done.

Relative Location with Descendants XPath Expression:

drink//price
This powerful little expression has a lot going on, so let's look at it in more detail to ensure you understand how it works.
  1. We start our expression with the relative location drink.
  2. We tell XPath that we want to select from the descendants of drink with "//"
  3. We specify that we specifically want to select the price descendant.
  4. We pat ourselves on the back for getting the job done efficiently!
With the use of the descendant sequence "//", you can save yourself a lot of time. The hard part of XPath is the sheer number of solutions for a given problem, but with some practice, you'll be able to choose which set of tricks will get the job done in the best way.

XPath - Relative Location

XPath - Relative Location

Now for something completely different! XPath expressions can also be created using relative location paths. Instead of starting at the root element, you can simply reference the element you want and go from there.

We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - Relative Location Means More Options

Expressions that use relative location paths open up a whole new set of options for you. You can do things like select every amount element in an XML document.
To construct an XPath expression to select every amount element, we only need to type one word.

XPath Expression:

amount
This piece of XPath will select all of the amount elements in our lemonade2 XML document. If you were trying to find the total items in stock, this type of XPath expression would be perfect!

XPath - Relative Can Have Children Too

Just as an absolute path is often a string of children elements, a relative path can follow the same type of form. If you wanted to select every price element that had a chips parent, you would use a relative location expression like the one below:

XPath Expression:

chips/price
This expression first select all the chips elements in the document (there's just one) and then chooses the child element price.
Understanding the difference between absolute and relative position paths is the key to creating appropriate expression to select the correct XML items!

XPath - Absolute Location

XPath - Absolute Location

Up to this point, we have been selecting elements by giving the full path. We start with the the root element and end with the desired descendant element. This method of using the complete path is referred to as absolute location path, and it is useful for selecting a specific element.


There is another method, relative location path, which is useful when you want to select many similar items (more on this later). This lesson will provide an overview of absolute location paths in XPath.
We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - Start with the Root Element

When you are using the absolute method, your expression must start with the root element of your XML document (inventory in our example). If you only wanted to select the root element, then you're done.

XPath Expression:

inventory
However, if you wanted one of the sub-elements, like lemonade, then you will have to create a string of children elements from the root node to the desired node.

XPath Expression:

inventory/drink/lemonade

XPath - Attribute

XPath - Attribute

You have already learned how to select any element in an XML document, but how would you get that element's attribute? Those attributes values are within your reach once you learn how to use @ !


We will be using our lemonade2.xml file, which you can download.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - @ is for Attribute!

After you have figured out how to select an element in your XML document, just take it one step further to get the attribute.
At the end of your XPath expression, which is normally the element you want to select, add the at sign "@" plus the name of the attribute you wish to select. The following XPath expression selects chips element.

XPath Expression:

inventory/snack/chips
If we wanted to select the supplier attribute of chips, we would add "@supplier" to our expression.

XPath Expression:

inventory/snack/chips@supplier
If we wanted to select the supplier attribute of pop, the XPath expression would have to be changed slightly.

XPath Expression:

inventory/drink/pop@supplier

XPath - Element

XPath - Element

The most common usage of XPath is for selecting elements in an XML document. This lesson will provide a walkthrough of selecting many different elements, at different levels, in the XML Tree.


XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - A Path of Elements

When trying to reach a specific element in your XML document, you often have to string together many elements to get there. In XPath you reference an element by using its name. For example, the root element's name in lemonade2.xml is inventory, so we would type the following to reference it.

XPath Expression:

inventory
If we wanted to reference the drink element of inventory, we would have to string together these two elements, starting with the root element, inventory.

XPath Expression:

inventory/drink
Notice: The elements are separated by a slash "/".

XPath - A Path of Children

If we wanted to select the price of pop, we would have to make an even longer path of elements to reach our final destination.

XPath Expression:

inventory/drink/pop/price
Notice: Each element added to this XPath expression is the child of the element before it.
  • drink is the child of inventory
  • pop is the child of drink
  • price is the child of pop
You can use this knowledge of parent-child relationships in an XPath expression to make sure your XPath is written properly.

XPath - Expressions

XPath - Expressions

XPath can locate any type of information in an XML document with one line of code. These one liners are referred to as "expressions," and every piece of XPath that you write will be an expression. Just to make it crystal clear, here's the definition of an expression as it relates to our usage.
  • expression: In programming, a line of source code that returns a value when executed. ~Computer Desktop Encyclopedia
An XPath expression is exactly that; it's a line of code that we use to get information from our XML document.

XPath - Our Sample XML File

We have slightly modified our lemonade XML document to make it more interesting. Our new XML document is lemonade2.xml, and it has had a couple new attributes and elements added to it, which we have highlighted.

XML Code, lemonade2.xml:

<inventory>
 <drink>
  <lemonade supplier="mother" id="1">
   <price>$2.50</price>
   <amount>20</amount>
  </lemonade>
  <pop supplier="store" id="2">
   <price>$1.50</price>
   <amount>10</amount>
  </pop>
 </drink>

 <snack>
  <chips supplier="store" id="3">
   <price>$4.50</price>
   <amount>60</amount>
   <calories>180</calories>
  </chips>
 </snack>

</inventory>

XPath - A Simple XPath Expression

An XPath expression describes the location of an element or attribute in our XML document. By starting at the root element, we can select any element in the document by carefully creating a chain of children elements. Each element is separated by a slash "/".
For example, if we wanted to know the number of chips we have in stock(element amount) in lemonade2.xml, the XPath expression would be:

XPath Expression:

inventory/snack/chips/amount
Let's break down this expression into easily digestible chunks:
  1. We specified the root node, inventory, at the beginning of our XPath expression.
  2. We chose inventory's child element, snack, because it is on the pathway toward our goal, "number of chips in stock".
  3. We chose snack's child element chips.
  4. Finally, we chose chips' child element amount, or in other words, "number of chips in stock".
If it helps to think of this XPath expression graphically, this is the pathway we followed from the root node inventory down to our desired element, amount.

XPath Trace:

XPath - Getting Started

Now that you've got your feet wet, it is time to learn the different types of XPath expressions and how to select those desired XML elements with XPath!

XPath - Introduction

XPath - Introduction

As you know, XML was created to be a self-describing markup format. As XML matured, new XML related creations were popping up. Although you could create a nicely structured document with XML, there didn't seem to be an easy way to find information inside the document.

XML documents can be thought of as a Tree Structure, made up of parent, child, and sibling relationships. Because of this very logical layout of an XML document it seems like there should be a standard way to find information.

XPath - Finding Information

XPath is the solution to finding information in an XML document. XPath uses expressions to find elements, attributes, and other information in your XML. If you have an XML document that contained a bunch of your favorite books, each with author children elements, you can use a one line XPath expression to find all the authors of your favorite books!
If you are thinking seriously about learning XML and its important related technologies, XPath is one area you can't pass up!

XPath - Used in XSLT

The most common place people might see XPath expressions are in XSLT (Extensible Stylesheet Language Transformation). We will be teaching XSLT after we have taught you the steppingstone knowledge of XPath.
XPointer and XQuery are both new XML-related technologies that extend or borrow from XPath. XPath truly is a keystone for XML related learning!

Thursday, 12 April 2012

XPath Injection Short Note

Similar to SQL Injection, XPath Injection attacks occur when a web site uses user-supplied information to construct an XPath query for XML data. By sending intentionally malformed information into the web site, an attacker can find out how the XML data is structured, or access data that he may not normally have access to. He may even be able to elevate his privileges on the web site if the XML data is being used for authentication (such as an XML based user file).
Querying XML is done with XPath, a type of simple descriptive statement that allows the XML query to locate a piece of information. Like SQL, you can specify certain attributes to find, and patterns to match. When using XML for a web site it is common to accept some form of input on the query string to identify the content to locate and display on the page. This input must be sanitized to verify that it doesn't mess up the XPath query and return the wrong data.
XPath is a standard language; its notation/syntax is always implementation independent, which means the attack may be automated.There are no different dialects as it takes place in requests to the SQL databeses.
Because there is no level access control it's possible to get the entire document. We won't encounter any limitations as we may know from SQL injection attacks.
Risk FactorsTBD


Examples We'll use this xml snippet for the examples.
<?xml version="1.0" encoding="utf-8"?><Employees> <Employee ID="1"> <FirstName>Arnold</FirstName> <LastName>Baker</LastName> <UserName>ABaker</UserName> <Password>SoSecret</Password> <Type>Admin</Type> </Employee> <Employee ID="2"> <FirstName>Peter</FirstName> <LastName>Pan</LastName> <UserName>PPan</UserName> <Password>NotTelling</Password> <Type>User</Type> </Employee></Employees>Suppose we have a user authentication system on a web page that used a data file of this sort to login users. Once a username and password have been supplied the software might use XPath to look up the user:
VB:Dim FindUserXPath as StringFindUserXPath = "//Employee[UserName/text()='" & Request("Username") & "' And Password/text()='" & Request("Password") & "']"C#:String FindUserXPath;FindUserXPath = "//Employee[UserName/text()='" + Request("Username") + "' And Password/text()='" + Request("Password") + "']";With a normal username and password this XPath would work, but an attacker may send a bad username and password and get an XML node selected without knowing the username or password, like this:
Username: blah' or 1=1 or 'a'='aPassword: blahFindUserXPath becomes //Employee[UserName/text()='blah' or 1=1 or 'a'='a' And Password/text()='blah']Logically this is equivalent to: //Employee[(UserName/text()='blah' or 1=1) or ('a'='a' And Password/text()='blah')]In this case, only the first part of the XPath needs to be true. The password part becomes irrelevant, and the UserName part will match ALL employees because of the "1=1" part.
Just like SQL injection, in order to protect yourself you must escape single quotes (or double quotes) if your application uses them.
VB:Dim FindUserXPath as StringFindUserXPath = "//Employee[UserName/text()='" & Request("Username").Replace("'", "'") & "' And Password/text()='" & Request("Password").Replace("'", "'") & "']"C#:String FindUserXPath;FindUserXPath = "//Employee[UserName/text()='" + Request("Username").Replace("'", "'") + "' And Password/text()='" + Request("Password").Replace("'", "'") + "']";Another better mitigation option is to use a precompiled XPath[1]. Precompiled XPaths are already preset before the program executes, rather than created on the fly after the user's input has been added to the string. This is a better route because you don't have to worry about missing a character that should have been escaped.