Friday, April 15, 2011

Xpath Injection



Hi every one , Today we will be learning about Xpath injection

First of all for understanding this attack you need to have knowledge about

1)XML
2)HTML or XHTML

What is xpath ??

XPath is a syntax for defining parts of an XML document.
XPath uses path expressions to navigate in XML documents .
XPath contains a library of standard functions.
XPath is a major element in XSLT.
XPath is a W3C recommendation.

Xpath injection is similar to Sql injection i.e, sql injection occurs when user supplies information and xpath injecion also occurs when the user supplies the input for construing xpath query .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 .

Xml querying can be done using 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.


Example :-

Lets take a xml document


<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>


Below code is used for user authentication

VB:
Dim FindUserXPath as String
FindUserXPath = "//Employee[UserName/text()='" &amp; Request("Username") &amp; "' And
        Password/text()='" &amp; Request("Password") &amp; "']"

C#:
String FindUserXPath;
FindUserXPath = "//Employee[UserName/text()='" + Request("Username") + "' And
        Password/text()='" + Request("Password") + "']";


When normal user enters his username and password above  code will work perfectly

But when a attacker uses malicious strings like below

Username: blah' or 1=1 or 'a'='a
Password: blah

FindUserXPath 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 code Xpath will show true because of 1=1 is always true no matter what happens and it is simliar to the sql injection

In this way a attacker can have a user access

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Vamshi krishnam raju | Bloggerized by Vamshi krishnam raju - Vamshi krishnam raju | Vamshi krishnam raju