This repository was archived by the owner on May 15, 2022. It is now read-only.
forked from demisto/content
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtraHop.py
More file actions
385 lines (337 loc) · 12.7 KB
/
Copy pathExtraHop.py
File metadata and controls
385 lines (337 loc) · 12.7 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
import demistomock as demisto
from CommonServerPython import *
from CommonServerUserPython import *
''' IMPORTS '''
import json
import requests
from distutils.util import strtobool
# Disable insecure warnings
requests.packages.urllib3.disable_warnings()
''' GLOBALS/PARAMS '''
APIKEY = demisto.params().get('apikey')
SERVER = demisto.params()['url'][:-1] if (demisto.params()['url'] and demisto.params()['url'].endswith('/')) else \
demisto.params()['url']
USE_SSL = not demisto.params().get('insecure', False)
BASE_URL = SERVER + '/api/v1/'
HEADERS = {
'Accept': 'application/json',
'Authorization': 'ExtraHop apikey={key}'.format(key=APIKEY)
}
if not demisto.params().get('proxy'):
del os.environ['HTTP_PROXY']
del os.environ['HTTPS_PROXY']
del os.environ['http_proxy']
del os.environ['https_proxy']
# 'response' is a container for paginated results
response = []
''' HELPER FUNCTIONS '''
def http_request(method, url_suffix, data=None, payload=None):
data = json.dumps(data)
try:
res = requests.request(
method,
BASE_URL + url_suffix,
verify=USE_SSL,
data=data,
headers=HEADERS,
params=payload
)
except requests.exceptions.RequestException: # This is the correct syntax
return_error('Failed to connect to - {url} - Please check the URL'.format(url=BASE_URL))
# Handle error responses gracefully
if res.status_code == 204:
return demisto.results('Successful Modification')
elif res.status_code not in {200, 204, 201}:
return_error('Error in API call to ExtraHop {code} - {reason}'.format(code=res.status_code, reason=res.reason))
return res
''' COMMANDS + REQUESTS FUNCTIONS '''
def test_module():
"""
Performs basic get request to check ExtraHop version
"""
test_result = http_request('GET', 'extrahop')
return test_result
def get_alerts():
res_raw = http_request('GET', 'alerts')
res = res_raw.json()
return res
def paginate(can_paginate, cursor):
while can_paginate is True:
body = {
"cursor": cursor,
"context_ttl": 400000
}
res_raw = http_request('POST', 'records/cursor', body)
res = res_raw.json()
response.append(res)
if 'cursor' in res:
paginate(True, res['cursor'])
else:
break
return response
def query_records(field, value, operator, query_from, limit):
data = {
"filter": {
"field": str(field),
"operand": str(value),
"operator": str(operator)
},
"from": int(query_from),
"limit": int(limit)
}
res_raw = http_request('POST', 'records/search', data)
res = res_raw.json()
response.append(res)
if 'cursor' in res:
response.append(paginate(True, res['cursor']))
return response
def devices():
active_from = demisto.args().get('active_from')
active_until = demisto.args().get('active_until')
search_type = demisto.args().get('search_type')
limit = demisto.args().get('limit')
payload = {}
if active_from:
payload['active_from'] = active_from
if active_until:
payload['active_until'] = active_until
if limit:
payload['limit'] = limit
payload['search_type'] = search_type
res_raw = http_request('GET', 'devices', data=None, payload=payload)
res = res_raw.json()
return res
def format_alerts(alerts):
hr = ''
ec = {
"ExtraHop": {
"Alert": []
}
} # type: dict
for alert in alerts:
hr += tableToMarkdown('Found Alert', alert, headerTransform=string_to_table_header, removeNull=True)
ec['ExtraHop']['Alert'].append(createContext(alert, keyTransform=string_to_context_key, removeNull=True))
if len(alerts) == 0:
demisto.results('No results were found')
else:
demisto.results({
'Type': entryTypes['note'],
'ContentsFormat': formats['markdown'],
'Contents': alerts,
'HumanReadable': hr,
'EntryContext': ec
})
def format_device_results(data):
hr_table = []
ec = {
"ExtraHop": {
"Device": []
}
} # type: dict
for device in data:
hr = {}
if 'id' in device:
hr['ID'] = device.get('id')
if 'display_name' in device:
hr['Display Name'] = device.get('display_name')
if 'ipaddr4' in device:
hr['IP Address'] = device.get('ipaddr4')
if 'macaddr' in device:
hr['MAC Address'] = device.get('macaddr')
if 'vendor' in device:
hr['Vendor'] = device.get('vendor')
hr_table.append(hr)
ec['ExtraHop']['Device'].append(createContext(device, keyTransform=string_to_context_key, removeNull=True))
demisto.results({
'Type': entryTypes['note'],
'ContentsFormat': formats['markdown'],
'Contents': data,
'HumanReadable': tableToMarkdown('Devices Found', hr_table),
'EntryContext': ec
})
def whitelist_modify(add, remove):
assignments = {}
if add:
add_items = add.split(',')
add_items = list(map(int, add_items))
assignments['assign'] = add_items
if remove:
remove_items = remove.split(',')
remove_items = list(map(int, remove_items))
assignments['unassign'] = remove_items
res = http_request('POST', 'whitelist/devices', data=assignments)
return res
def whitelist_retrieve():
res_raw = http_request('GET', 'whitelist/devices')
res = res_raw.json()
return res
def add_alert(apply_all, disabled, name, notify_snmp, refire_interval, severity, alert_type, object_type,
protocols, field_name, stat_name, units, interval_length, operand, operator, field_name2, field_op,
param, param2, alert_id=None):
data = {
"apply_all": apply_all,
"disabled": disabled,
"name": name,
"notify_snmp": notify_snmp,
"refire_interval": int(refire_interval),
"severity": int(severity),
"type": alert_type
}
if alert_type == 'detection':
data['object_type'] = object_type
data['protocols'] = [str(protocols)]
elif alert_type == 'threshold':
data['field_name'] = field_name
data['stat_name'] = stat_name
data['units'] = units
data['interval_length'] = int(interval_length)
data['operand'] = operand
data['operator'] = operator
if demisto.args().get('field_name2'):
data['field_name2'] = field_name2
if demisto.args().get('field_op'):
data['field_op'] = field_op
if demisto.args().get('param'):
data['param'] = param
if demisto.args().get('param2'):
data['param2'] = param2
if alert_id:
method = 'PATCH'
url_suffix = 'alerts/{alert_id}'.format(alert_id=alert_id)
else:
method = 'POST'
url_suffix = 'alerts'
data = json.dumps(data)
try:
res = requests.request(
method,
BASE_URL + url_suffix,
verify=USE_SSL,
data=data,
headers=HEADERS
)
except requests.exceptions.RequestException: # This is the correct syntax
return_error('Failed to connect to - {url} - Please check the URL'.format(url=BASE_URL))
# Handle error responses gracefully
if res.status_code == 204:
return demisto.results('Successful Modification')
if res.status_code == 400:
resp = res.json()
return_error('Error in request format - {message}'.format(message=resp['error_message']))
if res.status_code == 201:
return demisto.results('Alert successfully added')
elif res.status_code not in {200, 204, 201}:
return_error('Error in API call to ExtraHop {code} - {reason}'.format(code=res.status_code, reason=res.reason))
return res
def add_alert_command():
apply_all = bool(strtobool(demisto.args().get('apply_all', False)))
disabled = bool(strtobool(demisto.args().get('disabled', False)))
name = demisto.args().get('name')
notify_snmp = bool(strtobool(demisto.args().get('notify_snmp', False)))
field_name = demisto.args().get('field_name')
stat_name = demisto.args().get('stat_name')
units = demisto.args().get('units')
interval_length = demisto.args().get('interval_length')
operand = demisto.args().get('operand')
refire_interval = demisto.args().get('refire_interval')
severity = demisto.args().get('severity')
alert_type = demisto.args().get('type')
object_type = demisto.args().get('object_type')
protocols = demisto.args().get('protocols')
operator = demisto.args().get('operator')
field_name2 = demisto.args().get('field_name2')
field_op = demisto.args().get('field_op')
param = demisto.args().get('param')
param2 = demisto.args().get('param2')
add_alert(apply_all, disabled, name, notify_snmp, refire_interval, severity, alert_type, object_type,
protocols, field_name, stat_name, units, interval_length, operand, operator, field_name2, field_op,
param, param2)
def modify_alert_command():
alert_id = demisto.args().get('alert_id')
apply_all = bool(strtobool(demisto.args().get('apply_all', False)))
disabled = bool(strtobool(demisto.args().get('disabled', False)))
name = demisto.args().get('name')
notify_snmp = bool(strtobool(demisto.args().get('notify_snmp', False)))
field_name = demisto.args().get('field_name')
stat_name = demisto.args().get('stat_name')
units = demisto.args().get('units')
interval_length = demisto.args().get('interval_length')
operand = demisto.args().get('operand')
refire_interval = demisto.args().get('refire_interval')
severity = demisto.args().get('severity')
alert_type = demisto.args().get('type')
object_type = demisto.args().get('object_type')
protocols = demisto.args().get('protocols')
operator = demisto.args().get('operator')
field_name2 = demisto.args().get('field_name2')
field_op = demisto.args().get('field_op')
param = demisto.args().get('param')
param2 = demisto.args().get('param2')
add_alert(apply_all, disabled, name, notify_snmp, refire_interval, severity, alert_type, object_type,
protocols, field_name, stat_name, units, interval_length, operand, operator, field_name2, field_op,
param, param2, alert_id)
def get_alerts_command():
res = get_alerts()
format_alerts(res)
def whitelist_modify_command():
add = demisto.args().get('add')
remove = demisto.args().get('remove')
whitelist_modify(add, remove)
def query_records_command():
field = demisto.args().get('field')
value = demisto.args().get('value')
operator = demisto.args().get('operator')
query_from = demisto.args().get('query_from')
limit = demisto.args().get('limit')
res = query_records(field, value, operator, query_from, limit)
source = res[0]['records']
hr = ''
ec = {
"ExtraHop": {
"Query": []
}
} # type: dict
for record in source:
hr += tableToMarkdown('Incident result for ID {id}'.format(id=record['_id']), record['_source'])
ec['ExtraHop']['Query'].append(createContext(record, keyTransform=string_to_context_key, removeNull=True))
demisto.results({
'Type': entryTypes['note'],
'ContentsFormat': formats['markdown'],
'Contents': source,
'HumanReadable': hr,
'EntryContext': createContext(ec, removeNull=True)
})
def whitelist_retrieve_command():
res = whitelist_retrieve()
if len(res) == 0:
demisto.results('No devices found in whitelist')
elif len(res) > 0:
format_device_results(res)
def devices_command():
found_devices = devices()
format_device_results(found_devices)
''' COMMANDS MANAGER / SWITCH PANEL '''
LOG('Command being called is {command}'.format(command=demisto.command()))
try:
if demisto.command() == 'test-module':
test_module()
demisto.results('ok')
elif demisto.command() == 'extrahop-get-alert-rules':
get_alerts_command()
elif demisto.command() == 'extrahop-query':
query_records_command()
elif demisto.command() == 'extrahop-devices':
devices_command()
elif demisto.command() == 'extrahop-whitelist-modify':
whitelist_modify_command()
elif demisto.command() == 'extrahop-whitelist-retrieve':
whitelist_retrieve_command()
elif demisto.command() == 'extrahop-add-alert-rule':
add_alert_command()
elif demisto.command() == 'extrahop-modify-alert-rule':
modify_alert_command()
# Log exceptions
except Exception as e:
LOG(str(e))
LOG.print_log()
raise