-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstix_converter.py
More file actions
1363 lines (1149 loc) · 46.7 KB
/
stix_converter.py
File metadata and controls
1363 lines (1149 loc) · 46.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""STIX converter for CATALYST data."""
import ipaddress
import uuid
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple, Union
import stix2
from pycti import (
AttackPattern,
Campaign,
CustomObservableCryptocurrencyWallet,
Identity,
Indicator,
IntrusionSet,
Location,
Malware,
MarkingDefinition,
Report,
StixCoreRelationship,
ThreatActor,
Tool,
Vulnerability,
)
from .enums import ObservableType, TLPLevel
class StixConverter:
"""Converts CATALYST data to STIX 2.1 format."""
def __init__(
self,
author_name: str = "CATALYST",
tlp_level: str = "tlp:white",
create_observables: bool = True,
create_indicators: bool = True,
):
"""
Initialize the STIX converter.
Args:
author_name: Name of the identity that will be the author of STIX objects
tlp_level: TLP level for marking definitions ('TLP:CLEAR', 'TLP:GREEN', 'TLP:AMBER', 'TLP:AMBER+STRICT', 'TLP:RED')
"""
self.author_name = author_name
self.tlp_level = tlp_level.lower()
# Create author identity
self.identity = self._create_identity()
# Create TLP
self.tlp_marking = self._create_tlp_marking()
self.observable_type_map = {
ObservableType.BTC_ADDRESS.value: self._create_cryptocurrency_wallet_observable,
ObservableType.URL.value: self._create_url_observable,
ObservableType.DOMAIN_NAME.value: self._create_domain_observable,
ObservableType.IP_ADDRESS.value: self._create_ip_observable,
ObservableType.FILE_HASH_MD5.value: lambda value, report_reference=None: self._create_file_observable(
value, "MD5", report_reference
),
ObservableType.FILE_HASH_SHA1.value: lambda value, report_reference=None: self._create_file_observable(
value, "SHA-1", report_reference
),
ObservableType.FILE_HASH_SHA256.value: lambda value, report_reference=None: self._create_file_observable(
value, "SHA-256", report_reference
),
ObservableType.EMAIL.value: self._create_email_observable,
ObservableType.JABBER_ADDRESS.value: self._create_user_account_observable,
ObservableType.TOX_ADDRESS.value: self._create_user_account_observable,
ObservableType.TELEGRAM.value: self._create_user_account_observable,
ObservableType.X.value: self._create_user_account_observable,
}
self.tlp_map = {
TLPLevel.CLEAR.value: "tlp:clear",
TLPLevel.GREEN.value: "tlp:green",
TLPLevel.AMBER.value: "tlp:amber",
TLPLevel.RED.value: "tlp:red",
TLPLevel.AMBER_STRICT.value: "tlp:amber+strict",
}
self._external_ref_cache = {}
self._post_ref_cache = {}
self._entity_cache = {}
self.create_observables = create_observables
self.create_indicators = create_indicators
def _create_identity(self) -> Identity:
"""
Create an identity for the author of STIX objects.
Returns:
STIX Identity object
"""
return stix2.Identity(
id=Identity.generate_id(
name=self.author_name, identity_class="organization"
),
name=self.author_name,
identity_class="organization",
description=f"Data from {self.author_name} platform",
)
def _create_tlp_marking(self, tlp_level: str = None) -> stix2.MarkingDefinition:
"""
Create a TLP marking definition based on the specified TLP level.
Args:
tlp_level: TLP level ('white', 'green', 'amber', 'red')
If None, uses the instance default
Returns:
STIX MarkingDefinition object
"""
level = tlp_level.lower() if tlp_level else self.tlp_level
tlp_map = {
"tlp:white": stix2.TLP_WHITE,
"tlp:clear": stix2.TLP_WHITE,
"tlp:green": stix2.TLP_GREEN,
"tlp:amber": stix2.TLP_AMBER,
"tlp:amber+strict": stix2.MarkingDefinition(
id=MarkingDefinition.generate_id("TLP", "TLP:AMBER+STRICT"),
definition_type="statement",
definition={"statement": "custom"},
custom_properties={
"x_opencti_definition_type": "TLP",
"x_opencti_definition": "TLP:AMBER+STRICT",
},
),
"tlp:red": stix2.TLP_RED,
}
return tlp_map.get(level, stix2.TLP_WHITE)
def _create_external_reference(
self, source_name: str, external_id: str, is_report: bool = False
) -> stix2.ExternalReference:
"""
Create an external reference to the CATALYST platform. Uses caching to avoid duplicate external references for the same source and ID.
Args:
source_name: Name of the source
external_id: ID in the external source
Returns:
STIX ExternalReference object
"""
cache_key = f"{source_name}:{external_id}" # noqa: E231
if cache_key in self._external_ref_cache:
return self._external_ref_cache[cache_key]
if source_name == "PRODAFT CATALYST" and "--" not in external_id and is_report:
ext_ref = stix2.ExternalReference(
source_name=source_name,
external_id=external_id,
url=f"https://catalyst.prodaft.com/report/{external_id}/", # noqa: E231
)
else:
ext_ref = stix2.ExternalReference(
source_name=source_name,
external_id=external_id,
)
self._external_ref_cache[cache_key] = ext_ref
return ext_ref
def get_post_reference(self, post_id: str) -> str:
"""
Get a cached STIX ID for a post reference. Creates and caches it if it doesn't exist.
Args:
post_id: The ID of the post
Returns:
STIX ID for the post (report--UUID)
"""
if post_id in self._post_ref_cache:
return self._post_ref_cache[post_id]
post_stix_id = f"report--{uuid.uuid5(uuid.NAMESPACE_URL, post_id)}"
self._post_ref_cache[post_id] = post_stix_id
return post_stix_id
def create_relationship(
self,
source_ref: str,
target_ref: str,
relationship_type: str,
report_reference: stix2.ExternalReference = None,
) -> stix2.Relationship:
"""
Create a STIX relationship between two objects.
Args:
source_ref: Source object ID
target_ref: Target object ID
relationship_type: Type of relationship (e.g., 'uses', 'indicates', 'targets', 'based-on')
report_reference: Optional reference to the report this relationship is from
Returns:
STIX Relationship object
"""
cache_key = (
f"relationship:{relationship_type}:{source_ref}:{target_ref}" # noqa: E231
)
if cache_key in self._entity_cache:
rel_id = self._entity_cache[cache_key]
return stix2.Relationship(
id=rel_id,
source_ref=source_ref,
target_ref=target_ref,
relationship_type=relationship_type,
)
created_by_ref = self.get_created_by_ref()
external_references = []
if report_reference:
external_references = [report_reference]
relationship = stix2.Relationship(
id=StixCoreRelationship.generate_id(
relationship_type, source_ref, target_ref
),
source_ref=source_ref,
target_ref=target_ref,
relationship_type=relationship_type,
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
self._entity_cache[cache_key] = relationship.id
return relationship
def _create_ip_observable(
self, value: str, report_reference: stix2.ExternalReference = None
) -> Union[stix2.IPv4Address, stix2.IPv6Address]:
"""
Create an IP address observable.
Args:
value: IP address value
report_reference: Optional reference to the report this observable is from
Returns:
IPv4Address or IPv6Address STIX object
"""
try:
ip = ipaddress.ip_address(value)
created_by_ref = self.get_created_by_ref()
custom_properties = {"x_opencti_created_by_ref": created_by_ref}
if report_reference:
custom_properties["x_opencti_external_references"] = [report_reference]
if ip.version == 4:
return stix2.IPv4Address(
value=value,
object_marking_refs=[self.tlp_marking.id],
custom_properties=custom_properties,
)
else:
return stix2.IPv6Address(
value=value,
object_marking_refs=[self.tlp_marking.id],
custom_properties=custom_properties,
)
except ValueError:
raise ValueError(f"Invalid IP address: {value}")
def _create_domain_observable(
self, value: str, report_reference: stix2.ExternalReference = None
) -> stix2.DomainName:
"""
Create a domain name observable.
Args:
value: Domain name value
report_reference: Optional reference to the report this observable is from
Returns:
DomainName STIX object
"""
created_by_ref = self.get_created_by_ref()
custom_properties = {"x_opencti_created_by_ref": created_by_ref}
if report_reference:
custom_properties["x_opencti_external_references"] = [report_reference]
return stix2.DomainName(
value=value,
object_marking_refs=[self.tlp_marking.id],
custom_properties=custom_properties,
)
def _create_url_observable(
self, value: str, report_reference: stix2.ExternalReference = None
) -> stix2.URL:
"""
Create a URL observable.
Args:
value: URL value
report_reference: Optional reference to the report this observable is from
Returns:
URL STIX object
"""
created_by_ref = self.get_created_by_ref()
custom_properties = {"x_opencti_created_by_ref": created_by_ref}
if report_reference:
custom_properties["x_opencti_external_references"] = [report_reference]
return stix2.URL(
value=value,
object_marking_refs=[self.tlp_marking.id],
custom_properties=custom_properties,
)
def _create_email_observable(
self, value: str, report_reference: stix2.ExternalReference = None
) -> stix2.EmailAddress:
"""
Create an email address observable.
Args:
value: Email address value
report_reference: Optional reference to the report this observable is from
Returns:
EmailAddress STIX object
"""
created_by_ref = self.get_created_by_ref()
custom_properties = {"x_opencti_created_by_ref": created_by_ref}
if report_reference:
custom_properties["x_opencti_external_references"] = [report_reference]
return stix2.EmailAddress(
value=value,
object_marking_refs=[self.tlp_marking.id],
custom_properties=custom_properties,
)
def _create_file_observable(
self,
value: str,
hash_type: str,
report_reference: stix2.ExternalReference = None,
) -> stix2.File:
"""
Create a file observable with hash.
Args:
value: Hash value
hash_type: Type of hash (MD5, SHA-1, SHA-256)
report_reference: Optional reference to the report this observable is from
Returns:
File STIX object
"""
created_by_ref = self.get_created_by_ref()
custom_properties = {"x_opencti_created_by_ref": created_by_ref}
if report_reference:
custom_properties["x_opencti_external_references"] = [report_reference]
hashes = {hash_type: value}
return stix2.File(
hashes=hashes,
object_marking_refs=[self.tlp_marking.id],
custom_properties=custom_properties,
)
def _create_user_account_observable(
self, value: str, report_reference: stix2.ExternalReference = None
) -> stix2.UserAccount:
"""
Create a user account observable.
Args:
value: User account value
report_reference: Optional reference to the report this observable is from
Returns:
UserAccount STIX object
"""
created_by_ref = self.get_created_by_ref()
custom_properties = {"x_opencti_created_by_ref": created_by_ref}
if report_reference:
custom_properties["x_opencti_external_references"] = [report_reference]
return stix2.UserAccount(
user_id=value,
object_marking_refs=[self.tlp_marking.id],
custom_properties=custom_properties,
)
def _create_cryptocurrency_wallet_observable(
self, value: str, report_reference: stix2.ExternalReference = None
) -> stix2.CustomObservable:
"""
Create a cryptocurrency wallet observable.
Args:
value: Wallet address
report_reference: Optional reference to the report this observable is from
Returns:
CustomObservable STIX object
"""
created_by_ref = self.get_created_by_ref()
custom_properties = {"x_opencti_created_by_ref": created_by_ref}
if report_reference:
custom_properties["x_opencti_external_references"] = [report_reference]
return CustomObservableCryptocurrencyWallet(
value=value,
object_marking_refs=[self.tlp_marking.id],
custom_properties=custom_properties,
)
def _create_custom_observable(
self,
value: str,
observable_type: str,
report_reference: stix2.ExternalReference = None,
) -> stix2.CustomObservable:
"""
Create a custom observable for types not directly supported by STIX.
Args:
value: Observable value
observable_type: Type of observable
report_reference: Optional reference to the report this observable is from
Returns:
CustomObservable STIX object
"""
created_by_ref = self.get_created_by_ref()
custom_properties = {"x_opencti_created_by_ref": created_by_ref}
if report_reference:
custom_properties["x_opencti_external_references"] = [report_reference]
return stix2.CustomObservable(
id=f"x-{observable_type.lower()}--{str(uuid.uuid4())}",
type=f"x-{observable_type.lower()}",
value=value,
object_marking_refs=[self.tlp_marking.id],
custom_properties=custom_properties,
)
def _create_observable_from_data(self, observable: Dict) -> Optional[Any]:
"""
Create a STIX observable object from CATALYST observable data.
Args:
observable: CATALYST observable data
Returns:
STIX Observable object or None if can't be created
"""
observable_type = observable.get("type")
value = observable.get("value", "")
report_reference = observable.get("report_reference")
if not observable_type or not value:
return None
# Find the appropriate factory method for this observable type
factory_method = self.observable_type_map.get(observable_type)
if factory_method:
return factory_method(value, report_reference)
# If no specific factory method, try to create a custom observable
return self._create_custom_observable(value, observable_type, report_reference)
def _create_based_on_relationship(
self, indicator: stix2.Indicator, observable: Any
) -> stix2.Relationship:
"""
Create a 'based-on' relationship between an indicator and its observable.
Args:
indicator: STIX Indicator object
observable: STIX Observable object
Returns:
STIX Relationship object
"""
return stix2.Relationship(
id=StixCoreRelationship.generate_id(
"based-on", indicator.id, observable.id
),
relationship_type="based-on",
source_ref=indicator.id,
target_ref=observable.id,
created_by_ref=self.get_created_by_ref(),
object_marking_refs=[self.tlp_marking.id],
)
def create_organization_identity(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
) -> stix2.Identity:
"""
Create a STIX Identity object for an organization from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Organization name
context: Optional context/description for the organization
report_reference: Optional reference to the report this entity is from
Returns:
STIX Identity object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Organization {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
identity = stix2.Identity(
id=Identity.generate_id(entity_value, "organization"),
name=entity_value,
description=description,
identity_class="organization",
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
return identity
def create_industry_identity(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
) -> stix2.Identity:
"""
Create a STIX Identity object for an industry from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Industry name
context: Optional context/description for the industry
report_reference: Optional reference to the report this entity is from
Returns:
STIX Identity object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Industry {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
identity = stix2.Identity(
id=Identity.generate_id(entity_value, "class"),
name=entity_value,
description=description,
identity_class="class",
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
return identity
def create_sector_identity(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
) -> stix2.Identity:
"""
Create a STIX Identity object for a sector from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Sector name
context: Optional context/description for the sector
report_reference: Optional reference to the report this entity is from
Returns:
STIX Identity object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Sector {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
identity = stix2.Identity(
id=Identity.generate_id(entity_value, "class"),
name=entity_value,
description=description,
identity_class="class",
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
return identity
def create_country_location(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
) -> stix2.Location:
"""
Create a STIX Location object for a country from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Country name
context: Optional context/description for the country
report_reference: Optional reference to the report this entity is from
Returns:
STIX Location object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Country {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
location = stix2.Location(
id=Location.generate_id(entity_value, x_opencti_location_type="country"),
name=entity_value,
description=description,
country=entity_value,
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
return location
def create_threat_actor(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
actor_type: str = "threat-actor-group",
) -> stix2.ThreatActor:
"""
Create a STIX Threat Actor object from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Threat Actor name
context: Optional context/description for the threat actor
report_reference: Optional reference to the report this entity is from
actor_type: Type of threat actor ("threat-actor-group" or "threat-actor-individual")
Returns:
STIX Threat Actor object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Threat Actor {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
threat_actor = stix2.ThreatActor(
id=ThreatActor.generate_id(entity_value, actor_type),
name=entity_value,
description=description,
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
custom_properties={"x_catalyst_threat_actor_id": entity_id},
)
return threat_actor
def create_malware(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
) -> stix2.Malware:
"""
Create a STIX Malware object from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Malware name
context: Optional context/description for the malware
report_reference: Optional reference to the report this entity is from
Returns:
STIX Malware object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Malware {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
malware = stix2.Malware(
id=Malware.generate_id(entity_value),
name=entity_value,
description=description,
is_family=False,
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
return malware
def create_tool(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
) -> stix2.Tool:
"""
Create a STIX Tool object from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Tool name
context: Optional context/description for the tool
report_reference: Optional reference to the report this entity is from
Returns:
STIX Tool object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Tool {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
tool = stix2.Tool(
id=Tool.generate_id(entity_value),
name=entity_value,
description=description,
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
return tool
def create_vulnerability(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
) -> stix2.Vulnerability:
"""
Create a STIX Vulnerability object from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Vulnerability name
context: Optional context/description for the vulnerability
report_reference: Optional reference to the report this entity is from
Returns:
STIX Vulnerability object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Vulnerability {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
vulnerability = stix2.Vulnerability(
id=Vulnerability.generate_id(entity_value),
name=entity_value,
description=description,
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
return vulnerability
def create_attack_pattern(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
) -> stix2.AttackPattern:
"""
Create a STIX Attack Pattern object from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Attack Pattern name
context: Optional context/description for the attack pattern
report_reference: Optional reference to the report this entity is from
Returns:
STIX Attack Pattern object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Attack Pattern {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
attack_pattern = stix2.AttackPattern(
id=AttackPattern.generate_id(entity_value),
name=entity_value,
description=description,
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
return attack_pattern
def create_campaign(
self,
entity_id: str,
entity_value: str,
context: str = None,
report_reference: stix2.ExternalReference = None,
) -> stix2.Campaign:
"""
Create a STIX Campaign object from CATALYST entity data.
Args:
entity_id: CATALYST entity ID
entity_value: Campaign name
context: Optional context/description for the campaign
report_reference: Optional reference to the report this entity is from
Returns:
STIX Campaign object
"""
external_references = []
if report_reference:
external_references = [report_reference]
description = f"Campaign {entity_value} from CATALYST"
if context:
description = f"{context}"
created_by_ref = self.get_created_by_ref()
campaign = stix2.Campaign(
id=Campaign.generate_id(entity_value),
name=entity_value,
description=description,
created_by_ref=created_by_ref,
object_marking_refs=[self.tlp_marking.id],
external_references=external_references if external_references else None,
)
return campaign
def convert_observable_to_stix(
self,
observable_data: Dict,
report_reference: stix2.ExternalReference = None,
report_id: str = None,
) -> Tuple[Any, List[Optional[stix2.Relationship]], Optional[Any]]:
"""
Convert an observable dictionary to a STIX Cyber Observable object and an Indicator.
Args:
observable_data: Dictionary containing observable data (type and value)
May also include 'post_id' to link to a post
May include 'tlp_marking' for custom TLP level
report_reference: Optional reference to the report this observable is from
report_id: Optional STIX ID of the report to create a relationship with
Returns:
Tuple containing:
- STIX Indicator object or None if conversion fails
- List of relationships created (post relationship and/or report relationship)
- STIX Observable object or None if creation fails
"""
try:
observable_type = observable_data.get("type")
value = observable_data.get("value")
if not observable_type or not value:
return None, [], None
tlp_marking = observable_data.get("tlp_marking")
if self.create_indicators:
indicator = self.create_indicator_from_observable(
observable_data, tlp_marking, report_reference
)
else:
indicator = None
if self.create_observables:
observable_data_with_ref = observable_data.copy()
observable_data_with_ref["report_reference"] = report_reference
observable = self._create_observable_from_data(observable_data_with_ref)
else:
observable = None
if not observable and indicator:
return indicator, [], None
if not indicator and observable:
return None, [], observable
if indicator and observable:
based_on = self._create_based_on_relationship(indicator, observable)
relationships = [based_on]
return indicator, relationships, observable
except Exception as e:
observable_type = observable_data.get("type", "unknown")
value = observable_data.get("value", "unknown")
print(
f"Error creating STIX observable/indicator for {observable_type}:{value}: {str(e)}" # noqa: E231
)
return None, [], None
def create_indicator_from_observable(
self,
observable_data: Union[Dict, Any],
tlp_marking=None,
report_reference: stix2.ExternalReference = None,
) -> stix2.Indicator:
"""
Create a STIX Indicator object from an observable.
Args:
observable_data: Dictionary containing observable data with keys 'id', 'value', and 'type'
or a STIX Cyber Observable object
tlp_marking: Optional custom TLP marking to use instead of the default
report_reference: Optional reference to the report this indicator is from