Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend|backend]Show prerequisites info in atomic testing #1858

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openbas.execution;
package io.openbas.executors;

import static io.openbas.database.model.InjectStatusExecution.traceInfo;

Expand All @@ -9,6 +9,8 @@
import io.openbas.database.repository.InjectRepository;
import io.openbas.database.repository.InjectStatusRepository;
import io.openbas.database.repository.InjectorRepository;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.ExecutionExecutorService;
import jakarta.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -96,9 +98,9 @@ private InjectStatus executeInternal(ExecutableInject executableInject, Inject i
.orElseThrow(
() -> new UnsupportedOperationException("Inject does not have a contract"));

io.openbas.execution.Injector executor =
io.openbas.executors.Injector executor =
this.context.getBean(
injectorContract.getInjector().getType(), io.openbas.execution.Injector.class);
injectorContract.getInjector().getType(), io.openbas.executors.Injector.class);
Execution execution = executor.executeInjection(executableInject);
Inject executedInject = injectRepository.findById(inject.getId()).orElseThrow();
InjectStatus completeStatus = InjectStatus.fromExecution(execution, executedInject);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.openbas.execution;
package io.openbas.executors;

import static io.openbas.database.model.InjectStatusExecution.traceError;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.openbas.database.model.*;
import io.openbas.database.repository.DocumentRepository;
import io.openbas.execution.ExecutableInject;
import io.openbas.model.ExecutionProcess;
import io.openbas.model.expectation.*;
import io.openbas.service.FileService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -40,7 +40,7 @@ public void setFileService(FileService fileService) {
public abstract ExecutionProcess process(Execution execution, ExecutableInject injection)
throws Exception;

public InjectStatusCommandLine getCommandsLines(String externalId) {
public StatusPayload getPayloadOutput(String externalId) {
return null;
}

savacano28 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import io.openbas.asset.EndpointService;
import io.openbas.database.model.*;
import io.openbas.database.model.InjectExpectation.EXPECTATION_TYPE;
import io.openbas.database.model.PayloadCommandBlock;
import io.openbas.database.repository.InjectRepository;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.Injector;
import io.openbas.executors.Injector;
import io.openbas.inject_expectation.InjectExpectationService;
import io.openbas.injectors.caldera.client.model.Ability;
import io.openbas.injectors.caldera.client.model.Agent;
Expand Down Expand Up @@ -274,28 +275,32 @@
}

@Override
public InjectStatusCommandLine getCommandsLines(String externalId) {
InjectStatusCommandLine commandLine = new InjectStatusCommandLine();
Set<String> contents = new HashSet<>();
Set<String> cleanCommands = new HashSet<>();
public StatusPayload getPayloadOutput(String externalId) {
StatusPayload statusPayload = new StatusPayload();

Check warning on line 279 in openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java#L279

Added line #L279 was not covered by tests
Ability ability = calderaService.findAbilityById(externalId);
if (ability != null) {
ability
.getExecutors()
.forEach(
executor -> {
PayloadCommandBlock payloadCommandBlock = new PayloadCommandBlock();

Check warning on line 286 in openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java#L286

Added line #L286 was not covered by tests
if (executor.getCommand() != null && !executor.getCommand().isBlank()) {
contents.add(executor.getCommand());
payloadCommandBlock.setContent(executor.getCommand());

Check warning on line 288 in openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java#L288

Added line #L288 was not covered by tests
}
if (executor.getCleanup() != null && !executor.getCleanup().isEmpty()) {
cleanCommands.addAll(executor.getCleanup());
payloadCommandBlock.setCleanupCommand(executor.getCleanup());

Check warning on line 291 in openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java#L291

Added line #L291 was not covered by tests
}
if (executor.getCommandExecutor() != null
&& !executor.getCommandExecutor().isBlank()) {
payloadCommandBlock.setExecutor(executor.getCommandExecutor());

Check warning on line 295 in openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java#L295

Added line #L295 was not covered by tests
}
statusPayload.setPayloadCommandBlocks(
Collections.singletonList(payloadCommandBlock));

Check warning on line 298 in openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java#L297-L298

Added lines #L297 - L298 were not covered by tests
});
statusPayload.setExternalId(externalId);

Check warning on line 300 in openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java#L300

Added line #L300 was not covered by tests
}
commandLine.setExternalId(externalId);
commandLine.setContent(contents.stream().toList());
commandLine.setCleanupCommand(cleanCommands.stream().toList());
return commandLine;

return statusPayload;

Check warning on line 303 in openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaExecutor.java#L303

Added line #L303 was not covered by tests
}

// -- PRIVATE --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
private String name;
private String platform;
private String command;
private String commandExecutor;

Check warning on line 14 in openbas-api/src/main/java/io/openbas/injectors/caldera/client/model/Executor.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/client/model/Executor.java#L14

Added line #L14 was not covered by tests
private List<String> cleanup;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io.openbas.database.repository.ChallengeRepository;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.ExecutionContext;
import io.openbas.execution.Injector;
import io.openbas.executors.Injector;
import io.openbas.inject_expectation.InjectExpectationService;
import io.openbas.injectors.challenge.model.ChallengeContent;
import io.openbas.injectors.challenge.model.ChallengeVariable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io.openbas.database.repository.ArticleRepository;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.ExecutionContext;
import io.openbas.execution.Injector;
import io.openbas.executors.Injector;
import io.openbas.inject_expectation.InjectExpectationService;
import io.openbas.injectors.channel.model.ArticleVariable;
import io.openbas.injectors.channel.model.ChannelContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.openbas.database.model.*;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.ExecutionContext;
import io.openbas.execution.Injector;
import io.openbas.executors.Injector;
import io.openbas.inject_expectation.InjectExpectationService;
import io.openbas.injectors.email.model.EmailContent;
import io.openbas.injectors.email.service.EmailService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.openbas.database.model.Inject;
import io.openbas.database.model.InjectorContract;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.Injector;
import io.openbas.executors.Injector;
import io.openbas.injectors.lade.service.LadeService;
import io.openbas.model.ExecutionProcess;
import jakarta.validation.constraints.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.openbas.database.model.Execution;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.Injector;
import io.openbas.executors.Injector;
import io.openbas.model.ExecutionProcess;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import io.openbas.database.model.*;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.Injector;
import io.openbas.executors.Injector;
import io.openbas.injectors.mastodon.model.MastodonContent;
import io.openbas.injectors.mastodon.service.MastodonService;
import io.openbas.model.ExecutionProcess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import io.openbas.database.model.*;
import io.openbas.database.repository.InjectRepository;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.Injector;
import io.openbas.executors.Injector;
import io.openbas.inject_expectation.InjectExpectationService;
import io.openbas.injectors.openbas.model.OpenBASImplantInjectContent;
import io.openbas.model.ExecutionProcess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import io.openbas.database.model.*;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.Injector;
import io.openbas.executors.Injector;
import io.openbas.inject_expectation.InjectExpectationService;
import io.openbas.injectors.opencti.model.CaseContent;
import io.openbas.injectors.opencti.service.OpenCTIService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import io.openbas.database.model.Inject;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.ExecutionContext;
import io.openbas.execution.Injector;
import io.openbas.execution.ProtectUser;
import io.openbas.executors.Injector;
import io.openbas.inject_expectation.InjectExpectationService;
import io.openbas.injectors.ovh.model.OvhSmsContent;
import io.openbas.injectors.ovh.service.OvhSmsService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.openbas.migration;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.openbas.database.model.PayloadCommandBlock;
import io.openbas.database.model.StatusPayload;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.springframework.stereotype.Component;

@Component
public class V3_49__Update_Commands_In_Inject_Status extends BaseJavaMigration {

@Override
public void migrate(Context context) throws Exception {
Statement select = context.getConnection().createStatement();

select.execute("ALTER TABLE injects_statuses ADD status_payload_output json;");

select.execute(
"UPDATE injects_statuses SET status_commands_lines=null WHERE status_commands_lines='null';");
ResultSet results =
select.executeQuery("SELECT status_id,status_commands_lines FROM injects_statuses");

PreparedStatement statement =
context
.getConnection()
.prepareStatement(
"UPDATE injects_statuses SET status_payload_output = ?::json WHERE status_id=?");

ObjectMapper mapper = new ObjectMapper();
while (results.next()) {
String commandLine = results.getString("status_commands_lines");
String statusId = results.getString("status_id");

Check warning on line 39 in openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java#L38-L39

Added lines #L38 - L39 were not covered by tests
if (commandLine != null) {
JsonNode jsonNode = mapper.readTree(commandLine);
JsonNode content = jsonNode.get("content");
String contentString = "";

Check warning on line 43 in openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java#L41-L43

Added lines #L41 - L43 were not covered by tests
if (content.isArray()) {
for (JsonNode node : content) {
contentString += node.asText();
}

Check warning on line 47 in openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java#L46-L47

Added lines #L46 - L47 were not covered by tests
}
JsonNode cleanupCommand = jsonNode.get("cleanup_command");
List<String> cleanupCommandList = new ArrayList<>();

Check warning on line 50 in openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java#L49-L50

Added lines #L49 - L50 were not covered by tests
johanah29 marked this conversation as resolved.
Show resolved Hide resolved
if (cleanupCommand.isArray()) {
for (JsonNode node : cleanupCommand) {
cleanupCommandList.add(node.asText());
}

Check warning on line 54 in openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java#L53-L54

Added lines #L53 - L54 were not covered by tests
}
String externalId = jsonNode.get("external_id").asText();
StatusPayload statusPayload =

Check warning on line 57 in openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java#L56-L57

Added lines #L56 - L57 were not covered by tests
new StatusPayload(
null,
null,
null,
null,
null,
null,
null,
null,
externalId,
null,
null,
List.of(new PayloadCommandBlock(null, contentString, cleanupCommandList)),

Check warning on line 70 in openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java#L70

Added line #L70 was not covered by tests
null);
String value = mapper.writeValueAsString(statusPayload);
statement.setString(1, value);
statement.setString(2, statusId);
statement.addBatch();

Check warning on line 75 in openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java#L72-L75

Added lines #L72 - L75 were not covered by tests
}
}

Check warning on line 77 in openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/migration/V3_49__Update_Commands_In_Inject_Status.java#L77

Added line #L77 was not covered by tests
statement.executeBatch();
select.execute("ALTER TABLE injects_statuses DROP column status_commands_lines;");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import io.openbas.database.model.Inject;
import io.openbas.database.model.InjectExpectation;
import io.openbas.inject_expectation.InjectExpectationService;
import io.openbas.rest.atomic_testing.form.AtomicTestingInput;
import io.openbas.rest.atomic_testing.form.AtomicTestingUpdateTagsInput;
import io.openbas.rest.atomic_testing.form.InjectResultOutput;
import io.openbas.rest.atomic_testing.form.InjectResultOverviewOutput;
import io.openbas.rest.atomic_testing.form.*;
import io.openbas.rest.helper.RestBehavior;
import io.openbas.service.AtomicTestingService;
import io.openbas.telemetry.Tracing;
Expand Down Expand Up @@ -48,6 +45,13 @@
return atomicTestingService.findById(injectId);
}

@LogExecutionTime
@GetMapping("/{injectId}/payload")
@Tracing(name = "Get the payload of an atomic testing", layer = "api", operation = "GET")
public StatusPayloadOutput findAtomicTestingPayload(@PathVariable String injectId) {
return atomicTestingService.findPayloadOutputByInjectId(injectId);

Check warning on line 52 in openbas-api/src/main/java/io/openbas/rest/atomic_testing/AtomicTestingApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/atomic_testing/AtomicTestingApi.java#L52

Added line #L52 was not covered by tests
}

@PostMapping()
@Transactional(rollbackFor = Exception.class)
public InjectResultOverviewOutput createAtomicTesting(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.openbas.rest.atomic_testing.form;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.openbas.database.converter.ContentConverter;
import io.openbas.database.model.Endpoint;
import jakarta.persistence.Convert;
import jakarta.validation.constraints.NotBlank;
import java.util.*;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Setter

Check warning on line 14 in openbas-api/src/main/java/io/openbas/rest/atomic_testing/form/AtomicInjectorContractOutput.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/atomic_testing/form/AtomicInjectorContractOutput.java#L14

Added line #L14 was not covered by tests
@Getter
@Builder
public class AtomicInjectorContractOutput {

@JsonProperty("injector_contract_id")
@NotBlank
private String id;

@JsonProperty("injector_contract_content")
@NotBlank
private String content;

@JsonProperty("convertedContent")
@Convert(converter = ContentConverter.class)
private ObjectNode convertedContent;

@JsonProperty("injector_contract_platforms")
private Endpoint.PLATFORM_TYPE[] platforms;

@JsonProperty("injector_contract_payload")
private PayloadSimple payload;

@Builder.Default
@JsonProperty("injector_contract_labels")
@NotBlank
private Map<String, String> labels = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Setter

Check warning on line 10 in openbas-api/src/main/java/io/openbas/rest/atomic_testing/form/AttackPatternSimple.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/atomic_testing/form/AttackPatternSimple.java#L10

Added line #L10 was not covered by tests
@Builder
public class AttackPatternSimple {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.openbas.database.model.InjectStatusCommandLine;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
Expand Down Expand Up @@ -38,10 +37,6 @@ public class InjectResultOverviewOutput {
@JsonProperty("inject_content")
private ObjectNode content;

@Schema(description = "Command lines for inject")
@JsonProperty("inject_commands_lines")
private InjectStatusCommandLine commandsLines;

@Schema(description = "Type of inject")
@JsonProperty("inject_type")
private String type;
Expand All @@ -57,7 +52,7 @@ public class InjectResultOverviewOutput {
@Schema(description = "Full contract")
@JsonProperty("inject_injector_contract")
@NotNull
private InjectorContractSimple injectorContract;
private AtomicInjectorContractOutput injectorContract;

@Schema(description = "status")
@JsonProperty("inject_status")
Expand Down
Loading
Loading