-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
CarbonBlackResponseV2.py
979 lines (812 loc) · 44.5 KB
/
CarbonBlackResponseV2.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
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
import struct
import dateparser
import urllib3
from CommonServerUserPython import * # noqa
from typing import Callable, Dict, List, Any, Union, Tuple
# Disable insecure warnings
urllib3.disable_warnings()
''' CONSTANTS '''
INTEGRATION_NAME = 'Carbon Black EDR'
DATE_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
''' PARSING PROCESS EVENT COMPLEX FIELDS CLASS'''
class ProcessEventDetail:
"""
This class representing the Process Event Details as found here:
https://developer.carbonblack.com/reference/enterprise-response/6.3/rest-api/#process-event-details
Each sub-class representing a different piped-versioned field, and support the format method.
"""
def __init__(self, piped_version: Union[str, list], fields):
self.fields = []
if not isinstance(piped_version, list):
piped_version = [piped_version]
for entry in piped_version:
data = entry.split('|')
# zip works when number of values is not equal, which can result in incorrect data.
if len(data) != len(fields):
demisto.debug(f'{INTEGRATION_NAME} - Missing details. Ignoring entry: {entry}.')
self.fields.append(dict(zip(fields, data)))
def format(self):
return self.fields
class filemod_complete(ProcessEventDetail):
FIELDS = ['operation_type', 'event_time', 'file_path', 'md5_after_last_write',
'file_type', 'flagged_as_potential_tamper_attempt']
OPERATION_TYPE = {'1': 'Created the file',
'2': 'First wrote to the file',
'4': 'Deleted the file',
'8': 'Last wrote to the file'}
FILE_TYPE = {'1': 'PE',
'2': 'Elf',
'3': 'UniversalBin',
'8': 'EICAR',
'16': 'OfficeLegacy',
'17': 'OfficeOpenXml',
'48': 'Pdf',
'64': 'ArchivePkzip',
'65': 'ArchiveLzh',
'66': 'ArchiveLzw',
'67': 'ArchiveRar',
'68': 'ArchiveTar',
'69': 'Archive7zip'}
def __init__(self, piped_version):
super().__init__(piped_version, self.FIELDS)
def format(self):
for entry in self.fields:
entry['operation_type'] = self.OPERATION_TYPE.get(entry.get('operation_type', ''), '')
entry['file_type'] = self.FILE_TYPE.get(entry.get('file_type', ''), '')
return self.fields
class modload_complete(ProcessEventDetail):
FIELDS = ['event_time', 'loaded_module_md5', 'loaded_module_full_path']
def __init__(self, piped_version):
super().__init__(piped_version, self.FIELDS)
def format(self):
return self.fields
class regmod_complete(ProcessEventDetail):
FIELDS = ['operation_type', 'event_time', 'registry_key_path']
OPERATION_TYPE = {'1': 'Created the file',
'2': 'First wrote to the file',
'4': 'Deleted the file',
'8': 'Last wrote to the file'}
def __init__(self, piped_version):
super().__init__(piped_version, self.FIELDS)
def format(self):
for entry in self.fields:
entry['operation_type'] = self.OPERATION_TYPE.get(entry.get('operation_type', ''), '')
return self.fields
class netconn_complete(ProcessEventDetail):
"""
For netconn_complete, the v2 API and newer return an array of JSON objects instead of piped-versioned fields.
https://developer.carbonblack.com/reference/enterprise-response/5.1/rest-api/#netconn_complete
"""
def __init__(self, fields):
self.fields = fields
def format(self):
for entry in self.fields:
for ipfield in ("remote_ip", "local_ip"):
if isinstance(entry[ipfield], int):
entry[ipfield] = socket.inet_ntoa(struct.pack('>i', entry[ipfield]))
return self.fields
class crossproc_complete(ProcessEventDetail):
FIELDS = ['cross-process_access_type', 'event_time', 'targeted_process_unique_id',
'targeted_process_md5', 'targeted_process_path', 'ProcessOpen_sub-type',
'requested_access_priviledges', 'flagged_as_potential_tamper_attempt']
SUB_TYPES = {'1': 'handle open to process', '2': 'handle open to thread in process'}
def __init__(self, piped_version):
super().__init__(piped_version, self.FIELDS)
def format(self):
for entry in self.fields:
entry['ProcessOpen_sub-type'] = self.SUB_TYPES.get(entry.get('ProcessOpen_sub-type', ''), '')
return self.fields
''' CLIENT CLASS '''
class Client(BaseClient):
def __init__(self, base_url: str, apitoken: str, use_ssl: bool, use_proxy: bool):
headers = {'X-Auth-Token': apitoken, 'Accept': 'application/json', 'Content-Type': 'application/json'}
super().__init__(base_url, headers=headers, verify=use_ssl, proxy=use_proxy)
def http_request(self, url: str, method: str, params: dict = None, json_data: dict = None,
ok_codes: tuple = (200, 204), resp_type: str = 'json') -> dict:
"""
initiates a http request to openphish
"""
data = self._http_request(
method=method,
ok_codes=ok_codes,
url_suffix=url,
params=params,
resp_type=resp_type,
return_empty_response=True,
json_data=json_data,
timeout=30,
)
return data
def get_sensors(self, id: str = None, hostname: str = None, ipaddr: str = None, # noqa: F841
groupid: str = None, inactive_filter_days: str = None, # noqa: F841
limit: Union[int, str] = None) -> Tuple[int, List[dict]]:
url = f'/v1/sensor/{id}' if id else '/v1/sensor'
query_params = assign_params(
ip=ipaddr,
hostname=hostname,
groupid=groupid,
inactive_filter_days=inactive_filter_days
)
res = self.http_request(url=url, method='GET', params=query_params, ok_codes=(200, 204))
# When querying specific sensor without filters, the api returns dictionary instead of list.
return len(res), res[:arg_to_number(limit, 'limit')] if isinstance(res, list) else [res]
def get_alerts(self, status: str = None, username: str = None, feedname: str = None,
hostname: str = None, report: str = None, sort: str = None, query: str = None,
facet: str = None, limit: Union[str, int] = None, start: str = None,
allow_empty_params: bool = False) -> dict:
query_params = assign_params(
status=status,
username=username,
feedname=feedname,
hostname=hostname,
report=report,
query=query
)
query_string = _create_query_string(query_params, allow_empty_params=allow_empty_params)
params = assign_params(q=query_string,
rows=arg_to_number(limit, 'limit'),
start=start,
sort=sort,
facet=facet,
)
return self.http_request(url='/v2/alert', method='GET', params=params)
def get_binaries(self, md5: str = None, product_name: str = None, signed: str = None, # noqa: F841
group: str = None, hostname: str = None, digsig_publisher: str = None, # noqa: F841
company_name: str = None, sort: str = None,
observed_filename: str = None, query: str = None, facet: str = None,
limit: str = None, start: str = None) -> dict:
query_params = assign_params(
md5=md5,
product_name=product_name,
signed=signed,
group=group,
hostname=hostname,
digsig_publisher=digsig_publisher,
company_name=company_name,
observed_filename=observed_filename,
query=query
)
query_string = _create_query_string(query_params)
params = assign_params(q=query_string,
rows=arg_to_number(limit, 'limit'),
start=start,
sort=sort,
facet=facet,
)
return self.http_request(url='/v1/binary', method='GET', params=params)
def get_processes(self, process_name: str = None, group: str = None, hostname: str = None,
parent_name: str = None, process_path: str = None, md5: str = None,
query: str = None, group_by: str = None, sort: str = None, facet: str = None,
facet_field: str = None, limit: str = None, start: str = None, allow_empty: bool = False):
query_fields = ['process_name', 'group', 'hostname', 'parent_name', 'process_path', 'md5', 'query']
local_params = locals()
query_params = assign_params(
process_name=process_name,
parent_name=parent_name,
process_path=process_path,
group=group,
hostname=hostname,
md5=md5,
query=query
)
query_string = _create_query_string(query_params, allow_empty)
params = assign_params(q=query_string,
rows=arg_to_number(limit, 'limit'),
start=start,
sort=sort,
facet=facet,
)
if facet_field:
params['facet.field'] = facet_field
if group_by:
params['cb.group'] = group_by
return self.http_request(url='/v1/process', method='GET', params=params)
def get_formatted_ProcessEventDetail(self, process_json: dict):
complex_fields = {'filemod_complete': filemod_complete, 'modload_complete': modload_complete,
'regmod_complete': regmod_complete, 'crossproc_complete': crossproc_complete,
'netconn_complete': netconn_complete}
formatted_json = {}
for field in process_json:
if field in complex_fields:
# creating the relevant field object and formatting it.
field_object: ProcessEventDetail = complex_fields[field](process_json.get(field))
formatted_json[field] = field_object.format()
else:
formatted_json[field] = process_json.get(field)
return formatted_json
''' HELPER FUNCTIONS '''
def _create_query_string(params: dict, allow_empty_params: bool = False) -> str:
"""
Creating a cb query from params according to https://developer.carbonblack.com/resources/query_overview.pdf.
if 'query' in params, it overrides the other params.
allow_empty_params is used for testing and not production as it would overload the context.
"""
# If user provided both params and query, it means he is not experienced, and might expect different results,
# therefore we decided to prohibit the use of both in search commands.
if 'query' in params and len(params) > 1:
raise DemistoException(f'{INTEGRATION_NAME} - Searching with both query and other filters is not allowed. '
f'Please provide either a search query or one of the possible filters.')
elif 'query' in params:
return params['query']
current_query = [f"{query_field}:{params[query_field]}" for query_field in params]
current_query = ' AND '.join(current_query)
if not current_query and not allow_empty_params:
raise DemistoException(f'{INTEGRATION_NAME} - Search without any filter is not permitted.')
return current_query
def _add_to_current_query(current_query: str = '', params: dict = None) -> str:
new_query = ''
if not params:
return current_query
if current_query:
new_query += f'({current_query}) AND '
current_query_params = [f"{query_field}:{params[query_field]}" for query_field in params]
new_query += ' AND '.join(current_query_params)
return new_query
def _get_sensor_isolation_change_body(client: Client, sensor_id: str, new_isolation: bool) -> dict:
sensor_data = client.get_sensors(sensor_id)[1][0] # returns (length, [sensor_data])
new_sensor_data = {
'network_isolation_enabled': new_isolation,
'group_id': sensor_data.get('group_id')
}
return new_sensor_data
def _parse_field(raw_field: str, sep: str = ',', index_after_split: int = 0, chars_to_remove: str = '') -> str:
'''
This function allows getting a specific complex sub-string. "example,example2|" -> 'example2'
'''
if not raw_field:
demisto.debug(f'{INTEGRATION_NAME} - Got empty raw field to parse.')
return ''
try:
new_field = raw_field.split(sep)[index_after_split]
except IndexError:
demisto.error(f'{INTEGRATION_NAME} - raw: {raw_field}, split by {sep} has no index {index_after_split}')
return ''
chars_to_remove = set(chars_to_remove)
for char in chars_to_remove:
new_field = new_field.replace(char, '')
return new_field
def _get_isolation_status_field(isolation_activated: bool, is_isolated: bool) -> str:
# Logic for isolation can be found in:
# https://developer.carbonblack.com/reference/enterprise-response/6.3/rest-api/#sensorsendpoints
if isolation_activated:
sensor_isolation_status = 'Yes' if is_isolated else 'Pending isolation'
else:
sensor_isolation_status = 'Pending unisolation' if is_isolated else 'No'
return sensor_isolation_status
''' COMMAND FUNCTIONS '''
def unquarantine_device_command(client: Client, sensor_id: str) -> CommandResults:
url = f'/v1/sensor/{sensor_id}'
res = client.http_request(url=url, method='PUT',
json_data=_get_sensor_isolation_change_body(client, sensor_id, False))
if not res:
raise Exception(f'{INTEGRATION_NAME} - could not un-isolate sensor {sensor_id}')
return CommandResults(readable_output='Sensor was un-isolated successfully.')
def quarantine_device_command(client: Client, sensor_id: str) -> CommandResults:
url = f'/v1/sensor/{sensor_id}'
res = client.http_request(url=url, method='PUT',
json_data=_get_sensor_isolation_change_body(client, sensor_id, True))
if not res:
raise Exception(f'{INTEGRATION_NAME} - could not isolate sensor {sensor_id}')
return CommandResults(readable_output='Sensor was isolated successfully.')
def sensors_list_command(client: Client, id: str = None, hostname: str = None, ip: str = None,
group_id: str = None, inactive_filter_days: str = None, limit: int = None) -> CommandResults:
try:
total_num_of_sensors, res = client.get_sensors(id, hostname, ip, group_id, inactive_filter_days, limit)
human_readable_data = []
for sensor_data in res:
human_readable_data.append({
'Computer Name': sensor_data.get('computer_name'),
'Status': sensor_data.get('status'),
'OS Version': sensor_data.get('os_type'),
'Node Id': sensor_data.get('node_id'),
'Sensor Version': sensor_data.get('build_version_string'),
'Sensor Id': sensor_data.get('id'),
'IP Address/MAC Info': _parse_field(sensor_data.get('network_adapters', ''), index_after_split=1,
chars_to_remove='|'),
'Group ID': sensor_data.get('group_id'),
'Power State': sensor_data.get('power_state'),
'Health Score': sensor_data.get('sensor_health_status'),
'Is Isolating': sensor_data.get('is_isolating')
})
md = tableToMarkdown(f'{INTEGRATION_NAME} - Sensors', human_readable_data, removeNull=True, headers=[
'Sensor Id', 'Computer Name', 'Status', 'Power State', 'Group ID', 'OS Version', 'Health Score',
'Is Isolating', 'Node Id', 'Sensor Version', 'IP Address/MAC Info'])
md += f"\nShowing {len(res)} out of {total_num_of_sensors} results."
return CommandResults(outputs=res, outputs_prefix='CarbonBlackEDR.Sensor', outputs_key_field='id',
readable_output=md, raw_response=res)
except DemistoException as e:
if '404' in e.message:
raise Exception(f'{INTEGRATION_NAME} - The sensor {id} could not be found. '
f'Please try using a different sensor.')
else:
raise Exception(f'{INTEGRATION_NAME} - Error connecting to API. Error: {e.message}')
def watchlist_delete_command(client: Client, id: str) -> CommandResults:
res = client.http_request(url=f'/v1/watchlist/{id}', method='DELETE')
# res contains whether the task successful.
return CommandResults(readable_output=res.get('result'))
def watchlist_update_command(client: Client, id: str, search_query: str, description: str,
enabled: bool = None) -> CommandResults:
params = assign_params(enabled=enabled, search_query=search_query, description=description)
res = client.http_request(url=f'/v1/watchlist/{id}', method='PUT', json_data=params)
# res contains whether the task successful.
return CommandResults(readable_output=res.get('result'))
def watchlist_update_action_command(client: Client, id: str, action_type: str,
enabled: str) -> CommandResults:
enabled_bool = argToBoolean(enabled)
params = assign_params(enabled=enabled_bool)
res = client.http_request(url=f'/v1/watchlist/{id}/action_type/{action_type}', method='PUT', json_data=params)
# res contains whether the task successful.
return CommandResults(readable_output=res.get('result'))
def watchlist_create_command(client: Client, name: str, search_query: str, index_type: str = 'events',
description: str = '') -> CommandResults:
params = assign_params(name=name, search_query=search_query, description=description, index_type=index_type)
res = client.http_request(url='/v1/watchlist', method='POST', json_data=params)
watchlist_id = res.get('id')
if id:
output = {'id': watchlist_id}
return CommandResults(outputs=output, outputs_prefix='CarbonBlackEDR.Watchlist', outputs_key_field='id',
readable_output=f"Successfully created new watchlist with id {watchlist_id}")
return CommandResults(readable_output="Could not create new watchlist.")
def get_watchlist_list_command(client: Client, id: str = None, limit: str = None) -> CommandResults:
url = f'/v1/watchlist/{id}' if id else '/v1/watchlist'
res: Union[dict, list] = client.http_request(url=url, method='GET')
human_readable_data = []
# Handling case of only one record.
if id:
res = [res]
total_num_of_watchlists = len(res)
res = res[:arg_to_number(limit, 'limit')]
for watchlist in res:
human_readable_data.append({
'Name': watchlist.get('name'),
'ID': watchlist.get('id'),
'Group ID': watchlist.get('group_id'),
'Description': watchlist.get('description'),
'Total Hits': watchlist.get('total_hits'),
'Query': watchlist.get('search_query'),
})
md = f'{INTEGRATION_NAME} - Watchlists'
md += tableToMarkdown(f"\nShowing {len(res)} out of {total_num_of_watchlists} results.", human_readable_data,
removeNull=True)
return CommandResults(outputs=res, outputs_prefix='CarbonBlackEDR.Watchlist', outputs_key_field='name',
readable_output=md)
def binary_ban_command(client: Client, md5: str, text: str, last_ban_time: str = None, ban_count: str = None,
last_ban_host: str = None) -> CommandResults:
body = assign_params(md5hash=md5,
text=text, last_ban_time=last_ban_time, ban_count=ban_count,
last_ban_host=last_ban_host)
try:
client.http_request(url='/v1/banning/blacklist', method='POST', json_data=body)
except DemistoException as e:
if '409' in e.message:
return CommandResults(readable_output=f'Ban for md5 {md5} already exists')
else:
raise Exception(f'{INTEGRATION_NAME} - Error connecting to API. Error: {e.message}')
return CommandResults(readable_output='hash banned successfully')
def binary_bans_list_command(client: Client, limit: str = None) -> CommandResults:
res = client.http_request(url='/v1/banning/blacklist', method='GET')
res = res[:arg_to_number(limit, 'limit')] if limit else res
human_readable_data = []
for banned in res:
human_readable_data.append({
'md5': banned.get('md5hash'),
'Text': banned.get('text'),
'Timestamp': banned.get('timestamp'),
'User ID': banned.get('user_id'),
'Username': banned.get('username'),
})
return CommandResults(outputs=res, outputs_prefix='CarbonBlackEDR.BinaryBan', outputs_key_field='md5',
readable_output=tableToMarkdown(f'{INTEGRATION_NAME} -Banned Hashes', human_readable_data))
def alert_update_command(client: Client, alert_ids: str, status: str = None, set_ignored: bool = None,
query: str = None) -> CommandResults:
url = '/v1/alerts'
body = assign_params(alert_ids=argToList(alert_ids),
requested_status=status,
set_ignored=set_ignored,
query=query
)
res = client.http_request(url=url, method='POST', json_data=body)
if not res:
raise Exception(f"{INTEGRATION_NAME} - Could not find alerts: {', '.join(alert_ids)}.")
return CommandResults(readable_output='Alert was updated successfully.')
def alert_search_command(client: Client, status: str = None, username: str = None, feedname: str = None,
hostname: str = None, report: str = None, sort: str = None, query: str = None,
facet: str = None, limit: str = None, start: str = '0') -> CommandResults:
res = client.get_alerts(status, username, feedname, hostname, report, sort, query, facet, limit, start)
if not res:
raise Exception(f'{INTEGRATION_NAME} - Request cannot be processed.')
alerts = res.get('results', [])
human_readable_data = []
for alert in alerts:
human_readable_data.append({
'Alert ID': alert.get('unique_id'),
'File Name': alert.get('process_name'),
'File Path': alert.get('process_path'),
'Hostname': alert.get('hostname'),
'Source md5': alert.get('md5'),
'Segment ID': alert.get('segment_id'),
'Severity': alert.get('alert_severity'),
'Created Time': alert.get('created_time'),
'Status': alert.get('status'),
})
outputs = assign_params(Results=alerts, Facets=res.get('facets'), Terms=res.get('terms'),
total_results=res.get('total_results'))
md = f'{INTEGRATION_NAME} - Alert Search Results'
md += tableToMarkdown(
f"\nShowing {start} - {len(res.get('results', []))} out of {res.get('total_results', '0')} results.",
human_readable_data)
return CommandResults(outputs=outputs, outputs_prefix='CarbonBlackEDR.Alert',
outputs_key_field='Terms',
readable_output=md)
def binary_summary_command(client: Client, md5: str) -> CommandResults:
url = f'/v1/binary/{md5}/summary'
try:
res = client.http_request(url=url, method='GET')
if not res:
return CommandResults(
readable_output=f'Could not find data for file {md5}.')
human_readable_data = {
'Host Count': res.get('host_count'),
'Group': res.get('group'),
'OS Type': res.get('os_type'),
'Timestamp': res.get('timestamp'),
'md5': res.get('md5'),
'Last Seen': res.get('last_seen'),
'Is Executable Image': res.get('is_executable_image')
}
return CommandResults(outputs=res, outputs_prefix='CarbonBlackEDR.BinaryMetadata', outputs_key_field='md5',
readable_output=tableToMarkdown(f'{INTEGRATION_NAME} -Summary For File {md5}',
human_readable_data, removeNull=True))
except DemistoException as e:
if '404' in e.message:
return CommandResults(readable_output=f'File {md5} could not be found')
else:
raise Exception(f'{INTEGRATION_NAME} - Error connecting to API. Error: {e.message}')
def binary_download_command(client: Client, md5: str) -> CommandResults:
url = f'/v1/binary/{md5}'
try:
res = client.http_request(url=url, method='GET', ok_codes=(200, 204, 404), resp_type='content')
if not res:
return CommandResults(
readable_output=f'Could not find data for file {md5}.')
return fileResult(f'binary_{md5}.zip', res, file_type=9)
except DemistoException as e:
if '404' in e.message:
return CommandResults(readable_output=f'File {md5} could not be found')
else:
raise Exception(f'{INTEGRATION_NAME} - Error connecting to API. Error: {e.message}')
def binary_search_command(client: Client, md5: str = None, product_name: str = None, digital_signature: str = None,
group: str = None, hostname: str = None, publisher: str = None, company_name: str = None,
sort: str = None, observed_filename: str = None, query: str = None, facet: str = None,
limit: str = '50', start: str = '0') -> CommandResults:
res = client.get_binaries(md5, product_name, digital_signature, group, hostname, publisher, company_name, sort,
observed_filename, query, facet, limit, start)
if not res:
raise Exception(f'{INTEGRATION_NAME} - Request cannot be processed.')
outputs = assign_params(Results=res.get('results'), Facets=res.get('facets'), Terms=res.get('terms'),
total_results=res.get('total_results'))
human_readable_data = []
for binary_file in res.get('results', []):
human_readable_data.append({
'Host Count': binary_file.get('host_count'),
'Group': binary_file.get('group'),
'OS Type': binary_file.get('os_type'),
'Timestamp': binary_file.get('timestamp'),
'md5': binary_file.get('md5'),
'Last Seen': binary_file.get('last_seen'),
'Is Executable Image': binary_file.get('is_executable_image')
})
md = f'{INTEGRATION_NAME} - Binary Search Results'
md += tableToMarkdown(f"\nShowing {start} - {len(res.get('results', []))} out of {res.get('total_results', '0')} "
f"results.", human_readable_data, headers=['md5', 'Group', 'OS Type', 'Host Count',
'Last Seen', 'Is Executable Image', 'Timestamp'])
return CommandResults(outputs=outputs, outputs_prefix='CarbonBlackEDR.BinarySearch',
outputs_key_field='md5',
readable_output=md)
def process_events_list_command(client: Client, process_id: str, segment_id: str, start: str = None, count: str = None):
if not process_id or not segment_id:
raise Exception(f'{INTEGRATION_NAME} - Please provide both process id and segment id to run this command.')
url = f'/v3/process/{process_id}/{segment_id}/event'
start = int(start) if start else None
count = int(count) if count else None
params = {}
if start:
params['cb.event_start'] = start
if count:
params['cb.event_count'] = count
res = client.http_request(url=url, method='GET', params=params)
process = client.get_formatted_ProcessEventDetail(res.get('process', {}))
return CommandResults(outputs=process, outputs_prefix='CarbonBlackEDR.Events',
outputs_key_field='id',
readable_output=process, raw_response=res)
def process_segments_get_command(client: Client, process_id: str, limit: str = '50') -> CommandResults:
url = f'/v1/process/{process_id}/segment'
res = client.http_request(url=url, method='GET')
if not res:
return CommandResults(
readable_output=f'Could not find segment data for process id {process_id}.')
res = res.get('process', {}).get('segments')
res = res[:arg_to_number(limit, 'limit')] if limit else res
# Human readable is depending on request therefore is not prettified.
return CommandResults(outputs=res, outputs_prefix='CarbonBlackEDR.ProcessSegments',
outputs_key_field='unique_id',
readable_output=res)
def process_get_command(client: Client, process_id: str, segment_id: str,
get_related: bool = False) -> CommandResults:
get_related = argToBoolean(get_related)
url = f"/{'v1' if get_related else 'v2'}/process/{process_id}/{segment_id}"
try:
res = client.http_request(url=url, method='GET')
except DemistoException as e:
if "404" in e.message:
raise Exception(f'{INTEGRATION_NAME} - Could not find result for '
f'process id {process_id} with segment id {segment_id}.')
else:
raise Exception(f'{INTEGRATION_NAME} - Error connecting to API. Error: {e.message}')
data = res.get('process', {}) if get_related else res
human_readable_data = {
'Process Path': data.get('path'),
'Process md5': data.get('process_md5'),
'Process Name': data.get('process_name'),
'Process PID': data.get('process_pid'),
'Process ID': data.get('id'),
'Hostname': data.get('hostname'),
'Segment ID': data.get('segment_id'),
'Username': data.get('username'),
'Last Update': data.get('last_update'),
'Is Terminated': data.get('terminated')
}
return CommandResults(outputs=res, outputs_prefix='CarbonBlackEDR.Process', outputs_key_field='id',
readable_output=tableToMarkdown(f'{INTEGRATION_NAME} - Process', human_readable_data))
def processes_search_command(client: Client, process_name: str = None, group: str = None, hostname: str = None,
parent_name: str = None, process_path: str = None, md5: str = None,
query: str = None, group_by: str = None, sort: str = None, facet: str = None,
facet_field: str = None, limit: str = '50', start: str = '0'):
res = client.get_processes(process_name, group, hostname, parent_name, process_path, md5, query, group_by, sort,
facet, facet_field, limit, start)
if not res:
raise Exception(f'{INTEGRATION_NAME} - Request cannot be processed.')
outputs = assign_params(Results=res.get('results'), Facets=res.get('facets'), Terms=res.get('terms'),
total_results=res.get('total_results'))
human_readable_data = []
for process in res.get('results'):
human_readable_data.append(
{
'Process Path': process.get('path'),
'Process md5': process.get('process_md5'),
'Process Name': process.get('process_name'),
'Segment ID': process.get('segment_id'),
'Process PID': process.get('process_pid'),
'Process ID': process.get('id'),
'Hostname': process.get('hostname'),
'Username': process.get('username'),
'Last Update': process.get('last_update'),
'Is Terminated': process.get('terminated')
})
md = f'#### {INTEGRATION_NAME} - Process Search Results'
md += tableToMarkdown(
f"\nShowing {start} - {len(res.get('results', []))} out of {res.get('total_results', '0')} results.",
human_readable_data,
headers=['Process Path', 'Process ID', 'Segment ID', 'Process md5', 'Process Name', 'Hostname',
'Process PID', 'Username', 'Last Update', 'Is Terminated'],
removeNull=True)
return CommandResults(outputs=outputs, outputs_prefix='CarbonBlackEDR.ProcessSearch', outputs_key_field='Terms',
readable_output=md)
def sensor_installer_download_command(client: Client, os_type: str, group_id: str):
url = f"/v1/group/{group_id}/installer/{os_type.replace('_', '/')}"
res = client.http_request(url=url, method='GET', resp_type='content')
if not res:
return CommandResults(
readable_output=f'Could not find installer for group id {group_id} which compatible with {os_type}.')
return fileResult(f'sensor_installer_{group_id}_{os_type}.zip', res, file_type=9)
def endpoint_command(client: Client, id: str = None, ip: str = None, hostname: str = None):
if not id and not ip and not hostname:
raise Exception(f'{INTEGRATION_NAME} - In order to run this command, please provide valid id, ip or hostname')
# If multiple filters were given, we want to retrieve all results that match any filter ('OR', not 'AND')
# issue https://github.com/demisto/etc/issues/46353. Therefore, we make an API query for every filter separately.
ips = argToList(ip)
hostnames = argToList(hostname)
ids = argToList(id)
exceptions = []
res = []
if ips:
for current_ip in ips:
# Carbon Black returns an error in various scenarios (no results matching the query, etc.), wrapping with
# `try-except` to handle these exceptions here.
try:
res += client.get_sensors(ipaddr=current_ip)[1]
except Exception as e:
exceptions.append({'Query': f'ip: {current_ip}', 'Exception': str(e)})
if hostnames:
for current_hostname in hostnames:
try:
res += client.get_sensors(hostname=current_hostname)[1]
except Exception as e:
exceptions.append({'Query': f'hostname: {current_hostname}', 'Exception': str(e)})
if ids:
for current_id in ids:
try:
res += client.get_sensors(id=current_id)[1]
except Exception as e:
exceptions.append({'Query': f'id: {current_id}', 'Exception': str(e)})
# Remove duplicates by taking entries with unique `id`:
if res:
res = list({v['id']: v for v in res}.values())
endpoints = []
command_results = []
for sensor in res:
is_isolated = _get_isolation_status_field(sensor['network_isolation_enabled'],
sensor['is_isolating'])
endpoint = Common.Endpoint(
id=sensor.get('id'),
hostname=sensor.get('computer_name'),
ip_address=_parse_field(sensor.get('network_adapters', ''), index_after_split=0, chars_to_remove='|'),
mac_address=_parse_field(sensor.get('network_adapters', ''), index_after_split=1, chars_to_remove='|'),
os_version=sensor.get('os_environment_display_string'),
memory=sensor.get('physical_memory_size'),
status='Online' if sensor.get('status') == 'Online' else 'Offline',
is_isolated=is_isolated,
vendor='Carbon Black Response')
endpoints.append(endpoint)
endpoint_context = endpoint.to_context().get(Common.Endpoint.CONTEXT_PATH)
md = tableToMarkdown(f'{INTEGRATION_NAME} - Endpoint: {sensor.get("id")}', endpoint_context)
command_results.append(CommandResults(
readable_output=md,
raw_response=res,
indicator=endpoint
))
if exceptions:
md = tableToMarkdown('The following queries resulted in an error: ', exceptions, headers=['Query', 'Exception'])
command_results.append(CommandResults(readable_output=md))
return command_results
def fetch_incidents(client: Client, max_results: int, last_run: dict, first_fetch_time: str, status: str = None,
feedname: str = None, query: str = ''):
if (status or feedname) and query:
raise Exception(f'{INTEGRATION_NAME} - Search is not permitted with both query and filter parameters.')
max_results = arg_to_number(arg=max_results, arg_name='max_fetch', required=False) if max_results else 50
# How much time before the first fetch to retrieve incidents
first_fetch_time = dateparser.parse(first_fetch_time)
last_fetch = last_run.get('last_fetch', None) # {last_fetch: timestamp}
demisto.debug(f'{INTEGRATION_NAME} - last fetch: {last_fetch}')
# Handle first fetch time
if last_fetch is None:
last_fetch = first_fetch_time
else:
last_fetch = datetime.fromtimestamp(last_fetch)
latest_created_time = last_fetch.timestamp()
date_range = f'[{last_fetch.strftime("%Y-%m-%dT%H:%M:%S")} TO *]'
incidents: List[Dict[str, Any]] = []
alerts = []
# multiple statuses are not supported by api. If multiple statuses provided, gets the incidents for each status.
# Otherwise will run without status.
query_params = {'created_time': date_range}
if feedname:
query_params['feedname'] = feedname
if status:
for current_status in argToList(status):
demisto.debug(f'{INTEGRATION_NAME} - Fetching incident from Server with status: {current_status}')
query_params['status'] = f'"{current_status}"'
# we create a new query containing params since we do not allow both query and params.
res = client.get_alerts(query=_create_query_string(query_params), limit=max_results)
alerts += res.get('results', [])
demisto.debug(f'{INTEGRATION_NAME} - fetched {len(alerts)} so far.')
else:
query = _add_to_current_query(query, query_params)
demisto.debug(f'{INTEGRATION_NAME} - Fetching incident from Server with status: {status}')
res = client.get_alerts(query=query, limit=max_results)
alerts += res.get('results', [])
demisto.debug(f'{INTEGRATION_NAME} - Got total of {len(alerts)} alerts from CB server.')
for alert in alerts:
incident_created_time = dateparser.parse(alert.get('created_time'))
assert incident_created_time is not None
incident_created_time_ms = incident_created_time.timestamp()
# to prevent duplicates, adding incidents with creation_time > last fetched incident
if last_fetch:
if incident_created_time_ms <= last_fetch.timestamp():
demisto.debug(f'{INTEGRATION_NAME} - alert {str(alert)} was created at {incident_created_time_ms}.'
f' Skipping.')
continue
alert_id = alert.get('unique_id', '')
alert_name = alert.get('process_name', '')
incident_name = f'{INTEGRATION_NAME}: {alert_id} {alert_name}'
if not alert_id or not alert_name:
demisto.debug(f'{INTEGRATION_NAME} - Alert details are missing. {str(alert)}')
if ioc_attr := alert.get('ioc_attr'):
try:
alert['ioc_attr'] = json.loads(ioc_attr)
highlights = alert['ioc_attr'].get('highlights', [])
for i, attribute in enumerate(highlights):
highlights[i] = attribute.replace("PREPREPRE", "").replace("POSTPOSTPOST", "")
except json.JSONDecodeError as e:
demisto.debug(f"Failed to parse ioc_attr as JSON: {e}")
incident = {
'name': incident_name,
'occurred': timestamp_to_datestring(incident_created_time_ms),
'rawJSON': json.dumps(alert),
}
incidents.append(incident)
# Update last run and add incident if the incident is newer than last fetch
if incident_created_time_ms > latest_created_time:
latest_created_time = incident_created_time_ms
demisto.debug(f'Fetched {len(alerts)} alerts. Saving {len(incidents)} as incidents.')
# Save the next_run as a dict with the last_fetch key to be stored
next_run = {'last_fetch': latest_created_time}
return next_run, incidents
def test_module(client: Client, params: dict) -> str:
try:
client.get_processes(limit='5', allow_empty=True)
if params['isFetch']:
client.get_alerts(status=params.get('alert_status', None), feedname=params.get('alert_feed_name', None),
query=params.get('alert_query', None), allow_empty_params=False, limit='3')
return 'ok'
except DemistoException as e:
if 'Forbidden' in str(e) or 'UNAUTHORIZED' in str(e):
raise Exception('Authorization Error: make sure API Key is correctly set')
else:
raise e
''' MAIN FUNCTION '''
def main() -> None:
try:
params = demisto.params()
base_url = urljoin(params['url'], '/api')
if not params.get('credentials') or not (api_token := params.get('credentials', {}).get('password')):
raise DemistoException('Missing API Key. Fill in a valid key in the integration configuration.')
verify_certificate = not params.get('insecure', False)
proxy = params.get('proxy', False)
command = demisto.command()
args = demisto.args() if demisto.args() else {}
demisto.debug(f'Command being called is {command}')
client = Client(
base_url=base_url,
use_ssl=verify_certificate,
use_proxy=proxy,
apitoken=api_token
)
commands: Dict[str, Callable] = {'cb-edr-processes-search': processes_search_command,
'cb-edr-process-get': process_get_command,
'cb-edr-process-segments-get': process_segments_get_command,
'cb-edr-process-events-list': process_events_list_command,
'cb-edr-binary-search': binary_search_command,
'cb-edr-binary-download': binary_download_command,
'cb-edr-binary-summary': binary_summary_command,
'cb-edr-alert-search': alert_search_command,
'cb-edr-alert-update': alert_update_command,
'cb-edr-binary-bans-list': binary_bans_list_command,
'cb-edr-binary-ban': binary_ban_command,
'cb-edr-watchlists-list': get_watchlist_list_command,
'cb-edr-watchlist-create': watchlist_create_command,
'cb-edr-watchlist-update': watchlist_update_command,
'cb-edr-watchlist-update-action': watchlist_update_action_command,
'cb-edr-watchlist-delete': watchlist_delete_command,
'cb-edr-sensors-list': sensors_list_command,
'cb-edr-quarantine-device': quarantine_device_command,
'cb-edr-unquarantine-device': unquarantine_device_command,
'cb-edr-sensor-installer-download': sensor_installer_download_command,
'endpoint': endpoint_command
}
if command == 'test-module':
result = test_module(client, params)
return_results(result)
elif command == 'fetch-incidents':
next_run, incidents = fetch_incidents(
client=client,
max_results=params.get('max_fetch'),
last_run=demisto.getLastRun(),
first_fetch_time=params.get('first_fetch', '3 days'),
status=params.get('alert_status', None),
feedname=params.get('alert_feed_name', None),
query=params.get('alert_query', None))
demisto.setLastRun(next_run)
demisto.incidents(incidents)
elif command in commands:
return_results(commands[command](client, **args))
else:
raise NotImplementedError(f'command {command} was not implemented in this integration.')
# Log exceptions and return errors
except Exception as e:
return_error(f'Failed to execute {demisto.command()} command.\nError:\n{str(e)}')
''' ENTRY POINT '''
if __name__ in ('__main__', '__builtin__', 'builtins'):
main()