forked from splunk-soar-connectors/googlethreatintelligence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_threat_intelligence_utils.py
More file actions
1146 lines (937 loc) · 43.2 KB
/
google_threat_intelligence_utils.py
File metadata and controls
1146 lines (937 loc) · 43.2 KB
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: google_threat_intelligence_utils.py
#
# Copyright 2025 Google LLC
#
# 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 base64
import ipaddress
import json
import re
import time
from datetime import datetime, timedelta, timezone
from urllib.parse import urlencode, urlparse, urlunparse
import phantom.app as phantom
import phantom.rules as ph_rules
import requests
from bs4 import BeautifulSoup
from phantom.utils import config as ph_config
import google_threat_intelligence_consts as consts
class RetVal(tuple):
"""Return a tuple of two elements."""
def __new__(cls, val1, val2=None):
"""Create a new tuple object."""
return tuple.__new__(RetVal, (val1, val2))
class GoogleThreatIntelligenceUtils:
"""This class holds all the util methods."""
def __init__(self, connector=None):
self._connector = connector
def _get_error_message_from_exception(self, e):
"""
Extracts the error message and error code from an exception object.
Args:
e (Exception): The exception object to extract the error message and error code from.
Returns:
str: The error message and error code in the format "Error code: {error_code}. Error message: {error_msg}".
If the error code is not present, only the error message is returned.
"""
error_code = None
error_msg = consts.ERROR_MESSAGE_UNAVAILABLE
self._connector.error_print("Error occurred.", e)
try:
if hasattr(e, "args"):
if len(e.args) > 1:
error_code = e.args[0]
error_msg = e.args[1]
elif len(e.args) == 1:
error_msg = e.args[0]
except Exception as e:
self._connector.error_print(f"Error occurred while fetching exception information. Details: {e!s}")
if not error_code:
error_text = f"Error message: {error_msg}"
else:
error_text = f"Error code: {error_code}. Error message: {error_msg}"
return error_text
def _process_empty_response(self, response, action_result):
if response.status_code in consts.EMPTY_RESPONSE_STATUS_CODES:
return RetVal(phantom.APP_SUCCESS, {})
return RetVal(
action_result.set_status(
phantom.APP_ERROR,
f"Empty response and no information in the header, Status Code: {response.status_code}",
),
None,
)
def _process_html_response(self, response, action_result):
"""
Process an HTML response from a request.
Args:
response (requests.Response): The response object from the request.
action_result (ActionResult): The action result object to set the status on.
Returns:
tuple: A tuple containing the status of the processing and the data to return.
"""
# 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:
error_text = "Cannot parse error details"
message = consts.ERROR_GENERAL_MESSAGE.format(status_code, error_text)
message = message.replace("{", "{{").replace("}", "}}")
# Large HTML pages may be returned by the wrong URLs.
# Use default error message in place of large HTML page.
if len(message) > 500:
return RetVal(action_result.set_status(phantom.APP_ERROR, consts.ERROR_HTML_RESPONSE))
return RetVal(action_result.set_status(phantom.APP_ERROR, message))
def _process_json_response(self, r, action_result):
"""
Process a JSON response from a request.
Args:
r (requests.Response): The response object from the request.
action_result (ActionResult): The action result object to set the status on.
Returns:
tuple: A tuple containing the status of the processing and the data to return.
"""
try:
resp_json = r.json()
except Exception as e:
return RetVal(
action_result.set_status(
phantom.APP_ERROR,
f"Unable to parse JSON response. Error: {e!s}",
),
None,
)
# Please specify the status codes here
if 200 <= r.status_code < 399:
return RetVal(phantom.APP_SUCCESS, resp_json)
# Extract error message from JSON response
error_message = resp_json.get("error", {}).get("message", "No error message provided")
if resp_json.get("message"):
error_message = resp_json.get("message", "No error message provided")
message = f"Error from server. Status Code: {r.status_code} Data from server: {error_message}"
return RetVal(action_result.set_status(phantom.APP_ERROR, message))
def _process_response(self, r, action_result):
"""
Processes the response from the server.
Args:
r (requests.response): Response from the server.
action_result (phantom.action_result): Action result to store the results.
Returns:
RetVal: A RetVal of phantom.APP_SUCCESS or phantom.APP_ERROR.
"""
# store the r_text in debug data, it will get dumped in the logs if the action fails
"""
Process a response from a request.
This function takes a response object from a request and an action result object
and processes the response into a RetVal object.
If the response is a JSON response, it is processed using `_process_json_response`.
If the response is an HTML response, it is processed using `_process_html_response`.
If the response is empty, it is processed using `_process_empty_response`.
Otherwise, an error is raised.
Args:
r (requests.Response): The response object from the request.
action_result (ActionResult): The action result object to set the status on.
Returns:
tuple: A tuple containing the status of the processing and the data to return.
"""
if hasattr(action_result, "add_debug_data"):
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})
# 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 phantom 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", "") and r.text:
return self._process_html_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
message = (
f"Can't process response from server. Status Code: {r.status_code} Data from server: {r.text.replace('{', '{{').replace('}', '}}')}"
)
return RetVal(action_result.set_status(phantom.APP_ERROR, message), None)
def make_rest_call(self, endpoint, action_result, method="get", large_file=False, return_response_headers=False, **kwargs):
"""
Make a REST call to the API.
Args:
endpoint (str): The endpoint to make the REST call to.
action_result (ActionResult): The action result object to set the status on.
method (str, optional): The method to use for the REST call. Defaults to "get".
large_file (bool, optional): If True, the endpoint is a full URL. Defaults to False.
Returns:
tuple: A tuple containing the status of the processing and the data to return.
"""
resp_json = None
try:
request_func = getattr(requests, method)
except AttributeError:
return RetVal(
action_result.set_status(phantom.APP_ERROR, f"Invalid method: {method}"),
resp_json,
)
# Create a URL to connect to
if large_file:
url = endpoint
else:
url = f"{consts.BASE_URL.strip('/')}{endpoint}"
kwargs["headers"] = {
**self.get_auth_headers(self._connector.config),
**(kwargs.get("headers") or {}),
}
try:
r = request_func(
url,
timeout=consts.REQUEST_DEFAULT_TIMEOUT,
**kwargs,
)
except Exception as e:
return RetVal(
action_result.set_status(
phantom.APP_ERROR,
f"Error Connecting to server. Details: {e!s}",
),
resp_json,
)
processed_response = self._process_response(r, action_result)
return (processed_response, r.headers) if return_response_headers else processed_response
def _paginator(self, endpoint, action_result, method, limit=None, is_on_poll=False, **kwargs):
"""
Handle paginated API responses by automatically fetching all pages.
This method handles the API's pagination mechanism to retrieve complete result sets
that span multiple pages, up to the specified limit.
Args:
endpoint (str): The API endpoint.
action_result (ActionResult): The result of the action.
method (str): The method of the request (GET, POST, PUT, DELETE, PATCH).
limit (int, optional): The limit of the results. Defaults to 40.
is_on_poll (bool, optional): True if the action is on poll.
Returns:
list: List of results.
"""
results = []
cursor = None
if limit is None and is_on_poll:
limit = 1000
if limit is None:
while True:
updated_endpoint = f"{endpoint}?limit=40" if "?" not in endpoint else f"{endpoint}&limit=40"
if cursor:
updated_endpoint += f"&cursor={cursor}"
ret_val, json_resp = self.make_rest_call(updated_endpoint, action_result, method, **kwargs)
if phantom.is_fail(ret_val):
return ret_val, []
results.extend(json_resp.get("data", []))
cursor = json_resp.get("meta", {}).get("cursor")
if not cursor:
break
else:
limit = int(limit)
remaining = limit
while remaining > 0:
batch_size = min(remaining, 40)
updated_endpoint = f"{endpoint}?limit={batch_size}" if "?" not in endpoint else f"{endpoint}&limit={batch_size}"
if cursor:
updated_endpoint += f"&cursor={cursor}"
ret_val, json_resp = self.make_rest_call(updated_endpoint, action_result, method, **kwargs)
if phantom.is_fail(ret_val):
return ret_val, []
results.extend(json_resp.get("data", []))
cursor = json_resp.get("meta", {}).get("cursor")
remaining -= batch_size
if not cursor:
break
return phantom.APP_SUCCESS, results
def _paginator_dtm(self, endpoint, action_result, method, limit=1000, **kwargs):
"""
Handle paginated API responses by automatically fetching all pages for DTM alert.
This method handles the API's pagination mechanism to retrieve complete result sets
that span multiple pages, up to the specified limit.
Args:
endpoint (str): The API endpoint with the query parameters.
action_result (ActionResult): The result of the action.
method (str): The method of the request (GET, POST, PUT, DELETE, PATCH).
limit (int, optional): The limit of the results. Defaults to 1000.
Returns:
tuple: A tuple of two elements. Success flag and a list of results.
"""
page_size = 25 # Using max page size of 25 since DTM ref parameter is true
limit = int(limit)
remaining = limit
results = []
if limit <= page_size:
self._connector.debug_print(f"The limit for DTM alerts ({limit}) is less than the page size ({page_size}).")
updated_endpoint = f"{endpoint}?size={limit}" if "?" not in endpoint else f"{endpoint}&size={limit}"
ret_val, json_resp = self.make_rest_call(updated_endpoint, action_result, method, **kwargs)
if phantom.is_fail(ret_val):
return ret_val, []
results = json_resp.get("alerts", [])
return phantom.APP_SUCCESS, results
else:
self._connector.debug_print(f"The limit for DTM alerts ({limit}) is greater than the page size ({page_size}).")
next_page_token = None
remaining = limit
while remaining > 0:
if next_page_token:
parsed = urlparse(endpoint)
new_query = urlencode({"page": next_page_token})
updated = parsed._replace(query=new_query)
updated_endpoint = urlunparse(updated)
else:
updated_endpoint = f"{endpoint}?size={page_size}" if "?" not in endpoint else f"{endpoint}&size={page_size}"
(ret_val, json_resp), response_headers = self.make_rest_call(
updated_endpoint, action_result, method, return_response_headers=True, **kwargs
)
if phantom.is_fail(ret_val):
return ret_val, []
alerts_list = json_resp.get("alerts", [])
alert_count = len(alerts_list)
if remaining <= alert_count:
self._connector.debug_print(f"Remaining alerts ({remaining}) are less than or equal to the alert count ({alert_count}).")
results.extend(json_resp.get("alerts")[:remaining])
break
self._connector.debug_print(f"Remaining alerts ({remaining}) are greater than the alert count ({alert_count}).")
results.extend(json_resp.get("alerts"))
remaining -= alert_count
next_page_link = response_headers.get("link")
if not next_page_link:
self._connector.debug_print("No 'next' page link found in response headers.")
break # Break as there is no next page
m = re.search(r"[?&]page=([^&>;]+)", next_page_link)
if not m:
self._connector.debug_print("No page token found in the 'next' page link.")
break # Break as there is no next page
next_page_token = m.group(1)
self._connector.debug_print(f"Remaining alerts to fetch: {remaining}")
return phantom.APP_SUCCESS, results
def get_auth_headers(self, config):
"""
Generate headers with API key from the asset configuration.
Args:
config (dict): Asset configuration.
Returns:
dict: Headers with API key.
"""
headers = {}
if config.get("x-apikey"):
headers["x-apikey"] = config.get("x-apikey")
return headers
@staticmethod
def generate_basic_auth_header(username, password):
"""
Generate a Basic Auth header given a username and password.
Args:
username (str): The username to use for Basic Auth.
password (str): The password to use for Basic Auth.
Returns:
str: The Basic Auth header.
"""
credentials = f"{username}:{password}"
encoded_credentials = base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
return f"Basic {encoded_credentials}"
def generate_json_body(self, body, allow_none, allow_empty, param, default_values):
"""
Generate a JSON body given a body and template values.
Args:
body (dict): The body to generate the JSON from.
allow_none (list): A list of keys that can have a value of None.
allow_empty (dict): A dictionary of keys that can have an empty value.
param (dict): A dictionary of template values.
default_values (dict): A dictionary of default values.
Returns:
dict: The generated JSON body.
"""
def _get_empty_value(_type):
empty_values = {
"string": "",
"boolean": "",
"integer": 0,
"float": 0,
"dict": {},
"list": [],
}
return empty_values.get(_type, "")
def _handle_template_value(key, value, body):
if not isinstance(value, str) or not value.startswith("{{") or not value.endswith("}}"):
body[key] = value
return
value = value.strip("{}")
if value in param:
body[key] = param.get(value)
elif value in default_values:
body[key] = default_values.get(value)
elif value in allow_none:
body[key] = None
elif value in allow_empty:
body[key] = _get_empty_value(allow_empty[value])
def _format_value(input_body, body, path=()):
for key, value in input_body.items():
if isinstance(value, dict):
body[key] = _format_value(value, {}, (*path, key))
else:
_handle_template_value(key, value, body)
return body
output_body = _format_value(body, {})
return output_body
def get_data_type(self, entity):
"""
Determine the data type of the given entity.
Args:
entity (str): The entity to determine the data type for.
Returns:
str: The data type of the entity (e.g. "file", "ip", "domain", "url").
"""
try:
ipaddress.ip_address(entity)
return consts.IP_ADDRESS
except Exception:
domain_regex = re.compile(r"^(?=.{1,253}$)(?!://)([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$")
if phantom.is_url(entity):
return consts.URL
elif bool(domain_regex.match(entity)):
return consts.DOMAIN
else:
return consts.FILE
def _get_id_from_scan_entity(self, action_result, scan_result):
"""
Get the ID from the response.
Args:
- action_result (ActionResult): The result of the action.
- scan_result (dict): The response from the API.
Returns:
- Tuple[str, str]: A tuple containing the status of the action and the ID.
"""
try:
analysis_id = scan_result.get("data", {}).get("id")
if not analysis_id:
return action_result.set_status(phantom.APP_ERROR, f"Analysis ID not found in response: {scan_result}"), None
except Exception:
return (
action_result.set_status(phantom.APP_ERROR, "Error occurred while extracting 'id'"),
None,
)
return phantom.APP_SUCCESS, analysis_id
def _get_file_id_from_analysis(self, action_result, analysis):
"""
Get the File ID from the response.
Args:
- action_result (ActionResult): The result of the action.
- analysis (dict): The response from the API.
Returns:
- Tuple[str, str]: A tuple containing the status of the action and the ID.
"""
try:
file_id = analysis.get("meta", {}).get("file_info", {}).get("sha256")
except Exception:
return (
action_result.set_status(phantom.APP_ERROR, "Error occurred while extracting 'file_id'"),
None,
)
return phantom.APP_SUCCESS, file_id
def _get_url_id_from_analysis(self, action_result, analysis):
"""
Get the URL ID from the response.
Args:
- action_result (ActionResult): The result of the action.
- analysis (dict): The response from the API.
Returns:
- Tuple[str, str]: A tuple containing the status of the action and the ID.
"""
try:
url_id = analysis.get("meta", {}).get("url_info", {}).get("id")
except Exception:
return (
action_result.set_status(phantom.APP_ERROR, "Error occurred while extracting 'url_id'"),
None,
)
return phantom.APP_SUCCESS, url_id
def _poll_analysis_until_complete(self, action_result, entity_type, analysis_id, poll_interval, is_private=False):
"""
Poll the GTI API for analysis results until completion or timeout.
Args:
self: The instance of the class.
action_result (ActionResult): The result of the action.
entity_type (str): The type of entity (e.g. "file", "url").
analysis_id (str): The ID of the analysis.
poll_interval (int): The interval to poll for the analysis.
is_private (bool): To check if it's private anlaysis. Defaults to False
Returns:
Tuple[str, str]: A tuple containing the status of the action and the ID.
"""
attempt = 1
if is_private:
endpoint, method = (
consts.PRIVATE_ANALYSES_ENDPOINT.format(id=analysis_id),
"get",
)
else:
endpoint, method = consts.ANALYSES_ENDPOINT.format(id=analysis_id), "get"
args = {
"endpoint": endpoint,
"action_result": action_result,
"method": method.lower(),
"headers": {},
}
# equal to the number of attempts
poll_attempts = poll_interval
while attempt <= poll_attempts:
self._connector.save_progress(f"Analysing the entity... attempt {attempt} of {poll_attempts}")
ret_val, json_resp = self.make_rest_call(**args)
if phantom.is_fail(ret_val):
return ret_val, None
if json_resp.get("error", {}).get("code") in consts.PASS_ERROR_CODE.values():
return action_result.set_status(phantom.APP_SUCCESS, "Got error"), None
if json_resp.get("data").get("attributes").get("status") == "completed":
# Get id from the analysis
if entity_type == consts.URL:
ret_val, entity_id = self._get_url_id_from_analysis(action_result, json_resp)
elif entity_type == consts.FILE:
ret_val, entity_id = self._get_file_id_from_analysis(action_result, json_resp)
if phantom.is_fail(ret_val):
return action_result.get_status(), None
return phantom.APP_SUCCESS, entity_id
attempt += 1
time.sleep(60)
return (
action_result.set_status(
phantom.APP_ERROR,
"Reached max polling attempts. Try rerunning the action",
),
None,
)
def submit_url_for_analysis(self, action_result, url):
"""
Submit a URL to GTI API for scanning and retrieve its identifier.
Args:
self: The instance of the class.
action_result (ActionResult): The result of the action.
url (str): The URL to retrieve the ID for.
Returns:
Tuple[str, str]: A tuple containing the status of the action and the ID.
"""
endpoint, method = consts.SCAN_URL_ENDPOINT, "post"
args = {
"endpoint": endpoint,
"action_result": action_result,
"method": method.lower(),
"headers": {},
"data": {"url": url},
}
ret_val, json_resp = self.make_rest_call(**args)
if phantom.is_fail(ret_val):
return ret_val, None
# Get analysis id from the scan_result
ret_val, analysis_id = self._get_id_from_scan_entity(action_result, json_resp)
if phantom.is_fail(ret_val):
return action_result.get_status(), None
return self._poll_analysis_until_complete(action_result, consts.URL, analysis_id, consts.RETRY_COUNT)
def get_file_hash_from_input(self, action_result, file_hash, password=None):
"""
Retrieve a file from the Phantom vault and submit it to GTI API to obtain its file hash identifier.
Args:
self: The instance of the class.
action_result (ActionResult): The result of the action.
file_hash (str): The file_hash which could be a vault ID or a file hash.
password (str, optional): The password for the file (if required). Defaults to None.
Returns:
Tuple[str, str]: A tuple containing the status of the action and the ID.
"""
# Check if the file_hash is a known hash type (sha256, md5)
if phantom.is_sha256(file_hash) or phantom.is_md5(file_hash):
return phantom.APP_SUCCESS, file_hash
# If the file_hash might be a SHA1, check if it is a Vault ID
# Vault ID is a SHA1 hash format, ref:https://docs.splunk.com/Documentation/SOAR/current/PlaybookAPI/VaultAPI
if not phantom.is_sha1(file_hash):
return (
action_result.set_status(phantom.APP_ERROR, consts.ERROR_MESSAGE_INVALID_ENTITY),
None,
)
# Attempt to retrieve the file information from the vault
try:
_, _, file_info = ph_rules.vault_info(container_id=self._connector.get_container_id(), vault_id=file_hash)
if not file_info:
# If file_info is empty, the vault ID is not valid, but we treat it as a regular SHA1 file_hash
return phantom.APP_SUCCESS, file_hash
# File info found, meaning the file_hash is a valid Vault ID
# Extract the first item from the file_info list
file_info = next(iter(file_info))
file_path = file_info["path"]
file_name = file_info["name"]
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return (
action_result.set_status(
phantom.APP_ERROR,
f"Unable to retrieve file from vault: {error_message}",
),
None,
)
try:
files = [("file", (file_name, open(file_path, "rb"), "application/octet-stream"))]
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return (
action_result.set_status(
phantom.APP_ERROR,
f"Error occurred while reading file. {error_message}",
),
None,
)
endpoint, method = consts.SCAN_FILE_ENDPOINT, "get"
args = {
"endpoint": endpoint,
"action_result": action_result,
"method": method.lower(),
"headers": {},
}
ret_val, json_resp = self.make_rest_call(**args)
if phantom.is_fail(ret_val):
return action_result.get_status(), None
try:
upload_url = json_resp["data"]
except KeyError:
return (
action_result.set_status(phantom.APP_ERROR, "Couldn't fetch URL for uploading file"),
None,
)
args = {
"endpoint": upload_url,
"action_result": action_result,
"method": "post",
"headers": {},
"data": {"password": password},
"files": files,
}
ret_val, json_resp = self.make_rest_call(large_file=True, **args)
if phantom.is_fail(ret_val):
return action_result.get_status(), None
# Get analysis id from the scan_result
ret_val, analysis_id = self._get_id_from_scan_entity(action_result, json_resp)
if phantom.is_fail(ret_val):
return action_result.get_status(), None
return self._poll_analysis_until_complete(action_result, consts.FILE, analysis_id, consts.RETRY_COUNT)
def convert_unix_to_utc(self, unix_time):
"""
Convert a Unix timestamp to a UTC datetime string in ISO 8601 format.
Args:
self: The instance of the class.
unix_time (int): The Unix timestamp to convert.
Returns:
str: The UTC datetime string in ISO 8601 format.
"""
dt_utc = datetime.fromtimestamp(unix_time, tz=timezone.utc)
# Format the datetime as a string in ISO 8601 format
formatted_timestamp = dt_utc.strftime("%Y-%m-%dT%H:%M:%S")
return formatted_timestamp
def build_last_seen_after(self, days: int = 0, hours: int = 0) -> str:
"""
Returns last_seen_after in formatted UTC time string (e.g., 2025-05-14T11:00:00Z)
that is `days` and `hours` before the current UTC time.
Args:
days (int): Number of days before now
hours (int): Number of hours before now
Returns:
str: last_seen_after formatted string (e.g., '2025-05-14T10:00:00Z')
"""
target_time = datetime.now(timezone.utc) - timedelta(days=days, hours=hours)
value = target_time.strftime("%Y-%m-%dT%H:%M:%SZ")
self._connector.debug_print(f"last_seen_after value {days} days, {hours} hours before the current UTC time: {value}")
return value
def clean_search_string(self, search_string):
"""
Cleans the given search string by removing keyword:value pairs and normalizing spacing.
Args:
search_string (str): The search string to be cleaned.
Returns:
str: The cleaned search string.
Steps:
1. Inserts a space before any known keyword that is stuck to the previous word.
2. Builds a regex pattern to remove keyword:value pairs.
3. Removes all matching pairs from the search string.
4. Normalizes spacing by removing multiple consecutive spaces and trimming leading/trailing spaces.
5. Prints the cleaned search string for debugging purposes.
"""
keywords = ["last_seen_after", "last_seen_before", "first_seen_after"]
# Step 1: Insert space before any known keyword that is stuck to previous word
for keyword in keywords:
search_string = re.sub(r"(?<![\s])(" + re.escape(keyword) + r")\s*:", r" \1:", search_string)
# Step 2: Build regex to remove keyword:value pairs
pattern = r"\b(?:" + "|".join(re.escape(k) for k in keywords) + r")\s*:\s*[^ \n\t]+"
# Remove all matching pairs
cleaned = re.sub(pattern, "", search_string)
# Step 3: Normalize spacing
cleaned = re.sub(r"\s+", " ", cleaned).strip()
self._connector.debug_print(f"Cleaned search string for ASM: {cleaned}")
return cleaned
def _common_message_handler_for_soar(self, response, operation):
"""
Message handler to handle the logs from SOAR APIs
Parameters:
response (object): The response object received from the API call.
operation (str): The operation being performed.
Returns:
dict: The parsed response data.
"""
data = {}
try:
data = json.loads(response.text)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
self._connector.debug_print(f"Failed while parsing the response {error_message}")
return data
if isinstance(data, dict) and data.get("failed", False) and data.get("message"):
self._connector.debug_print("Error occurred while {}: {}".format(operation, data.get("message")))
return data
def get_container_details(self, container_id):
"""
Retrieves the container details from the Phantom server.
Args:
container_id (str): The ID of the container to retrieve.
Returns:
dict: The container details if successful, else an empty dictionary.
"""
url = consts.SPLUNK_SOAR_CONTAINER_ENDPOINT.format(url=self._connector.get_phantom_base_url(), container_id=container_id)
data = {}
try:
r = requests.get(url, verify=ph_config.platform_strict_tls)
except Exception as e:
self._connector.debug_print(f"Unable to get the container deatale for {container_id}", e)
return data
data = self._common_message_handler_for_soar(r, "getting the container details")
return data
def update_container_status(self, container_id, status):
"""
Updates the status of a container in Phantom.
Args:
container_id (str): The ID of the container to update.
status (str): The status to update the container with.
Returns:
None
"""
body = self.get_container_details(container_id)
if body:
body["status"] = status
self._connector.debug_print("Updating the status of container")
url = consts.SPLUNK_SOAR_CONTAINER_ENDPOINT.format(url=self._connector.get_phantom_base_url(), container_id=container_id)
try:
r = requests.post(url, json=body, verify=ph_config.platform_strict_tls)
except Exception as e:
self._connector.debug_print(f"Unable to update the status of container for {container_id}", e)
self._common_message_handler_for_soar(r, "Updating the container status")
def _get_artifact_of_container_id(self, container_id):
"""
Retrieve the artifact associated with container.
Args:
container_id (str): The ID of the container.
Returns:
dict: A dictionary containing the artifact IDs as keys and the corresponding IDs as values.
Returns an empty dictionary if no artifact is found.
"""
url = consts.SPLUNK_SOAR_GET_CONTAINER_ARTIFACT_ENDPOINT.format(url=self._connector.get_phantom_base_url(), container_id=container_id)
artifact_ids = {}
try:
r = requests.get(url, verify=ph_config.platform_strict_tls)
except Exception as e:
self._connector.debug_print("Unable to query for artifact", e)
return artifact_ids
resp_json = self._common_message_handler_for_soar(r, "querying for artifact")
if resp_json.get("count", 0) <= 0:
self._connector.debug_print("No artifact matched")
return artifact_ids
try:
artifact_ids.update({artifact.get("source_data_identifier"): artifact.get("id") for artifact in resp_json.get("data", [])})
self._connector.debug_print(f"Artifact ids updated: {artifact_ids}")
except Exception as e:
self._connector.debug_print("Artifact results are not proper: ", e)
return artifact_ids
return artifact_ids
def _update_artifact(self, artifact_id, artifact):
"""
Update an artifact.
Parameters:
artifact_id (str): The ID of the artifact to be updated.
Returns:
None
"""
url = consts.SPLUNK_SOAR_ARTIFACT_ENDPOINT.format(url=self._connector.get_phantom_base_url(), artifact_id=artifact_id)
self._connector.debug_print(f"Updating artifact with id {artifact_id}")
try:
resp = requests.post(url, json=artifact, verify=ph_config.platform_strict_tls)
except Exception as e:
self._connector.debug_print("Unable to update the artifact", e)
self._common_message_handler_for_soar(resp, "updating artifact")
class Validator:
@staticmethod
def validate_integer(action_result, parameter, key, allow_zero=False, allow_negative=False):
"""
Validate if a given parameter is an integer.
Args:
action_result (ActionResult): The ActionResult object to append
error messages to.
parameter (str): The parameter to validate.
key (str): The key of the parameter to validate.
allow_zero (bool): Whether to allow zero as a valid integer
(default is False).
allow_negative (bool): Whether to allow negative integers as valid
(default is False).
Returns:
Tuple[int, int]: A tuple containing the status of the action and the
validated integer. If the parameter is not a valid integer, the
status will be phantom.APP_ERROR and the second element of the
tuple will be None.
"""
try:
if not float(parameter).is_integer():
return (
action_result.set_status(
phantom.APP_ERROR,
consts.ERROR_INVALID_INT_PARAM.format(key=key),
),
None,
)