-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marine LM <[email protected]>
- Loading branch information
Showing
10 changed files
with
216 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
openbas-api/src/main/java/io/openbas/injectors/openbas/util/OpenBASObfuscationMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.openbas.injectors.openbas.util; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
import lombok.Getter; | ||
|
||
public class OpenBASObfuscationMap { | ||
private final Map<String, OpenBASObfuscation> obfuscationMap; | ||
|
||
@Getter | ||
public static class OpenBASObfuscation { | ||
private final String information; | ||
private final BiFunction<String, String, String> obfuscate; | ||
|
||
public OpenBASObfuscation(String information, BiFunction<String, String, String> obfuscate) { | ||
this.information = information; | ||
this.obfuscate = obfuscate; | ||
} | ||
} | ||
|
||
public OpenBASObfuscationMap() { | ||
this.obfuscationMap = new HashMap<>(); | ||
this.registerObfuscation("plain-text", "", this::obfuscatePlainText); | ||
this.registerObfuscation( | ||
"base64", "CMD does not support base64 obfuscation", this::obfuscateBase64); | ||
} | ||
|
||
public void registerObfuscation( | ||
String key, String information, BiFunction<String, String, String> function) { | ||
if (key == null || function == null) { | ||
throw new IllegalArgumentException("Key and function must not be null."); | ||
} | ||
obfuscationMap.put(key, new OpenBASObfuscation(information, function)); | ||
} | ||
|
||
public String executeObfuscation(String key, String command, String executor) { | ||
OpenBASObfuscation obfuscation = obfuscationMap.get(key); | ||
if (obfuscation != null) { | ||
return obfuscation.getObfuscate().apply(command, executor); | ||
} | ||
throw new IllegalArgumentException("No obfuscation found for key: " + key); | ||
} | ||
|
||
public Map<String, String> getAllObfuscationInfo() { | ||
Map<String, String> keyInfoMap = new HashMap<>(); | ||
for (Map.Entry<String, OpenBASObfuscation> entry : obfuscationMap.entrySet()) { | ||
keyInfoMap.put(entry.getKey(), entry.getValue().getInformation()); | ||
} | ||
return keyInfoMap; | ||
} | ||
|
||
private String obfuscatePlainText(String command, String executor) { | ||
return command; | ||
} | ||
|
||
private String obfuscateBase64(String command, String executor) { | ||
String obfuscatedCommand = command; | ||
|
||
if (executor.equals("psh") || executor.equals("cmd")) { | ||
byte[] utf16Bytes = command.getBytes(StandardCharsets.UTF_16LE); | ||
String base64 = Base64.getEncoder().encodeToString(utf16Bytes); | ||
obfuscatedCommand = String.format("powershell -Enc %s", base64); | ||
|
||
} else if (executor.equals("bash") || executor.equals("sh")) { | ||
obfuscatedCommand = | ||
String.format( | ||
"eval \"$(echo %s | base64 --decode)\"", | ||
Base64.getEncoder().encodeToString(command.getBytes())); | ||
} | ||
return obfuscatedCommand; | ||
} | ||
|
||
public String getDefaultObfuscator() { | ||
return "plain-text"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ramework/src/main/java/io/openbas/injector_contract/fields/ContractChoiceInformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.openbas.injector_contract.fields; | ||
|
||
import io.openbas.injector_contract.ContractCardinality; | ||
import io.openbas.injector_contract.ContractType; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
public class ContractChoiceInformation extends ContractCardinalityElement { | ||
private List<ChoiceItem> choices = List.of(); | ||
|
||
public ContractChoiceInformation(String key, String label, ContractCardinality cardinality) { | ||
super(key, label, cardinality); | ||
} | ||
|
||
@Getter | ||
public static class ChoiceItem { | ||
private final String label; | ||
private final String value; | ||
private final String information; | ||
|
||
public ChoiceItem(String label, String value, String information) { | ||
this.information = information; | ||
this.label = label; | ||
this.value = value; | ||
} | ||
} | ||
|
||
public static ContractChoiceInformation choiceInformationField( | ||
String key, String label, Map<String, String> choiceInformations, String def) { | ||
ContractChoiceInformation contractChoice = | ||
new ContractChoiceInformation(key, label, ContractCardinality.One); | ||
|
||
ArrayList<ChoiceItem> choiceItems = new ArrayList<>(); | ||
for (Map.Entry<String, String> entry : choiceInformations.entrySet()) { | ||
choiceItems.add(new ChoiceItem(entry.getKey(), entry.getKey(), entry.getValue())); | ||
} | ||
|
||
contractChoice.setChoices(choiceItems); | ||
contractChoice.setDefaultValue(List.of(def)); | ||
return contractChoice; | ||
} | ||
|
||
@Override | ||
public ContractType getType() { | ||
return ContractType.Choice; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.