-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathCoreIRApiModule.py
4338 lines (3689 loc) · 157 KB
/
CoreIRApiModule.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
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
import urllib3
import copy
import re
from operator import itemgetter
import json
from typing import Tuple, Callable
# Disable insecure warnings
urllib3.disable_warnings()
TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
XSOAR_RESOLVED_STATUS_TO_XDR = {
'Other': 'resolved_other',
'Duplicate': 'resolved_duplicate',
'False Positive': 'resolved_false_positive',
'Resolved': 'resolved_true_positive',
'Security Testing': 'resolved_security_testing',
}
XDR_RESOLVED_STATUS_TO_XSOAR = {
'resolved_known_issue': 'Other',
'resolved_duplicate_incident': 'Duplicate',
'resolved_duplicate': 'Duplicate',
'resolved_false_positive': 'False Positive',
'resolved_true_positive': 'Resolved',
'resolved_security_testing': 'Security Testing',
'resolved_other': 'Other',
'resolved_auto': 'Resolved'
}
ALERT_GENERAL_FIELDS = {
'detection_modules',
'alert_full_description',
'matching_service_rule_id',
'variation_rule_id',
'content_version',
'detector_id',
'mitre_technique_id_and_name',
'silent',
'mitre_technique_ids',
'activity_first_seet_at',
'_type',
'dst_association_strength',
'alert_description',
}
ALERT_EVENT_GENERAL_FIELDS = {
"_time",
"vendor",
"event_timestamp",
"event_type",
"event_id",
"cloud_provider",
"project",
"cloud_provider_event_id",
"cloud_correlation_id",
"operation_name_orig",
"operation_name",
"identity_orig",
"identity_name",
"identity_uuid",
"identity_type",
"identity_sub_type",
"identity_invoked_by_name",
"identity_invoked_by_uuid",
"identity_invoked_by_type",
"identity_invoked_by_sub_type",
"operation_status",
"operation_status_orig",
"operation_status_orig_code",
"operation_status_reason_provided",
"resource_type",
"resource_type_orig",
"resource_sub_type",
"resource_sub_type_orig",
"region",
"zone",
"referenced_resource",
"referenced_resource_name",
"referenced_resources_count",
"user_agent",
"caller_ip",
'caller_ip_geolocation',
"caller_ip_asn",
'caller_project',
'raw_log',
"log_name",
"caller_ip_asn_org",
"event_base_id",
"ingestion_time",
}
ALERT_EVENT_AWS_FIELDS = {
"eventVersion",
"userIdentity",
"eventTime",
"eventSource",
"eventName",
"awsRegion",
"sourceIPAddress",
"userAgent",
"requestID",
"eventID",
"readOnly",
"eventType",
"apiVersion",
"managementEvent",
"recipientAccountId",
"eventCategory",
"errorCode",
"errorMessage",
"resources",
}
ALERT_EVENT_GCP_FIELDS = {
"labels",
"operation",
"protoPayload",
"resource",
"severity",
"timestamp",
}
ALERT_EVENT_AZURE_FIELDS = {
"time",
"resourceId",
"category",
"operationName",
"operationVersion",
"schemaVersion",
"statusCode",
"statusText",
"callerIpAddress",
"correlationId",
"identity",
"level",
"properties",
"uri",
"protocol",
"resourceType",
"tenantId",
}
RBAC_VALIDATIONS_VERSION = '8.6.0'
RBAC_VALIDATIONS_BUILD_NUMBER = '992980'
FORWARD_USER_RUN_RBAC = is_xsiam() and is_demisto_version_ge(version=RBAC_VALIDATIONS_VERSION,
build_number=RBAC_VALIDATIONS_BUILD_NUMBER) and not is_using_engine()
class CoreClient(BaseClient):
def __init__(self, base_url: str, headers: dict, timeout: int = 120, proxy: bool = False, verify: bool = False):
super().__init__(base_url=base_url, headers=headers, proxy=proxy, verify=verify)
self.timeout = timeout
# For Xpanse tenants requiring direct use of the base client HTTP request instead of the _apiCall,
def _http_request(self, method, url_suffix='', full_url=None, headers=None, json_data=None,
params=None, data=None, timeout=None, raise_on_status=False, ok_codes=None,
error_handler=None, with_metrics=False, resp_type='json'):
'''
"""A wrapper for requests lib to send our requests and handle requests and responses better.
:type method: ``str``
:param method: The HTTP method, for example: GET, POST, and so on.
:type url_suffix: ``str``
:param url_suffix: The API endpoint.
:type full_url: ``str``
:param full_url:
Bypasses the use of self._base_url + url_suffix. This is useful if you need to
make a request to an address outside of the scope of the integration
API.
:type headers: ``dict``
:param headers: Headers to send in the request. If None, will use self._headers.
:type params: ``dict``
:param params: URL parameters to specify the query.
:type data: ``dict``
:param data: The data to send in a 'POST' request.
:type raise_on_status ``bool``
:param raise_on_status: Similar meaning to ``raise_on_redirect``:
whether we should raise an exception, or return a response,
if status falls in ``status_forcelist`` range and retries have
been exhausted.
:type timeout: ``float`` or ``tuple``
:param timeout:
The amount of time (in seconds) that a request will wait for a client to
establish a connection to a remote machine before a timeout occurs.
can be only float (Connection Timeout) or a tuple (Connection Timeout, Read Timeout).
'''
if (not FORWARD_USER_RUN_RBAC):
return BaseClient._http_request(self, # we use the standard base_client http_request without overriding it
method=method,
url_suffix=url_suffix,
full_url=full_url,
headers=headers,
json_data=json_data, params=params, data=data,
timeout=timeout,
raise_on_status=raise_on_status,
ok_codes=ok_codes,
error_handler=error_handler,
with_metrics=with_metrics,
resp_type=resp_type)
headers = headers if headers else self._headers
data = json.dumps(json_data) if json_data else data
address = full_url if full_url else urljoin(self._base_url, url_suffix)
response = demisto._apiCall(
method=method,
path=address,
data=data,
headers=headers,
timeout=timeout
)
if ok_codes and response.get('status') not in ok_codes:
self._handle_error(error_handler, response, with_metrics)
try:
return json.loads(response['data'])
except json.JSONDecodeError:
demisto.debug(f"Converting data to json was failed. Return it as is. The data's type is {type(response['data'])}")
return response['data']
def get_incidents(self, incident_id_list=None, lte_modification_time=None, gte_modification_time=None,
lte_creation_time=None, gte_creation_time=None, status=None, starred=None,
starred_incidents_fetch_window=None, sort_by_modification_time=None, sort_by_creation_time=None,
page_number=0, limit=100, gte_creation_time_milliseconds=0):
"""
Filters and returns incidents
:param incident_id_list: List of incident ids - must be list
:param lte_modification_time: string of time format "2019-12-31T23:59:00"
:param gte_modification_time: string of time format "2019-12-31T23:59:00"
:param lte_creation_time: string of time format "2019-12-31T23:59:00"
:param gte_creation_time: string of time format "2019-12-31T23:59:00"
:param starred_incidents_fetch_window: string of time format "2019-12-31T23:59:00"
:param starred: True if the incident is starred, else False
:param status: string of status
:param sort_by_modification_time: optional - enum (asc,desc)
:param sort_by_creation_time: optional - enum (asc,desc)
:param page_number: page number
:param limit: maximum number of incidents to return per page
:param gte_creation_time_milliseconds: greater than time in milliseconds
:return:
"""
search_from = page_number * limit
search_to = search_from + limit
request_data = {
'search_from': search_from,
'search_to': search_to,
}
if sort_by_creation_time and sort_by_modification_time:
raise ValueError('Should be provide either sort_by_creation_time or '
'sort_by_modification_time. Can\'t provide both')
if sort_by_creation_time:
request_data['sort'] = {
'field': 'creation_time',
'keyword': sort_by_creation_time
}
elif sort_by_modification_time:
request_data['sort'] = {
'field': 'modification_time',
'keyword': sort_by_modification_time
}
filters = []
if incident_id_list is not None and len(incident_id_list) > 0:
filters.append({
'field': 'incident_id_list',
'operator': 'in',
'value': incident_id_list
})
if status:
filters.append({
'field': 'status',
'operator': 'eq',
'value': status
})
if starred and starred_incidents_fetch_window and demisto.command() == 'fetch-incidents':
filters.append({
'field': 'starred',
'operator': 'eq',
'value': True
})
filters.append({
'field': 'creation_time',
'operator': 'gte',
'value': starred_incidents_fetch_window
})
if len(filters) > 0:
request_data['filters'] = filters
incidents = self.handle_fetch_starred_incidents(limit, page_number, request_data)
return incidents
if starred is not None and demisto.command() != 'fetch-incidents':
filters.append({
'field': 'starred',
'operator': 'eq',
'value': starred
})
if lte_creation_time:
filters.append({
'field': 'creation_time',
'operator': 'lte',
'value': date_to_timestamp(lte_creation_time, TIME_FORMAT)
})
if gte_creation_time:
filters.append({
'field': 'creation_time',
'operator': 'gte',
'value': date_to_timestamp(gte_creation_time, TIME_FORMAT)
})
elif starred and starred_incidents_fetch_window and demisto.command() != 'fetch-incidents':
# backwards compatibility of starred_incidents_fetch_window
filters.append({
'field': 'creation_time',
'operator': 'gte',
'value': starred_incidents_fetch_window
})
if lte_modification_time:
filters.append({
'field': 'modification_time',
'operator': 'lte',
'value': date_to_timestamp(lte_modification_time, TIME_FORMAT)
})
if gte_modification_time:
filters.append({
'field': 'modification_time',
'operator': 'gte',
'value': date_to_timestamp(gte_modification_time, TIME_FORMAT)
})
if gte_creation_time_milliseconds > 0:
filters.append({
'field': 'creation_time',
'operator': 'gte',
'value': gte_creation_time_milliseconds
})
if len(filters) > 0:
request_data['filters'] = filters
res = self._http_request(
method='POST',
url_suffix='/incidents/get_incidents/',
json_data={'request_data': request_data},
headers=self._headers,
timeout=self.timeout
)
incidents = res.get('reply', {}).get('incidents', [])
return incidents
def handle_fetch_starred_incidents(self, limit: int, page_number: int, request_data: Dict[Any, Any]) -> List[Any]:
"""Called from get_incidents if the command is fetch-incidents. Implement in child classes."""
return []
def get_endpoints(self,
endpoint_id_list=None,
dist_name=None,
ip_list=None,
public_ip_list=None,
group_name=None,
platform=None,
alias_name=None,
isolate=None,
hostname=None,
page_number=0,
limit=30,
first_seen_gte=None,
first_seen_lte=None,
last_seen_gte=None,
last_seen_lte=None,
sort_by_first_seen=None,
sort_by_last_seen=None,
status=None,
username=None
):
search_from = page_number * limit
search_to = search_from + limit
request_data = {
'search_from': search_from,
'search_to': search_to,
}
filters = create_request_filters(
status=status, username=username, endpoint_id_list=endpoint_id_list, dist_name=dist_name,
ip_list=ip_list, group_name=group_name, platform=platform, alias_name=alias_name, isolate=isolate,
hostname=hostname, first_seen_gte=first_seen_gte, first_seen_lte=first_seen_lte,
last_seen_gte=last_seen_gte, last_seen_lte=last_seen_lte, public_ip_list=public_ip_list
)
if search_from:
request_data['search_from'] = search_from
if search_to:
request_data['search_to'] = search_to
if sort_by_first_seen:
request_data['sort'] = {
'field': 'first_seen',
'keyword': sort_by_first_seen
}
elif sort_by_last_seen:
request_data['sort'] = {
'field': 'last_seen',
'keyword': sort_by_last_seen
}
request_data['filters'] = filters
response = self._http_request(
method='POST',
url_suffix='/endpoints/get_endpoint/',
json_data={'request_data': request_data},
timeout=self.timeout
)
endpoints = response.get('reply', {}).get('endpoints', [])
return endpoints
def set_endpoints_alias(self, filters: list[dict[str, str]], new_alias_name: str | None) -> dict: # pragma: no cover
"""
This func is used to set the alias name of an endpoint.
args:
filters: list of filters to get the endpoints
new_alias_name: the new alias name to set
returns: dict of the response(True if success else error message)
"""
request_data = {'filters': filters, 'alias': new_alias_name}
return self._http_request(
method='POST',
url_suffix='/endpoints/update_agent_name/',
json_data={'request_data': request_data},
timeout=self.timeout,
)
def isolate_endpoint(self, endpoint_id, incident_id=None):
request_data = {
'endpoint_id': endpoint_id,
}
if incident_id:
request_data['incident_id'] = incident_id
reply = self._http_request(
method='POST',
url_suffix='/endpoints/isolate',
json_data={'request_data': request_data},
timeout=self.timeout
)
return reply.get('reply')
def unisolate_endpoint(self, endpoint_id, incident_id=None):
request_data = {
'endpoint_id': endpoint_id,
}
if incident_id:
request_data['incident_id'] = incident_id
reply = self._http_request(
method='POST',
url_suffix='/endpoints/unisolate',
json_data={'request_data': request_data},
timeout=self.timeout
)
return reply.get('reply')
def insert_alerts(self, alerts):
self._http_request(
method='POST',
url_suffix='/alerts/insert_parsed_alerts/',
json_data={
'request_data': {
'alerts': alerts
}
},
timeout=self.timeout
)
def insert_cef_alerts(self, alerts):
self._http_request(
method='POST',
url_suffix='/alerts/insert_cef_alerts/',
json_data={
'request_data': {
'alerts': alerts
}
},
timeout=self.timeout
)
def get_distribution_url(self, distribution_id, package_type):
reply = self._http_request(
method='POST',
url_suffix='/distributions/get_dist_url/',
json_data={
'request_data': {
'distribution_id': distribution_id,
'package_type': package_type
}
},
timeout=self.timeout
)
return reply.get('reply').get('distribution_url')
def get_distribution_status(self, distribution_id):
reply = self._http_request(
method='POST',
url_suffix='/distributions/get_status/',
json_data={
'request_data': {
'distribution_id': distribution_id
}
},
timeout=self.timeout
)
return reply.get('reply').get('status')
def get_distribution_versions(self):
reply = self._http_request(
method='POST',
url_suffix='/distributions/get_versions/',
json_data={},
timeout=self.timeout
)
return reply.get('reply')
def create_distribution(self, name, platform, package_type, agent_version, description):
request_data = {}
if package_type == 'standalone':
request_data = {
'name': name,
'platform': platform,
'package_type': package_type,
'agent_version': agent_version,
'description': description,
}
elif package_type == 'upgrade':
request_data = {
'name': name,
'package_type': package_type,
'description': description,
}
if platform == 'windows':
request_data['windows_version'] = agent_version
elif platform == 'linux':
request_data['linux_version'] = agent_version
elif platform == 'macos':
request_data['macos_version'] = agent_version
reply = self._http_request(
method='POST',
url_suffix='/distributions/create/',
json_data={
'request_data': request_data
},
timeout=self.timeout
)
return reply.get('reply').get('distribution_id')
def audit_management_logs(self, email, result, _type, sub_type, search_from, search_to, timestamp_gte,
timestamp_lte, sort_by, sort_order):
request_data: Dict[str, Any] = {}
filters = []
if email:
filters.append({
'field': 'email',
'operator': 'in',
'value': email
})
if result:
filters.append({
'field': 'result',
'operator': 'in',
'value': result
})
if _type:
filters.append({
'field': 'type',
'operator': 'in',
'value': _type
})
if sub_type:
filters.append({
'field': 'sub_type',
'operator': 'in',
'value': sub_type
})
if timestamp_gte:
filters.append({
'field': 'timestamp',
'operator': 'gte',
'value': timestamp_gte
})
if timestamp_lte:
filters.append({
'field': 'timestamp',
'operator': 'lte',
'value': timestamp_lte
})
if filters:
request_data['filters'] = filters
if search_from > 0:
request_data['search_from'] = search_from
if search_to:
request_data['search_to'] = search_to
if sort_by:
request_data['sort'] = {
'field': sort_by,
'keyword': sort_order
}
reply = self._http_request(
method='POST',
url_suffix='/audits/management_logs/',
json_data={'request_data': request_data},
timeout=self.timeout
)
return reply.get('reply').get('data', [])
def get_audit_agent_reports(self, endpoint_ids, endpoint_names, result, _type, sub_type, search_from, search_to,
timestamp_gte, timestamp_lte, sort_by, sort_order):
request_data: Dict[str, Any] = {}
filters = []
if endpoint_ids:
filters.append({
'field': 'endpoint_id',
'operator': 'in',
'value': endpoint_ids
})
if endpoint_names:
filters.append({
'field': 'endpoint_name',
'operator': 'in',
'value': endpoint_names
})
if result:
filters.append({
'field': 'result',
'operator': 'in',
'value': result
})
if _type:
filters.append({
'field': 'type',
'operator': 'in',
'value': _type
})
if sub_type:
filters.append({
'field': 'sub_type',
'operator': 'in',
'value': sub_type
})
if timestamp_gte:
filters.append({
'field': 'timestamp',
'operator': 'gte',
'value': timestamp_gte
})
if timestamp_lte:
filters.append({
'field': 'timestamp',
'operator': 'lte',
'value': timestamp_lte
})
if filters:
request_data['filters'] = filters
if search_from > 0:
request_data['search_from'] = search_from
if search_to:
request_data['search_to'] = search_to
if sort_by:
request_data['sort'] = {
'field': sort_by,
'keyword': sort_order
}
reply = self._http_request(
method='POST',
url_suffix='/audits/agents_reports/',
json_data={'request_data': request_data},
timeout=self.timeout
)
return reply.get('reply').get('data', [])
def blocklist_files(self, hash_list, comment=None, incident_id=None, detailed_response=False):
request_data: Dict[str, Any] = {"hash_list": hash_list}
if comment:
request_data["comment"] = comment
if incident_id:
request_data['incident_id'] = incident_id
if detailed_response:
request_data['detailed_response'] = detailed_response
self._headers['content-type'] = 'application/json'
reply = self._http_request(
method='POST',
url_suffix='/hash_exceptions/blocklist/',
json_data={'request_data': request_data},
ok_codes=(200, 201, 500),
timeout=self.timeout
)
return reply.get('reply')
def remove_blocklist_files(self, hash_list, comment=None, incident_id=None):
request_data: Dict[str, Any] = {"hash_list": hash_list}
if comment:
request_data["comment"] = comment
if incident_id:
request_data['incident_id'] = incident_id
self._headers['content-type'] = 'application/json'
reply = self._http_request(
method='POST',
url_suffix='/hash_exceptions/blocklist/remove/',
json_data={'request_data': request_data},
ok_codes=(200, 201, 500),
timeout=self.timeout
)
res = reply.get('reply')
if isinstance(res, dict) and res.get('err_code') == 500:
raise DemistoException(f"{res.get('err_msg')}\nThe requested hash might not be in the blocklist.")
return res
def allowlist_files(self, hash_list, comment=None, incident_id=None, detailed_response=False):
request_data: Dict[str, Any] = {"hash_list": hash_list}
if comment:
request_data["comment"] = comment
if incident_id:
request_data['incident_id'] = incident_id
if detailed_response:
request_data['detailed_response'] = detailed_response
self._headers['content-type'] = 'application/json'
reply = self._http_request(
method='POST',
url_suffix='/hash_exceptions/allowlist/',
json_data={'request_data': request_data},
ok_codes=(201, 200),
timeout=self.timeout
)
return reply.get('reply')
def remove_allowlist_files(self, hash_list, comment=None, incident_id=None):
request_data: Dict[str, Any] = {"hash_list": hash_list}
if comment:
request_data["comment"] = comment
if incident_id:
request_data['incident_id'] = incident_id
self._headers['content-type'] = 'application/json'
reply = self._http_request(
method='POST',
url_suffix='/hash_exceptions/allowlist/remove/',
json_data={'request_data': request_data},
timeout=self.timeout
)
return reply.get('reply')
def quarantine_files(self, endpoint_id_list, file_path, file_hash, incident_id):
request_data: Dict[str, Any] = {}
filters = []
if endpoint_id_list:
filters.append({
'field': 'endpoint_id_list',
'operator': 'in',
'value': endpoint_id_list
})
if filters:
request_data['filters'] = filters
request_data['file_path'] = file_path
request_data['file_hash'] = file_hash
if incident_id:
request_data['incident_id'] = incident_id
self._headers['content-type'] = 'application/json'
reply = self._http_request(
method='POST',
url_suffix='/endpoints/quarantine/',
json_data={'request_data': request_data},
ok_codes=(200, 201),
timeout=self.timeout
)
return reply.get('reply')
def restore_file(self, file_hash, endpoint_id=None, incident_id=None):
request_data: Dict[str, Any] = {'file_hash': file_hash}
if incident_id:
request_data['incident_id'] = incident_id
if endpoint_id:
request_data['endpoint_id'] = endpoint_id
self._headers['content-type'] = 'application/json'
reply = self._http_request(
method='POST',
url_suffix='/endpoints/restore/',
json_data={'request_data': request_data},
ok_codes=(200, 201),
timeout=self.timeout
)
return reply.get('reply')
def endpoint_scan(self, url_suffix, endpoint_id_list=None, dist_name=None, gte_first_seen=None, gte_last_seen=None,
lte_first_seen=None,
lte_last_seen=None, ip_list=None, group_name=None, platform=None, alias=None, isolate=None,
hostname: list = None, incident_id=None):
request_data: Dict[str, Any] = {}
filters = []
if endpoint_id_list:
filters.append({
'field': 'endpoint_id_list',
'operator': 'in',
'value': endpoint_id_list
})
if dist_name:
filters.append({
'field': 'dist_name',
'operator': 'in',
'value': dist_name
})
if ip_list:
filters.append({
'field': 'ip_list',
'operator': 'in',
'value': ip_list
})
if group_name:
filters.append({
'field': 'group_name',
'operator': 'in',
'value': group_name
})
if platform:
filters.append({
'field': 'platform',
'operator': 'in',
'value': platform
})
if alias:
filters.append({
'field': 'alias',
'operator': 'in',
'value': alias
})
if isolate:
filters.append({
'field': 'isolate',
'operator': 'in',
'value': [isolate]
})
if hostname:
filters.append({
'field': 'hostname',
'operator': 'in',
'value': hostname
})
if gte_first_seen:
filters.append({
'field': 'first_seen',
'operator': 'gte',
'value': gte_first_seen
})
if lte_first_seen:
filters.append({
'field': 'first_seen',
'operator': 'lte',
'value': lte_first_seen
})
if gte_last_seen:
filters.append({
'field': 'last_seen',
'operator': 'gte',
'value': gte_last_seen
})
if lte_last_seen:
filters.append({
'field': 'last_seen',
'operator': 'lte',
'value': lte_last_seen
})
if filters:
request_data['filters'] = filters
else:
request_data['filters'] = 'all'
if incident_id:
request_data['incident_id'] = incident_id
self._headers['content-type'] = 'application/json'
reply = self._http_request(
method='POST',
url_suffix=url_suffix,
json_data={'request_data': request_data},
ok_codes=(200, 201),
timeout=self.timeout
)
return reply.get('reply')
def get_quarantine_status(self, file_path, file_hash, endpoint_id):
request_data: Dict[str, Any] = {'files': [{
'endpoint_id': endpoint_id,
'file_path': file_path,
'file_hash': file_hash
}]}
self._headers['content-type'] = 'application/json'
reply = self._http_request(
method='POST',
url_suffix='/quarantine/status/',
json_data={'request_data': request_data},
timeout=self.timeout
)
reply_content = reply.get('reply')
if isinstance(reply_content, list):
return reply_content[0]
else:
raise TypeError(f'got unexpected response from api: {reply_content}\n')
def delete_endpoints(self, endpoint_ids: list):
request_data: Dict[str, Any] = {
'filters': [
{
'field': 'endpoint_id_list',
'operator': 'in',
'value': endpoint_ids
}
]
}
self._http_request(
method='POST',
url_suffix='/endpoints/delete/',
json_data={'request_data': request_data},
timeout=self.timeout
)
def get_policy(self, endpoint_id) -> Dict[str, Any]:
request_data: Dict[str, Any] = {
'endpoint_id': endpoint_id
}
reply = self._http_request(
method='POST',
url_suffix='/endpoints/get_policy/',