Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 535103e

Browse files
committed
[client] Added samples to malware
1 parent 7ab3329 commit 535103e

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

pycti/entities/opencti_malware.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def __init__(self, opencti):
108108
created
109109
modified
110110
}
111+
samples {
112+
id
113+
}
111114
"""
112115
self.properties_with_files = """
113116
id
@@ -221,6 +224,9 @@ def __init__(self, opencti):
221224
created
222225
modified
223226
}
227+
samples {
228+
id
229+
}
224230
importFiles {
225231
edges {
226232
node {
@@ -411,6 +417,7 @@ def create(self, **kwargs):
411417
x_opencti_stix_ids = kwargs.get("x_opencti_stix_ids", None)
412418
granted_refs = kwargs.get("objectOrganization", None)
413419
x_opencti_workflow_id = kwargs.get("x_opencti_workflow_id", None)
420+
samples = kwargs.get("samples", None)
414421
update = kwargs.get("update", False)
415422

416423
if name is not None:
@@ -453,6 +460,7 @@ def create(self, **kwargs):
453460
"killChainPhases": kill_chain_phases,
454461
"x_opencti_stix_ids": x_opencti_stix_ids,
455462
"x_opencti_workflow_id": x_opencti_workflow_id,
463+
"samples": samples,
456464
"update": update,
457465
}
458466
},
@@ -572,6 +580,7 @@ def import_from_stix2(self, **kwargs):
572580
if "x_opencti_workflow_id" in stix_object
573581
else None
574582
),
583+
samples=(extras["sample_ids"] if "sample_ids" in extras else None),
575584
update=update,
576585
)
577586
else:

pycti/utils/opencti_stix2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,10 @@ def extract_embedded_relationships(
770770
)
771771
elif "x_opencti_granted_refs" in stix_object:
772772
granted_refs_ids = stix_object["x_opencti_granted_refs"]
773+
# Sample refs
774+
sample_refs_ids = (
775+
stix_object["sample_refs"] if "sample_refs" in stix_object else []
776+
)
773777

774778
return {
775779
"created_by": created_by_id,
@@ -779,6 +783,7 @@ def extract_embedded_relationships(
779783
"kill_chain_phases": kill_chain_phases_ids,
780784
"object_refs": object_refs_ids,
781785
"granted_refs": granted_refs_ids,
786+
"sample_refs": sample_refs_ids,
782787
"external_references": external_references_ids,
783788
"reports": reports,
784789
}
@@ -863,6 +868,7 @@ def import_object(
863868
object_refs_ids = embedded_relationships["object_refs"]
864869
external_references_ids = embedded_relationships["external_references"]
865870
reports = embedded_relationships["reports"]
871+
sample_refs_ids = embedded_relationships["sample_refs"]
866872

867873
# Extra
868874
extras = {
@@ -874,6 +880,7 @@ def import_object(
874880
"object_ids": object_refs_ids,
875881
"external_references_ids": external_references_ids,
876882
"reports": reports,
883+
"sample_ids": sample_refs_ids,
877884
}
878885

879886
# Import
@@ -1000,6 +1007,7 @@ def import_observable(
10001007
object_refs_ids = embedded_relationships["object_refs"]
10011008
external_references_ids = embedded_relationships["external_references"]
10021009
reports = embedded_relationships["reports"]
1010+
sample_refs_ids = embedded_relationships["sample_refs"]
10031011

10041012
# Extra
10051013
extras = {
@@ -1012,6 +1020,7 @@ def import_observable(
10121020
"object_ids": object_refs_ids,
10131021
"external_references_ids": external_references_ids,
10141022
"reports": reports,
1023+
"sample_ids": sample_refs_ids,
10151024
}
10161025
if stix_object["type"] == "simple-observable":
10171026
stix_observable_result = self.opencti.stix_cyber_observable.create(
@@ -1176,6 +1185,7 @@ def import_relationship(
11761185
object_refs_ids = embedded_relationships["object_refs"]
11771186
external_references_ids = embedded_relationships["external_references"]
11781187
reports = embedded_relationships["reports"]
1188+
sample_refs_ids = embedded_relationships["sample_refs"]
11791189

11801190
# Extra
11811191
extras = {
@@ -1188,6 +1198,7 @@ def import_relationship(
11881198
"object_ids": object_refs_ids,
11891199
"external_references_ids": external_references_ids,
11901200
"reports": reports,
1201+
"sample_ids": sample_refs_ids,
11911202
}
11921203

11931204
# Create the relation
@@ -1271,6 +1282,7 @@ def import_sighting(
12711282
object_refs_ids = embedded_relationships["object_refs"]
12721283
external_references_ids = embedded_relationships["external_references"]
12731284
reports = embedded_relationships["reports"]
1285+
sample_refs_ids = embedded_relationships["sample_refs"]
12741286

12751287
# Extra
12761288
extras = {
@@ -1283,6 +1295,7 @@ def import_sighting(
12831295
"object_ids": object_refs_ids,
12841296
"external_references_ids": external_references_ids,
12851297
"reports": reports,
1298+
"sample_ids": sample_refs_ids,
12861299
}
12871300

12881301
# Create the sighting
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# coding: utf-8
2+
3+
4+
def test_malware_import_with_sample_refs(api_client):
5+
with open("tests/data/basicMalwareWithSample.json", "r") as content_file:
6+
content = content_file.read()
7+
8+
imported_malware_bundle = api_client.stix2.import_bundle_from_json(
9+
json_data=content
10+
)
11+
assert imported_malware_bundle is not None
12+
13+
for importObject in imported_malware_bundle:
14+
if importObject["type"] == "malware":
15+
assert (
16+
api_client.malware.read(id=importObject["id"]).get("samples") is not []
17+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"type": "bundle",
3+
"id": "bundle--59860f2d-2245-4901-b40e-46b51856bde4",
4+
"objects": [
5+
{
6+
"id": "malware--d650c5b9-4b43-5781-8576-ea52bd6c7ce0",
7+
"spec_version": "2.1",
8+
"revoked": false,
9+
"confidence": 100,
10+
"created": "2024-03-13T09:56:18.259Z",
11+
"modified": "2024-03-13T09:56:18.259Z",
12+
"name": "BasicMalware",
13+
"is_family": false,
14+
"x_opencti_id": "75f2a512-fcc6-4cbc-a2ef-52ca9c57df46",
15+
"x_opencti_type": "Malware",
16+
"type": "malware",
17+
"sample_refs": [
18+
"file--9fb3f45e-ffce-5525-95a5-906a6695aa85"
19+
]
20+
},
21+
{
22+
"id": "file--9fb3f45e-ffce-5525-95a5-906a6695aa85",
23+
"spec_version": "2.1",
24+
"x_opencti_score": 50,
25+
"name": "basicFile",
26+
"x_opencti_id": "8c555399-ad2c-4862-8113-0889a3c14116",
27+
"x_opencti_type": "StixFile",
28+
"type": "file"
29+
}
30+
]
31+
}

0 commit comments

Comments
 (0)