Skip to content

Commit 607c3d5

Browse files
author
Samuel Hassine
committed
[client] Fix the direction of exported relations in STIX2
1 parent 0533167 commit 607c3d5

34 files changed

+50
-38
lines changed

examples/add_external_reference_to_report.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/add_organization_to_sector.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Variables
88
api_url = "https://demo.opencti.io"
9-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
9+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
1010

1111
# OpenCTI initialization
1212
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/add_tag_to_malware.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/add_tool_usage_to_intrusion-set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Variables
99
api_url = "https://demo.opencti.io"
10-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
10+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
1111

1212
# OpenCTI initialization
1313
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_campaign_attributed-to_intrusion_set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_hashes_and_link_together.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_incident_with_ttps_and_indicators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Variables
99
api_url = "https://demo.opencti.io"
10-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
10+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
1111

1212
# OpenCTI initialization
1313
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_indicator_of_campaign.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Variables
99
api_url = "https://demo.opencti.io"
10-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
10+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
1111

1212
# OpenCTI initialization
1313
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_intrusion_set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_ip_address_resolves_domain.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_marking_definition.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_report_with_author.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/delete_intrusion_set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/delete_relation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/export_async_of_indicators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/export_async_of_malware.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/export_incident_stix2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)
@@ -22,4 +22,4 @@
2222
# Write the bundle
2323
f = open("My new incident.json", "w")
2424
f.write(json_bundle)
25-
f.close()
25+
f.close()

examples/export_incidents_stix2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/export_intrusion_set_stix2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/export_report_stix2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/get_all_indicators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/get_attack_pattern_by_mitre_id.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/get_entity_by_name_or_alias.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/get_indicators_of_intrusion_set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/get_malwares_of_intrusion_set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/get_marking_definitions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/get_reports_about_intrusion_set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/import_stix2_file.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/search_attack_pattern.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/search_malware.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Variables
66
api_url = "https://demo.opencti.io"
7-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/update_entity_attribute.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/upload_file.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/upload_file_to_intrusion_set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Variables
77
api_url = "https://demo.opencti.io"
8-
api_token = "609caced-7610-4c84-80b4-f3a380d1939b"
8+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
99

1010
# OpenCTI initialization
1111
opencti_api_client = OpenCTIApiClient(api_url, api_token)

pycti/entities/opencti_stix_relation.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, opencti):
2323
modified
2424
created_at
2525
updated_at
26+
fromRole
2627
from {
2728
id
2829
stix_id_key
@@ -32,6 +33,7 @@ def __init__(self, opencti):
3233
description
3334
}
3435
}
36+
toRole
3537
to {
3638
id
3739
stix_id_key
@@ -625,16 +627,26 @@ def to_stix2(self, **kwargs):
625627
if id is not None and entity is None:
626628
entity = self.read(id=id)
627629
if entity is not None:
630+
roles = self.opencti.resolve_role(entity["relationship_type"], entity["from"]["entity_type"], entity["to"]["entity_type"])
631+
if roles is not None:
632+
final_from_id = entity["from"]["stix_id_key"]
633+
final_to_id = entity["to"]["stix_id_key"]
634+
else:
635+
roles = self.opencti.resolve_role(entity["relationship_type"], entity["to"]["entity_type"], entity["from"]["entity_type"])
636+
if roles is not None:
637+
final_from_id = entity["to"]["stix_id_key"]
638+
final_to_id = entity["from"]["stix_id_key"]
639+
628640
stix_relation = dict()
629641
stix_relation["id"] = entity["stix_id_key"]
630642
stix_relation["type"] = "relationship"
631643
stix_relation["relationship_type"] = entity["relationship_type"]
632644
if self.opencti.not_empty(entity["description"]):
633645
stix_relation["description"] = entity["description"]
634-
stix_relation["source_ref"] = entity["from"]["stix_id_key"]
635-
stix_relation["target_ref"] = entity["to"]["stix_id_key"]
636-
stix_relation[CustomProperties.SOURCE_REF] = entity["from"]["stix_id_key"]
637-
stix_relation[CustomProperties.TARGET_REF] = entity["to"]["stix_id_key"]
646+
stix_relation["source_ref"] = final_from_id
647+
stix_relation["target_ref"] = final_to_id
648+
stix_relation[CustomProperties.SOURCE_REF] = final_from_id
649+
stix_relation[CustomProperties.TARGET_REF] = final_to_id
638650
stix_relation["created"] = self.opencti.stix2.format_date(entity["created"])
639651
stix_relation["modified"] = self.opencti.stix2.format_date(
640652
entity["modified"]

0 commit comments

Comments
 (0)