@@ -301,7 +301,7 @@ def extract_embedded_relationships(self, stix_object, types=None):
301
301
entity = report ,
302
302
external_reference_id = external_reference_id
303
303
)
304
- reports [external_reference_id ] = report [ 'id' ]
304
+ reports [external_reference_id ] = report
305
305
306
306
return {
307
307
'created_by_ref' : created_by_ref_id ,
@@ -376,7 +376,8 @@ def import_object(self, stix_object, update=False, types=None):
376
376
)
377
377
if external_reference_id in reports :
378
378
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 ],
380
381
entity_id = stix_object_result ['id' ]
381
382
)
382
383
# Add kill chain phases
@@ -405,88 +406,82 @@ def import_relationship(self, stix_relation, update=False, types=None):
405
406
external_references_ids = embedded_relationships ['external_references' ]
406
407
reports = embedded_relationships ['reports' ]
407
408
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' ]
413
413
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 ])
418
416
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' ]
433
421
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
444
424
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' ]
487
436
else :
437
+ self .opencti .log ('error' , 'Target ref of the relationship not found, doing nothing...' )
488
438
return None
489
439
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
+
490
485
# Update created by ref
491
486
if created_by_ref_id is not None :
492
487
self .opencti .stix_entity .update_created_by_ref (
@@ -510,15 +505,18 @@ def import_relationship(self, stix_relation, update=False, types=None):
510
505
)
511
506
if external_reference_id in reports :
512
507
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 ],
514
510
entity_id = stix_relation_result ['id' ]
515
511
)
516
512
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 ],
518
515
entity_id = source_id
519
516
)
520
517
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 ],
522
520
entity_id = target_id
523
521
)
524
522
# Add kill chain phases
0 commit comments