diff --git a/openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java b/openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java index 3ffc284211..c6863fda79 100644 --- a/openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java +++ b/openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java @@ -73,6 +73,32 @@ public static String replaceCmdVariables(String cmd) { return result.toString(); } + private static String formatMultilineCommand(String command) { + String[] lines = command.split("\n"); + StringBuilder formattedCommand = new StringBuilder(); + + for (int i = 0; i < lines.length; i++) { + String line = lines[i]; + String trimmedLine = line.trim(); + if (trimmedLine.isEmpty()) { + continue; + } + formattedCommand.append(trimmedLine); + + boolean isLastLine = (i == lines.length - 1); + boolean isAfterParentheses = trimmedLine.endsWith("("); + boolean isBeforeParentheses = !isLastLine && lines[i + 1].trim().startsWith(")"); + + if (!isAfterParentheses && !isBeforeParentheses && !isLastLine) { + formattedCommand.append(" & "); + } else { + formattedCommand.append(" "); + } + } + + return formattedCommand.toString(); + } + private String processAndEncodeCommand( String command, String executor, @@ -83,7 +109,7 @@ private String processAndEncodeCommand( if (executor.equals("cmd")) { computedCommand = replaceCmdVariables(computedCommand); - computedCommand = computedCommand.trim().replace("\n", " & "); + computedCommand = formatMultilineCommand(computedCommand); } if (obfuscator.equals("base64")) {