-
Notifications
You must be signed in to change notification settings - Fork 8
/
splunk_connector.py
1565 lines (1262 loc) · 66.9 KB
/
splunk_connector.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
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
# File: splunk_connector.py
#
# Copyright (c) 2016-2024 Splunk Inc.
#
# 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.
#
import hashlib
import os
import re
import ssl
import sys
import tempfile
import time
import traceback
from builtins import map # noqa
from builtins import range # noqa
from builtins import str # noqa
from datetime import datetime
from io import BytesIO
from typing import Optional
from urllib.error import HTTPError as UrllibHTTPError # noqa
from urllib.error import URLError # noqa
from urllib.parse import urlencode, urlparse # noqa
from urllib.request import ProxyHandler # noqa
from urllib.request import Request # noqa
from urllib.request import build_opener # noqa
from urllib.request import install_opener # noqa
from urllib.request import urlopen # noqa
import phantom.app as phantom
import phantom.rules as soar_vault
import pytz
import requests
import simplejson as json
import splunklib.binding as splunk_binding
import splunklib.client as splunk_client
import splunklib.results as splunk_results
import xmltodict
from bs4 import BeautifulSoup, UnicodeDammit
from dateutil.parser import ParserError
from dateutil.parser import parse as dateutil_parse
from past.utils import old_div # noqa
from phantom.base_connector import BaseConnector
from phantom.vault import Vault
from pytz import timezone
from splunklib.binding import HTTPError
import splunk_consts as consts
class RetVal(tuple):
def __new__(cls, val1, val2=None):
return tuple.__new__(RetVal, (val1, val2))
class SplunkConnector(BaseConnector):
ACTION_ID_POST_DATA = "post_data"
ACTION_ID_RUN_QUERY = "run_query"
ACTION_ID_UPDATE_EVENT = "update_event"
ACTION_ID_GET_HOST_EVENTS = "get_host_events"
def __init__(self):
# Call the BaseConnectors init first
super(SplunkConnector, self).__init__()
self._service = None
self._base_url = None
self.splunk_server = None
self.retry_count = None
self.port = None
self.max_container = None
self._splunk_status_dict = None
self._splunk_disposition_dict = None
self.container_update_state = None
self.remove_empty_cef = None
self.sleeptime_in_requests = None
def _get_error_message_from_exception(self, e):
"""This method is used to get appropriate error message from the exception.
:param e: Exception object
:return: error message
"""
error_code = None
error_message = consts.SPLUNK_ERR_MESSAGE_UNAVAILABLE
self.error_print("Traceback: {}".format(traceback.format_stack()))
try:
if hasattr(e, "args"):
if len(e.args) > 1:
error_code = e.args[0]
error_message = e.args[1]
elif len(e.args) == 1:
error_message = e.args[0]
else:
error_message = consts.SPLUNK_ERR_MESSAGE_UNAVAILABLE
if error_message == consts.SPLUNK_ERR_MESSAGE_UNAVAILABLE:
error_message = str(e).strip().replace("'", "").replace('"', "").replace("\n", "").replace("\r", "")
if len(error_message) > 500:
error_message = "{} - truncated".format(error_message[:500])
error_message = "{} ({})".format(error_message, sys.exc_info()[-1].tb_lineno)
except Exception as e:
self._dump_error_log(e, "Error occurred while fetching exception information")
if not error_code:
error_message = "Error Message: {}".format(error_message)
else:
error_message = "Error Code: {}. Error Message: {}".format(error_code, error_message)
return error_message
def initialize(self):
config = self.get_config()
self.splunk_server = config[phantom.APP_JSON_DEVICE]
self._username = config.get(phantom.APP_JSON_USERNAME)
self._password = config.get(phantom.APP_JSON_PASSWORD)
self._api_token = config.get(consts.SPLUNK_JSON_API_KEY)
self._base_url = "https://{0}:{1}/".format(self.splunk_server, config.get(phantom.APP_JSON_PORT, 8089))
self._state = self.load_state()
if not isinstance(self._state, dict):
self.debug_print("State file format is not valid")
self._state = {}
self.save_state(self._state)
self.debug_print("Recreated the state file with current app_version")
self._state = self.load_state()
if self._state is None:
self.debug_print("Please check the owner, owner group, and the permissions of the state file")
self.debug_print(
"The Splunk SOAR user should have correct access rights and ownership for the \
corresponding state file (refer readme file for more information)"
)
return phantom.APP_ERROR
self._proxy = {}
# Either username and password or API token must be provided
if not self._api_token and (not self._username or not self._password):
return self.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_REQUIRED_CONFIG_PARAMS)
if "http_proxy" in os.environ:
self._proxy["http"] = os.environ.get("http_proxy")
elif "HTTP_PROXY" in os.environ:
self._proxy["http"] = os.environ.get("HTTP_PROXY")
if "https_proxy" in os.environ:
self._proxy["https"] = os.environ.get("https_proxy")
elif "HTTPS_PROXY" in os.environ:
self._proxy["https"] = os.environ.get("HTTPS_PROXY")
self._container_name_prefix = config.get("container_name_prefix", "")
container_name_values = config.get("container_name_values")
if container_name_values:
self._container_name_values = [x.strip() for x in container_name_values.split(",")]
else:
self._container_name_values = []
# Validate retry_count
ret_val, self.retry_count = self._validate_integer(self, config.get("retry_count", 3), consts.SPLUNK_RETRY_COUNT_KEY)
if phantom.is_fail(ret_val):
return self.get_status()
# Validate port
ret_val, self.port = self._validate_integer(self, config.get("port", 8089), consts.SPLUNK_PORT_KEY)
if phantom.is_fail(ret_val):
return self.get_status()
# Validate max_container
ret_val, self.max_container = self._validate_integer(self, config.get("max_container", 100), consts.SPLUNK_MAX_CONTAINER_KEY, True)
if phantom.is_fail(ret_val):
return self.get_status()
# Validate container_update_state
ret_val, self.container_update_state = self._validate_integer(
self, config.get("container_update_state", 100), consts.SPLUNK_CONTAINER_UPDATE_STATE_KEY
)
if phantom.is_fail(ret_val):
return self.get_status()
# Validate splunk_job_timeout
ret_val, self.splunk_job_timeout = self._validate_integer(self, config.get("splunk_job_timeout"), consts.SPLUNK_JOB_TIMEOUT_KEY)
if phantom.is_fail(ret_val):
return self.get_status()
# Validate sleeptime_in_requests
ret_val, self.sleeptime_in_requests = self._validate_integer(
self, config.get("sleeptime_in_requests", 1), consts.SPLUNK_SLEEPTIME_IN_REQUESTS_KEY
)
if phantom.is_fail(ret_val):
return self.get_status()
# Validate if user has entered more than 120 seconds
if self.sleeptime_in_requests > 120:
return self.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_INVALID_SLEEP_TIME.format(param=consts.SPLUNK_SLEEPTIME_IN_REQUESTS_KEY))
self.remove_empty_cef = config.get("remove_empty_cef", False)
return phantom.APP_SUCCESS
def finalize(self):
if self._state is not None:
self.save_state(self._state)
return phantom.APP_SUCCESS
def _dump_error_log(self, error, message="Exception occurred."):
self.error_print(message, dump_object=error)
def request(self, url, message, **kwargs):
"""Splunk SDK Proxy handler"""
method = message["method"].lower()
config = self.get_config()
data = message.get("body", "") if method == "post" else None
headers = dict(message.get("headers", []))
req = Request(url, data, headers)
try:
response = urlopen(req)
self.debug_print(response)
except URLError:
# If running Python 2.7.9+, disable SSL certificate validation and try again
if sys.version_info >= (2, 7, 9) and not config[phantom.APP_JSON_VERIFY]:
response = urlopen(req, context=ssl._create_unverified_context()) # nosemgrep
else:
raise
except UrllibHTTPError:
self.save_progress("Check the proxy settings")
pass # Propagate HTTP errors via the returned response message
return {"status": response.code, "reason": response.msg, "headers": response.getheaders(), "body": BytesIO(response.read())}
def handler(self, proxy):
"""Splunk SDK Proxy Request Handler"""
proxy_handler = ProxyHandler({"http": proxy, "https": proxy})
opener = build_opener(proxy_handler)
install_opener(opener)
return self.request
def _connect(self, action_result):
if self._service is not None:
return phantom.APP_SUCCESS
config = self.get_config()
kwargs_config_flags = {
"host": self.splunk_server,
"port": self.port,
"username": self._username,
"password": self._password,
"owner": config.get("splunk_owner", None),
"app": config.get("splunk_app", None),
}
# token-based authentication
if self._api_token:
self.save_progress("Using token-based authentication")
kwargs_config_flags["splunkToken"] = self._api_token
kwargs_config_flags.pop(phantom.APP_JSON_USERNAME)
kwargs_config_flags.pop(phantom.APP_JSON_PASSWORD)
self.save_progress(phantom.APP_PROG_CONNECTING_TO_ELLIPSES, self.splunk_server)
proxy_param = None
if self._proxy.get("http", None) is not None:
proxy_param = self._proxy.get("http")
if self._proxy.get("https", None) is not None:
proxy_param = self._proxy.get("https")
no_proxy_host = os.environ.get("no_proxy", os.environ.get("NO_PROXY", ""))
if self.splunk_server in no_proxy_host.split(","):
pass
elif self._api_token:
if any(proxy_var in os.environ for proxy_var in ["HTTPS_PROXY", "https_proxy"]):
self.save_progress("[-] Engaging Proxy")
else:
if any(proxy_var in os.environ for proxy_var in ["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"]):
self.save_progress("[-] Engaging Proxy")
try:
if proxy_param:
self._service = splunk_client.connect(handler=self.handler(proxy_param), **kwargs_config_flags)
else:
self._service = splunk_client.connect(**kwargs_config_flags)
except splunk_binding.HTTPError as e:
error_text = self._get_error_message_from_exception(e)
self._dump_error_log(e, "Error occurred while connecting to the Splunk server.")
if "405 Method Not Allowed" in error_text:
return action_result.set_status(phantom.APP_ERROR, "Error occurred while connecting to the Splunk server")
else:
return action_result.set_status(
phantom.APP_ERROR, "Error occurred while connecting to the Splunk server. Details: {}".format(error_text)
)
except Exception as e:
self._dump_error_log(e)
error_text = consts.SPLUNK_EXCEPTION_ERR_MESSAGE.format(
msg=consts.SPLUNK_ERR_CONNECTIVITY_FAILED, error_text=self._get_error_message_from_exception(e)
)
return action_result.set_status(phantom.APP_ERROR, error_text)
# Must return success if we want handle_action to be called
return phantom.APP_SUCCESS
def _validate_integer(self, action_result, parameter, key, allow_zero=False):
if parameter is not None:
try:
if not float(parameter).is_integer():
return action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_INVALID_INTEGER.format(param=key)), None
parameter = int(parameter)
except Exception:
return action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_INVALID_INTEGER.format(param=key)), None
if parameter < 0:
return action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_NON_NEGATIVE_INTEGER.format(param=key)), None
if not allow_zero and parameter == 0:
return action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_INVALID_PARAM.format(param=key)), None
return phantom.APP_SUCCESS, parameter
def _make_rest_call_retry(self, action_result, endpoint, data, params=None, method=requests.post):
if params is None:
params = {}
RETRY_LIMIT = self.retry_count
for _ in range(0, RETRY_LIMIT):
ret_val, resp_data = self._make_rest_call(action_result, endpoint, data, params, method)
if not phantom.is_fail(ret_val):
break
return ret_val, resp_data
def _make_rest_call(self, action_result, endpoint, data, params=None, method=requests.post):
if params is None:
params = {}
config = self.get_config()
url = "{0}services/{1}".format(self._base_url, endpoint)
self.debug_print("Making REST call to {0}".format(url))
auth, auth_headers = None, None
if self._api_token:
# Splunk token-based authentication
self.debug_print("Using token-based authentication")
auth_headers = {"Authorization": "Bearer {token}".format(token=self._api_token)}
else:
# Splunk username/password based authentication
auth = (self._username, self._password)
try:
r = method(
url,
data=data,
params=params,
auth=auth,
headers=auth_headers,
verify=config[phantom.APP_JSON_VERIFY],
timeout=consts.SPLUNK_DEFAULT_REQUEST_TIMEOUT,
)
except Exception as e:
error_text = consts.SPLUNK_EXCEPTION_ERR_MESSAGE.format(
msg=consts.SPLUNK_ERR_CONNECTIVITY_FAILED, error_text=self._get_error_message_from_exception(e)
)
return action_result.set_status(phantom.APP_ERROR, error_text), None
return self._process_response(r, action_result)
def _process_response(self, r, action_result):
"""
Process API response.
:param r: response object
:param action_result: object of Action Result
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
# store the r_text in debug data, it will get dumped in the logs if an error occurs
if hasattr(action_result, "add_debug_data"):
if r is not None:
action_result.add_debug_data({"r_status_code": r.status_code})
action_result.add_debug_data({"r_text": r.text})
action_result.add_debug_data({"r_headers": r.headers})
else:
action_result.add_debug_data({"r_text": "r is None"})
# Process each 'Content-Type' of response separately
# Process a json response
if "json" in r.headers.get("Content-Type", ""):
return self._process_json_response(r, action_result)
# Process an HTML response, Do this no matter what the api talks.
# There is a high chance of a PROXY in between Splunk SOAR and the rest of
# world, in case of errors, PROXY's return HTML, this function parses
# the error and adds it to the action_result.
if "html" in r.headers.get("Content-Type", ""):
return self._process_html_response(r, action_result)
if "xml" in r.headers.get("Content-Type", ""):
return self._process_xml_response(r, action_result)
# it's not content-type that is to be parsed, handle an empty response
if not r.text:
return self._process_empty_response(r, action_result)
# everything else is actually an error at this point
error_text = r.text.replace("{", "{{").replace("}", "}}")
message = "Can't process response from server. Status Code: {} Data from server: {}".format(r.status_code, error_text)
return RetVal(action_result.set_status(phantom.APP_ERROR, message), None)
def _process_empty_response(self, response, action_result):
"""
Process empty response.
:param response: response object
:param action_result: object of Action Result
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
if response.status_code == 200 or response.status_code == 204:
return RetVal(phantom.APP_SUCCESS, {})
return RetVal(action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_EMPTY_RESPONSE.format(code=response.status_code)), None)
def _process_xml_response(self, r, action_result):
resp_json = None
try:
if r.text:
resp_json = xmltodict.parse(r.text)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return RetVal(action_result.set_status(phantom.APP_ERROR, "Unable to parse XML response. Error: {0}".format(error_message)))
if 200 <= r.status_code < 400:
return RetVal(phantom.APP_SUCCESS, resp_json)
error_type = resp_json.get("response", {}).get("messages", {}).get("msg", {}).get("@type")
error_message = resp_json.get("response", {}).get("messages", {}).get("msg", {}).get("#text")
if error_type or error_message:
error = "ErrorType: {} ErrorMessage: {}".format(error_type, error_message)
else:
error = "Unable to parse xml response"
message = "Error from server. Status Code: {0} Data from server: {1}".format(r.status_code, error)
return RetVal(action_result.set_status(phantom.APP_ERROR, message), resp_json)
def _process_html_response(self, response, action_result):
"""
Process html response.
:param response: response object
:param action_result: object of Action Result
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
# An html response, treat it like an error
status_code = response.status_code
try:
soup = BeautifulSoup(response.text, "html.parser")
# Remove the script, style, footer and navigation part from the HTML message
for element in soup(["script", "style", "footer", "nav"]):
element.extract()
error_text = soup.text
split_lines = error_text.split("\n")
split_lines = [x.strip() for x in split_lines if x.strip()]
error_text = "\n".join(split_lines)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
error_text = consts.SPLUNK_ERR_UNABLE_TO_PARSE_HTML_RESPONSE.format(error=error_message)
if not error_text:
error_text = "Empty response and no information received"
message = "Status Code: {}. Data from server:\n{}\n".format(status_code, error_text)
message = message.replace("{", "{{").replace("}", "}}")
if len(message) > 500:
message = "Error occurred while connecting to the Splunk server"
return RetVal(action_result.set_status(phantom.APP_ERROR, message), None)
def _process_json_response(self, r, action_result):
"""
Process json response.
:param r: response object
:param action_result: object of Action Result
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
status_code = r.status_code
# Try a json parse
try:
resp_json = r.json()
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return RetVal(
action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_UNABLE_TO_PARSE_JSON_RESPONSE.format(error=error_message)), None
)
# Please specify the status codes here
if 200 <= r.status_code < 399:
return RetVal(phantom.APP_SUCCESS, resp_json)
if isinstance(resp_json, str):
message = "Error from server. Details: {}".format(resp_json)
elif resp_json.get("error") or resp_json.get("error_description"):
error = resp_json.get("error", "Unavailable")
error_details = resp_json.get("error_description", "Unavailable")
message = "Error from server. Status Code: {}. Error: {}. Error Details: {}".format(status_code, error, error_details)
elif resp_json.get("messages"):
if resp_json["messages"]:
error_type = resp_json["messages"][0].get("type")
error_message = resp_json["messages"][0].get("text")
if error_type or error_message:
error = "ErrorType: {} ErrorMessage: {}".format(error_type, error_message)
else:
error = "Unable to parse json response"
else:
error = "Unable to parse json response"
message = "Error from server. Status Code: {0} Data from server: {1}".format(r.status_code, error)
else:
# You should process the error returned in the json
error_text = r.text.replace("{", "{{").replace("}", "}}")
message = "Error from server. Status Code: {}. Data from server: {}".format(status_code, error_text)
return RetVal(action_result.set_status(phantom.APP_ERROR, message), None)
def _get_server_version(self, action_result):
endpoint = "authentication/users?output_mode=json"
ret_val, resp_data = self._make_rest_call_retry(action_result, endpoint, {}, method=requests.get)
if phantom.is_fail(ret_val):
return "FAILURE"
splunk_version = resp_data.get("generator", {}).get("version")
if not splunk_version:
splunk_version = "UNKNOWN"
return splunk_version
def _check_for_es(self, action_result):
endpoint = "apps/local/SplunkEnterpriseSecuritySuite"
ret_val, resp_data = self._make_rest_call_retry(action_result, endpoint, {}, method=requests.get)
if phantom.is_fail(ret_val) or not resp_data:
return False
return True
def _resolve_event_id(self, sidandrid, action_result, kwargs_create=dict()):
"""Query the splunk instance using the SID+RID of the notable to find the notable ID"""
self.send_progress("Running search_query: {}".format(consts.SPLUNK_RID_SID_NOTABLE_QUERY))
result = self._return_first_row_from_query(consts.SPLUNK_RID_SID_NOTABLE_QUERY.format(sidandrid), action_result)
if phantom.is_fail(result):
return RetVal(action_result.get_status(), None)
if "event_id" in result:
return RetVal(phantom.APP_SUCCESS, result["event_id"])
return RetVal(action_result.set_status(phantom.APP_ERROR, "could not find event_id of splunk event"), None)
def _return_first_row_from_query(self, search_query, action_result, kwargs_create=dict()):
"""Function that executes the query on splunk"""
self.debug_print("Search Query:", search_query)
RETRY_LIMIT = self.retry_count
if phantom.is_fail(self._connect(action_result)):
return action_result.get_status()
# Validate the search query
for attempt_count in range(0, RETRY_LIMIT):
try:
self._service.parse(search_query, parse_only=True)
break
except HTTPError as e:
error_text = consts.SPLUNK_EXCEPTION_ERR_MESSAGE.format(
msg=consts.SPLUNK_ERR_INVALID_QUERY, error_text=self._get_error_message_from_exception(e)
)
return action_result.set_status(phantom.APP_ERROR, error_text, query=search_query)
except Exception as e:
if attempt_count == RETRY_LIMIT - 1:
error_text = consts.SPLUNK_EXCEPTION_ERR_MESSAGE.format(
msg=consts.SPLUNK_ERR_CONNECTIVITY_FAILED, error_text=self._get_error_message_from_exception(e)
)
return action_result.set_status(phantom.APP_ERROR, error_text)
self.debug_print(consts.SPLUNK_PROG_CREATED_QUERY.format(query=search_query))
# Creating search job
self.save_progress(consts.SPLUNK_PROG_CREATING_SEARCH_JOB)
# Set any search creation flags here
kwargs_create.update({"exec_mode": "normal"})
self.debug_print("kwargs_create", kwargs_create)
# Create the job
for search_attempt_count in range(0, RETRY_LIMIT):
# Create the job
is_created_successfully, job = self._create_splunk_job(
action_result=action_result, retry_limit=RETRY_LIMIT, search_query=search_query, kwargs_create=kwargs_create
)
if phantom.is_fail(is_created_successfully):
return phantom.APP_ERROR
while True:
is_job_successful: bool = self._wait_until_splunk_job_results_are_ready(action_result, job, RETRY_LIMIT)
if phantom.is_fail(is_job_successful):
return phantom.APP_ERROR
stats = self._get_stats(job)
status = (
"Progress: %(progress)03.1f%% %(scan_count)d scanned " "%(event_count)d matched %(result_count)d results"
) % stats
self.send_progress(status)
if stats["is_done"] == "1":
break
time.sleep(self.sleeptime_in_requests)
self.send_progress("Parsing results...")
try:
results = splunk_results.JSONResultsReader(job.results(count=0, output_mode="json"))
except Exception as e:
error_text = consts.SPLUNK_EXCEPTION_ERR_MESSAGE.format(
msg="Error retrieving results", error_text=self._get_error_message_from_exception(e)
)
return action_result.set_status(phantom.APP_ERROR, error_text)
for result in results:
if isinstance(result, dict):
return result
time.sleep(20)
return action_result.set_status(phantom.APP_ERROR)
def _post_data(self, param):
action_result = self.add_action_result(phantom.ActionResult(dict(param)))
host = param.get(consts.SPLUNK_JSON_HOST)
index = param.get(consts.SPLUNK_JSON_INDEX)
source = param.get(consts.SPLUNK_JSON_SOURCE, consts.SPLUNK_DEFAULT_SOURCE)
source_type = param.get(consts.SPLUNK_JSON_SOURCE_TYPE, consts.SPLUNK_DEFAULT_SOURCE_TYPE)
try:
post_data = UnicodeDammit(param[consts.SPLUNK_JSON_DATA]).unicode_markup.encode("utf-8")
except Exception as e:
self._dump_error_log(e, "Error while encoding data.")
get_params = {"source": source, "sourcetype": source_type}
if host:
get_params["host"] = host
if index:
get_params["index"] = index
endpoint = "receivers/simple"
ret_val, resp_data = self._make_rest_call_retry(action_result, endpoint, post_data, params=get_params)
if phantom.is_fail(ret_val):
return ret_val
return action_result.set_status(phantom.APP_SUCCESS, "Successfully posted the data")
def _get_stats(self, job):
stats = {
"is_done": job["isDone"] if ("isDone" in job) else "Unknown status",
"progress": (
float(job["doneProgress"]) * 100
if ("doneProgress" in job)
else consts.SPLUNK_JOB_FIELD_NOT_FOUND_MESSAGE.format(field="Done progress")
),
"scan_count": (
int(job["scanCount"]) if ("scanCount" in job) else consts.SPLUNK_JOB_FIELD_NOT_FOUND_MESSAGE.format(field="Scan count")
),
"event_count": (
int(job["eventCount"]) if ("eventCount" in job) else consts.SPLUNK_JOB_FIELD_NOT_FOUND_MESSAGE.format(field="Event count")
),
"result_count": (
int(job["resultCount"]) if ("resultCount" in job) else consts.SPLUNK_JOB_FIELD_NOT_FOUND_MESSAGE.format(field="Result count")
),
}
return stats
def _set_splunk_status_dict(self, action_result, type):
splunk_dict = {}
endpoint = "alerts/reviewstatuses?count=-1&output_mode=json"
ret_val, resp_data = self._make_rest_call_retry(action_result, endpoint, {}, method=requests.get)
if phantom.is_fail(ret_val) or not resp_data:
return splunk_dict
entry = resp_data.get("entry")
if not entry:
return splunk_dict
for data in entry:
object_id = data.get("name").split(":")[-1]
object_name = data.get("content", {}).get("label")
is_enabled = str(data.get("content", {}).get("disabled")) == "0"
is_allowed_type = data.get("content", {}).get("status_type") == type
if object_id and object_id.isdigit() and object_name and is_enabled and is_allowed_type:
if type == "notable":
object_name = object_name.lower()
splunk_dict[object_name] = int(object_id)
return splunk_dict
def _update_event(self, param):
action_result = self.add_action_result(phantom.ActionResult(dict(param)))
if not self._check_for_es(action_result):
return action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_NOT_ES)
owner = param.get(consts.SPLUNK_JSON_OWNER)
ids = param.get(consts.SPLUNK_JSON_EVENT_IDS)
status = param.get(consts.SPLUNK_JSON_STATUS)
ret_val, integer_status = self._validate_integer(
action_result, param.get("integer_status"), consts.SPLUNK_INT_STATUS_KEY, allow_zero=True
)
if phantom.is_fail(ret_val):
return action_result.get_status()
ret_val, integer_disposition = self._validate_integer(
action_result, param.get("integer_disposition"), consts.SPLUNK_INT_DISPOSITION_KEY, allow_zero=True
)
if phantom.is_fail(ret_val):
return action_result.get_status()
comment = param.get(consts.SPLUNK_JSON_COMMENT)
urgency = param.get(consts.SPLUNK_JSON_URGENCY)
wait_for_confirmation = param.get("wait_for_confirmation", False)
disposition = param.get("disposition", "")
regexp = re.compile(r"\+\d*(\.\d+)?[\"$]")
if regexp.search(json.dumps(ids)):
self.send_progress("Interpreting the event ID as an SID + RID combo; querying for the actual event_id...")
self.debug_print("Interpreting the event ID as an SID + RID combo; querying for the actual event_id...")
ret_val, event_id = self._resolve_event_id(ids, action_result, param)
if phantom.is_fail(ret_val):
return action_result.set_status(phantom.APP_ERROR, "Unable to find underlying event_id from SID + RID combo")
ids = event_id
if not any([comment, status, urgency, owner, disposition]) and integer_status is None and integer_disposition is None:
return action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_NEED_PARAM)
if status or integer_status is not None:
self._splunk_status_dict = self._set_splunk_status_dict(action_result, "notable")
if not self._splunk_status_dict:
return action_result.set_status(phantom.APP_ERROR, "Error occurred while fetching Splunk event status")
if disposition or integer_disposition is not None:
self._splunk_disposition_dict = self._set_splunk_status_dict(action_result, "disposition")
if not self._splunk_disposition_dict:
return action_result.set_status(phantom.APP_ERROR, "Error occurred while fetching Splunk event disposition")
self.debug_print("Attempting to create a connection")
# 1. Connect and validate whether the given Event IDs are valid or not
if phantom.is_fail(self._connect(action_result)):
return action_result.get_status()
self.debug_print("Connection established.")
if wait_for_confirmation:
self.debug_print("Searching for the event ID.")
search_query = "search `notable_by_id({0})`".format(ids)
ret_val = self._run_query(search_query, action_result)
if phantom.is_fail(ret_val):
return action_result.set_status(
phantom.APP_ERROR, "Error occurred while validating the provided event ID. Error: {0}".format(action_result.get_message())
)
if int(action_result.get_data_size()) <= 0:
return action_result.set_status(phantom.APP_ERROR, "Please provide a valid event ID")
self.debug_print("Event ID found")
# 2. Re-initialize the action_result object for update event
self.remove_action_result(action_result)
action_result = self.add_action_result(phantom.ActionResult(dict(param)))
# 3. Update the provided Events ID
request_body = {"ruleUIDs": ids}
if integer_status is not None:
if int(integer_status) not in list(self._splunk_status_dict.values()):
return action_result.set_status(
phantom.APP_ERROR,
"Please provide a valid value in 'integer_status' action\
parameter. Valid values: {}".format(
(", ".join(map(str, list(self._splunk_status_dict.values()))))
),
)
request_body["status"] = str(integer_status)
elif status:
if status not in self._splunk_status_dict:
if not status.isdigit():
return action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_BAD_STATUS)
request_body["status"] = status
else:
request_body["status"] = self._splunk_status_dict[status]
if integer_disposition is not None:
if int(integer_disposition) not in self._splunk_disposition_dict.values():
self.debug_print(f"int disposition: {self._splunk_disposition_dict}")
return action_result.set_status(
phantom.APP_ERROR,
"Please provide a valid value in 'integer_disposition' action\
parameter. Valid values: {}".format(
(", ".join(map(str, self._splunk_disposition_dict.values())))
),
)
request_body["disposition"] = consts.SPLUNK_DISPOSITION_QUERY_FORMAT.format(integer_disposition)
elif disposition:
if disposition not in self._splunk_disposition_dict:
if not disposition.isdigit():
return action_result.set_status(phantom.APP_ERROR, consts.SPLUNK_ERR_BAD_DISPOSITION)
request_body["disposition"] = consts.SPLUNK_DISPOSITION_QUERY_FORMAT.format(disposition)
else:
request_body["disposition"] = consts.SPLUNK_DISPOSITION_QUERY_FORMAT.format(self._splunk_disposition_dict[disposition])
param_mapping = {"urgency": urgency, "comment": comment, "newOwner": owner}
request_body.update({k: v for k, v in param_mapping.items() if v})
self.debug_print("Updating the event")
endpoint = "notable_update"
ret_val, resp_data = self._make_rest_call_retry(action_result, endpoint, request_body)
if not ret_val:
return ret_val
if "success" in resp_data and not resp_data.get("success"):
msg = resp_data.get("message")
return action_result.set_status(phantom.APP_ERROR, msg if msg else "Unable to update the notable event")
action_result.add_data(resp_data)
action_result.update_summary({consts.SPLUNK_JSON_UPDATED_EVENT_ID: ids})
if wait_for_confirmation:
return action_result.set_status(phantom.APP_SUCCESS)
return action_result.set_status(
phantom.APP_SUCCESS,
"Updated Event ID: {}. The event_id has not been verified. \
Please confirm that the provided event_id corresponds to an actual notable event".format(
ids
),
)
def _get_host_events(self, param):
"""Executes the query to get events pertaining to a host
Gets the events for a host for the last 'N' number of days
"""
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(phantom.ActionResult(dict(param)))
# Connect
if phantom.is_fail(self._connect(action_result)):
return action_result.get_status()
ip_hostname = param[phantom.APP_JSON_IP_HOSTNAME]
# Validate last_n_days
ret_val, last_n_days = self._validate_integer(action_result, param.get(consts.SPLUNK_JSON_LAST_N_DAYS), consts.SPLUNK_LAST_N_DAYS_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
search_query = 'search host="{0}"{1}'.format(ip_hostname, " earliest=-{0}d".format(last_n_days) if last_n_days else "")
self.debug_print("search_query: {0}".format(search_query))
return self._run_query(search_query, action_result)
def _get_fips_enabled(self):
try:
from phantom_common.install_info import is_fips_enabled
except ImportError:
return False
fips_enabled = is_fips_enabled()
if fips_enabled:
self.debug_print("FIPS is enabled")
else:
self.debug_print("FIPS is not enabled")
return fips_enabled
def _on_poll(self, param): # noqa: C901
action_result = self.add_action_result(phantom.ActionResult(dict(param)))
if phantom.is_fail(self._connect(action_result)):
return action_result.get_status()
config = self.get_config()
search_command = config.get("on_poll_command")
search_string = config.get("on_poll_query")
po = config.get("on_poll_parse_only", False)
include_cim_fields = config.get("include_cim_fields", False)
if not search_string:
self.save_progress("Need to specify Query String to use polling")
return action_result.set_status(phantom.APP_ERROR)
try:
if not search_command:
if (search_string[0] != "|") and (search_string.find("search", 0) != 0):
search_string = "search {}".format(search_string.strip())
search_query = search_string
else:
search_query = "{0} {1}".format(search_command.strip(), search_string.strip())
except Exception:
return action_result.set_status(phantom.APP_ERROR, "Error occurred while parsing the search query")
search_params = {}
if self.is_poll_now():
search_params["max_count"] = param.get("container_count", 100)
else:
search_params["max_count"] = self.max_container
start_time = self._state.get("start_time")
if start_time:
search_params["index_earliest"] = start_time
if int(search_params["max_count"]) <= 0:
self.debug_print(
"The value of 'container_count' parameter must be a positive integer. \
The value provided in the 'container_count' parameter is {}.\
Therefore, 'container_count' parameter will be ignored".format(
int(search_params["max_count"])
)
)
search_params.pop("max_count")
ret_val = self._run_query(search_query, action_result, kwargs_create=search_params, parse_only=po)
if phantom.is_fail(ret_val):
if "Invalid index_earliest" in action_result.get_message():
self.debug_print(
"The value of 'start_time' parameter {} is not a valid epoch time. Re-invoking api without start_time".format(
search_params.get("index_earliest")
)
)
del self._state["start_time"]
else:
self.save_progress(action_result.get_message())
return action_result.set_status(phantom.APP_ERROR)
display = config.get("on_poll_display")
header_set = None
if display:
header_set = [x.strip().lower() for x in display.split(",")]
# Set the most recent event to data[0]
data = list(reversed(action_result.get_data()))
self.save_progress("Finished search")
self.debug_print("Total {} event(s) fetched".format(len(data)))
count = 1
for item in data:
container = {}
cef = {}
if "_serial" in item:
item.pop("_serial")
if header_set:
name_mappings = {}
for k, v in list(item.items()):
if k.lower() in header_set:
# Use this to keep the orignal capitalization from splunk
name_mappings[k.lower()] = k