Skip to content

Commit

Permalink
[backend] add command line with interpolated values
Browse files Browse the repository at this point in the history
  • Loading branch information
isselparra committed Nov 12, 2024
1 parent 7d15dd7 commit 037c5e1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static io.openbas.model.expectation.PreventionExpectation.preventionExpectationForAsset;
import static io.openbas.model.expectation.PreventionExpectation.preventionExpectationForAssetGroup;

import com.fasterxml.jackson.databind.node.ObjectNode;
import io.openbas.asset.AssetGroupService;
import io.openbas.database.model.*;
import io.openbas.database.repository.InjectRepository;
Expand All @@ -22,6 +23,8 @@
import io.openbas.model.expectation.PreventionExpectation;
import jakarta.validation.constraints.NotNull;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
Expand Down Expand Up @@ -260,13 +263,21 @@ public ExecutionProcess process(Execution execution, ExecutableInject injection)
.type(EXPECTATION_SIGNATURE_TYPE_PARENT_PROCESS_NAME)
.value("obas-implant-" + inject.getId())
.build());

String interpolatedCommand =
interpolateCommand(payloadCommand.getContent(), inject.getContent());

Check warning on line 268 in openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java#L267-L268

Added lines #L267 - L268 were not covered by tests
injectExpectationSignatures.add(
InjectExpectationSignature.builder()
.type(EXPECTATION_SIGNATURE_TYPE_COMMAND_LINE)
.type(EXPECTATION_SIGNATURE_TYPE_COMMAND_LINE_BASE64)

Check warning on line 271 in openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java#L271

Added line #L271 was not covered by tests
.value(
Base64.getEncoder()
.encodeToString(payloadCommand.getContent().getBytes()))
.build()); // Add parsing: base64 for example
.encodeToString(interpolatedCommand.getBytes()))
.build());
injectExpectationSignatures.add(
InjectExpectationSignature.builder()
.type(EXPECTATION_SIGNATURE_TYPE_COMMAND_LINE)
.value(interpolatedCommand)
.build());

Check warning on line 280 in openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java#L274-L280

Added lines #L274 - L280 were not covered by tests
totalActionsCount = totalActionsCount + 1;
if (payloadCommand.getPrerequisites() != null) {
totalActionsCount =
Expand Down Expand Up @@ -360,4 +371,24 @@ public ExecutionProcess process(Execution execution, ExecutableInject injection)
expectations, content, assetGroup, new ArrayList<>())));
return new ExecutionProcess(true, expectations);
}

private static String interpolateCommand(String commandMask, ObjectNode injectContent) {
List<String> placeholders = extractPlaceholderNames(commandMask);
String interpolatedCommand = commandMask;

Check warning on line 377 in openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java#L376-L377

Added lines #L376 - L377 were not covered by tests
for (String placeholder : placeholders) {
String value = injectContent.get(placeholder).asText();
interpolatedCommand = interpolatedCommand.replace("#{" + placeholder + "}", value);
}
return interpolatedCommand;

Check warning on line 382 in openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java#L379-L382

Added lines #L379 - L382 were not covered by tests
}

private static List<String> extractPlaceholderNames(String command) {
List<String> placeholders = new ArrayList<>();
Pattern pattern = Pattern.compile("#\\{(.*?)\\}");
Matcher matcher = pattern.matcher(command);

Check warning on line 388 in openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java#L386-L388

Added lines #L386 - L388 were not covered by tests
while (matcher.find()) {
placeholders.add(matcher.group(1));

Check warning on line 390 in openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java#L390

Added line #L390 was not covered by tests
}
return placeholders;

Check warning on line 392 in openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/openbas/OpenBASImplantExecutor.java#L392

Added line #L392 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class InjectExpectationSignature {
public static final String EXPECTATION_SIGNATURE_TYPE_PARENT_PROCESS_NAME = "parent_process_name";
public static final String EXPECTATION_SIGNATURE_TYPE_PROCESS_NAME = "process_name";
public static final String EXPECTATION_SIGNATURE_TYPE_COMMAND_LINE = "command_line";
public static final String EXPECTATION_SIGNATURE_TYPE_COMMAND_LINE_BASE64 = "command_line_base64";
public static final String EXPECTATION_SIGNATURE_TYPE_HASH = "hash";
public static final String EXPECTATION_SIGNATURE_TYPE_FILE_NAME = "file_name";
public static final String EXPECTATION_SIGNATURE_TYPE_IPV4_ADDRESS = "ipv4_address";
Expand Down

0 comments on commit 037c5e1

Please sign in to comment.