Copyright © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP.

Slides:



Advertisements
Similar presentations
Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
Advertisements

Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: XPath, NameSpaces.
XML: Extensible Markup Language
Dr. Alexandra I. Cristea XPath and Namespaces.
Bottom-up Evaluation of XPath Queries Stephanie H. Li Zhiping Zou.
Managing Data Exchange: XPath
XPath XML Path Language. Outline XML Path Language (XPath) Data Model Description Node values XPath expressions Relative expressions Simple subset of.
XPath Eugenia Fernandez IUPUI. XML Path Language (XPath) a data model for representing an XML document as an abstract node tree a mechanism for addressing.
XML 6.6 XPath 6. What is XPath? XPath is a syntax used for selecting parts of an XML document The way XPath describes paths to elements is similar to.
XPath Carissa Mills Jill Kerschbaum. What is XPath? n A language designed to be used by both XSL Transformations (XSLT) and XPointer. n Provides common.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
XPath Tao Wan March 04, What is XPath? n A language designed to be used by XSL Transformations (XSLT), Xlink, Xpointer and XML Query. n Primary.
Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – Carl Lagoze – Cornell University.
XML –Query Languages, Extracting from Relational Databases ADVANCED DATABASES Khawaja Mohiuddin Assistant Professor Department of Computer Sciences Bahria.
Lecture 14. node-tests again: namespace-qualified node-tests I found a book. The fred:* node test admits only element in the fred namespace The match="book"
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
CIS 375—Web App Dev II XPath. 2 XPath IntroductionIntroduction What is XPath? XPath is a syntax for defining parts of an _____ document XPath uses paths.
Overview of XPath Author: Dan McCreary Date: October, 2008 Version: 0.2 with TEI Examples M D.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
XP ATH - XML Path Language. W HAT IS XP ATH ? XPath, the XML Path Language, is a query language for selecting nodes from an XML document.query languagenodesXML.
SD2520 Databases using XML and JQuery
JSP Standard Tag Library
Navigating XML. Overview  Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is  to address parts of an.
CSE3201/CSE4500 XPath. 2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
1/17 ITApplications XML Module Session 7: Introduction to XPath.
CG0119 Web Database Systems Using XPath to Navigate & Filter XML (via SimpleXML)‏
Introduction to XPath Web Engineering, SS 2007 Tomáš Pitner.
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
XML DOCUMENTS & DATABASES. Summary of Introduction to XML HTML vs. XML HTML vs. XML Types of Data Types of Data Basics of XML Basics of XML XML Syntax,
CSE3201/CSE4500 Information Retrieval Systems
XPath XPath is used to navigate through elements and attributes in an XML document. XPath is a major element in W3C's XSLT standard - and XQuery and XPointer.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
XSLT and XPath, by Dr. Khalil1 XSL, XSLT and XPath Dr. Awad Khalil Computer Science Department AUC.
WORKING WITH XSLT AND XPATH
1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.
Extensible Markup and Beyond
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
XPath Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Processing of structured documents Spring 2003, Part 7 Helena Ahonen-Myka.
XPath. Why XPath? Common syntax, semantics for [XSLT] [XPointer][XSLT] [XPointer] Used to address parts of an XML document Provides basic facilities for.
1 Displaying XML Document Web and Database Management System.
IS432: Semi-Structured Data Dr. Azeddine Chikh. 6. XML Path (XPath)
August Chapter 6 - XPath & XPointer Learning XML by Erik T. Ray Slides were developed by Jack Davis College of Information Science and Technology.
Database Systems Part VII: XML Querying Software School of Hunan University
XPath Aug ’10 – Dec ‘10. XPath   XML Path Language   Technology that allows to select a part or parts of an XML document to process   XPath was.
Advanced Selenium.  XPath is used to navigate through elements and attributes in a document.  XPath includes over 100 built-in functions.  Functions.
[ Part III of The XML seminar ] Presenter: Xiaogeng Zhao A Introduction of XQL.
XPath Presented by Kushan Athukorala. 2 Agenda XPath XPath Terminology Selecting Nodes Predicates.
1 XML Data Management XPath Principles Werner Nutt.
Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa ME (Modular) CSE 2011 Batch.
Lesser Known Injections XML Injections AMol NAik.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
Session II Chapter 3 – Chapter 3 – XPath Patterns & Expressions Chapter 4 – XPath Functions Chapter 15 – XPath 2.0http://
University of Nottingham School of Computer Science & Information Technology Introduction to XML 2. XSLT Tim Brailsford.
IS432 Semi-Structured Data Lecture 4: XPath Dr. Gamal Al-Shorbagy.
CSE3201/CSE4500 XPath. 2 XPath A locator for items in XML document. XPath expression gives direction of navigation.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
1 XPath. 2 Agenda XPath Introduction XPath Nodes XPath Syntax XPath Operators XPath Q&A.
CITA 330 Section 5 XPath. XSL XSL (Extensible Stylesheet Language) is the standard language for writing stylesheets to transform XML documents among different.
5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath.
Beginning XML 4th Edition.
Querying and Transforming XML Data
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XML Path Language Andy Clark 17 Apr 2002.
More XML XML schema, XPATH, XSLT
XML DOCUMENTS & DATABASES
Presentation transcript:

Copyright © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP Foundation OWASP OWASP – XPath Injection overview Roberto Suggi Liverani Security Consultant Security-Assessment.com 21 February 2008

OWASP Who am I? Roberto Suggi Liverani  Security Consultant, CISSP Security-Assessment.com  4 + years in Information Security, focusing on web application and network security  OWASP New Zealand leader 2

OWASP Agenda  Understanding Xpath (the theory part…  ) -What is XPath? -XPath Syntax -XPath Predicates -XPath Location Path -XPath Functions  XPath Injection (the funny part… ) -XPath Injection (techniques and examples) -Blind XPath Injection (techniques and examples) -XPath Injection countermeasures 3

OWASP What is XPath?  XPath is a language solely used for selecting nodes from an XML document  XPath formats XML data as tree-structured values  There are some similarities between SQL and XPath  XPath v.1.0 is a W3C standard and it is still the most used - XPath v.2.0 recently released.  Many languages support XPath such as Java, JavaScript,.NET framework, PHP, Python, Perl and Ruby. 4

OWASP  XPath Nodes: An XML document from XPath perspective (1/2) 5

OWASP An XML document from Xpath perspective (2/2) Relationships of Nodes: root OAhhgg root Relationships: is the parent node of,,,, are children nodes of the element,, are all siblings (they have the same parent) and are ancestors of,,,, are descendants of the element 6

OWASP XPath Syntax (1/3)  XPath uses path expressions to select nodes or node-sets in an XML document.  Path expressions is very similar to URI syntax and file path syntax.  Selecting Nodes: 7 ExpressionDescription nodenameSelects all child nodes of the named node /Selects from the root node //Selects nodes in the document from the current node that match the selection no matter where they are.Selects the current node..Selects the parent of the current node

OWASP XPath Syntax (2/3)  Example: 8

OWASP XPath Syntax – other query examples (3/3) 9 ExpressionResult usersSelects all the child nodes of the users element /usersSelects the root element users users/userSelects all user elements that are children of users //usersSelects all users elements no matter where they are in the document users//userSelects all user elements that are descendant of the users element, no matter where they are under the users element

OWASP XPath Predicates  Predicates are used to find a specific node or a node that contains a specific value. Predicates can use XPath operators.  Predicates are always embedded in square brackets. 10 ExpressionResult /users/user[1]Selects the first user element that is the child of the users element. /users/user[last()]Selects the last user element that is the child of the users element /users/user[position()<3]Selects the first two user elements that are children of the users element all the username elements that have an attribute named id with a value of ‘1' XPath operators are shown in red.

OWASP XPath Location Path (1/2)  Location path is a special case of XPath Expression.  Two types: absolute and relative location path Absolute Location Path starts with a (forward) slash Relative Location Path starts without a slash  In both cases the location path consists of one or more steps, each separated by a slash. Example: Absolute Location Path: /users/user/username  A step is composed by: an axis (defines the tree-relationship between the selected nodes and the current node) a node-test (identifies a node within an axis) zero or more predicates (to further refine the selected node-set)  The syntax for a location step is: axisname::nodetest[predicate]  There are several axisname that can be used. Most common are: ancestor, attribute, descendant, child 11

OWASP XPath Location Path – Examples (2/2) ExampleResult child::userSelects all user nodes that are children of the current node attribute::idSelects the id attribute of the current node child::*Selects all children of the current node attribute::*Selects all attributes of the current node child::text()Selects all text child nodes of the current node child::node()Selects all child nodes of the current node descendant::usersSelects all users descendants of the current node 12 XPath Wilcards are bolded in red. XPath Axisname are underlined.

OWASP XPath Functions  Functions specified for XSLT and Xquery can also be used for XPath.  Functions are related to strings, boolean, date/time, error and trace, numeric, node, sequence, qname, anyURI, context.  Short list of the most important functions: 13 Function NameDescription substring(string,start,len)Returns the substring from the start position to the specified length. Index of the first character is 1. If length is omitted it returns the substring from the start position to the end string-length(string)Returns the length of the specified string. count((item,item,...))Returns the count of nodes starts-with(string1,string2)Returns true if string1 starts with string2, otherwise it returns false contains(string1,string2)Returns true if string1 contains string2, otherwise it returns false number(arg)Returns the numeric value of the argument. The argument could be a boolean, string, or node-set string(arg)Returns the string value of the argument. The argument could be a number, boolean, or node-set

OWASP XPath Injection (1/2)  Scenario: authentication system which performs XPath query  This is a standard authentication query. 14 VB: Dim FindUserXPath as String FindUserXPath = "//Users/user[username/text()='" & Request("Username") & "' And password/text()='" & Request("Password") & "']" C#: String FindUserXPath; FindUserXPath = "//Users/user[username/text()='" + Request("Username") + "' And password/text()='" + Request("Password") + "']"; Username = user Password = password XPath query becomes: //users/user[username/text()=‘user’ and password/text()=‘password’]

OWASP XPath Injection (2/2)  In this case, injection is possible in the Username variable. The same attack logic of SQL injection can be applied for XPath.  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 users because of the "1=1" condition.  This injection will allow the attacker to bypass the authentication system.  Note that the big difference between XML files and SQL databases is the lack of access control.  XPath does not have any restrictions when querying the XML file. Therefore it is possible to retrieve data from the entire document. 15 Username = user’ or ‘1’ = ‘1 Password = password XPath query becomes: //users/user[username/text()=‘user’ or ‘1’ = ‘1’ and password/text()=‘password’]

OWASP Blind XPath Injection (1/3)  Blind XPath Injection – Amit Klein – white paper  XPath disallows commenting out the rest of expression. The attacker needs to use ‘OR’ to void all expressions.  Original Xpath Request:  1) Extracting XML file structure: (confirming if “username” node exists) 16 Username = user Password = password XPath query becomes: //users/user[username/text()=‘user’ and password/text()=‘password’] Username = jjj' or name(//users/user/username[1]) = 'username' or 'a'='b Password = password XPath query becomes: //users/user[username/text()=‘jjj' or name(//users/user/username[1]) = 'username' or 'a'='b' and password/text()=‘password’]

OWASP Blind XPath Injection (2/3)  2) Considering we have valid credentials for one user, we can then use these TRUE conditions to get other user credentials in the database. In this scenario, this query will return TRUE if also the first character of the second user password element is “a”.  This blind Xpath injection can also make use of the functions “contains” and “string-length” and all relative functions. In this case, AND must be used so that all conditions must be true. 17 count(//user/child::node()) Username = root' and substring((//user[position()=2]/child::node()[position()= 1]),1,1)="a" and '1' = '1 Password = OAhhgg XPath query becomes: //users/user[username/text()=‘root’ and substring((//user[position()=2]/child::node()[position()= 1]),1,1)="a" and '1' = '1' and password/text()=‘OAhhgg’]

OWASP Blind XPath Injection – (3/3)  Other XML crawling techniques that can be used:  Return number of nodes in the XML file  Return True if the length of the first username element is equal to 4 digits  Return True if the first username element contains the string “r” 18 string-length(//username[position()=1]/child::node()[position()=1])=4 count(//user/child::node()) contains(//username[position()=1]/child::node()[position()=1],”r”)

OWASP XPath Injection Countermeasures  Input Validation  Always filter input and escape output  Parameterisation  It is possible to parametirise expressions that are passed to the XPath parser for dynamic execution at run time.  The query can be parameterised by creating an external file and using XQuery to query the file.  Precompiled XPath  Use precompiled XPath. If you are using.NET, consider Dynamic Context of Daniel Cazzulino. 19 XPathNodeIterator custData = XPathCache.Select( customersDocument, new XPathVariable("name", txtName.Text), new XPathVariable("password", txtPassword.Text));

OWASP Questions/Conclusion  Thank you! Presentation can be downloaded here: 20

OWASP References – Misc.  XPath W3C   Software – XPath Builder   Blind XPath injection – Amit Klein  xpath-injection.pdf xpath-injection.pdf  Avoid the dangers of XPath Injection  /x-xpathinjection.html /x-xpathinjection.html 21

OWASP References  Blind XPath Injection  njection njection  XPath Tutorial   OWASP – Test XPath Injection  n_Testing_AoC n_Testing_AoC  Dynamic Context  7/30888.aspx 7/30888.aspx 22

OWASP References  Signs on the sand – Mitigating XPath injection  5.html 5.html 23