CWE-643 XPath表达式中数据转义处理不恰当(XPath注入)

Improper Neutralization of Data within XPath Expressions ('XPath Injection')

结构: Simple

Abstraction: Base

状态: Incomplete

被利用可能性: High

基本描述

The software uses external input to dynamically construct an XPath expression used to retrieve data from an XML database, but it does not neutralize or incorrectly neutralizes that input. This allows an attacker to control the structure of the query.

扩展描述

The net effect is that the attacker will have control over the information selected from the XML database and may use that ability to control application flow, modify logic, retrieve unauthorized data, or bypass important checks (e.g. authentication).

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 943 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 943 cwe_View_ID: 699 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 91 cwe_View_ID: 1000

  • cwe_Nature: ChildOf cwe_CWE_ID: 91 cwe_View_ID: 699

适用平台

Language: {'cwe_Class': 'Language-Independent', 'cwe_Prevalence': 'Undetermined'}

常见的影响

范围 影响 注释
Access Control Bypass Protection Mechanism Controlling application flow (e.g. bypassing authentication).
Confidentiality Read Application Data The attacker could read restricted XML content.

可能的缓解方案

Implementation

策略:

Use parameterized XPath queries (e.g. using XQuery). This will help ensure separation between data plane and control plane.

Implementation

策略:

Properly validate user input. Reject data where appropriate, filter where appropriate and escape where appropriate. Make sure input that will be used in XPath queries is safe in that context.

示例代码

Consider the following simple XML document that stores authentication information and a snippet of Java code that uses XPath query to retrieve authentication information:

informative XML

<users>
<user>
<login>john</login>
<password>abracadabra</password>
<home_dir>/home/john</home_dir>
</user>
<user>
<login>cbc</login>
<password>1mgr8</password>
<home_dir>/home/cbc</home_dir>
</user>
</users>

The Java code used to retrieve the home directory based on the provided credentials is:

bad Java

XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression xlogin = xpath.compile("//users/user[login/text()='" + login.getUserName() + "' and password/text() = '" + login.getPassword() + "']/home_dir/text()");
Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("db.xml"));
String homedir = xlogin.evaluate(d);

Assume that user "john" wishes to leverage XPath Injection and login without a valid password. By providing a username "john" and password "' or ''='" the XPath expression now becomes

attack

//users/user[login/text()='john' or ''='' and password/text() = '' or ''='']/home_dir/text()

which, of course, lets user "john" login without a valid password, thus bypassing authentication.

Notes

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
WASC 39 XPath Injection
Software Fault Patterns SFP24 Tainted input to command

引用