forked from splunk-soar-connectors/recordedfuture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecordedfuture_view.py
523 lines (404 loc) · 18.4 KB
/
recordedfuture_view.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
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
# File: recordedfuture_view.py
#
# Copyright (c) Recorded Future, Inc, 2019-2024
#
# This unpublished material is proprietary to Recorded Future. All
# rights reserved. The methods and techniques described herein are
# considered trade secrets and/or confidential. Reproduction or
# distribution, in whole or in part, is forbidden except by express
# written permission of Recorded Future.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language governing permissions
# and limitations under the License.
from datetime import datetime
from recordedfuture_consts import RF_PLAYBOOK_STATUS_MAP
APP_URL = "https://app.recordedfuture.com/live/sc/entity/%s%%3A%s"
VULN_APP_URL = "https://app.recordedfuture.com/live/sc/entity/%s"
ENTITY_LIST_STATUS_VALUE_TO_LITERAL_MAPPING = {
"ready": "Ready (no pending updates)",
"pending": "Processing update",
"processing": "Processing update",
"added": "added",
"unchanged": "is already in list",
"not_in_list": "is not in list",
"removed": "removed from list",
}
PLAYBOOK_ALERT_CATEGORY_DISPLAY_MAPPING = {
"domain_abuse": "Domain Abuse",
"cyber_vulnerability": "Vulnerability",
"code_repo_leakage": "Code Repo Leakage",
}
def format_datetime_string(datetime_string):
try:
return datetime.strptime(datetime_string, "%Y-%m-%dT%H:%M:%S.%f%z").strftime("%Y-%m-%d, %H:%M")
except ValueError:
return datetime_string
def format_domain_abuse_details_result(result):
retval = {"param": result.get_param()}
data = result.get_data()
if data:
data = data[0]
else:
retval["data"] = data
return retval
data["panel_status"]["created"] = format_datetime_string(data["panel_status"]["created"])
data["panel_status"]["updated"] = format_datetime_string(data["panel_status"]["updated"])
panel_evidence_whois = data.get("panel_evidence_whois", {})
if panel_evidence_whois and not isinstance(panel_evidence_whois.get("body"), list):
panel_evidence_whois["body"] = []
retval["data"] = data
return retval
def format_result(result, all_data=False):
retval = {"param": result.get_param()}
data = result.get_data()
if data:
retval["data"] = data[0]
try:
# assemble the string needed for an URL to Recorded Future portal
if data and "risk" in retval["data"] and retval["data"]["risk"]["score"] is not None:
if "domain" in retval["param"]:
retval["intelCard"] = APP_URL % ("idn", retval["param"]["domain"])
elif "ip" in retval["param"]:
retval["intelCard"] = APP_URL % ("ip", retval["param"]["ip"])
elif "hash" in retval["param"]:
retval["intelCard"] = APP_URL % ("hash", retval["param"]["hash"])
elif "url" in retval["param"]:
retval["intelCard"] = APP_URL % ("url", retval["param"]["url"])
elif "vulnerability" in retval["param"]:
retval["intelCard"] = VULN_APP_URL % (retval["data"]["entity"]["id"])
for rule in retval["data"]["risk"]["evidenceDetails"]:
rule["timestampShort"] = rule["timestamp"][:10]
# add cvss info only if present (should only be applicable by vulnerabilities)
if data and "cvss" in retval["data"] and "published" in retval["data"]["cvss"]:
retval["data"]["cvss"]["publishedShort"] = retval["data"]["cvss"]["published"][:10]
retval["data"]["cvss"]["lastModifiedShort"] = retval["data"]["cvss"]["lastModified"][:10]
# format date and time to be shorter and easier to read
retval["data"]["timestamps"]["firstSeenShort"] = retval["data"]["timestamps"]["firstSeen"][:10]
retval["data"]["timestamps"]["lastSeenShort"] = retval["data"]["timestamps"]["lastSeen"][:10]
except Exception:
retval["data"] = None
# set summary, status and message for the action
summary = result.get_summary()
if summary:
retval["summary"] = summary
status = result.get_status()
if status:
retval["status"] = "Success"
else:
retval["status"] = "Failure"
message = result.get_message()
if message:
retval["message"] = message
return retval
def format_reputation_result(result, all_data=False):
retval = {"param": result.get_param()}
data = result.get_data()
if data:
retval["data"] = data[0]
if data and retval["data"]["riskscore"] is not None:
if "domain" in retval["param"]:
retval["intelCard"] = APP_URL % ("idn", retval["param"]["domain"])
elif "ip" in retval["param"]:
retval["intelCard"] = APP_URL % ("ip", retval["param"]["ip"])
elif "hash" in retval["param"]:
retval["intelCard"] = APP_URL % ("hash", retval["param"]["hash"])
elif "url" in retval["param"]:
retval["intelCard"] = APP_URL % ("url", retval["param"]["url"])
elif "vulnerability" in retval["param"]:
retval["intelCard"] = VULN_APP_URL % (retval["data"]["id"])
summary = result.get_summary()
if summary:
retval["summary"] = summary
status = result.get_status()
if status:
retval["status"] = "Success"
else:
retval["status"] = "Failure"
message = result.get_message()
if message:
retval["message"] = message
return retval
def format_contexts_result(result, all_data=False):
retval = {"param": result.get_param()}
data = result.get_data()
if data:
retval["data"] = data
summary = result.get_summary()
if summary:
retval["summary"] = summary
status = result.get_status()
if status:
retval["status"] = "Success"
else:
retval["status"] = "Failure"
message = result.get_message()
if message:
retval["message"] = message
return retval
def intelligence_results(provides, all_app_runs, context):
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
formatted = format_result(result)
if not formatted:
continue
results.append(formatted)
return "intelligence_results.html"
def reputation_results(provides, all_app_runs, context):
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
formatted = format_reputation_result(result)
if not formatted:
continue
results.append(formatted)
return "reputation_results.html"
def contexts_results(provides, all_app_runs, context):
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
formatted = format_contexts_result(result)
if not formatted:
continue
results.append(formatted)
return "contexts_results.html"
def format_alert_result(result):
return format_result(result)
def alert_lookup_results(provides, all_app_runs, context):
"""Setup the view for an alert."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
formatted = {"param": result.get_param(), "data": result.get_data()}
if not formatted:
continue
results.append(formatted)
return "alert_lookup_results.html"
def alert_update_results(provides, all_app_runs, context):
"""Setup the view for an alert."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
formatted = {"param": result.get_param(), "data": result.get_data()}
if not formatted:
continue
results.append(formatted)
return "alert_update_results.html"
def alert_search_results(provides, all_app_runs, context):
"""Setup the view for alert results."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
formatted = {"param": result.get_param(), "data": result.get_data()}
if not formatted:
continue
results.append(formatted)
return "alert_search_results.html"
def alert_rule_search_results(provides, all_app_runs, context):
"""Render the list of Alert Rules that match the search."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
formatted = {"param": result.get_param(), "data": result.get_data()}
if not formatted:
continue
results.append(formatted)
return "alert_rule_search_results.html"
def format_threat_assessment_result(result, all_data=False):
retval = {"param": result.get_param()}
data = result.get_data()
if data:
ret_data = {key: data[0][key] for key in data[0].keys() if key != "entities"}
entities = data[0]["entities"]
entities.sort(key=lambda x: int(x.get("riskscore", "0")))
ret_data["entities"] = [entity for entity in entities if entity["riskscore"]]
retval["data"] = ret_data
else:
retval["data"] = "NO DATA"
summary = result.get_summary()
if summary:
retval["summary"] = summary
status = result.get_status()
if status:
retval["status"] = "Success"
else:
retval["status"] = "Failure"
message = result.get_message()
if message:
retval["message"] = message
return retval
def threat_assessment_results(provides, all_app_runs, context):
"""Setup the view for Threat Assessment results."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
formatted = format_threat_assessment_result(result)
if not formatted:
continue
results.append(formatted)
return "threat_assessment_results.html"
def list_search_results(provides, all_app_runs, context):
"""Setup the view for list search results."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
results.append({"param": result.get_param(), "data": result_data})
return "list_search_results.html"
def list_create_results(provides, all_app_runs, context):
"""Setup the view for list create result"""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
results.append({"param": result.get_param(), "data": result_data})
return "list_create_results.html"
def list_details_results(provides, all_app_runs, context):
"""Setup the view for list details result"""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
result_data["created"] = format_datetime_string(result_data["created"])
result_data["updated"] = format_datetime_string(result_data["updated"])
results.append({"param": result.get_param(), "data": result_data})
return "list_details_results.html"
def list_status_results(provides, all_app_runs, context):
"""Setup the view for list status info"""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
result_data["status"] = ENTITY_LIST_STATUS_VALUE_TO_LITERAL_MAPPING.get(result_data["status"])
results.append({"param": result.get_param(), "data": result_data})
return "list_status_results.html"
def list_entities_results(provides, all_app_runs, context):
"""Setup the view for list status info"""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
for entity in result_data:
entity["added"] = format_datetime_string(entity["added"])
entity["status"] = ENTITY_LIST_STATUS_VALUE_TO_LITERAL_MAPPING.get(entity["status"])
results.append({"param": result.get_param(), "data": result_data})
return "list_entities_results.html"
def list_entities_management_results(provides, all_app_runs, context):
"""Setup the view for list status info"""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
result_data["result"] = ENTITY_LIST_STATUS_VALUE_TO_LITERAL_MAPPING.get(result_data["result"])
results.append({"param": result.get_param(), "data": result_data})
return "list_entities_management_results.html"
def playbook_alert_search_results(provides, all_app_runs, context):
"""Setup the view for Playbook alerts search result"""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
for search_result in result_data:
search_result["created"] = format_datetime_string(search_result["created"])
search_result["updated"] = format_datetime_string(search_result["updated"])
search_result["status"] = RF_PLAYBOOK_STATUS_MAP.get(search_result["status"], search_result["status"])
search_result["category_display"] = PLAYBOOK_ALERT_CATEGORY_DISPLAY_MAPPING.get(
search_result["category"], search_result["category"]
)
results.append({"param": result.get_param(), "data": result_data})
return "playbook_alert_search_results.html"
def playbook_alert_update_results(provides, all_app_runs, context):
"""Setup the view for Playbook alert update"""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
results.append({"param": result.get_param(), "data": result_data})
return "playbook_alert_update_results.html"
def playbook_alert_details_results(provides, all_app_runs, context):
"""Setup the view for Playbook alert details"""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
results.append(format_domain_abuse_details_result(result))
return "playbook_alert_details_results.html"
def entity_search_results(provides, all_app_runs, context):
"""Setup the view for entity search results."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
results.append({"param": result.get_param(), "data": result_data})
return "entity_search_results.html"
def links_search_results(provides, all_app_runs, context):
"""Setup the view for links search results."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
results.append({"param": result.get_param(), "data": result_data})
return "links_search_results.html"
def detection_rule_search_results(provides, all_app_runs, context):
"""Setup the view for detection rule search results."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
results.append({"param": result.get_param(), "data": result_data})
return "detection_rule_search_results.html"
def threat_actor_intelligence_results(provides, all_app_runs, context):
"""Setup the view for threat actor intelligence results."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
result_data["categories"] = [category.get("name") for category in result_data.get("categories", [])]
results.append({"param": result.get_param(), "data": result_data})
return "threat_actor_intelligence_results.html"
def threat_map_results(provides, all_app_runs, context):
"""Setup the view for threat map results."""
context["results"] = results = []
for summary, action_results in all_app_runs:
for result in action_results:
result_data = result.get_data()
if result_data:
result_data = result_data[0]
for actor in result_data.get("threatActor", []):
actor["categories"] = [category.get("name") for category in actor.get("categories", [])]
results.append({"param": result.get_param(), "data": result_data})
return "threat_map_results.html"
def collective_insights_submission_results(provides, all_app_runs, context):
"""Setup the view for collective insights submission."""
return "collective_insights_submission_results.html"