-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathGuruculGRA.py
352 lines (287 loc) · 14.1 KB
/
GuruculGRA.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
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
''' IMPORTS '''
import traceback
from typing import Any, Dict, List, Optional, Tuple, cast
import dateparser
import urllib3
# Disable insecure warnings
urllib3.disable_warnings()
''' CONSTANTS '''
MAX_INCIDENTS_TO_FETCH = 25
API_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
''' CLIENT CLASS '''
class Client(BaseClient):
def fetch_command_result(self, url_suffix, params, post_url):
incidents: List = list()
try:
if post_url is None:
method = 'GET'
else:
method = 'POST'
params = None
r = self._http_request(
method=method,
url_suffix=url_suffix,
data=post_url,
params=params
)
incidents = r if isinstance(r, list) else [r]
except Exception:
demisto.error("Unable to fetch command result" + traceback.format_exc())
return incidents
def validate_api_key(self):
self._http_request(
method='GET',
url_suffix='/validate',
params={}
)
return 'ok'
''' HELPER FUNCTIONS '''
def arg_to_int(arg: Any, arg_name: str, required: bool = False) -> Optional[int]:
if arg is None:
if required is True:
raise ValueError(f'Missing "{arg_name}"')
return None
if isinstance(arg, str):
if arg.isdigit():
return int(arg)
raise ValueError(f'Invalid number: "{arg_name}"="{arg}"')
if isinstance(arg, int):
return arg
raise ValueError(f'Invalid number: "{arg_name}"')
def arg_to_timestamp(arg: Any, arg_name: str, required: bool = False) -> Optional[int]:
if arg is None:
if required is True:
raise ValueError(f'Missing "{arg_name}"')
return None
if isinstance(arg, str) and arg.isdigit():
return int(arg)
if isinstance(arg, str):
date = dateparser.parse(arg, settings={'TIMEZONE': 'UTC'})
if date is None:
raise ValueError(f'Invalid date: {arg_name}')
return int(date.timestamp())
if isinstance(arg, (int, float)):
return int(arg)
raise ValueError(f'Invalid date: "{arg_name}"')
''' COMMAND FUNCTIONS '''
def fetch_record_command(client: Client, url_suffix, prefix, key, params, post_url=None):
incidents: List = list()
r = client.fetch_command_result(url_suffix, params, post_url)
incidents.extend(r)
results = CommandResults(
outputs_prefix=prefix,
outputs_key_field=key,
outputs=incidents
)
return results
def fetch_records(client: Client, url_suffix, prefix, key, params):
results = fetch_record_command(client, url_suffix, prefix, key, params)
return_results(results)
def fetch_post_records(client: Client, url_suffix, prefix, key, params, post_url):
results = fetch_record_command(client, url_suffix, prefix, key, params, post_url)
return_results(results)
def fetch_incidents(client: Client, max_results: int, last_run: Dict[str, int],
first_fetch_time: Optional[int]
) -> Tuple[Dict[str, int], List[dict]]:
last_fetch = last_run.get('last_fetch', None)
case_status = 'OPEN'
url_access_time = datetime.now().timestamp()
endDate = (datetime.fromtimestamp(cast(int, url_access_time)).strftime(API_DATE_FORMAT))
case_url = '/cases/opendate'
if last_fetch is None:
last_fetch = first_fetch_time
startDate = (
datetime.fromtimestamp(cast(int, last_fetch)).replace(microsecond=0, second=0).strftime(API_DATE_FORMAT))
else:
last_fetch = int(last_fetch)
startDate = (datetime.fromtimestamp(cast(int, last_fetch) + 1).strftime(API_DATE_FORMAT))
incidents: List[Dict[str, Any]] = []
page = 1
isContinue = True
while isContinue:
params = {'page': page, 'max': max_results, 'timezone': 'UTC', 'status': case_status, 'startDate': startDate,
'endDate': endDate}
case_data = client.fetch_command_result(case_url, params, None)
if len(case_data) < max_results:
isContinue = False
else:
page += 1
for record in case_data:
incident_created_time = datetime.now().timestamp()
incident_created_time_ms = incident_created_time * 1000
record['incidentType'] = 'GRACase'
if record.get('caseId') is not None:
inc = {
'name': record.get('entity'),
'occurred': timestamp_to_datestring(incident_created_time_ms),
'rawJSON': json.dumps(record)
}
incidents.append(inc)
next_run = {'last_fetch': int(url_access_time)}
return next_run, incidents
def test_module_command(client: Client) -> str:
try:
client.validate_api_key()
except DemistoException as e:
if 'Forbidden' in str(e):
return 'Authorization Error: make sure API Key is correctly set'
else:
raise e
return 'ok'
''' MAIN FUNCTION '''
def main() -> None:
try:
arguments = demisto.args()
api_key = demisto.params().get('apikey')
base_url = urljoin(demisto.params()['url'], '/api/')
verify_certificate = not demisto.params().get('insecure', False)
first_fetch_time = arg_to_timestamp(
arg=demisto.params().get('first_fetch', '1 days'),
arg_name='First fetch time',
required=True
)
assert isinstance(first_fetch_time, int)
proxy = demisto.params().get('proxy', False)
page = arguments.get('page', "1")
page_count_no = arguments.get('max', "25")
demisto.debug(f'Command being called is {demisto.command()}')
params = {'page': page, 'max': page_count_no}
headers = {
'Authorization': f'Bearer {api_key}'
}
client = Client(
base_url=base_url,
verify=verify_certificate,
headers=headers,
proxy=proxy)
if demisto.command() == 'test-module':
result = test_module_command(client)
return_results(result)
elif demisto.command() == 'fetch-incidents':
max_results = arg_to_int(
arg=demisto.params().get('max_fetch'),
arg_name='max_fetch',
required=False
)
if not max_results or max_results > MAX_INCIDENTS_TO_FETCH:
max_results = MAX_INCIDENTS_TO_FETCH
next_run, incidents = fetch_incidents(
client=client,
max_results=max_results,
last_run=demisto.getLastRun(), # getLastRun() gets the last run dict
first_fetch_time=first_fetch_time
)
demisto.setLastRun(next_run)
demisto.incidents(incidents)
elif demisto.command() == 'gra-fetch-users':
fetch_records(client, '/users', 'Gra.Users', 'employeeId', params)
elif demisto.command() == 'gra-fetch-accounts':
fetch_records(client, '/accounts', 'Gra.Accounts', 'id', params)
elif demisto.command() == 'gra-fetch-active-resource-accounts':
resource_name = arguments.get('resource_name', 'Windows Security')
active_resource_url = '/resources/' + resource_name + '/accounts'
fetch_records(client, active_resource_url, 'Gra.Active.Resource.Accounts', 'id', params)
elif demisto.command() == 'gra-fetch-user-accounts':
employee_id = arguments.get('employee_id')
user_account_url = '/users/' + employee_id + '/accounts'
fetch_records(client, user_account_url, 'Gra.User.Accounts', 'id', params)
elif demisto.command() == 'gra-fetch-resource-highrisk-accounts':
res_name = arguments.get('Resource_name', 'Windows Security')
high_risk_account_resource_url = '/resources/' + res_name + '/accounts/highrisk'
fetch_records(client, high_risk_account_resource_url, 'Gra.Resource.Highrisk.Accounts', 'id', params)
elif demisto.command() == 'gra-fetch-hpa':
fetch_records(client, '/accounts/highprivileged', 'Gra.Hpa', 'id', params)
elif demisto.command() == 'gra-fetch-resource-hpa':
resource_name = arguments.get('Resource_name', 'Windows Security')
resource_hpa = '/resources/' + resource_name + '/accounts/highprivileged'
fetch_records(client, resource_hpa, 'Gra.Resource.Hpa', 'id', params)
elif demisto.command() == 'gra-fetch-orphan-accounts':
fetch_records(client, '/accounts/orphan', 'Gra.Orphan.Accounts', 'id', params)
elif demisto.command() == 'gra-fetch-resource-orphan-accounts':
resource_name = arguments.get('resource_name', 'Windows Security')
resource_orphan = '/resources/' + resource_name + '/accounts/orphan'
fetch_records(client, resource_orphan, 'Gra.Resource.Orphan.Accounts', 'id', params)
elif demisto.command() == 'gra-user-activities':
employee_id = arguments.get('employee_id')
user_activities_url = '/user/' + employee_id + '/activity'
fetch_records(client, user_activities_url, 'Gra.User.Activity', 'employee_id', params)
elif demisto.command() == 'gra-fetch-users-details':
employee_id = arguments.get('employee_id')
fetch_records(client, '/users/' + employee_id, 'Gra.User', 'employeeId', params)
elif demisto.command() == 'gra-highRisk-users':
fetch_records(client, '/users/highrisk', 'Gra.Highrisk.Users', 'employeeId', params)
elif demisto.command() == 'gra-cases':
status = arguments.get('status')
cases_url = '/cases/' + status
fetch_records(client, cases_url, 'Gra.Cases', 'caseId', params)
elif demisto.command() == 'gra-user-anomalies':
employee_id = arguments.get('employee_id')
anomaly_url = '/users/' + employee_id + '/anomalies/'
fetch_records(client, anomaly_url, 'Gra.User.Anomalies', 'anomaly_name', params)
elif demisto.command() == 'gra-case-action':
action = arguments.get('action')
caseId = arguments.get('caseId')
subOption = arguments.get('subOption')
caseComment = arguments.get('caseComment')
riskAcceptDate = arguments.get('riskAcceptDate')
cases_url = '/cases/' + action
if action == 'riskManageCase':
post_url = {"caseId": int(caseId), "subOption": subOption, "caseComment": caseComment,
"riskAcceptDate": riskAcceptDate}
else:
post_url = {"caseId": int(caseId), "subOption": subOption, "caseComment": caseComment}
post_url_json = json.dumps(post_url)
fetch_post_records(client, cases_url, 'Gra.Case.Action', 'caseId', params, post_url_json)
elif demisto.command() == 'gra-case-action-anomaly':
action = arguments.get('action')
caseId = arguments.get('caseId')
anomalyNames = arguments.get('anomalyNames')
subOption = arguments.get('subOption')
caseComment = arguments.get('caseComment')
riskAcceptDate = arguments.get('riskAcceptDate')
cases_url = '/cases/' + action
if action == 'riskAcceptCaseAnomaly':
post_url = {"caseId": int(caseId), "anomalyNames": anomalyNames, "subOption": subOption,
"caseComment": caseComment, "riskAcceptDate": riskAcceptDate}
else:
post_url = {"caseId": int(caseId), "anomalyNames": anomalyNames, "subOption": subOption,
"caseComment": caseComment}
post_url_json = json.dumps(post_url)
fetch_post_records(client, cases_url, 'Gra.Cases.Action.Anomaly', 'caseId', params, post_url_json)
elif demisto.command() == 'gra-investigate-anomaly-summary':
fromDate = arguments.get('fromDate')
toDate = arguments.get('toDate')
modelName = arguments.get('modelName')
if fromDate is not None and toDate is not None:
investigateAnomaly_url = '/investigateAnomaly/anomalySummary/' + modelName + '?fromDate=' + fromDate \
+ ' 00:00:00&toDate=' + toDate + ' 23:59:59'
else:
investigateAnomaly_url = '/investigateAnomaly/anomalySummary/' + modelName
fetch_records(client, investigateAnomaly_url, 'Gra.Investigate.Anomaly.Summary', 'modelId', params)
elif demisto.command() == 'gra-analytical-features-entity-value':
fromDate = arguments.get('fromDate')
toDate = arguments.get('toDate')
modelName = arguments.get('modelName')
entityValue = arguments.get('entityValue')
entityTypeId = arguments.get('entityTypeId')
if fromDate is not None and toDate is not None:
analyticalFeatures_url = 'profile/analyticalFeatures/' + entityValue + '?fromDate=' + fromDate \
+ ' 00:00:00&toDate=' + toDate + ' 23:59:59&modelName=' + modelName
else:
analyticalFeatures_url = 'profile/analyticalFeatures/' + entityValue + '?modelName=' + modelName
if entityTypeId is not None:
analyticalFeatures_url += '&entityTypeId=' + entityTypeId
fetch_records(client, analyticalFeatures_url, 'Gra.Analytical.Features.Entity.Value', 'entityID', params)
elif demisto.command() == 'gra-cases-anomaly':
caseId = arguments.get('caseId')
anomaliesUrl = '/anomalies/' + caseId
fetch_records(client, anomaliesUrl, 'Gra.Cases.anomalies', 'caseId', params)
# Log exceptions and return errors
except Exception as e:
demisto.error(traceback.format_exc()) # print the traceback
return_error(f'Failed to execute {demisto.command()} command.\nError:\n{str(e)}')
''' ENTRY POINT '''
if __name__ in ('__main__', '__builtin__', 'builtins'):
main()