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 cd3da86dd0..3ffc284211 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 @@ -1,5 +1,7 @@ package io.openbas.rest.inject.service; +import static org.springframework.util.StringUtils.hasText; + import com.fasterxml.jackson.databind.node.ObjectNode; import io.openbas.database.model.*; import io.openbas.database.repository.InjectRepository; @@ -18,6 +20,7 @@ public class ExecutableInjectService { private final InjectRepository injectRepository; private static final Pattern argumentsRegex = Pattern.compile("#\\{([^#{}]+)}"); + private static final Pattern cmdVariablesRegex = Pattern.compile("%(\\w+)%"); private List getArgumentsFromCommandLines(String command) { Matcher matcher = argumentsRegex.matcher(command); @@ -57,6 +60,19 @@ private String replaceArgumentsByValue( return command; } + public static String replaceCmdVariables(String cmd) { + Matcher matcher = cmdVariablesRegex.matcher(cmd); + + StringBuilder result = new StringBuilder(); + while (matcher.find()) { + String variableName = matcher.group(1); + matcher.appendReplacement(result, "!" + variableName + "!"); + } + matcher.appendTail(result); + + return result.toString(); + } + private String processAndEncodeCommand( String command, String executor, @@ -66,6 +82,7 @@ private String processAndEncodeCommand( String computedCommand = replaceArgumentsByValue(command, defaultArguments, injectContent); if (executor.equals("cmd")) { + computedCommand = replaceCmdVariables(computedCommand); computedCommand = computedCommand.trim().replace("\n", " & "); } @@ -88,7 +105,7 @@ public Payload getExecutablePayloadInject(String injectId) { .getPrerequisites() .forEach( prerequisite -> { - if (prerequisite.getCheckCommand() != null) { + if (hasText(prerequisite.getCheckCommand())) { prerequisite.setCheckCommand( processAndEncodeCommand( prerequisite.getCheckCommand(), @@ -97,7 +114,7 @@ public Payload getExecutablePayloadInject(String injectId) { inject.getContent(), "base64")); } - if (prerequisite.getGetCommand() != null) { + if (hasText(prerequisite.getGetCommand())) { prerequisite.setGetCommand( processAndEncodeCommand( prerequisite.getGetCommand(),