天天看點

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

apache struts2的jakarta multipart parser插件存在遠端代碼執行漏洞,漏洞編号為cnnvd-201703-152。攻擊者可以在使用該插件上傳檔案時,修改http請求頭中的content-type值來觸發該漏洞,導緻遠端執行代碼。

相關連結如下:

<a href="https://cwiki.apache.org/confluence/display/ww/s2-045?from=timeline&amp;isappinstalled=0">https://cwiki.apache.org/confluence/display/ww/s2-045?from=timeline&amp;isappinstalled=0</a>

struts 2.3.5 - struts 2.3.31

struts 2.5 - struts 2.5.10

struts 2.3.32

struts 2.5.10.1

全球分布圖

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

國内分布圖

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

全球排行

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

國内排行

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

apache struts2存在遠端代碼執行漏洞,攻擊者可以将惡意代碼通過http封包頭部的content-type字段傳遞給存在漏洞的伺服器,導緻任意代碼執行漏洞。

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的
struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的
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"傳遞給存在漏洞的伺服器,如下圖所示:

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

在傳入的參數中,通過#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})來判斷目标主機的作業系統類型,并選擇性的進行指令指派,最終,通過如下圖代碼,将攻擊指令執行:

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

下面先看一下指令執行注入點:

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

在jakartamultipartrequest.java 的builderrormessage函數中,這個函數裡的localizedtextutil.findtext會執行ognl表達式,進而導緻指令執行,我們先看下findtext的定義:

<a href="https://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/util/localizedtextutil.html">https://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/util/localizedtextutil.html</a>
struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

接下來它被jakartamultipartrequest.java中的parse調用。struts2的入口filterdispatcher.java接下來執行dofilter函數,執行完一些過濾後進入preparedispatcherandwraprequest函數,再執行dispatcher.wraprequest進入request處理分支,下圖就是preparedispatcherandwraprequest的實作,該函數對方法進行了處理:

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

接着我們看dispatcher.wraprequest,當content-type為multipart/form-data的時候會調用multipartrequestwrapper,這個是一個對各種不同上傳方式的封裝,其中就包含jakarta等傳輸方式:

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

multipartrequestwrapper.java封裝了parse函數:

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

我們來看下parse函數,如下圖所示:

struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

在parse函數中,當content-type格式不被識别時,出現異常,導緻ognl表達式被執行,這就是我們分析的最初的注入點。

2.5.10.1版本的修複方案:

<a href="https://github.com/apache/struts/commit/b06dd50af2a3319dd896bf5c2f4972d2b772cf2b">https://github.com/apache/struts/commit/b06dd50af2a3319dd896bf5c2f4972d2b772cf2b</a>
struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

2.3.32版本的修複方案:

<a href="https://github.com/apache/struts/commit/352306493971e7d5a756d61780d57a76eb1f519a">https://github.com/apache/struts/commit/352306493971e7d5a756d61780d57a76eb1f519a</a>
struts2 漏洞分析與防護方案 CVE-2017-5638 S2-045 除了更新外還是有修複方案的

官方已經釋出了版本更新,建議使用者更新到不受影響的最新版本(struts2 2.3.32或struts 2.5.10.1),下載下傳連結如下所示:

struts 2.3.32:

<a href="https://github.com/apache/struts/releases/tag/struts_2_3_32">https://github.com/apache/struts/releases/tag/struts_2_3_32</a>

struts 2.5.10.1:

<a href="https://github.com/apache/struts/releases/tag/struts_2_5_10_1">https://github.com/apache/struts/releases/tag/struts_2_5_10_1</a>

在使用者不便進行更新的情況下,作為臨時的解決方案,使用者可以進行以下操作來規避風險:

修改web-inf/classes目錄下的struts.xml中的配置

在web-inf/classes目錄下的struts.xml 中的struts 标簽下添加&lt;constant name="struts.custom.i18n.resources" value="global" /&gt;;

在web-inf/classes/ 目錄下添加 global.properties,檔案内容如下

struts.messages.upload.error.invalidcontenttypeexception=1

配置過濾器過濾content-type的内容

在web應用的web.xml中配置過濾器,在過濾器中對content-type内容的合法性進行檢測:

public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws java.io.ioexception, servletexception {

string contenttype = request.getcontenttype().tolowercase(locale.english);

if (contenttype != null &amp;&amp; contenttype.contains("multipart/form-data") &amp;&amp; !contenttype.startswith("multipart/form-data"))

{ response.getwriter().write("reject!"); }

else { chain.dofilter(request, response); }

}

如果您不清楚是否受此漏洞影響:

公網資産 可使用綠盟雲 緊急漏洞線上檢測,檢測位址如下:

<a href="http://t.cn/ripbq1c">http://t.cn/ripbq1c</a>

内網資産 可以使用綠盟科技的遠端安全評估系統(rsas v5、v6)或 web應用漏洞掃描系統(wvss) 進行檢測。

遠端安全評估系統(rsas v5)

<a href="http://update.nsfocus.com/update/listaurora/v/5%c2%a0" target="_blank">http://update.nsfocus.com/update/listaurora/v/5</a>

遠端安全評估系統(rsas v6)

<a href="http://update.nsfocus.com/update/listrsasdetail/v/vulweb">http://update.nsfocus.com/update/listrsasdetail/v/vulweb</a>

web應用漏洞掃描系統(wvss)

<a href="http://update.nsfocus.com/update/listwvss">http://update.nsfocus.com/update/listwvss</a>

通過上述連結,更新至最新版本即可進行檢測!

使用綠盟科技防護類産品(nips/nids/nf/waf)進行防護:

入侵防護系統(nips)

<a href="http://update.nsfocus.com/update/listips">http://update.nsfocus.com/update/listips</a>

入侵檢測系統(nids)

<a href="http://update.nsfocus.com/update/listids">http://update.nsfocus.com/update/listids</a>

下一代防火牆系統(nf)

<a href="http://update.nsfocus.com/update/listnf">http://update.nsfocus.com/update/listnf</a>

web應用防護系統(waf)

<a href="http://update.nsfocus.com/update/wafindex">http://update.nsfocus.com/update/wafindex</a>

通過上述連結,更新至最新版本即可進行防護!

本安全公告僅用來描述可能存在的安全問題,綠盟科技不為此安全公告提供任何保證或承諾。由于傳播、利用此安全公告所提供的資訊而造成的任何直接或者間接的後果及損失,均由使用者本人負責,綠盟科技以及安全公告作者不為此承擔任何責任。綠盟科技擁有對此安全公告的修改和解釋權。如欲轉載或傳播此安全公告,必須保證此安全公告的完整性,包括版權聲明等全部内容。未經綠盟科技允許,不得任意修改或者增減此安全公告内容,不得以任何方式将其用于商業目的。

原文釋出時間:2017年3月24日

本文由:綠盟科技釋出,版權歸屬于原作者

原文連結:http://toutiao.secjia.com/struts2-vulnerability-analysis-and-protection-cve-2017-5638

本文來自雲栖社群合作夥伴安全加,了解相關資訊可以關注安全加網站