forked from OpenCTI-Platform/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencti_threat_actor.py
297 lines (272 loc) · 9.72 KB
/
opencti_threat_actor.py
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
# coding: utf-8
import json
import uuid
from typing import Union
from stix2.canonicalization.Canonicalize import canonicalize
from pycti.entities.opencti_threat_actor_group import ThreatActorGroup
from pycti.entities.opencti_threat_actor_individual import ThreatActorIndividual
class ThreatActor:
"""Main ThreatActor class for OpenCTI
:param opencti: instance of :py:class:`~pycti.api.opencti_api_client.OpenCTIApiClient`
"""
def __init__(self, opencti):
"""Create an instance of ThreatActor"""
self.opencti = opencti
self.threat_actor_group = ThreatActorGroup(opencti)
self.threat_actor_individual = ThreatActorIndividual(opencti)
self.properties = """
id
standard_id
entity_type
parent_types
spec_version
created_at
updated_at
createdBy {
... on Identity {
id
standard_id
entity_type
parent_types
spec_version
identity_class
name
description
roles
contact_information
x_opencti_aliases
created
modified
objectLabel {
id
value
color
}
}
... on Organization {
x_opencti_organization_type
x_opencti_reliability
}
... on Individual {
x_opencti_firstname
x_opencti_lastname
}
}
objectOrganization {
id
standard_id
name
}
objectMarking {
id
standard_id
entity_type
definition_type
definition
created
modified
x_opencti_order
x_opencti_color
}
objectLabel {
id
value
color
}
externalReferences {
edges {
node {
id
standard_id
entity_type
source_name
description
url
hash
external_id
created
modified
importFiles {
edges {
node {
id
name
size
metaData {
mimetype
version
}
}
}
}
}
}
}
revoked
confidence
created
modified
name
description
aliases
threat_actor_types
first_seen
last_seen
roles
goals
sophistication
resource_level
primary_motivation
secondary_motivations
importFiles {
edges {
node {
id
name
size
metaData {
mimetype
version
}
}
}
}
"""
@staticmethod
def generate_id(name):
name = name.lower().strip()
data = {"name": name}
data = canonicalize(data, utf8=False)
id = str(uuid.uuid5(uuid.UUID("00abedb4-aa42-466c-9c01-fed23315a9b7"), data))
return "threat-actor--" + id
def list(self, **kwargs) -> dict:
"""List Threat-Actor objects
The list method accepts the following kwargs:
:param list filters: (optional) the filters to apply
:param str search: (optional) a search keyword to apply for the listing
:param int first: (optional) return the first n rows from the `after` ID
or the beginning if not set
:param str after: (optional) OpenCTI object ID of the first row for pagination
:param str orderBy: (optional) the field to order the response on
:param bool orderMode: (optional) either "`asc`" or "`desc`"
:param bool getAll: (optional) switch to return all entries (be careful to use this without any other filters)
:param bool withPagination: (optional) switch to use pagination
"""
filters = kwargs.get("filters", None)
search = kwargs.get("search", None)
first = kwargs.get("first", 500)
after = kwargs.get("after", None)
order_by = kwargs.get("orderBy", None)
order_mode = kwargs.get("orderMode", None)
custom_attributes = kwargs.get("customAttributes", None)
get_all = kwargs.get("getAll", False)
with_pagination = kwargs.get("withPagination", False)
if get_all:
first = 500
self.opencti.app_logger.info(
"Listing Threat-Actors with filters", {"filters": json.dumps(filters)}
)
query = (
"""
query ThreatActors($filters: FilterGroup, $search: String, $first: Int, $after: ID, $orderBy: ThreatActorsOrdering, $orderMode: OrderingMode) {
threatActors(filters: $filters, search: $search, first: $first, after: $after, orderBy: $orderBy, orderMode: $orderMode) {
edges {
node {
"""
+ (custom_attributes if custom_attributes is not None else self.properties)
+ """
}
}
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
globalCount
}
}
}
"""
)
result = self.opencti.query(
query,
{
"filters": filters,
"search": search,
"first": first,
"after": after,
"orderBy": order_by,
"orderMode": order_mode,
},
)
return self.opencti.process_multiple(
result["data"]["threatActors"], with_pagination
)
def read(self, **kwargs) -> Union[dict, None]:
"""Read a Threat-Actor object
read can be either used with a known OpenCTI entity `id` or by using a
valid filter to search and return a single Threat-Actor entity or None.
The list method accepts the following kwargs.
Note: either `id` or `filters` is required.
:param str id: the id of the Threat-Actor
:param list filters: the filters to apply if no id provided
"""
id = kwargs.get("id", None)
filters = kwargs.get("filters", None)
custom_attributes = kwargs.get("customAttributes", None)
if id is not None:
self.opencti.app_logger.info("Reading Threat-Actor", {"id": id})
query = (
"""
query ThreatActor($id: String!) {
threatActor(id: $id) {
"""
+ (
custom_attributes
if custom_attributes is not None
else self.properties
)
+ """
}
}
"""
)
result = self.opencti.query(query, {"id": id})
return self.opencti.process_multiple_fields(result["data"]["threatActor"])
elif filters is not None:
result = self.list(filters=filters)
if len(result) > 0:
return result[0]
else:
return None
else:
self.opencti.app_logger.error(
"[opencti_threat_actor] Missing parameters: id or filters"
)
return None
@DeprecationWarning
def create(self, **kwargs):
# For backward compatibility, please use threat_actor_group or threat_actor_individual
return self.threat_actor_group.create(**kwargs)
"""
Import an Threat-Actor object from a STIX2 object
:param stixObject: the Stix-Object Intrusion-Set
:return Intrusion-Set object
"""
def import_from_stix2(self, **kwargs):
stix_object = kwargs.get("stixObject", None)
if "x_opencti_type" in stix_object:
type = stix_object["x_opencti_type"].lower()
elif self.opencti.get_attribute_in_extension("type", stix_object) is not None:
type = self.opencti.get_attribute_in_extension("type", stix_object).lower()
elif (
"resource_level" in stix_object
and stix_object["resource_level"].lower() == "individual"
):
type = "threat-actor-individual"
else:
type = "threat-actor-group"
if type == "threat-actor-individual":
return self.threat_actor_individual.import_from_stix2(**kwargs)
else:
return self.threat_actor_group.import_from_stix2(**kwargs)