如何从代码层防护10大安全威胁中的 Xpath Injection?

广泛性和可检测性:

Xpath 注入是 OWASP TOP10 安全威胁中 A1 Injection 中的一种,注入漏洞发生在应用程序将不可信的数据发送到解释器时。虽然注入漏洞很容易经过审查代码发现,可是却不容易在测试中发现。html

影响严重:

注入能致使数据丢失或数据破坏、缺少可审计性或者是拒绝服务。注入漏洞有时候甚至能致使彻底主机接管。前端

从代码层次如何防护:

首先咱们先来看一下在 Java 中引用 xpath 须要用的 lib 库:java

  • javax.xml.xpath
  • org.jdom.xpath
  • org.jdom2.xpath等

那么xpath注入是从哪些途径进入到代码逻辑的呢?你们仔细思考一下无外乎三个途径:cookieheaderrequest parameters/input。若是咱们能对这三个注入源头进行严格得入参检查是否就可以防护绝大部分的注入攻击了呢?git

答案是能够防护大部分注入攻击,下面咱们就一块儿来看一下如何进行有效得进行入参检查: 咱们将入参都转化为 Map 对象,Map<K, Collection<V>> asMap();github

而后经过CheckMap(finnal Map<String, String> params)方法,检查入参是否合法。安全

下面咱们来实现这个CheckMap内部方法:

1. 经过遍历检查Map中key得合法性cookie

for (final String key : params.keySet()) {
        if (this.checkString(key)) {
            return true;
        }

2.检查每个 key 对应得 value 得合法性:网络

final Collection<String> coll = (Collection<String>)params.get((Object)key);
        for (final String input : coll) {
            if (this.checkString(input)) {
                return true;
            }
        }

作完这些就是细节方面得检测了,具体得就是 checkString 如何实现。笔者暂时能想到认为一个入参是 xpath 得检测条件大概有如下几点:架构

private boolean checkString(final String input) {
    return null != input && input.length() > 1 && (this.parser.IsOutBoundary(input) || (-1 != input.indexOf(39) && (this.parser.IsOutBoundary(input.replace("'", "''")) || this.parser.IsOutBoundary(input.replace("'", "\\'")))) || (-1 != input.indexOf(34) && (this.parser.IsOutBoundary(input.replace("\"", "\"\"")) || this.parser.IsOutBoundary(input.replace("\"", "\\\"")))) || this.parser.IsQuoteUnbalanced(input));
}

经过查 ASCII 码表咱们知道39对应“'”,34对应“"”;因此有了检测条件less

-1!=input.indexOf(39)&&(this.parser.IsOutBoundary(input.replace("'","''")
-1!=input.indexOf(34)&& this.parser.IsOutBoundary(input.replace("\"", "\"\""))

上述检测条件中用到两个关键方法IsOutBoundaryIsQuoteUnbalance

public boolean IsOutBoundary(String input) {
    int offset = 0;
    if (null == input || input.length() <= 1) {
        return false;
    }
    input = input.toLowerCase();
    while (true) {
        final int x = this.getRawValue().indexOf(input, offset);
        final int y = x + input.length();
        if (-1 == x) {
            return false;
        }
        final int ceil = this.getCeiling(this.boundaries, x + 1);
        if (-1 != ceil && ceil < y) {
            return true;
        }
        offset = y;
    }
}

public boolean IsQuoteUnbalanced(String input) {
    input = input.toLowerCase();
    return this.getRawValue().contains(input) && this.stack.size() > 0 && input.indexOf(this.stack.peek()) != -1;
}

 public String getRawValue() {
        return this.input;
    }
 
 private int getCeiling(final List<Integer> boundaries, final int value) {
    for (final int x : boundaries) {
        if (x >= value) {
            return x;
        }
    }
    return -1;
}
漏洞攻击示例

看完代码是如何检查得咱们来一个真真正正 Xpath 注入的示例;来检验一下咱们代码是都有效。

WebGoat 是 OWASP 推出得一款开源的含有大量漏洞攻击的应用,在 Github 上能够直接搜到源码。

咱们找到 Xpath Injection 得 lession,以下图:

如何从代码层防护10大安全威胁中的 Xpath Injection

hints提示咱们攻击的入参

Try username: Smith' or 1=1 or 'a'='a and a password: anything

点击 Submit 以后神奇得事情出现了!

如何从代码层防护10大安全威胁中的 Xpath Injection

面对这样得一种攻击那么咱们该如何防护呢?若是对代码感兴趣得同窗能够把 WebGoat 得源码 down 下来;而后将上面得入参检测得方法封装一下嵌入到 WebGoat 得源码中,而后咱们再攻击一下,那么接下来会发生什么样的事情呢?

Xpath 查询失败了,并无返回任何结果,攻击被拦截以后,前端页面没有渲染任何东西。因而可知入参检查在注入类得漏洞防护中能够起到立竿见影得做用。

如何从代码层防护10大安全威胁中的 Xpath Injection

Xpath 查询失败了,并无返回任何结果,攻击被拦截以后,前端页面没有渲染任何东西。因而可知入参检查在注入类得漏洞防护中能够起到立竿见影得做用。

参考文献:

[1] OWASP TOP10-2013 release

[2] ASCII(wikipedia)

[3] WebGoat

本文系 OneAPM 架构师吕龙涛原创文章。现在,多样化的攻击手段层出不穷,传统安全解决方案愈来愈难以应对网络安全攻击。OneASP 自适应安全平台集成了预测、预防、检测和响应的能力,为您提供精准、持续、可视化的安全防御。想阅读更多技术文章,请访问 OneAPM 官方技术博客

本文转自 OneAPM 官方博客

相关文章
相关标签/搜索