-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathca_netskope.py
More file actions
473 lines (373 loc) · 15.4 KB
/
ca_netskope.py
File metadata and controls
473 lines (373 loc) · 15.4 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
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
#!/usr/bin/python3
# Standard Python libraries
from calendar import weekheader
import ssl
import datetime
import logging
import json
import urllib.request
import urllib.parse
# Third party Python libraries
import requests
import mysql.connector
# Custom libraries
from modules.secrets import SECRETS
from modules.dbConn import *
########################
# FUNCTIONS
########################
def fix_datetime(firstSeen):
try:
new_datetime = datetime.datetime.strftime(datetime.datetime.strptime(firstSeen, iso), mysqliso)
except ValueError as err:
# Remove the 26th charactor
# python date objects only support upto 6 charactors in the %f position
# Defender sometimes has 7, so we gotta remove the last charactor before the 'Z'
n = 26
first_part = firstSeen[0:n]
second_part = firstSeen[n+1:]
newFirstSeen = first_part+second_part
new_datetime = datetime.datetime.strftime(datetime.datetime.strptime(newFirstSeen, iso), mysqliso)
return new_datetime
# Close function fix_datetime
# Get a list of all assets in Defnder
#####################################
def netskope_get_machines():
# Connect to the Defender API and get machines
url = 'https://' + base_url + '/api/v1/clients?token=' + token
logging.info('url: %s', url)
response = requests.get(url)
results = response.json()
return results
# Close function defender_get_machines
def netskope_insert_asset(results):
# Clear the current entries in the defender_asset, defender_tag tables
# Alternativly, find a way to keep them in sync so we don't have to keep DELETEing
sql = "truncate table " + str(table_asset)
try:
ret = db.doExec(sql)
logging.info('Truncated table "%s".', table_asset)
except mysql.connector.Error as err:
logging.error(err)
for i in results['data']:
asset_id = i['attributes']['_id']
logging.debug('asset_id: %s', asset_id)
client_version = i['attributes']['client_version']
logging.debug('client_version: %s', client_version)
hostname = i['attributes']['host_info']['hostname']
logging.info('hostname: %s', hostname)
last_event_actor = i['attributes']['last_event']['actor']
logging.debug('last_event_actor: %s', last_event_actor)
last_event_event = i['attributes']['last_event']['event']
logging.debug('last_event_event: %s', last_event_event)
last_event_npa_status = i['attributes']['last_event']['npa_status']
logging.debug('last_event_npa_status: %s', last_event_npa_status)
last_event_status = i['attributes']['last_event']['status']
logging.debug('last_event_status: %s', last_event_status)
timestamp = i['attributes']['last_event']['timestamp']
logging.debug('timestamp: %s', timestamp)
# TODO COnvert itmestamp to ISO time
# TODO for each user in users, build list
break
sql = "INSERT INTO " + str(table_asset) + " (\
id, \
asset_id, \
computerDnsName, \
firstSeen, \
lastSeen, \
osPlatform, \
osVersion, \
osProcessor, \
version, \
lastIpAddress, \
lastExternalIpAddress, \
agentVersion, \
osBuild, \
healthStatus, \
deviceValue, \
rbacGroupId, \
rbacGroupName, \
riskScore, \
exposureLevel, \
isAadJoined, \
aadDeviceId, \
defenderAvStatus, \
onboardingStatus, \
osArchitecture, \
managedBy, \
managedByStatus\
) VALUES (\
NULL,\
'" + str(asset_id) + "', \
'" + str(computerDnsName) + "', \
'" + str(firstSeen) + "', \
'" + str(lastSeen) + "', \
'" + str(osPlatform) + "', \
'" + str(osVersion) + "', \
'" + str(osProcessor) + "', \
'" + str(version) + "', \
'" + str(lastIpAddress) + "', \
'" + str(lastExternalIpAddress) + "', \
'" + str(agentVersion) + "', \
'" + str(osBuild) + "', \
'" + str(healthStatus) + "', \
'" + str(deviceValue) + "', \
'" + str(rbacGroupId) + "', \
'" + str(rbacGroupName) + "', \
'" + str(riskScore) + "', \
'" + str(exposureLevel) + "', \
'" + str(isAadJoined) + "', \
'" + str(aadDeviceId) + "', \
'" + str(defenderAvStatus) + "', \
'" + str(onboardingStatus) + "', \
'" + str(osArchitecture) + "', \
'" + str(managedBy) + "', \
'" + str(managedByStatus) + "'\
)"
try:
ret = db.doExec(sql)
logging.info('Successfully INSERTed into table "%s": %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s', table_asset, asset_id, computerDnsName, firstSeen, lastSeen, osPlatform, osVersion, osProcessor, version, lastIpAddress, lastExternalIpAddress, agentVersion, osBuild, healthStatus, deviceValue, rbacGroupName, riskScore, exposureLevel, isAadJoined, aadDeviceId, defenderAvStatus, onboardingStatus, osArchitecture, managedBy, managedByStatus)
except mysql.connector.Error as err:
logging.error(err)
# Close function defender_insert_machines
def defender_insert_tag(results):
# Create an empty set for which we will store our tags
# Use a set as we want each item in the set to be unique, which is not true of a list
tags = []
# Clear the defender_tag table - may not need to
sql = "truncate table " + str(table_tag)
try:
ret = db.doExec(sql)
logging.info('Truncated table "%s".', table_tag)
except mysql.connector.Error as err:
logging.error(err)
for i in results['value']:
# Set the machine tags
for tag in i['machineTags']:
tag_name = tag
logging.info('tag: %s', tag)
tags.append(tag_name)
tags = list(dict.fromkeys(tags))
logging.info('tags: %s', tags)
# Add tags to the defender_tag table
for tag in tags:
sql = "INSERT INTO " + str(table_tag) + " (\
tag_id, \
tag_name \
) VALUES (\
NULL, \
'" + str(tag) + "' \
)"
try:
ret = db.doExec(sql)
logging.info('Successfully INSERTed into table "%s": %s.', table_tag, tag)
except mysql.connector.Error as err:
logging.error(err)
# Close function defender_insert_tag
def defender_insert_asset_tag(results):
# Clear the defender_asset_tag table - may not need to
sql = "truncate table " + str(table_asset_tag)
try:
ret = db.doExec(sql)
logging.info('Truncated table "%s".', table_asset_tag)
except mysql.connector.Error as err:
logging.error(err)
for i in results['value']:
# Set the asset_id and tags
hash = i['id']
computerDnsName = i['computerDnsName']
machineTags = i['machineTags']
# If the asset has a tag value...
try:
machineTags = i['machineTags'][0]
logging.info('Asset %s has tags: %s', hash, i['machineTags'])
# ...process each tag in the list
for tag in i['machineTags']:
# Lookup the tag_id from the defender_tag table
sql = "select tag_id from " + str(table_tag) + " where tag_name = '" + str(tag) + "'"
logging.debug("sql: %s", sql)
try:
ret = db.doSel(sql)
tag_id = ret[0][0]
logging.debug('tag_id: %s', tag_id)
except mysql.connector.Error as err:
logging.error(err)
# Lookup the id from the defender_asset table
sql = "select id from " + str(table_asset) + " where asset_id = '" + str(hash) + "'"
logging.debug("sql: %s", sql)
try:
ret = db.doSel(sql)
asset_id = ret[0][0]
logging.debug('tag_id: %s', asset_id)
except mysql.connector.Error as err:
logging.error(err)
# Add a row to the defender_asset_tag table
sql = "INSERT INTO " + str(table_asset_tag) + " (\
asset_id, \
tag_id \
) VALUES (\
" + str(asset_id) + ", \
" + str(tag_id) + " \
)"
try:
ret = db.doExec(sql)
logging.info('Successfully INSERTed into table "%s": %s, %s.', table_asset_tag, asset_id, tag_id)
except mysql.connector.Error as err:
logging.error(err)
except IndexError:
logging.debug('Asset %s does not have any tags.', computerDnsName)
# Close function defender_insert_asset_tag
# Calcuate data and insert into the rollup table
def defender_metrics_rollup():
# total agents - used for calculating a later query
sql = "select count(*) from " + str(table_asset)
try:
ret = db.doSel(sql)
total_agents = ret[0][0]
logging.info('total_agents: %s', total_agents)
except mysql.connector.Error as err:
logging.error(err)
# health status - returns a number 1-100 indicating a ratio of all active devices over total devices
sql = "select round(a.num/a.den*100, 0) as health_status\
from (\
select\
(select count(*) from " + str(table_asset) + " where healthStatus = 'Active') as num,\
(select count(*) from " + str(table_asset) + ") as den\
) a"
try:
ret = db.doSel(sql)
health_status = ret[0][0]
logging.info('health_status: %s', health_status)
except mysql.connector.Error as err:
logging.error(err)
# onboarded - returns a number 1-100 indicating the ratio of all onboarded devices over total onboardable devices
sql = "select round(a.num/a.den*100, 0) as onboarded\
from (\
select\
(select count(*) from " + str(table_asset) + " where onboardingStatus = 'Onboarded') as num,\
(select count(*) from " + str(table_asset) + " where find_in_set(onboardingStatus, 'Onboarded,CanBeOnboarded')) as den\
) a"
try:
ret = db.doSel(sql)
onboarded = ret[0][0]
logging.info('onboarded: %s', onboarded)
except mysql.connector.Error as err:
logging.error(err)
# av status - returns a number 1-100 indicating the status of the Defender AV status of all assets
sql = "select round(a.num/a.den*100, 0) as av_status\
from (\
select\
(select count(*) from " + str(table_asset) + " where defenderAvStatus = 'Updated') as num,\
(select count(*) from " + str(table_asset) + " where find_in_set(defenderAvStatus, 'Updated,Unknown,Disabled,NotUpdated')) as den\
) a"
try:
ret = db.doSel(sql)
av_status = ret[0][0]
logging.info('av_status: %s', av_status)
except mysql.connector.Error as err:
logging.error(err)
# duplicates - counts up all assets that have duplicates based on the computerDnsName
sql = "select sum(a.count) as duplicates\
from (\
select count(computerDnsName) as count\
from " + str(table_asset) + "\
where computerDnsName <> 'None'\
group by computerDnsName\
having count(computerDnsName) > 1\
) a"
try:
ret = db.doSel(sql)
duplicates = ret[0][0]
logging.info('duplicates: %s', duplicates)
except mysql.connector.Error as err:
logging.error(err)
# TODO
# version - returns a number 1-100 indicating the ratio of agents with current updates over all agents
# TODO
# engine_version - returns a number 1-100 indicating the ratio of agents with current engine updates over all agents
# TODO
# signature_last_updated - returns a number 1-100 indicating the ratio of agents that have updated the av signatures within an expected timeframe
# TODO
# last_scan - returns a number 1-100 indicating the ratio of agents that have been scanned within an expected timeframe
# Insert all of the above data into the table
# TODO - insert the above TODOs into the below sql
sql = "INSERT INTO " + str(table_metrics_rollup) + " (\
id, \
date, \
health_status, \
onboarded, \
av_status, \
duplicates \
) VALUES (\
NULL,\
curdate(),\
'" + str(health_status) + "', \
'" + str(onboarded) + "', \
'" + str(av_status) + "', \
'" + str(duplicates) + "'\
)"
try:
ret = db.doExec(sql)
logging.info('Successfully INSERTed into table "%s": %s, %s, %s, %s', table_metrics_rollup, health_status, onboarded, av_status, duplicates)
except mysql.connector.Error as err:
logging.error(err)
# daily_total = a daily rollup weighted average of the day's metrics
sql = "select round(\
(health_status*.9 +\
onboarded*2.5 +\
av_status*1.5 +\
(" + str(total_agents) + " - duplicates)/" + str(total_agents) + " * 100 * .1\
)/4, 0)\
from " + str(table_metrics_rollup) + "\
where date = curdate()"
try:
ret = db.doSel(sql)
daily_total = ret[0][0]
logging.info('daily_total: %s', daily_total)
except mysql.connector.Error as err:
logging.error(err)
# Insert the daily rollup into the table
sql = "UPDATE " + table_metrics_rollup + " SET daily_total = '" + str(daily_total) + "' WHERE date = curdate()"
try:
ret = db.doExec(sql)
logging.info('Successfully INSERTed into table "%s": %s', table_metrics_rollup, daily_total)
except mysql.connector.Error as err:
logging.error(err)
# Close function defender_metrics_rollup
########################
# Global Variables
########################
# Valid log levels include:
# INFO
# WARNING
# ERROR
# CRITICAL
# DEBUG
# Logging options
log_File = '/usr/local/scripts/log/ca_netskope.log'
logging.basicConfig(format='%(asctime)s %(levelname)s %(lineno)d %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename=log_File, level=logging.INFO, filemode='w')
# Netskope API Token
token = '{0}'.format(SECRETS['netskope']['token'])
base_url = '{0}'.format(SECRETS['netskope']['url'])
logging.info("base_url: %s", base_url)
# MySQL connection info
db = dbConn(
'{0}'.format(SECRETS['mysql_ca']['host']),
'{0}'.format(SECRETS['mysql_ca']['user']),
'{0}'.format(SECRETS['mysql_ca']['password']),
'{0}'.format(SECRETS['mysql_ca']['database'])
)
# Define the MySQL tables
table_asset = "netskope_asset"
table_user = "netskope_user"
table_asset_user = "netskope_asset_user"
# Define the format of the date that Defender presents via the API
iso = '%Y-%m-%dT%H:%M:%S.%fZ'
# We will take the date format from Defender's API and convert it to a
# format that we can then insert into MySQL
mysqliso = '%Y-%m-%d %H:%M:%S'
assets = netskope_get_machines()
netskope_insert_asset(assets)
#defender_insert_tag(assets)
#defender_insert_asset_tag(assets)
#defender_metrics_rollup()