forked from splunk/security_content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceNow_Related_Tickets_Search.py
452 lines (342 loc) · 20.1 KB
/
ServiceNow_Related_Tickets_Search.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
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
"""
Accepts a user or device and identifies if related tickets exists in a timeframe of last 30 days. Generates a global report and list of observables.
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
@phantom.playbook_block()
def on_start(container):
phantom.debug('on_start() called')
# call 'default_table_list' block
default_table_list(container=container)
return
@phantom.playbook_block()
def default_table_list(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("default_table_list() called")
################################################################################
# Adjust the table list variable to change which tables should be searched.
################################################################################
default_table_list__output = None
################################################################################
## Custom Code Start
################################################################################
# Default tables list to find related tickets. Adjust as needed.
default_table_list = [
'incident',
'change_request',
'change_task',
'problem',
'sc_request',
'sc_task',
'sc_req_item',
]
default_table_list__output = default_table_list
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="default_table_list:output", value=json.dumps(default_table_list__output))
convert_table_list(container=container)
return
@phantom.playbook_block()
def convert_table_list(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("convert_table_list() called")
default_table_list__output = json.loads(_ if (_ := phantom.get_run_data(key="default_table_list:output")) != "" else "null") # pylint: disable=used-before-assignment
parameters = []
parameters.append({
"input_list": default_table_list__output,
})
################################################################################
## Custom Code Start
################################################################################
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/list_demux", parameters=parameters, name="convert_table_list", callback=calculate_earliest_time)
return
@phantom.playbook_block()
def space_delimiter_input(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("space_delimiter_input() called")
################################################################################
# Convert playbook input into space delimiter string for ServiceNow query.
################################################################################
template = """{0}\n"""
# parameter list for template variable replacement
parameters = [
"filtered-data:input_filter:condition_1:playbook_input:search_term"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="space_delimiter_input", separator=" ", drop_none=True)
run_ticket_query(container=container)
return
@phantom.playbook_block()
def input_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("input_filter() called")
################################################################################
# Creates a dataset without None values.
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["playbook_input:search_term", "!=", None]
],
name="input_filter:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
space_delimiter_input(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
@phantom.playbook_block()
def process_results(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("process_results() called")
################################################################################
# Iterates through the results of the run ticket query to link playbook input
# search term to their associated tickets.
################################################################################
filtered_input_0_search_term = phantom.collect2(container=container, datapath=["filtered-data:input_filter:condition_1:playbook_input:search_term"])
run_ticket_query_result_data = phantom.collect2(container=container, datapath=["run_ticket_query:action_result.data","run_ticket_query:action_result.parameter.query_table"], action_results=results)
filtered_input_0_search_term_values = [item[0] for item in filtered_input_0_search_term]
run_ticket_query_result_item_0 = [item[0] for item in run_ticket_query_result_data]
run_ticket_query_parameter_query_table = [item[1] for item in run_ticket_query_result_data]
process_results__output = None
################################################################################
## Custom Code Start
################################################################################
process_results__output = {}
for search_term in filtered_input_0_search_term_values:
process_results__output[search_term] = []
for result, query_table in zip(run_ticket_query_result_item_0, run_ticket_query_parameter_query_table):
if not isinstance(result, list):
list_result = [result]
else:
list_result = result
for result_item in list_result:
result_item_values = [item.lower() for item in result_item.values() if isinstance(item, str)]
match = False
for string_value in result_item_values:
if search_term.lower() in string_value:
match = True
break
if match:
process_results__output[search_term].append({**result_item, **{"ticket_type": query_table}})
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="process_results:output", value=json.dumps(process_results__output))
build_output(container=container)
return
@phantom.playbook_block()
def build_output(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("build_output() called")
################################################################################
# Extract relevant data and add them to an observable array.
################################################################################
process_results__output = json.loads(_ if (_ := phantom.get_run_data(key="process_results:output")) != "" else "null") # pylint: disable=used-before-assignment
build_output__observable_array = None
build_output__name = None
build_output__number = None
build_output__message = None
build_output__start_time = None
build_output__end_time = None
build_output__assignee = None
build_output__creator_name = None
build_output__state = None
build_output__matched_fields = None
build_output__source_link = None
build_output__source = None
################################################################################
## Custom Code Start
################################################################################
import re
build_output__observable_array = []
build_output__name = []
build_output__number = []
build_output__message = []
build_output__start_time = []
build_output__end_time = []
build_output__assignee = []
build_output__creator_name = []
build_output__state = []
build_output__matched_fields = []
build_output__source_link = []
build_output__source = []
def generate_ticket_link(sample_url, ticket_type, sys_id):
extract_host = re.search(r"https*:\/\/[^\/]+", sample_url).group(0)
extract_host += f"/nav_to.do?uri={ticket_type}.do?sys_id={sys_id}"
return extract_host
for key in process_results__output.keys():
for value in process_results__output[key]:
assigned_to = None
caller_id = None
matched_fields = []
if value.get("assigned_to"):
assigned_to = value["assigned_to"]["display_value"]
if value.get("caller_id"):
caller_id = value["caller_id"]["display_value"]
for k, v in value.items():
# generate matched fields where the searched entity appears
if isinstance(v, str) and key.lower() in v.lower():
matched_fields.append(k)
# search for any link sample:
if isinstance(v, dict):
sample_link = v.get('link')
source_link = generate_ticket_link(sample_link, value['ticket_type'], value['sys_id'])
observable_object = {
"value": key,
"ticket": {
"name": value["short_description"],
"id": value["sys_id"],
"number": value["number"],
"message": json.dumps(value["description"]),
"start_time": value["sys_created_on"],
"end_time": value["closed_at"],
"assigned_to": assigned_to,
"creator_name": caller_id,
"state": value["state"],
"notes": [value["work_notes"]],
"comments": [value["comments"]]
},
"matched_fields": matched_fields,
"source": "ServiceNow",
"source_link": source_link
}
build_output__observable_array.append(observable_object)
build_output__name.append(value["short_description"])
build_output__number.append(value["number"])
build_output__message.append(json.dumps(value["description"])) # eliminate new line issues
build_output__start_time.append(value["sys_created_on"])
build_output__end_time.append(value["closed_at"])
build_output__assignee.append(assigned_to)
build_output__creator_name.append(caller_id)
build_output__state.append(value["state"])
build_output__matched_fields.append(matched_fields)
build_output__source.append("ServiceNow")
build_output__source_link.append(source_link)
#phantom.debug(observable_object)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="build_output:observable_array", value=json.dumps(build_output__observable_array))
phantom.save_run_data(key="build_output:name", value=json.dumps(build_output__name))
phantom.save_run_data(key="build_output:number", value=json.dumps(build_output__number))
phantom.save_run_data(key="build_output:message", value=json.dumps(build_output__message))
phantom.save_run_data(key="build_output:start_time", value=json.dumps(build_output__start_time))
phantom.save_run_data(key="build_output:end_time", value=json.dumps(build_output__end_time))
phantom.save_run_data(key="build_output:assignee", value=json.dumps(build_output__assignee))
phantom.save_run_data(key="build_output:creator_name", value=json.dumps(build_output__creator_name))
phantom.save_run_data(key="build_output:state", value=json.dumps(build_output__state))
phantom.save_run_data(key="build_output:matched_fields", value=json.dumps(build_output__matched_fields))
phantom.save_run_data(key="build_output:source_link", value=json.dumps(build_output__source_link))
phantom.save_run_data(key="build_output:source", value=json.dumps(build_output__source))
format_report(container=container)
return
@phantom.playbook_block()
def calculate_earliest_time(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("calculate_earliest_time() called")
create_time_value = container.get("create_time", None)
parameters = []
parameters.append({
"input_datetime": create_time_value,
"amount_to_modify": -30,
"modification_unit": "days",
"input_format_string": "%Y-%m-%d %H:%M:%S.%f+00",
"output_format_string": "'%Y-%m-%d','%H:%M:%S'",
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/datetime_modify", parameters=parameters, name="calculate_earliest_time", callback=input_filter)
return
@phantom.playbook_block()
def run_ticket_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("run_ticket_query() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
query_formatted_string = phantom.format(
container=container,
template="""sysparm_query=active=true^IR_OR_QUERY={0}^opened_at>javascript:gs.dateGenerate({1})&sysparm_display_value=true \n\n""",
parameters=[
"space_delimiter_input:formatted_data",
"calculate_earliest_time:custom_function_result.data.datetime_string"
])
################################################################################
# Perform a text search match within ServiceNow.
################################################################################
calculate_earliest_time__result = phantom.collect2(container=container, datapath=["calculate_earliest_time:custom_function_result.data.datetime_string"])
convert_table_list__result = phantom.collect2(container=container, datapath=["convert_table_list:custom_function_result.data.output"])
space_delimiter_input = phantom.get_format_data(name="space_delimiter_input")
parameters = []
# build parameters list for 'run_ticket_query' call
for calculate_earliest_time__result_item in calculate_earliest_time__result:
for convert_table_list__result_item in convert_table_list__result:
if query_formatted_string is not None and convert_table_list__result_item[0] is not None:
parameters.append({
"query": query_formatted_string,
"max_results": 100,
"query_table": convert_table_list__result_item[0],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("run query", parameters=parameters, name="run_ticket_query", assets=["servicenow"], callback=process_results)
return
@phantom.playbook_block()
def format_report(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("format_report() called")
################################################################################
# Format a summary table with the information gathered from the playbook.
################################################################################
template = """SOAR retrieved tickets from Service Now. The table below shows a summary of the information gathered.\n\n| Name | Number | Message | Start Time | End Time | Assignee | Creator Name | State | Matched Fields | Source | Source Link |\n| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n%%\n| {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} | {8} | {9} | {10} |\n%%\n"""
# parameter list for template variable replacement
parameters = [
"build_output:custom_function:name",
"build_output:custom_function:number",
"build_output:custom_function:message",
"build_output:custom_function:start_time",
"build_output:custom_function:end_time",
"build_output:custom_function:assignee",
"build_output:custom_function:creator_name",
"build_output:custom_function:state",
"build_output:custom_function:matched_fields",
"build_output:custom_function:source",
"build_output:custom_function:source_link"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_report")
return
@phantom.playbook_block()
def on_finish(container, summary):
phantom.debug("on_finish() called")
format_report = phantom.get_format_data(name="format_report")
build_output__observable_array = json.loads(_ if (_ := phantom.get_run_data(key="build_output:observable_array")) != "" else "null") # pylint: disable=used-before-assignment
output = {
"observable": build_output__observable_array,
"markdown_report": format_report,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.save_playbook_output_data(output=output)
return