XPATH Injection

Description

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

类似于SQL注入,XPATH注入发生在网站使用用户输入的信息构造XPath查询获取XML数据。通过发送畸形的信息至网站,攻击者可以获取XML数据的组织结构,或者访问在正常情况下不允许访问的数据。如果xml数据被用于用户认证,那么攻击者就可以提升他的权限。

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.

XML查询是通过XPATH实现的,XPATH是一种简单的描述性声明,可以使XML查询定位在一条信息上。与SQL类似,可以指定查找特定的属性,或者匹配指定的模式。当网站使用XML时,网站多在查询字符串中接受一些输入来确定页面要显示的内容。输入必须被验证以防其打乱XPATH查询而返回错误数据。

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.

XPath是标准的语言;XPath的标记/语法是单独实现,这就可以实现自动化攻击。因为XPath替代了SQL数据库请求,所以XPath不存在不同的语法/标记

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.

因为XPath不存在级别访问控制,所以存在访问整个文档的可能。正如从SQL注入攻击获知的那样,攻击者不会遇到任何限制。

Risk Factors

TBD


Example Vulnerability

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 String
FindUserXPath = "//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'='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 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.

XPath Injection Defenses

Just like the techniques to avoid SQL injection, you need to use a parameterized XPath interface if one is available, or escape the user input to make it safe to include in a dynamically constructed query. If you are using quotes to terminate untrusted input in a dynamically constructed XPath query, then you need to escape that quote in the untrusted input to ensure the untrusted data can't try to break out of that quoted context. In the following example, single quotes (') are used to terminate the Username and Password parameters. So, we need to replace any ' characters in this input with the XML encoded version of that character, which is "&apos;".

正如防护SQL注入一样,如果要过滤用户输入以保障动态构造查询的安全性,XPath需要使用参数化XPath接口。如在动态XPath查询构造语句中使用引号作为不可信输入的结尾符,那么开发者需要从不可信输入中过滤引号,以保证不可信输入不会破坏引号内的内容。在下面的例子中,单引号作为Username和Password参数的结束符,所以,对输入数据中的单引号用"&apos;"替换。

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

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

Another better mitigation option is to use a precompiled XPath[1] query. Precompiled XPath queries 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.

另一个缓解的方法是使用已编译的XPath查询。预编译的XPath查询在程序执行前预设置,而不是待用户输入添加进字符串后创建。这是一个不错的途径因为不需要担心因为过滤丢失字符。

Related Threat Agents

Related Attacks

Related Vulnerabilities

Related Controls

References

TBD

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值