-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmvision_edr_threats.py
335 lines (268 loc) · 14.5 KB
/
mvision_edr_threats.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
#!/usr/bin/env python3
# Written by mohlcyber v.1.7 (13.06.2022)
# Script to retrieve all threats from the monitoring dashboard
import sys
import requests
import time
import logging
import json
import os
import pytz
from datetime import datetime, timedelta
from dateutil import tz
from logging.handlers import SysLogHandler
from dotenv import load_dotenv
load_dotenv(verbose=True)
class EDR():
def __init__(self):
self.iam_url = 'iam.mcafee-cloud.com/iam/v1.1'
if edr_region == 'EU':
self.base_url = 'soc.eu-central-1.trellix.com'
elif edr_region == 'US-W':
self.base_url = 'soc.trellix.com'
elif edr_region == 'US-E':
self.base_url = 'soc.us-east-1.trellix.com'
elif edr_region == 'SY':
self.base_url = 'soc.ap-southeast-2.trellix.com'
elif edr_region == 'GOV':
self.base_url = 'soc.mcafee-gov.com'
self.session = requests.Session()
if valid == 'False':
self.session.verify = False
else:
self.session.verify = True
if proxy is not None:
self.session.proxies['https'] = proxy
creds = (edr_client_id, edr_client_secret)
self.pattern = '%Y-%m-%dT%H:%M:%S.%f'
self.cache_fname = '{0}/cache.log'.format(cache_dir)
if os.path.isfile(self.cache_fname):
cache = open(self.cache_fname, 'r')
last_detection = datetime.strptime(cache.read(), '%Y-%m-%dT%H:%M:%SZ')
last_detection_utc = last_detection.replace(tzinfo=pytz.UTC)
next_pull = last_detection_utc.astimezone(tz.tzlocal()) + timedelta(seconds=1)
logger.debug('Cache exists. Last detection date UTC: {0}'.format(last_detection))
logger.debug('Pulling newest threats from: {0}'.format(next_pull))
cache.close()
else:
logger.debug('Cache does not exists. Pulling data from last {0} days.'.format(initial_pull))
next_pull = datetime.now() - timedelta(days=int(initial_pull))
self.epoch_pull = str(datetime.timestamp(next_pull)*1000)[:13]
logger.debug('New pulling date {0} - epoch {1}'.format(next_pull, self.epoch_pull))
self.auth(creds)
self.limit = 10000
def auth(self, creds):
try:
payload = {
'scope': 'soc.hts.c soc.hts.r soc.rts.c soc.rts.r soc.qry.pr',
'grant_type': 'client_credentials',
'audience': 'mcafee'
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
res = self.session.post('https://{0}/token'.format(self.iam_url), headers=headers, data=payload, auth=creds)
if res.ok:
token = res.json()['access_token']
self.session.headers = {'Authorization': 'Bearer {}'.format(token)}
logger.debug('AUTHENTICATION: Successfully authenticated.')
else:
logger.error('Error in retrieving edr.auth(). Request url: {}'.format(res.url))
logger.error('Error in retrieving edr.auth(). Request headers: {}'.format(res.request.headers))
logger.error('Error in retrieving edr.auth(). Request body: {}'.format(res.request.body))
raise Exception('Error in retrieving edr.auth(). Error: {0} - {1}'.format(str(res.status_code), res.text))
except Exception as error:
exc_type, exc_obj, exc_tb = sys.exc_info()
logger.error("Error in {location}.{funct_name}() - line {line_no} : {error}"
.format(location=__name__, funct_name=sys._getframe().f_code.co_name,
line_no=exc_tb.tb_lineno, error=str(error)))
raise
def get_threats(self):
try:
tthreat = 0
tdetect = 0
skip = 0
tnextflag = True
filter = {}
severities = ["s0", "s1", "s2", "s3", "s4", "s5"]
filter['severities'] = severities
filter['scoreRange'] = [30]
while(tnextflag):
res = self.session.get(
'https://api.{0}/ft/api/v2/ft/threats?sort=-lastDetected&filter={1}&from={2}&limit={3}&skip={4}'
.format(self.base_url, json.dumps(filter), self.epoch_pull, self.limit, skip))
if res.ok:
res = res.json()
if int(res['skipped']) + int(res['items']) == int(res['total']):
tnextflag = False
else:
skip = int(res['skipped']) + int(res['items'])
if len(res['threats']) > 0:
if os.path.isfile(self.cache_fname):
cache = open(self.cache_fname, 'r')
last_detection = datetime.strptime(cache.read(), '%Y-%m-%dT%H:%M:%SZ')
cache.close()
if last_detection < (datetime.strptime(res['threats'][0]['lastDetected'], '%Y-%m-%dT%H:%M:%SZ')):
logger.debug('More recent detection timestamp detected. Updating cache.log.')
cache = open(self.cache_fname, 'w')
cache.write(res['threats'][0]['lastDetected'])
cache.close()
else:
logger.debug('More recent detection timestamp in cache.log already saved.')
else:
cache = open(self.cache_fname, 'w')
cache.write(res['threats'][0]['lastDetected'])
cache.close()
for threat in res['threats']:
affhosts = self.get_affected_hosts(threat['id'])
ddetect_count = 0
for host in affhosts:
detections = self.get_detections(threat['id'], host['id'])
for detection in detections:
threat['detection'] = detection
traceid = detection['traceId']
maguid = detection['host']['maGuid']
sha256 = detection['sha256']
threat['url'] = 'https://ui.{0}/monitoring/#/workspace/72,TOTAL_THREATS,{1}?traceId={2}&maGuid={3}&sha256={4}' \
.format(self.base_url, threat['id'], traceid, maguid, sha256)
logger.debug(json.dumps(threat))
logger.info('Retrieved new MVISION EDR Threat Detection. {0}'.format(threat['name']))
if syslog_ip and syslog_port:
syslog.info(json.dumps(threat, sort_keys=True))
logger.info('Successfully send data to Syslog IP {}'.format(syslog_ip))
if threat_log == 'True':
if os.path.exists(threat_dir) is False:
os.mkdir(threat_dir)
time_detect = detection['firstDetected']
ptime_detect = datetime.strptime(time_detect, '%Y-%m-%dT%H:%M:%SZ')
filename = '{}-{}.log'.format(ptime_detect.strftime('%Y%m%d%H%M%S'), threat['name'])
file = open('{}/{}'.format(threat_dir, filename), 'w')
file.write(json.dumps(threat))
file.close()
tdetect += 1
ddetect_count += 1
logger.debug('For threat {0} identified {1} new detections.'.format(threat['name'], ddetect_count))
tthreat += 1
else:
logger.debug('No new threats identified. Exiting. {0}'.format(res))
else:
logger.error('Error in retrieving edr.get_threats(). Request url: {}'.format(res.url))
logger.error('Error in retrieving edr.get_threats(). Request headers: {}'.format(res.request.headers))
logger.error('Error in retrieving edr.get_threats(). Request body: {}'.format(res.request.body))
raise Exception('Error in retrieving edr.get_threats(). Error: {0} - {1}'.format(str(res.status_code), res.text))
logger.debug('Pulled total {0} Threats and {1} Detections.'.format(tthreat, tdetect))
except Exception as error:
exc_type, exc_obj, exc_tb = sys.exc_info()
logger.error("Error in {location}.{funct_name}() - line {line_no} : {error}"
.format(location=__name__, funct_name=sys._getframe().f_code.co_name,
line_no=exc_tb.tb_lineno, error=str(error)))
raise
def get_affected_hosts(self, threatId):
try:
skip = 0
anextflag = True
affhosts = []
while(anextflag):
res = self.session.get(
'https://api.{0}/ft/api/v2/ft/threats/{1}/affectedhosts?sort=-rank&from={2}&limit={3}&skip={4}'
.format(self.base_url, threatId, self.epoch_pull, self.limit, skip))
if res.ok:
res = res.json()
if int(res['skipped']) + int(res['items']) == int(res['total']):
anextflag = False
else:
skip = int(res['skipped']) + int(res['items'])
if len(affhosts) == 0:
affhosts = res['affectedHosts']
else:
for affhost in res['affectedHosts']:
affhosts.append(affhost)
else:
logger.error('Error in retrieving edr.get_affectedHosts(). Request url: {}'.format(res.url))
logger.error('Error in retrieving edr.get_affectedHosts(). Request headers: {}'.format(res.request.headers))
logger.error('Error in retrieving edr.get_affectedHosts(). Request body: {}'.format(res.request.body))
raise Exception('Error in retrieving edr.get_affectedHosts(). Error: {0} - {1}'.format(str(res.status_code), res.text))
return affhosts
except Exception as error:
exc_type, exc_obj, exc_tb = sys.exc_info()
logger.error("Error in {location}.{funct_name}() - line {line_no} : {error}"
.format(location=__name__, funct_name=sys._getframe().f_code.co_name,
line_no=exc_tb.tb_lineno, error=str(error)))
raise
def get_detections(self, threatId, affhost):
try:
skip = 0
dnextflag = True
detections = []
while(dnextflag):
filter = {
'affectedHostId': affhost
}
res = self.session.get(
'https://api.{0}/ft/api/v2/ft/threats/{1}/detections?sort=-rank&from={2}&filter={3}&limit={4}&skip={5}'
.format(self.base_url, threatId, self.epoch_pull, json.dumps(filter), self.limit, skip))
if res.ok:
res = res.json()
if int(res['skipped']) + int(res['items']) == int(res['total']):
dnextflag = False
else:
skip = int(res['skipped']) + int(res['items'])
if len(detections) == 0:
detections = res['detections']
else:
for detection in res['detections']:
detections.append(detection)
else:
logger.error('Error in retrieving edr.get_detections(). Request url: {}'.format(res.url))
logger.error('Error in retrieving edr.get_detections(). Request headers: {}'.format(res.request.headers))
logger.error('Error in retrieving edr.get_detections(). Request body: {}'.format(res.request.body))
raise Exception('Error in retrieving edr.get_detections(). Error: {0} - {1}'.format(str(res.status_code), res.text))
return detections
except Exception as error:
exc_type, exc_obj, exc_tb = sys.exc_info()
logger.error("Error in {location}.{funct_name}() - line {line_no} : {error}"
.format(location=__name__, funct_name=sys._getframe().f_code.co_name,
line_no=exc_tb.tb_lineno, error=str(error)))
raise
if __name__ == '__main__':
edr_region = os.getenv('EDR_REGION')
edr_client_id = os.getenv('EDR_CLIENT_ID')
edr_client_secret = os.getenv('EDR_CLIENT_SECRET')
interval = os.getenv('INTERVAL')
initial_pull = os.getenv('INITIAL_PULL')
syslog_ip = os.getenv('SYSLOG_IP')
syslog_port = os.getenv('SYSLOG_PORT')
proxy = os.getenv('PROXY')
valid = os.getenv('VALID')
cache_dir = os.getenv('CACHE_DIR')
log_level = os.getenv('LOG_LEVEL')
log_dir = os.getenv('LOG_DIR')
threat_log = os.getenv('THREAT_LOG')
threat_dir = os.getenv('THREAT_DIR')
# setup logging
logger = logging.getLogger('mvedr_logger')
logger.setLevel(log_level)
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s")
# setup the console logger
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
# setup the file logger
if os.path.exists(log_dir) is False:
os.mkdir(log_dir)
file_handler = logging.handlers.RotatingFileHandler('{0}/mvedr_logger.log'.format(log_dir), maxBytes=25000000,
backupCount=5)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
if syslog_ip and syslog_port:
syslog = logging.getLogger('syslog')
syslog.setLevel(log_level)
syslog.addHandler(SysLogHandler(address=(syslog_ip, int(syslog_port))))
while True:
try:
edr = EDR()
edr.get_threats()
edr.session.close()
time.sleep(int(interval))
except Exception:
time.sleep(60)