-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomation-StixParser.yml
355 lines (310 loc) · 11.6 KB
/
automation-StixParser.yml
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
commonfields:
id: StixParser
version: -1
name: StixParser
script: |-
import json
import re
import tempfile
from datetime import datetime
from stix.core import STIXPackage
""" HELPER FUNCTIONS"""
def convert_to_json(string):
"""Will try to convert given string to json.
Args:
string: str of stix/json file. may be xml, then function will fail
Returns:
json object if succeed
False if failed
"""
try:
js = json.loads(string)
return js
except ValueError:
return None
def create_timestamp(timestamp):
"""Takes a timestamp and checks if it matches pattern.
Args:
timestamp: (str) given timestamp
Returns:
str: timestamp or None if not matches pattern
"""
try:
datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ")
return timestamp
except ValueError:
return None
def create_indicator_entry(
indicator_type,
value,
pkg_id,
ind_id,
timestamp,
source=None,
score=None,
):
"""Creating a JSON object of given args
Args:
indicator_type: (str) indicator type
value: (str) indicator value
pkg_id: (str) package id
ind_id: (str) indicator id
timestamp: (str) timestamp
source: (str) source of indicator (custom field)
score: (str) score of indicator (custom field)
Returns:
dict:
{
"indicator_type": indicator_type,
"value": value,
"CustomFields": {
"indicatorId": ind_id,
"stixPackageId": pkg_id
}
"source": ind_id.split("-")[0] or source if provided
"score": if source is "DBot" then score should be here.
}
"""
entry = dict()
entry["indicator_type"] = indicator_type
entry["value"] = value
entry["CustomFields"] = {"indicatorId": ind_id, "stixPackageId": pkg_id}
entry["source"] = source if source else ind_id.split("-")[0]
entry["score"] = score
# Times
entry["timestamp"] = timestamp
return entry
def get_score(description):
"""Getting score from description field in STIX2
Args:
description: (str)
Returns:
str: `IMPACT` field
"""
if description and isinstance(description, (str, basestring, unicode)):
regex = re.compile("(IMPACT:)(Low|Medium|High)")
groups = regex.match(description)
score = groups.group(2) if groups else None
if score in ("Low", "Medium"):
return 2
if score == "High":
return 3
return 0
def dscore(score):
if score == "High":
return 3
if score == "Medium":
return 2
if score == "Low":
return 1
return 0
def build_entry(stx_obj, indicators):
"""Extracting all pattern from stix object
function will take care of one bundle only.
Args:
indicators: (dict) output
stx_obj: json stix2 format
"""
if "objects" in stx_obj:
results_list = list()
pkg_id = stx_obj.get("id")
objects = stx_obj.get("objects")
if isinstance(objects, list):
for obj in objects:
if isinstance(obj, dict):
# Creating parameters
ind_id = obj.get("id")
source = obj.get("source")
# times
timestamp = obj.get("created")
score = (
dscore(obj.get("score"))
if "score" in obj
else get_score(obj.get("description"))
)
for k, v in indicators.items():
if isinstance(v, list):
for indicator in v:
if indicator:
result = create_indicator_entry(
indicator_type=k,
value=indicator,
pkg_id=pkg_id,
ind_id=ind_id,
timestamp=timestamp,
source=source,
score=score,
)
if result:
results_list.append(result)
elif v:
result = create_indicator_entry(
indicator_type=k,
value=v,
pkg_id=pkg_id,
ind_id=ind_id,
timestamp=timestamp,
source=source,
score=score,
)
if result:
results_list.append(result)
return results_list
return None
def extract_indicators(data):
"""Gets dict of STIX2.0 object (one in a time)
Args:
data: (dict) of STIX2 object. can be
Returns:
dict: containing all indicators data
"""
regex = re.compile("(\w.*?) = '(.*?)'")
patterns_dict = {
"file:hashes": "File",
"ipv6-addr": "IP",
"ipv4-addr:": "IP",
"url:": "URL",
"domain-name:": "Domain",
"email:": "Email",
}
patterns_lists = {
"File": list(),
"IP": list(),
"Domain": list(),
"Email": list(),
"URL": list(),
}
# Check if is it's
if isinstance(data, dict) and data.get("objects"):
# Create objects
objects = data.get("objects")
# Use regex to extract indicators
if isinstance(objects, list):
for obj in objects:
pattern = obj.get("pattern")
groups = regex.findall(pattern)
for key, value in patterns_dict.items():
for term in groups:
if len(term) == 2 and key in term[0]:
patterns_lists[value].append(term[1])
else:
pattern = objects.get("pattern")
groups = regex.findall(pattern)
for key, value in patterns_dict.items():
for term in groups:
if len(term) == 2 and key in term[0]:
patterns_lists[value].append(term[1])
# Make all the values unique
for k, v in patterns_lists.items():
patterns_dict[k] = list(set(v))
return patterns_lists
else:
return_error("No STIX2 object could be parsed")
def stix2_to_demisto(stx_obj):
"""Converts stix2 json to demisto object
Args:
stx_obj: json object
"""
data = list()
if isinstance(stx_obj, dict):
indicators = extract_indicators(stx_obj)
entry = build_entry(stx_obj, indicators)
if isinstance(entry, list):
data.extend(entry)
else:
data.append(entry)
elif isinstance(stx_obj, list):
for obj in stx_obj:
indicators = extract_indicators(obj)
entry = build_entry(stx_obj, indicators)
if entry:
if isinstance(entry, list):
data.extend(entry)
else:
data.append(entry)
dumped = json.dumps(data)
demisto.results(dumped)
""" STIX 1 """
def create_new_ioc(data, i, timestamp, pkg_id, ind_id):
data.append({})
data[i]["CustomFields"] = {"indicatorId": ind_id, "stixPackageId": pkg_id}
data[i]["source"] = ind_id.split(":")[0]
if timestamp:
data[i]["timestamp"] = timestamp.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
# SCRIPT START
# IF STIX2 FILE
txt = demisto.args().get("iocXml").encode("utf-8")
stx = convert_to_json(txt)
if stx:
stix2_to_demisto(stx)
else:
with tempfile.NamedTemporaryFile() as temp:
temp.write(demisto.args()["iocXml"].encode("utf-8"))
temp.flush()
stix_package = STIXPackage.from_xml(temp.name)
data = []
i = 0
stix_id = stix_package.id_
if stix_package.indicators:
for ind in stix_package.indicators:
ind_id = ind.id_
for obs in ind.observables:
if hasattr(obs.object_.properties, "hashes"):
# File object
for digest in obs.object_.properties.hashes:
if hasattr(digest, "simple_hash_value"):
if isinstance(digest.simple_hash_value.value, list):
for hash in digest.simple_hash_value.value:
create_new_ioc(
data, i, ind.timestamp, stix_id, ind_id
)
data[i]["indicator_type"] = "File"
data[i]["value"] = hash
i = i + 1
else:
create_new_ioc(data, i, ind.timestamp, stix_id, ind_id)
data[i]["indicator_type"] = "File"
data[i][
"value"
] = digest.simple_hash_value.value.strip()
i = i + 1
elif hasattr(obs.object_.properties, "category"):
# Address object
category = obs.object_.properties.category
if category.startswith("ip"):
for ip in obs.object_.properties.address_value.values:
create_new_ioc(data, i, ind.timestamp, stix_id, ind_id)
data[i]["indicator_type"] = "IP"
data[i]["value"] = ip
i = i + 1
elif hasattr(obs.object_.properties, "type_"):
if obs.object_.properties.type_ == "URL":
# URI object
create_new_ioc(data, i, ind.timestamp, stix_id, ind_id)
data[i]["indicator_type"] = "URL"
data[i]["value"] = obs.object_.properties.value.value
i = i + 1
elif hasattr(obs.object_.properties.value, "values"):
for url in obs.object_.properties.value.values:
# URI object
create_new_ioc(data, i, ind.timestamp, stix_id, ind_id)
data[i]["indicator_type"] = "URL"
data[i]["value"] = url
i = i + 1
json_data = json.dumps(data)
demisto.results(json_data)
type: python
tags:
- stix
- ioc
comment: Parse Stix files to Demisto indicators
enabled: true
system: true
args:
- name: iocXml
required: true
default: true
description: ioc xml in stix format
scripttarget: 0
runonce: false
dockerimage: demisto/stix