refactor: simplify CommandProbe

This commit is contained in:
ReaJason
2025-09-12 22:51:50 +08:00
parent 88af60fea4
commit aa140a8777
3 changed files with 27 additions and 7 deletions
+1
View File
@@ -41,6 +41,7 @@ dependencies {
implementation(libs.bcel) implementation(libs.bcel)
implementation(libs.jackson.databind) implementation(libs.jackson.databind)
testImplementation(libs.junit.jupiter) testImplementation(libs.junit.jupiter)
testImplementation(libs.hamcrest)
testRuntimeOnly(libs.junit.platform.launcher) testRuntimeOnly(libs.junit.platform.launcher)
testImplementation(libs.bundles.mockito) testImplementation(libs.bundles.mockito)
} }
@@ -3,7 +3,6 @@ package com.reajason.javaweb.probe.payload;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
import java.util.NoSuchElementException;
import java.util.Scanner; import java.util.Scanner;
/** /**
@@ -20,12 +19,8 @@ public class CommandProbe {
@Advice.OnMethodExit @Advice.OnMethodExit
public static String exit(@Advice.Argument(0) String data, @Advice.Return(readOnly = false) String ret) throws Exception { public static String exit(@Advice.Argument(0) String data, @Advice.Return(readOnly = false) String ret) throws Exception {
String[] cmd = System.getProperty("os.name").toLowerCase().contains("window") ? new String[]{"cmd.exe", "/c", data} : new String[]{"/bin/sh", "-c", data}; String[] cmd = System.getProperty("os.name").toLowerCase().contains("window") ? new String[]{"cmd.exe", "/c", data} : new String[]{"/bin/sh", "-c", data};
Process process = new ProcessBuilder(cmd).start(); Process process = new ProcessBuilder(cmd).redirectErrorStream(true).start();
try { return ret = new Scanner(process.getInputStream()).useDelimiter("\\A").next();
return ret = new Scanner(process.getInputStream()).useDelimiter("\\A").next();
} catch (NoSuchElementException e) {
return ret = new Scanner(process.getErrorStream()).useDelimiter("\\A").next();
}
} }
@Override @Override
@@ -0,0 +1,24 @@
package com.reajason.javaweb.probe.payload;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author ReaJason
* @since 2025/8/26
*/
class CommandProbeTest {
@Test
@Disabled
void test() {
String result = new CommandProbe("hello").toString();
assertThat(result, anyOf(
containsString("not found")
));
}
}