Skip to content

Commit b991e2f

Browse files
author
Samuel Hassine
committed
[client] Fix some issues in the creation of relations
1 parent 5e64db6 commit b991e2f

File tree

3 files changed

+82
-85
lines changed

3 files changed

+82
-85
lines changed

pycti/entities/opencti_stix_entity.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# coding: utf-8
22

3-
import json
4-
5-
63
class StixEntity:
74
def __init__(self, opencti):
85
self.opencti = opencti
@@ -178,7 +175,7 @@ def read(self, **kwargs):
178175
result = self.opencti.query(query, {'id': id})
179176
return self.opencti.process_multiple_fields(result['data']['stixEntity'])
180177
else:
181-
self.opencti.log('error', 'Missing parameters: id or filters')
178+
self.opencti.log('error', 'Missing parameters: id')
182179
return None
183180

184181
"""

pycti/entities/opencti_stix_relation.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ def create(self, **kwargs):
316316
ignore_dates = kwargs.get('ignore_dates', False)
317317

318318
stix_relation_result = None
319-
if stix_id_key is not None:
319+
if id is not None:
320+
stix_relation_result = self.read(id=id)
321+
if stix_relation_result is None and stix_id_key is not None:
320322
stix_relation_result = self.read(id=stix_id_key)
321323
if stix_relation_result is None:
322324
if ignore_dates is False and first_seen is not None and last_seen is not None:

pycti/utils/opencti_stix2.py

+78-80
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def extract_embedded_relationships(self, stix_object, types=None):
301301
entity=report,
302302
external_reference_id=external_reference_id
303303
)
304-
reports[external_reference_id] = report['id']
304+
reports[external_reference_id] = report
305305

306306
return {
307307
'created_by_ref': created_by_ref_id,
@@ -376,7 +376,8 @@ def import_object(self, stix_object, update=False, types=None):
376376
)
377377
if external_reference_id in reports:
378378
self.opencti.report.add_stix_entity(
379-
id=reports[external_reference_id],
379+
id=reports[external_reference_id]['id'],
380+
report=reports[external_reference_id],
380381
entity_id=stix_object_result['id']
381382
)
382383
# Add kill chain phases
@@ -405,88 +406,82 @@ def import_relationship(self, stix_relation, update=False, types=None):
405406
external_references_ids = embedded_relationships['external_references']
406407
reports = embedded_relationships['reports']
407408

408-
# Check relation
409-
stix_relation_result = self.opencti.stix_relation.read(id=stix_relation['id'])
410-
if stix_relation_result is not None:
411-
source_id = stix_relation_result['from']['id']
412-
target_id = stix_relation_result['to']['id']
409+
# Create the relation
410+
if stix_relation['source_ref'] in self.mapping_cache:
411+
source_id = self.mapping_cache[stix_relation['source_ref']]['id']
412+
source_type = self.mapping_cache[stix_relation['source_ref']]['type']
413413
else:
414-
# Create the relation
415-
if stix_relation['source_ref'] in self.mapping_cache:
416-
source_id = self.mapping_cache[stix_relation['source_ref']]['id']
417-
source_type = self.mapping_cache[stix_relation['source_ref']]['type']
414+
if CustomProperties.SOURCE_REF in stix_relation:
415+
stix_object_result = self.opencti.stix_entity.read(id=stix_relation[CustomProperties.SOURCE_REF])
418416
else:
419-
if CustomProperties.SOURCE_REF in stix_relation:
420-
stix_object_result = self.opencti.stix_entity.read(id=stix_relation[CustomProperties.SOURCE_REF])
421-
else:
422-
stix_object_result = self.opencti.stix_entity.read(id=stix_relation['source_ref'])
423-
if stix_object_result is not None:
424-
source_id = stix_object_result['id']
425-
source_type = stix_object_result['entity_type']
426-
else:
427-
self.opencti.log('error', 'Source ref of the relationship not found, doing nothing...')
428-
return None
429-
430-
if stix_relation['target_ref'] in self.mapping_cache:
431-
target_id = self.mapping_cache[stix_relation['target_ref']]['id']
432-
target_type = self.mapping_cache[stix_relation['target_ref']]['type']
417+
stix_object_result = self.opencti.stix_entity.read(id=stix_relation['source_ref'])
418+
if stix_object_result is not None:
419+
source_id = stix_object_result['id']
420+
source_type = stix_object_result['entity_type']
433421
else:
434-
if CustomProperties.TARGET_REF in stix_relation:
435-
stix_object_result = self.opencti.stix_entity.read(id=stix_relation[CustomProperties.TARGET_REF])
436-
else:
437-
stix_object_result = self.opencti.stix_entity.read(id=stix_relation['target_ref'])
438-
if stix_object_result is not None:
439-
target_id = stix_object_result['id']
440-
target_type = stix_object_result['entity_type']
441-
else:
442-
self.opencti.log('error', 'Target ref of the relationship not found, doing nothing...')
443-
return None
422+
self.opencti.log('error', 'Source ref of the relationship not found, doing nothing...')
423+
return None
444424

445-
date = None
446-
if 'external_references' in stix_relation:
447-
for external_reference in stix_relation['external_references']:
448-
try:
449-
if 'description' in external_reference:
450-
matches = list(datefinder.find_dates(external_reference['description']))
451-
else:
452-
matches = list(datefinder.find_dates(external_reference['source_name']))
453-
except:
454-
matches = []
455-
if len(matches) > 0:
456-
date = matches[0].strftime('%Y-%m-%dT%H:%M:%SZ')
457-
else:
458-
date = datetime.datetime.today().strftime('%Y-%m-%dT%H:%M:%SZ')
459-
if date is None:
460-
date = datetime.datetime.utcnow().replace(microsecond=0, tzinfo=datetime.timezone.utc).isoformat()
461-
462-
stix_relation_result = self.opencti.stix_relation.create(
463-
fromId=source_id,
464-
fromType=source_type,
465-
toId=target_id,
466-
toType=target_type,
467-
relationship_type=stix_relation['relationship_type'],
468-
description=self.convert_markdown(
469-
stix_relation['description']) if 'description' in stix_relation else None,
470-
first_seen=stix_relation[
471-
CustomProperties.FIRST_SEEN] if CustomProperties.FIRST_SEEN in stix_relation else date,
472-
last_seen=stix_relation[
473-
CustomProperties.LAST_SEEN] if CustomProperties.LAST_SEEN in stix_relation else date,
474-
weight=stix_relation[CustomProperties.WEIGHT] if CustomProperties.WEIGHT in stix_relation else 1,
475-
role_played=stix_relation[
476-
CustomProperties.ROLE_PLAYED] if CustomProperties.ROLE_PLAYED in stix_relation else None,
477-
id=stix_relation[CustomProperties.ID] if CustomProperties.ID in stix_relation else None,
478-
stix_id_key=stix_relation['id'] if 'id' in stix_relation else None,
479-
created=stix_relation['created'] if 'created' in stix_relation else None,
480-
modified=stix_relation['modified'] if 'modified' in stix_relation else None,
481-
update=update,
482-
ignore_dates=stix_relation[
483-
CustomProperties.IGNORE_DATES] if CustomProperties.IGNORE_DATES in stix_relation else None,
484-
)
485-
if stix_relation_result is not None:
486-
self.mapping_cache[stix_relation['id']] = {'id': stix_relation_result['id']}
425+
if stix_relation['target_ref'] in self.mapping_cache:
426+
target_id = self.mapping_cache[stix_relation['target_ref']]['id']
427+
target_type = self.mapping_cache[stix_relation['target_ref']]['type']
428+
else:
429+
if CustomProperties.TARGET_REF in stix_relation:
430+
stix_object_result = self.opencti.stix_entity.read(id=stix_relation[CustomProperties.TARGET_REF])
431+
else:
432+
stix_object_result = self.opencti.stix_entity.read(id=stix_relation['target_ref'])
433+
if stix_object_result is not None:
434+
target_id = stix_object_result['id']
435+
target_type = stix_object_result['entity_type']
487436
else:
437+
self.opencti.log('error', 'Target ref of the relationship not found, doing nothing...')
488438
return None
489439

440+
date = None
441+
if 'external_references' in stix_relation:
442+
for external_reference in stix_relation['external_references']:
443+
try:
444+
if 'description' in external_reference:
445+
matches = list(datefinder.find_dates(external_reference['description']))
446+
else:
447+
matches = list(datefinder.find_dates(external_reference['source_name']))
448+
except:
449+
matches = []
450+
if len(matches) > 0:
451+
date = matches[0].strftime('%Y-%m-%dT%H:%M:%SZ')
452+
else:
453+
date = datetime.datetime.today().strftime('%Y-%m-%dT%H:%M:%SZ')
454+
if date is None:
455+
date = datetime.datetime.utcnow().replace(microsecond=0, tzinfo=datetime.timezone.utc).isoformat()
456+
457+
stix_relation_result = self.opencti.stix_relation.create(
458+
fromId=source_id,
459+
fromType=source_type,
460+
toId=target_id,
461+
toType=target_type,
462+
relationship_type=stix_relation['relationship_type'],
463+
description=self.convert_markdown(
464+
stix_relation['description']) if 'description' in stix_relation else None,
465+
first_seen=stix_relation[
466+
CustomProperties.FIRST_SEEN] if CustomProperties.FIRST_SEEN in stix_relation else date,
467+
last_seen=stix_relation[
468+
CustomProperties.LAST_SEEN] if CustomProperties.LAST_SEEN in stix_relation else date,
469+
weight=stix_relation[CustomProperties.WEIGHT] if CustomProperties.WEIGHT in stix_relation else 1,
470+
role_played=stix_relation[
471+
CustomProperties.ROLE_PLAYED] if CustomProperties.ROLE_PLAYED in stix_relation else None,
472+
id=stix_relation[CustomProperties.ID] if CustomProperties.ID in stix_relation else None,
473+
stix_id_key=stix_relation['id'] if 'id' in stix_relation else None,
474+
created=stix_relation['created'] if 'created' in stix_relation else None,
475+
modified=stix_relation['modified'] if 'modified' in stix_relation else None,
476+
update=update,
477+
ignore_dates=stix_relation[
478+
CustomProperties.IGNORE_DATES] if CustomProperties.IGNORE_DATES in stix_relation else None,
479+
)
480+
if stix_relation_result is not None:
481+
self.mapping_cache[stix_relation['id']] = {'id': stix_relation_result['id']}
482+
else:
483+
return None
484+
490485
# Update created by ref
491486
if created_by_ref_id is not None:
492487
self.opencti.stix_entity.update_created_by_ref(
@@ -510,15 +505,18 @@ def import_relationship(self, stix_relation, update=False, types=None):
510505
)
511506
if external_reference_id in reports:
512507
self.opencti.report.add_stix_entity(
513-
id=reports[external_reference_id],
508+
id=reports[external_reference_id]['id'],
509+
report=reports[external_reference_id],
514510
entity_id=stix_relation_result['id']
515511
)
516512
self.opencti.report.add_stix_entity(
517-
id=reports[external_reference_id],
513+
id=reports[external_reference_id]['id'],
514+
report=reports[external_reference_id],
518515
entity_id=source_id
519516
)
520517
self.opencti.report.add_stix_entity(
521-
id=reports[external_reference_id],
518+
id=reports[external_reference_id]['id'],
519+
report=reports[external_reference_id],
522520
entity_id=target_id
523521
)
524522
# Add kill chain phases

0 commit comments

Comments
 (0)