Skip to content

Commit

Permalink
Add expiration time on automatic expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
johanah29 committed Sep 26, 2024
1 parent a86cff0 commit 5c209cb
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,126 +38,130 @@
@RequiredArgsConstructor
public class CalderaContract extends Contractor {

public static final String TYPE = "openbas_caldera";

private final CalderaInjectorConfig config;
private final CalderaInjectorService injectorCalderaService;

@Override
public boolean isExpose() {
return this.config.isEnable();
public static final String TYPE = "openbas_caldera";

private final CalderaInjectorConfig config;
private final CalderaInjectorService injectorCalderaService;

@Override
public boolean isExpose() {
return this.config.isEnable();
}

@Override
public String getType() {
return TYPE;

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L53

Added line #L53 was not covered by tests
}

@Override
public ContractConfig getConfig() {
Map<SupportedLanguage, String> labels = Map.of(en, "Caldera", fr, "Caldera");
return new ContractConfig(TYPE, labels, "#8b0000", "#8b0000", "/img/icon-caldera.png", isExpose());

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L58-L59

Added lines #L58 - L59 were not covered by tests
}

@Override
public List<Contract> contracts() {
if (this.config.isEnable()) {
ContractConfig contractConfig = getConfig();

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L65

Added line #L65 was not covered by tests
// Add contract based on abilities
return new ArrayList<>(abilityContracts(contractConfig));

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L67

Added line #L67 was not covered by tests
}

@Override
public String getType() {
return TYPE;
}

@Override
public ContractConfig getConfig() {
Map<SupportedLanguage, String> labels = Map.of(en, "Caldera", fr, "Caldera");
return new ContractConfig(TYPE, labels, "#8b0000", "#8b0000", "/img/icon-caldera.png", isExpose());
}

@Override
public List<Contract> contracts() {
if (this.config.isEnable()) {
ContractConfig contractConfig = getConfig();
// Add contract based on abilities
return new ArrayList<>(abilityContracts(contractConfig));
return List.of();

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L69

Added line #L69 was not covered by tests
}

// -- PRIVATE --

private ContractSelect obfuscatorField() {
List<Obfuscator> obfuscators = this.injectorCalderaService.obfuscators();
Map<String, String> obfuscatorChoices = obfuscators.stream()
.collect(Collectors.toMap(Obfuscator::getName, Obfuscator::getName));
return selectFieldWithDefault(

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L75-L78

Added lines #L75 - L78 were not covered by tests
"obfuscator",
"Obfuscators",
obfuscatorChoices,
"base64"
);
}

private ContractExpectations expectations() {
// Prevention
Expectation preventionExpectation = new Expectation();
preventionExpectation.setType(PREVENTION);
preventionExpectation.setName("Expect inject to be prevented");
preventionExpectation.setScore(100.0);
preventionExpectation.setExpirationTime(21600L);

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L88-L92

Added lines #L88 - L92 were not covered by tests
// Detection
Expectation detectionExpectation = new Expectation();
detectionExpectation.setType(DETECTION);
detectionExpectation.setName("Expect inject to be detected");
detectionExpectation.setScore(100.0);
detectionExpectation.setExpirationTime(21600L);
return expectationsField("expectations", "Expectations", List.of(preventionExpectation, detectionExpectation));

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L94-L99

Added lines #L94 - L99 were not covered by tests
}

private List<Contract> abilityContracts(@NotNull final ContractConfig contractConfig) {
// Fields
ContractSelect obfuscatorField = obfuscatorField();
ContractAsset assetField = assetField("assets", "Assets", Multiple);
ContractAssetGroup assetGroupField = assetGroupField("assetgroups", "Asset groups", Multiple);
ContractExpectations expectationsField = expectations();

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L104-L107

Added lines #L104 - L107 were not covered by tests

List<Ability> abilities = this.injectorCalderaService.abilities().stream()

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L109

Added line #L109 was not covered by tests
.filter(ability -> !ability.getTactic().equals("openbas")).toList();
// Build contracts
return abilities.stream().map((ability -> {
ContractDef builder = contractBuilder();
builder.mandatoryGroup(assetField, assetGroupField);
builder.optional(obfuscatorField);
builder.optional(expectationsField);
List<PLATFORM_TYPE> platforms = new ArrayList<>();
ability.getExecutors().forEach(executor -> {
String command = executor.getCommand();

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L112-L119

Added lines #L112 - L119 were not covered by tests
if (command != null && !command.isEmpty()) {
Matcher matcher = Pattern.compile("#\\{(.*?)\\}").matcher(command);

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L121

Added line #L121 was not covered by tests
while (matcher.find()) {
if (!matcher.group(1).isEmpty()) {
builder.mandatory(ContractText.textField(matcher.group(1), matcher.group(1)));

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L124

Added line #L124 was not covered by tests
}
}
}
return List.of();
}

// -- PRIVATE --

private ContractSelect obfuscatorField() {
List<Obfuscator> obfuscators = this.injectorCalderaService.obfuscators();
Map<String, String> obfuscatorChoices = obfuscators.stream().collect(Collectors.toMap(Obfuscator::getName, Obfuscator::getName));
return selectFieldWithDefault(
"obfuscator",
"Obfuscators",
obfuscatorChoices,
"base64"
);
}

private ContractExpectations expectations() {
// Prevention
Expectation preventionExpectation = new Expectation();
preventionExpectation.setType(PREVENTION);
preventionExpectation.setName("Expect inject to be prevented");
preventionExpectation.setScore(100.0);
// Detection
Expectation detectionExpectation = new Expectation();
detectionExpectation.setType(DETECTION);
detectionExpectation.setName("Expect inject to be detected");
detectionExpectation.setScore(100.0);
return expectationsField("expectations", "Expectations", List.of(preventionExpectation, detectionExpectation));
}

private List<Contract> abilityContracts(@NotNull final ContractConfig contractConfig) {
// Fields
ContractSelect obfuscatorField = obfuscatorField();
ContractAsset assetField = assetField("assets", "Assets", Multiple);
ContractAssetGroup assetGroupField = assetGroupField("assetgroups", "Asset groups", Multiple);
ContractExpectations expectationsField = expectations();

List<Ability> abilities = this.injectorCalderaService.abilities().stream().filter(ability -> !ability.getTactic().equals("openbas")).toList();
// Build contracts
return abilities.stream().map((ability -> {
ContractDef builder = contractBuilder();
builder.mandatoryGroup(assetField, assetGroupField);
builder.optional(obfuscatorField);
builder.optional(expectationsField);
List<PLATFORM_TYPE> platforms = new ArrayList<>();
ability.getExecutors().forEach(executor -> {
String command = executor.getCommand();
if (command != null && !command.isEmpty()) {
Matcher matcher = Pattern.compile("#\\{(.*?)\\}").matcher(command);
while (matcher.find()) {
if (!matcher.group(1).isEmpty()) {
builder.mandatory(ContractText.textField(matcher.group(1), matcher.group(1)));
}
}
}
if (!executor.getPlatform().equals("unknown")) {
PLATFORM_TYPE platform = toPlatform(executor.getPlatform());
if (!platforms.contains(platform)) {
platforms.add(platform);
}
} else {
if (executor.getName().equals("psh")) {
if (!platforms.contains(PLATFORM_TYPE.Windows)) {
platforms.add(PLATFORM_TYPE.Windows);
}
} else if (executor.getName().equals("sh")) {
if (!platforms.contains(PLATFORM_TYPE.Linux)) {
platforms.add(PLATFORM_TYPE.Linux);
}
} else if (executor.getName().equals("cmd")) {
if (!platforms.contains(PLATFORM_TYPE.Windows)) {
platforms.add(PLATFORM_TYPE.Windows);
}
}
}
});
Contract contract = executableContract(
contractConfig,
ability.getAbility_id(),
Map.of(en, ability.getName(), fr, ability.getName()),
builder.build(),
platforms,
true
);
contract.addAttackPattern(ability.getTechnique_id());
return contract;
})).collect(Collectors.toList());
}

@Override
public ContractorIcon getIcon() {
InputStream iconStream = getClass().getResourceAsStream("/img/icon-caldera.png");
return new ContractorIcon(iconStream);
}
if (!executor.getPlatform().equals("unknown")) {
PLATFORM_TYPE platform = toPlatform(executor.getPlatform());

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L129

Added line #L129 was not covered by tests
if (!platforms.contains(platform)) {
platforms.add(platform);

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L131

Added line #L131 was not covered by tests
}
} else {

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L133

Added line #L133 was not covered by tests
if (executor.getName().equals("psh")) {
if (!platforms.contains(PLATFORM_TYPE.Windows)) {
platforms.add(PLATFORM_TYPE.Windows);

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L136

Added line #L136 was not covered by tests
}
} else if (executor.getName().equals("sh")) {
if (!platforms.contains(PLATFORM_TYPE.Linux)) {
platforms.add(PLATFORM_TYPE.Linux);

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L140

Added line #L140 was not covered by tests
}
} else if (executor.getName().equals("cmd")) {
if (!platforms.contains(PLATFORM_TYPE.Windows)) {
platforms.add(PLATFORM_TYPE.Windows);

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L144

Added line #L144 was not covered by tests
}
}
}
});
Contract contract = executableContract(

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L148-L149

Added lines #L148 - L149 were not covered by tests
contractConfig,
ability.getAbility_id(),
Map.of(en, ability.getName(), fr, ability.getName()),
builder.build(),

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L151-L153

Added lines #L151 - L153 were not covered by tests
platforms,
true
);
contract.addAttackPattern(ability.getTechnique_id());
return contract;
})).collect(Collectors.toList());

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L157-L159

Added lines #L157 - L159 were not covered by tests
}

@Override
public ContractorIcon getIcon() {
InputStream iconStream = getClass().getResourceAsStream("/img/icon-caldera.png");
return new ContractorIcon(iconStream);

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

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/injectors/caldera/CalderaContract.java#L164-L165

Added lines #L164 - L165 were not covered by tests
}
}
Loading

0 comments on commit 5c209cb

Please sign in to comment.