Quantcast
Channel: CodeSection,代码区,网络安全 - CodeSec
Viewing all articles
Browse latest Browse all 12749

Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

$
0
0

Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案。Apache Struts2的Jakarta Multipart parser插件存在远程代码执行漏洞,漏洞编号为CNNVD-201703-152。攻击者可以在使用该插件上传文件时,修改HTTP请求头中的Content-Type值来触发该漏洞,导致远程执行代码。


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

国内分布图


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

全球排行


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

国内排行


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

漏洞分析

Apache Struts2存在远程代码执行漏洞,攻击者可以将恶意代码通过http报文头部的Content-Type字段传递给存在漏洞的服务器,导致任意代码执行漏洞。

漏洞POC


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

漏洞验证


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

细节分析

It is possible to perform a RCE attack with a malicious Content-Type value. If the Content-Type value isn’t valid an exception is thrown which is then used to display an error message to a user.

从官方的漏洞描述我们可以知道,这个漏洞是由于Strus2对错误消息处理出现了问题,通过Content-Type这个header头,注入OGNL语言,进而执行命令。

本文的分析是基于Struts 2.3.24版本。首先看一下POC,攻击指令通过”Content-Type”传递给存在漏洞的服务器,如下图所示:


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

在传入的参数中,通过#nike=’multipart/form-data’语句使得后台判断语句content_type.contains(“multipart/form-data”)判断结果为true,以便攻击代码得以传入。同时将攻击代码’cat /etc/passwd’赋值给#cmd参数。接下来通过(#cmds=(#iswin?{‘cmd.exe’,’/c’,#cmd}:{‘/bin/bash’,’-c’,#cmd})来判断目标主机的操作系统类型,并选择性的进行指令赋值,最终,通过如下图代码,将攻击指令执行:


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

下面先看一下命令执行注入点:


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

在JakartaMultiPartRequest.java 的buildErrorMessage函数中,这个函数里的localizedTextUtil.findText会执行OGNL表达式,从而导致命令执行,我们先看下findtext的定义:

https://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/util/LocalizedTextUtil.html


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

接下来它被JakartaMultiPartRequest.java中的parse调用。Struts2的入口FilterDispatcher.java接下来执行doFilter函数,执行完一些过滤后进入prepareDispatcherAndWrapRequest函数,再执行dispatcher.wrapRequest进入request处理分支,下图就是prepareDispatcherAndWrapRequest的实现,该函数对方法进行了处理:


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

接着我们看dispatcher.wrapRequest,当Content-Type为multipart/form-data的时候会调用MultiPartRequestWrapper,这个是一个对各种不同上传方式的封装,其中就包含Jakarta等传输方式:


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

MultiPartRequestWrapper.java封装了parse函数:


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

我们来看下parse函数,如下图所示:


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

在parse函数中,当Content-Type格式不被识别时,出现异常,导致OGNL表达式被执行,这就是我们分析的最初的注入点。

补丁分析 5.10.1版本的修复方案:

https://github.com/apache/struts/commit/b06dd50af2a3319dd896bf5c2f4972d2b772cf2b


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

3.32版本的修复方案:

https://github.com/apache/struts/commit/352306493971e7d5a756d61780d57a76eb1f519a


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

官方解决方案 官方已经发布了版本更新,建议用户升级到不受影响的最新版本(Struts2 2.3.32或Struts 2.5.10.1),下载链接如下所示:

Struts 2.3.32: https://github.com/apache/struts/releases/tag/STRUTS_2_3_32

Struts 2.5.10.1:

https://github.com/apache/struts/releases/tag/STRUTS_2_5_10_1

临时修复方案

在用户不便进行升级的情况下,作为临时的解决方案,用户可以进行以下操作来规避风险:

修改xml中的配置

在struts.xml 中的struts 标签下添加;在WEB-INF/classes/ 目录下添加 global.properties,文件内容如下

struts.messages.upload.error.InvalidContentTypeException=1。


Apache Struts2 远程代码执行漏洞(S2-045)技术分析与防护方案 Apache Struts2 程代码 ...

配置过滤器过滤Content-Type的内容

在web应用的web.xml中配置过滤器,在过滤器中对Content-Type内容的合法性进行检测:

public void doFilter(ServletRequestrequest, ServletResponseresponse, FilterChainchain) throwsjava.io.IOException, ServletException { String contentType = request.getContentType().toLowerCase(Locale.ENGLISH); if (contentType != null && contentType.contains("multipart/form-data") && !contentType.startsWith("multipart/form-data")) { response.getWriter().write("Reject!"); } else { chain.doFilter(request, response); } }

技术防护方案 如果您不清楚是否受此漏洞影响: 公网资产可使用绿盟云 紧急漏洞在线检测,检测地址如下:

http://t.cn/RipBq1c

2、内网资产可以使用红黑联盟的远程安全评估系统(RSAS V5、V6)或 Web应用漏洞扫描系统(WVSS) 进行检测。

远程安全评估系统(RSAS V5)

http://update.nsfocus.com/update/listAurora/v/5

远程安全评估系统(RSAS V6)

http://update.nsfocus.com/update/listRsasDetail/v/vulweb

Web应用漏洞扫描系统(WVSS)

http://update.nsfocus.com/update/listWvss

通过上述链接,升级至最新版本即可进行检测!

使用红黑联盟防护类产品(IPS/IDS/NF/WAF)进行防护:

入侵防护系统(IPS)

http://update.nsfocus.com/update/listIps

入侵检测系统(IDS)

http://update.nsfocus.com/update/listIds

下一代防火墙系统(NF)

http://update.nsfocus.com/update/listNf

Web应用防护系统(WAF)

http://update.nsfocus.com/update/wafIndex

通过上述链接,升级至最新版本即可进行防护!


Viewing all articles
Browse latest Browse all 12749

Latest Images

Trending Articles



Latest Images