Skip to content

Commit 8e9d2bc

Browse files
camrrxRomuDeuxfois
andcommitted
[frontend] Impossible to create a payload of type executable file
Co-authored-by: Romuald Lemesle <[email protected]>
1 parent a32d2b3 commit 8e9d2bc

File tree

11 files changed

+68
-59
lines changed

11 files changed

+68
-59
lines changed

openbas-api/src/test/java/io/openbas/rest/DocumentApiTest.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@
4141
class DocumentApiTest extends IntegrationTest {
4242

4343
@Resource protected ObjectMapper mapper;
44-
45-
@Autowired private MockMvc mvc;
46-
4744
@Autowired DocumentComposer documentComposer;
4845
@Autowired ChallengeComposer challengeComposer;
4946
@Autowired PayloadComposer payloadComposer;
47+
@Autowired private MockMvc mvc;
5048
@Autowired private DocumentRepository documentRepository;
5149
@Autowired private ChallengeRepository challengeRepository;
5250

@@ -62,6 +60,34 @@ void afterAll() {
6260
documentComposer.reset();
6361
}
6462

63+
private Document getDocumentWithChallenge() {
64+
65+
ChallengeComposer.Composer challenge =
66+
challengeComposer.forChallenge(ChallengeFixture.createDefaultChallenge());
67+
68+
BinaryFile badCoffeeFileContent = FileFixture.getBadCoffeeFileContent();
69+
return documentComposer
70+
.forDocument(DocumentFixture.getDocument(badCoffeeFileContent))
71+
.withInMemoryFile(badCoffeeFileContent)
72+
.withChallenge(challenge)
73+
.persist()
74+
.get();
75+
}
76+
77+
private Document getDocumentWithPayload() {
78+
79+
PayloadComposer.Composer payload =
80+
payloadComposer.forPayload(PayloadFixture.createDefaultExecutable());
81+
82+
BinaryFile badCoffeeFileContent = FileFixture.getBadCoffeeFileContent();
83+
return documentComposer
84+
.forDocument(DocumentFixture.getDocument(badCoffeeFileContent))
85+
.withInMemoryFile(badCoffeeFileContent)
86+
.withPayloadExecutable(payload)
87+
.persist()
88+
.get();
89+
}
90+
6591
@Nested
6692
@DisplayName("Documents CRUD")
6793
@WithMockAdminUser
@@ -115,32 +141,4 @@ void givenDocumentShouldFetchRelatedEntities() throws Exception {
115141
assertThatJson(response).when(IGNORING_ARRAY_ORDER).isEqualTo(relationJson);
116142
}
117143
}
118-
119-
private Document getDocumentWithChallenge() {
120-
121-
ChallengeComposer.Composer challenge =
122-
challengeComposer.forChallenge(ChallengeFixture.createDefaultChallenge());
123-
124-
BinaryFile badCoffeeFileContent = FileFixture.getBadCoffeeFileContent();
125-
return documentComposer
126-
.forDocument(DocumentFixture.getDocument(badCoffeeFileContent))
127-
.withInMemoryFile(badCoffeeFileContent)
128-
.withChallenge(challenge)
129-
.persist()
130-
.get();
131-
}
132-
133-
private Document getDocumentWithPayload() {
134-
135-
PayloadComposer.Composer payload =
136-
payloadComposer.forPayload(PayloadFixture.createDefaultExecutable());
137-
138-
BinaryFile badCoffeeFileContent = FileFixture.getBadCoffeeFileContent();
139-
return documentComposer
140-
.forDocument(DocumentFixture.getDocument(badCoffeeFileContent))
141-
.withInMemoryFile(badCoffeeFileContent)
142-
.withPayloadExecutable(payload)
143-
.persist()
144-
.get();
145-
}
146144
}

openbas-api/src/test/java/io/openbas/rest/PayloadApiSearchTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1515

1616
import io.openbas.IntegrationTest;
17+
import io.openbas.database.model.Document;
1718
import io.openbas.database.model.Payload;
19+
import io.openbas.database.repository.DocumentRepository;
1820
import io.openbas.database.repository.PayloadRepository;
21+
import io.openbas.utils.fixtures.DocumentFixture;
1922
import io.openbas.utils.fixtures.PaginationFixture;
2023
import io.openbas.utils.mockUser.WithMockAdminUser;
2124
import io.openbas.utils.pagination.SearchPaginationInput;
@@ -30,11 +33,10 @@
3033
@TestInstance(PER_CLASS)
3134
public class PayloadApiSearchTest extends IntegrationTest {
3235

36+
private static final List<String> PAYLOAD_COMMAND_IDS = new ArrayList<>();
3337
@Autowired private MockMvc mvc;
34-
3538
@Autowired private PayloadRepository payloadRepository;
36-
37-
private static final List<String> PAYLOAD_COMMAND_IDS = new ArrayList<>();
39+
@Autowired private DocumentRepository documentRepository;
3840

3941
@BeforeAll
4042
void beforeAll() {
@@ -46,7 +48,10 @@ void beforeAll() {
4648
Payload dnsResolutionSaved = this.payloadRepository.save(dnsResolution);
4749
PAYLOAD_COMMAND_IDS.add(dnsResolutionSaved.getId());
4850

49-
Payload executable = createDefaultExecutable();
51+
Document document = DocumentFixture.getDocumentJpeg();
52+
Document documentSaved = this.documentRepository.save(document);
53+
54+
Payload executable = createDefaultExecutable(documentSaved);
5055
Payload executableSaved = this.payloadRepository.save(executable);
5156
PAYLOAD_COMMAND_IDS.add(executableSaved.getId());
5257
}

openbas-api/src/test/java/io/openbas/rest/inject/InjectImportTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
@DisplayName("Importing injects tests")
5353
public class InjectImportTest extends IntegrationTest {
5454

55+
public final String INJECT_IMPORT_URI = INJECT_URI + "/import";
56+
private final Map<String, ArticleComposer.Composer> staticArticleWrappers = new HashMap<>();
57+
private final String KNOWN_ARTICLE_WRAPPER_KEY = "known article key";
5558
@Autowired ObjectMapper objectMapper;
5659
@Autowired MockMvc mvc;
5760
@Autowired InjectExportService exportService;
@@ -78,7 +81,6 @@ public class InjectImportTest extends IntegrationTest {
7881
@Autowired private InjectRepository injectRepository;
7982
@Autowired private ArticleService articleService;
8083
@Autowired private InjectorFixture injectorFixture;
81-
8284
@MockBean private Ee eeService;
8385

8486
@BeforeEach
@@ -108,10 +110,6 @@ void before() throws Exception {
108110
clearEntityManager();
109111
}
110112

111-
public final String INJECT_IMPORT_URI = INJECT_URI + "/import";
112-
private final Map<String, ArticleComposer.Composer> staticArticleWrappers = new HashMap<>();
113-
private final String KNOWN_ARTICLE_WRAPPER_KEY = "known article key";
114-
115113
private Map<String, ArticleComposer.Composer> getStaticArticleWrappers() {
116114
if (!staticArticleWrappers.containsKey(KNOWN_ARTICLE_WRAPPER_KEY)) {
117115
staticArticleWrappers.put(

openbas-api/src/test/java/io/openbas/utils/fixtures/PayloadFixture.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ public static Payload createDefaultDnsResolution() {
7272
return dnsResolution;
7373
}
7474

75+
public static Payload createDefaultExecutable(Document document) {
76+
final Executable executable =
77+
new Executable("executable-id", Executable.EXECUTABLE_TYPE, "executable payload");
78+
executable.setExecutionArch(Payload.PAYLOAD_EXECUTION_ARCH.arm64);
79+
executable.setExecutableFile(document);
80+
initializeDefaultPayload(executable, MACOS_PLATFORM);
81+
return executable;
82+
}
83+
7584
public static Payload createDefaultExecutable() {
7685
final Executable executable =
7786
new Executable("executable-id", Executable.EXECUTABLE_TYPE, "executable payload");

openbas-front/src/admin/components/payloads/CreatePayload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CreatePayload extends Component {
5454
R.assoc('payload_platforms', data.payload_platforms),
5555
R.assoc('payload_tags', data.payload_tags),
5656
R.assoc('payload_attack_patterns', data.payload_attack_patterns),
57-
R.assoc('executable_file', data.executable_file?.id),
57+
R.assoc('executable_file', data.executable_file),
5858
R.assoc('payload_cleanup_executor', handleCleanupExecutorValue(data.payload_cleanup_executor, data.payload_cleanup_command)),
5959
R.assoc('payload_cleanup_command', handleCleanupCommandValue(data.payload_cleanup_command)),
6060
R.assoc('payload_detection_remediations', Object.entries(data.remediations).filter(value => value[1]).map(value => ({

openbas-front/src/admin/components/payloads/PayloadForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const PayloadForm = ({
3636
payload_attack_patterns: [],
3737
payload_cleanup_command: '',
3838
payload_cleanup_executor: '',
39-
executable_file: undefined,
39+
executable_file: '',
4040
file_drop_file: '',
4141
dns_resolution_hostname: '',
4242
payload_tags: [],
@@ -161,7 +161,7 @@ const PayloadForm = ({
161161
const executableSchema = z.object({
162162
...baseSchema,
163163
payload_type: z.literal('Executable').describe('Commands-tab'),
164-
executable_file: z.string().optional().describe('Commands-tab'),
164+
executable_file: z.string().min(1, { message: t('Should not be empty') }).describe('Commands-tab'),
165165
});
166166
const fileDropSchema = z.object({
167167
...baseSchema,

openbas-front/src/admin/components/payloads/PayloadPopover.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ import DialogDelete from '../../../components/common/DialogDelete';
1414
import Drawer from '../../../components/common/Drawer';
1515
import Transition from '../../../components/common/Transition';
1616
import { useFormatter } from '../../../components/i18n';
17-
import { documentOptions } from '../../../utils/Option';
1817
import { download } from '../../../utils/utils.js';
1918
import PayloadForm from './PayloadForm';
2019

21-
const PayloadPopover = ({ payload, documentsMap, onUpdate, onDelete, onDuplicate, disableUpdate, disableDelete }) => {
20+
const PayloadPopover = ({ payload, onUpdate, onDelete, onDuplicate, disableUpdate, disableDelete }) => {
2221
const [openDuplicate, setOpenDuplicate] = useState(false);
2322
const [openEdit, setOpenEdit] = useState(false);
2423
const [anchorEl, setAnchorEl] = useState(null);
@@ -48,7 +47,7 @@ const PayloadPopover = ({ payload, documentsMap, onUpdate, onDelete, onDuplicate
4847
R.assoc('payload_platforms', data.payload_platforms),
4948
R.assoc('payload_tags', data.payload_tags),
5049
R.assoc('payload_attack_patterns', data.payload_attack_patterns),
51-
R.assoc('executable_file', data.executable_file?.id),
50+
R.assoc('executable_file', data.executable_file),
5251
R.assoc('payload_cleanup_executor', handleCleanupExecutorValue(data.payload_cleanup_executor, data.payload_cleanup_command)),
5352
R.assoc('payload_cleanup_command', handleCleanupCommandValue(data.payload_cleanup_command)),
5453
R.assoc('payload_detection_remediations', Object.entries(data.remediations).filter(value => value[1]).map(value => ({
@@ -108,7 +107,6 @@ const PayloadPopover = ({ payload, documentsMap, onUpdate, onDelete, onDuplicate
108107
});
109108
};
110109

111-
const payloadExecutableFiles = documentOptions(payload.executable_file ? [payload.executable_file] : [], documentsMap);
112110
const initialValues = {
113111
payload_name: payload.payload_name,
114112
payload_description: payload.payload_description,
@@ -124,7 +122,7 @@ const PayloadPopover = ({ payload, documentsMap, onUpdate, onDelete, onDuplicate
124122
payload_execution_arch: payload.payload_execution_arch,
125123
payload_output_parsers: payload.payload_output_parsers,
126124
payload_platforms: payload.payload_platforms,
127-
executable_file: R.head(payloadExecutableFiles),
125+
executable_file: payload.executable_file,
128126
payload_cleanup_executor: payload.payload_cleanup_executor === null ? '' : payload.payload_cleanup_executor,
129127
payload_cleanup_command: payload.payload_cleanup_command === null ? '' : payload.payload_cleanup_command,
130128
remediations: {},

openbas-front/src/admin/components/payloads/Payloads.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const Payloads = () => {
107107
const dispatch = useAppDispatch();
108108

109109
const [selectedPayload, setSelectedPayload] = useState<Payload | null>(null);
110-
const { documentsMap, collectorsMap } = useHelper((helper: DocumentHelper & CollectorHelper) => ({
110+
const { collectorsMap } = useHelper((helper: DocumentHelper & CollectorHelper) => ({
111111
documentsMap: helper.getDocumentsMap(),
112112
collectorsMap: helper.getCollectorsMap(),
113113
}));
@@ -297,7 +297,6 @@ const Payloads = () => {
297297
divider
298298
secondaryAction={(
299299
<PayloadPopover
300-
documentsMap={documentsMap}
301300
payload={payload}
302301
onUpdate={(result: Payload) => setPayloads(payloads.map(a => (a.payload_id !== result.payload_id ? a : result)))}
303302
onDuplicate={(result: Payload) => setPayloads([result, ...payloads])}

openbas-front/src/admin/components/payloads/form/CommandsFormTab.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ const CommandsFormTab = ({ disabledPayloadType = false }: Props) => {
132132
name="executable_file"
133133
label={t('Executable file')}
134134
setFieldValue={(_name, document) => {
135-
onChange(document);
135+
onChange(document?.id);
136136
}}
137-
initialValue={{ id: value?.id }}
137+
initialValue={{ id: value }}
138138
InputLabelProps={{ required: true }}
139139
error={!!error}
140140
/>

openbas-front/src/utils/api-types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ export interface EvaluationInput {
18131813
}
18141814

18151815
export interface Executable {
1816-
executable_file?: string;
1816+
executable_file: string;
18171817
listened?: boolean;
18181818
payload_arguments?: PayloadArgument[];
18191819
payload_attack_patterns?: string[];

0 commit comments

Comments
 (0)