漏洞描述
Apache Struts2框架是一個用於開發Java EE網絡應用程序的Web框架。Apache Struts於2020年8月13日披露 S2-059 Struts 遠程代碼執行漏洞(CVE-2019-0230),在使用某些tag等情況下可能存在OGNL表達式注入漏洞,從而造成遠程代碼執行,風險極大。阿里雲應急響應中心提醒Apache Struts用戶盡快采取安全措施阻止漏洞攻擊。
影響版本
Apache Struts 2.0.0 - 2.5.20
安全版本
Apache Struts >= 2.5.22
安全建議
將Apache Struts框架升級至最新版本。
升級步驟:
一、替換struts相關maven依賴包 ,版本改為2.5.22,同時刪除struts.xwork依賴,2.5版本core已經集成該依賴。
本人項目中只替換了struts的這些maven依賴,看網上有其他人替換了很多其他包,如果項目報錯那就替換。
二、struts.xml配置文件修改,包括以下幾個方面:
1)<!DOCTYPE改為2.5版本
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
2)增加配置,這個不加應該也行,百度出來好多都加了。
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.enable.SlashesInActionNames" value="true"/>
3)struts2從2.5版本開始,為了提升安全性,默認開啟了嚴格的方法調用。
action中如果要使用通配符*,必須在package中設置 strict-method-invocation="false" 或者 添加<global-allowed-methods>regex:.*</global-allowed-methods>
例如:
<package name="jspPath" extends="struts-default" strict-method-invocation="false">
<interceptors>
<interceptor name="pathMatch"
class="com.cmcc.open.devportal.mlabs.common.interceptor.PageSwitchInterceptor"/>
<interceptor name="ajaxTokenFilter"
class="com.cmcc.open.devportal.mlabs.common.filter.AjaxTokenFilter">
<param name="includeMethods">templateCallVoiceTemplate</param>
</interceptor>
<interceptor-stack name="pathMatchInterceptor">
<interceptor-ref name="pathMatch"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="pathMatchInterceptor"/>
<!--struts 2.5版本 action通配符* 這個能被其他package繼承,比較好用,如果你其他package都extends了某個基礎的package -->
<global-allowed-methods>regex:.*</global-allowed-methods>
</package>
或者
<package name="apiData" extends="struts-default,json-default" namespace="/apiData">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="calls" method="calls" class="com.cmcc.open.devportal.web.ApiCallsDataAction" >
</action>
</package>
三、web.xml調整
1)<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
2)
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
把 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 中的.ng 去掉 修改為
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
四、jsp頁面涉及的id都不能用,會報錯,利用正則全局替換。
<s:iterator value="codeTypes" id="codeTypes" status="status">,idea工具 全局正則替換如下:
<s:iterator(.*)id=
<s:iterator$1var=
其他參考:
1.https://blog.csdn.net/qq_34128089/article/details/80804882?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
2.https://blog.csdn.net/qq_40248086/article/details/104752778?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
3.https://blog.csdn.net/blue_hh/article/details/79270850?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param