-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Accessdata.py
430 lines (336 loc) · 15 KB
/
Accessdata.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
import demistomock as demisto
from CommonServerPython import *
from CommonServerUserPython import *
''' IMPORTS '''
import os
import json
import requests
from urllib3 import disable_warnings
from requests.exceptions import HTTPError
# Disable insecure warnings
disable_warnings()
''' CONSTANTS '''
DATE_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
ACCESS_DATA = 'Accessdata'
JOBID_KEY = 'Accessdata.Job.ID'
JOBRESULT_KEY = 'Accessdata.Job.Result'
JOBSTATE_KEY = 'Accessdata.Job.State'
JOB_KEY = 'Accessdata.Job'
''' HELPERS '''
def create_jobstate_context(contents):
if 'CaseJobID' not in contents:
return {
JOB_KEY + '(val.ID == obj.ID)': contents
}
return {
JOB_KEY + '(val.CaseJobID == obj.CaseJobID)': contents
}
def create_contents(caseID, jobID, state=None, result=None):
ec = {
'CaseID': caseID,
'ID': jobID,
'CaseJobID': str(caseID) + "_" + str(jobID)
}
if state is not None:
ec['State'] = state
if result is not None:
ec['Result'] = result
return ec
def wrap_jobstate_context(contents, humanReadableMessage=""):
return {
'Type': entryTypes['note'],
'ContentsFormat': formats['json'],
'Contents': contents,
'HumanReadable': humanReadableMessage,
'EntryContext': create_jobstate_context(contents)
}
class Client:
"""
Client will implement the service API, should not contain Demisto logic.
Should do requests and return data
# """
def __init__(self, base_url=None, verify=None, proxy=None, token=None):
self.base_url = base_url
self.verify = verify
self.headers = {
'EnterpriseApiKey': token,
'Accept': 'application/json',
'Content-Type': 'application/json'
}
self.proxy = proxy
def http_request(self, method, url_suffix, body=None, params=None):
try:
full_url = self.base_url + url_suffix
res = requests.request(
method, full_url, data=body, headers=self.headers,
params=params, proxies=self.proxy, verify=self.verify)
res.raise_for_status()
return res.json()
except ValueError:
return res.text
except HTTPError:
err_json = res.json()
err_msg = ''
if 'message' in err_json:
err_msg = err_msg + \
'Error: {0}.\n'.format(err_json['message'])
elif 'http_response' in err_json:
err_msg = err_msg + \
'Error: {0}.\n'.format(err_json['http_response'])
if 'code' in err_json:
err_msg = err_msg + \
'QRadar Error Code: {0}'.format(err_json['code'])
return_error(err_msg)
return ""
def test_quinc(self):
res = self.http_request('GET', 'api/v2/enterpriseapi/statuscheck')
return res.lower()
def get_jobstatus(self, args):
caseID = args['caseID']
jobID = args['jobID']
url = 'api/v2/enterpriseapi/core/' + str(caseID) + \
'/getjobstatus/' + str(jobID)
res = self.http_request('GET', url)
if 'resultData' not in res: # if no data take state from first level of nesting
contents = create_contents(caseID, jobID, res['state'], res)
return wrap_jobstate_context(contents, "Current job state: " + res['state'])
resultData = json.loads(res['resultData'])
if 'RealData' not in resultData: # if no data take state from first level of nesting
contents = create_contents(caseID, jobID, res['state'], resultData)
return wrap_jobstate_context(contents, "Current job state: " + res['state'])
RealData = json.loads(resultData['RealData'])
if 'TaskStatusList' not in RealData: # if no data take state from first level of nesting
contents = create_contents(caseID, jobID, res['state'], RealData)
return wrap_jobstate_context(contents, "Current job state: " + res['state'])
task = RealData['TaskStatusList'][0]
if task is not None:
if task['State'] is None:
contents = create_contents(caseID, jobID, 'Unknown', task)
return wrap_jobstate_context(contents, "Cannot get job state")
if task['State'] != 'Success':
contents = create_contents(caseID, jobID, task['State'], task)
return wrap_jobstate_context(contents, "Current job state: " + task['State'])
else:
result = json.loads(task['Results'][0]['Data'])
res = json.loads(task['Results'][1]['Data'])
if result['OperationType'] == 24: # software inventory
contents = create_contents(
caseID, jobID, 'Success', res['Applications'])
return wrap_jobstate_context(
contents,
tableToMarkdown(
'Applications', res['Applications'],
[
'Name', 'Version', 'Publisher', 'InstallDate',
'InstallLocation', 'InstallSource',
'EstimatedSizeInBytes'
]))
elif result['OperationType'] == 12: # volatile data
contents = create_contents(caseID, jobID, 'Success', res)
return wrap_jobstate_context(contents, "Job completed successfully")
elif result['OperationType'] == 11: # memory dump
memdumpres = json.loads(task['Results'][2]['Data'])
contents = create_contents(caseID, jobID, 'Success', memdumpres)
return wrap_jobstate_context(contents, "Job completed successfully")
else:
contents = create_contents(caseID, jobID, 'Success', task)
return wrap_jobstate_context(contents, "Job completed successfully")
contents = create_contents(caseID, jobID, 'Failed', None)
return wrap_jobstate_context(contents, "Job exited unexpectedly")
def get_jobstatus_processlist(self, args):
jobStatus = self.get_jobstatus(args)
contents = jobStatus['Contents']
newContents = create_contents(contents['CaseID'], contents['ID'], contents['State'], "")
message = "(to get path to snapshot job should be finished) "
if 'Result' not in contents:
message += "No Result in response scheme"
return wrap_jobstate_context(newContents, message)
if 'SnapshotDetails' not in contents['Result']:
message += "No Result.SnapshotDetails in response scheme"
return wrap_jobstate_context(newContents, message)
if 'File' not in contents['Result']['SnapshotDetails']:
message += "No Result.SnapshotDetails.File in response scheme"
return wrap_jobstate_context(newContents, message)
newContents['Result'] = contents['Result']['SnapshotDetails']['File']
return wrap_jobstate_context(newContents, newContents['Result'])
def get_jobstatus_memorydump(self, args):
jobStatus = self.get_jobstatus(args)
contents = jobStatus['Contents']
newContents = create_contents(
contents['CaseID'],
contents['ID'],
contents['State'],
"")
contents = contents['Result']
message = "(to get path to memory dump job should be finished) "
if 'ResultFiles' not in contents:
message += "No ResultFiles in response scheme"
return wrap_jobstate_context(newContents, message)
if not contents['ResultFiles']:
message += "ResultFiles is empty"
return wrap_jobstate_context(newContents, message)
contents = contents['ResultFiles'][0]
if 'Path' not in contents:
message += "No Path in response scheme"
return wrap_jobstate_context(newContents, message)
newContents['Result'] = contents['Path']
return wrap_jobstate_context(newContents, newContents['Result'])
def jobstatus_scan(self, args):
caseJobID = args['caseJobID']
preparedParameters = caseJobID.split("_")
preparedParameters = {
'caseID': preparedParameters[0],
'jobID': preparedParameters[1]
}
jobStatus = self.get_jobstatus(preparedParameters)
contents = jobStatus['Contents']
newContents = create_contents(preparedParameters['caseID'],
preparedParameters['jobID'],
'Unknown')
if 'State' not in contents:
return wrap_jobstate_context(newContents, "No state in job result")
newContents['State'] = contents['State']
return wrap_jobstate_context(newContents, "Current job state: " + newContents['State'])
def legacyagent_runvolatilejob(self, args):
if ('caseid' not in args or args['caseid'] is None):
args['caseid'] = self.get_processing_case_id()['Contents']
url = 'api/v2/enterpriseapi/agent/' + str(args['caseid']) + '/volatile'
data = {
'ips': {'targets': [args['target_ip']]},
'Volatile': {
'Operation': 12,
'IncludeProcessTree': True,
'ProcessTreeOptions': {
'DetectHiddenProcesses': True,
'IncludeDlls': True,
'IncludeSockets': True
}
}
}
jobID = self.http_request('POST', url, json.dumps(data))
contents = create_contents(args['caseid'], jobID, 'Unknown')
contents['Type'] = 'Volatile'
return {
'Type': entryTypes['note'],
'ContentsFormat': formats['text'],
'Contents': contents,
'HumanReadable': "JobID: " + str(jobID),
'EntryContext': create_jobstate_context(contents)
}
def legacyagent_runmemoryacquisition(self, args):
if ('caseid' not in args or args['caseid'] is None):
args['caseid'] = self.get_processing_case_id()['Contents']
url = 'api/v2/enterpriseapi/agent/' + str(args['caseid']) + \
'/memoryacquistion'
data = {
'ips': {'targets': [args['target_ip']]},
'MemoryAcquistion': {'Operation': 11}
}
jobID = self.http_request('POST', url, json.dumps(data))
contents = create_contents(args['caseid'], jobID, 'Unknown')
contents['Type'] = 'LegacyMemoryDump'
return {
'Type': entryTypes['note'],
'ContentsFormat': formats['json'],
'Contents': contents,
'HumanReadable': "JobID: " + str(jobID),
'EntryContext': create_jobstate_context(contents)
}
def read_casefile(self, args):
url = 'api/v2/enterpriseapi/core/readfilecontents'
filepath = args['filepath']
data = self.http_request('POST', url, json.dumps(filepath))
return {
'Type': entryTypes['note'],
'ContentsFormat': formats['text'],
'Contents': data,
'EntryContext': {'Accessdata.File.Contents': data}
}
def get_processing_case_id(self):
url = 'api/v2/enterpriseapi/processingcaseid'
processingcaseId = self.http_request('GET', url)
return {
'Type': entryTypes['note'],
'ContentsFormat': formats['text'],
'Contents': processingcaseId,
'EntryContext': {ACCESS_DATA + '.ProcessingCaseId': processingcaseId}
}
def test_module(client):
return client.test_quinc()
def quinc_get_jobstatus_command(client, args):
return client.get_jobstatus(args)
def quinc_get_jobstatus_processlist_command(client, args):
return client.get_jobstatus_processlist(args)
def quinc_get_jobstatus_memorydump_command(client, args):
return client.get_jobstatus_memorydump(args)
def quinc_jobstatus_scan_command(client, args):
return client.jobstatus_scan(args)
def quinc_legacyagent_runvolatilejob_command(client, args):
return client.legacyagent_runvolatilejob(args)
def quinc_legacyagent_runmemoryacquisition_command(client, args):
return client.legacyagent_runmemoryacquisition(args)
def quinc_read_casefile_command(client, args):
return client.read_casefile(args)
def quinc_get_processing_case_id_command(client):
return client.get_processing_case_id()
def main():
use_ssl = not demisto.params().get('insecure', True)
url = demisto.params().get('server_name') + ':4443/'
token = demisto.params()['Token']
if not demisto.params().get('proxy'):
if 'HTTP_PROXY' in os.environ:
del os.environ['HTTP_PROXY']
if 'HTTPS_PROXY' in os.environ:
del os.environ['HTTPS_PROXY']
if 'http_proxy' in os.environ:
del os.environ['http_proxy']
if 'https_proxy' in os.environ:
del os.environ['https_proxy']
PROXIES = {
'http': None,
'https': None
} # type: dict
else:
PROXIES = {
'http': os.environ['http_proxy'] or os.environ['HTTP_PROXY'],
'https': os.environ['https_proxy'] or os.environ['HTTPS_PROXY']
}
LOG('Command being called is %s' % (demisto.command()))
try:
client = Client(url, use_ssl, PROXIES, token)
if demisto.command() == 'test-module':
# This is the call made when pressing the integration Test button.
demisto.results(test_module(client))
if demisto.command() == 'accessdata-get-jobstatus-processlist':
demisto.results(
quinc_get_jobstatus_processlist_command(
client, demisto.args()))
if demisto.command() == 'accessdata-get-jobstatus-memorydump':
demisto.results(
quinc_get_jobstatus_memorydump_command(
client, demisto.args()))
if demisto.command() == 'accessdata-jobstatus-scan':
demisto.results(
quinc_jobstatus_scan_command(
client, demisto.args()))
if demisto.command() == 'accessdata-legacyagent-get-processlist':
demisto.results(
quinc_legacyagent_runvolatilejob_command(
client, demisto.args()))
if demisto.command() == 'accessdata-legacyagent-get-memorydump':
demisto.results(
quinc_legacyagent_runmemoryacquisition_command(
client, demisto.args()))
if demisto.command() == 'accessdata-read-casefile':
demisto.results(
quinc_read_casefile_command(
client, demisto.args()))
if demisto.command() == 'accessdata-get-processing-case-id':
demisto.results(
quinc_get_processing_case_id_command(client))
except Exception as e:
return_error(
"Failed to execute {} command. Error: {}".format(
demisto.command(), str(e)))
if __name__ in ['__main__', 'builtin', 'builtins']:
main()