-
Notifications
You must be signed in to change notification settings - Fork 1
/
panda.py
executable file
·418 lines (380 loc) · 16.8 KB
/
panda.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
#!/usr/bin/env python3
import sys,os,requests,json,base64
import optparse,logging,datetime
from metadata import *
try:
import boto3
except ImportError:
pass
try:
import pickle
except ImportError:
pass
# keywords plus date format which contain timestamp in returned json
# Timestamps need to be timezone aware, assume all is in UTC for now.
# Aether spec does not state anything about timezones......
standarddateformat = '%Y-%m-%dT%H:%M:%S.%f%z'
datestrings = (('date','%Y-%m-%dT%H:%M:%S.%f'), ('security_event_date', '%Y-%m-%dT%H:%M:%S%z'))
# {event type:date}
class Cache(object) :
def __init__(self, region,logger,starttime):
self.logger = logger
self.starttime = starttime
self.counter = 0
self.cache = None
self.d = {}
def Get(self,key) :
return(datetime.datetime.strptime(self.starttime, standarddateformat))
def Write(self,key, value):
self.d[key]=value
self.counter+=1
if self.counter % 10 == 0:
self.Flush()
def Close(self) :
self.Flush()
def Flush(self) :
pass
def Print(self,fp) :
for k,v in self.d.items() :
fp.write('{} -- {}\n'.format(k,v.strftime(standarddateformat)))
class CacheFile(Cache) :
def __init__(self, region,logger,starttime):
self.logger = logger
self.starttime = starttime
self.counter = 0
self.file = region
self.fp = None
self.d={}
try:
self.fp = open(os.path.join(sys.path[0],self.file),'rb')
# use dict as fast cache
self.d = pickle.load(self.fp)
self.fp.close()
except Exception as e:
self.logger.warning(str(e))
self.logger.warning('Filecache could not be opened: {}'.format(self.file))
self.d={}
def Get(self,key) :
try:
return(self.d[key])
except KeyError:
# Not in cache, use earliest date
return(datetime.datetime.strptime(self.starttime, standarddateformat))
# write dict to cache
def Flush(self) :
try:
self.fp = open(os.path.join(sys.path[0],self.file),'wb')
pickle.dump(self.d,self.fp)
self.fp.truncate()
self.fp.flush()
self.fp.close()
except Exception as e:
self.logger.warning(str(e))
self.logger.warning('Error writing last_eventid to cache')
class CacheAWSParameter(Cache) :
def __init__(self,region,logger,starttime):
self.logger = logger
self.starttime = starttime
self.counter = 0
self.cache = boto3.client('ssm', region_name=region)
self.d = {}
# Check last event tokens or timestamps per key in AWS cache
# key is event type, value is date: panda-1:20211123:13:02:45
def Get(self,key) :
# First try local cache
try:
return(self.d[key])
except KeyError:
pass
# Retrieve from remote cache
cachekey = 'panda-{}'.format(key)
try:
cachevalue = self.cache.get_parameter(Name=cachekey)['Parameter']['Value']
# We have a string back from the cache, should be timestamp
# Try timestamp
self.d[key] = datetime.datetime.strptime(cachevalue, standarddateformat)
return(self.d[key])
except Exception as e:
# No cache value found
self.logger.warning(str(e))
self.logger.warning('Error: {} not found in cache! Starting from {}. \n'.format(cachekey,self.starttime))
return(datetime.datetime.strptime(self.starttime, standarddateformat))
# write dict to cache
def Flush(self) :
for k,v in self.d.items() :
cachekey = 'panda-{}'.format(k)
try:
# try date format
x = self.cache.put_parameter(Name=cachekey, Type='String', Overwrite=True,Value=v.strftime(standarddateformat))
except Exception as e:
self.logger.warning(str(e))
self.logger.warning('Error writing last_eventid to cache')
# Returns authentication token
def GetAuthentication(url,token,key) :
ClientToken = (token + ":" + key).encode()
Base64ClientToken = base64.b64encode(ClientToken)
data = {'grant_type':'client_credentials','scope':'api-access'}
response = requests.post(url=url,headers={"Authorization": "Basic " + Base64ClientToken.decode(),
"accept":"application/json","Content-Type": "application/x-www-form-urlencoded"},data=data)
tokens = json.loads(response.text)
return(tokens['access_token'])
def RetrievePandaEvents(accountid,apikey,token,url,params):
api_call_headers = {'Authorization': 'Bearer ' + token, 'Content-Type':'application/json', 'WatchGuard-API-Key':apikey, 'Accept':'application/json'}
response = requests.get(url, headers=api_call_headers,params=params)
return(response)
def Marshal(d) :
# Try to convert strings to ints, which will make the auto mapping in sumo work
l=[]
for k,v in d.items():
if isinstance(v, str) :
if len(v) == 0:
l.append(k)
try:
d[k]=int(v)
except ValueError:
pass
# remove empty strings
for k in l:
del(d[k])
# Do lookups
for k,v in d.items() :
try:
lookup=lookups[k]
try:
d[k]=lookup[v]
except KeyError:
pass
except KeyError:
pass
return(d)
# Class for writing events to destination, either Sumo or stdout.
# Overwrites possible for alternate destinations
class EventWriter(object) :
def __init__(self, **kwargs):
self.index = kwargs.get('index','knmi')
self.marshal = kwargs.get('mfunc',None)
self.logger = kwargs.get('logger',None)
# Send a kv-formatted message to stdout
# message is json dict
def send(self, message):
if self.marshal != None :
data=self.marshal(message)
else :
data = message.encode('utf-8', 'replace')
sys.stdout.buffer.write(data)
class EventWriterJSON(EventWriter) :
def __init__(self, **kwargs):
self.marshal = kwargs.get('mfunc',None)
self.logger = kwargs.get('logger',None)
self.url = kwargs.get('url','')
# message is dict
def send(self, message):
if self.marshal != None :
data=self.marshal(message)
else :
data=message
r = requests.post(url=self.url, data=json.dumps(data))
r.raise_for_status()
if self.logger != None:
self.logger.info('EventWriterJSON returned status {}'.format(r.status_code))
class EventWriterJSONStdOut(EventWriter) :
def __init__(self, **kwargs):
self.marshal = kwargs.get('mfunc',None)
self.url = kwargs.get('url','')
self.logger = kwargs.get('logger',None)
# message is dict
def send(self, message):
if self.marshal != None :
data=self.marshal(message)
else :
data=message
sys.stdout.write(json.dumps(data))
sys.stdout.write('\n')
def ExtractSecurityEvents(baseurl,auth,opts,logger,cache,eventwriter) :
# Start extaction from panda portal
totalcount=0
for type,descr in EventTypes.items() :
eventcount=0
logger.info('Extracting {}:{}'.format(type,descr))
command = 'api/{}/accounts/{}/securityevents/{}/export/{}'.format(opts.version,opts.accountid,type,opts.period)
url='{}{}'.format(baseurl,command)
#params={'top':'2','count':'true'}
params={}
r = RetrievePandaEvents(opts.accountid,opts.apikey,auth,url,params)
if r.status_code != 200:
# Output debug information
logger.warning("\n Return Code: " + str(r.status_code) + " " + r.reason)
logger.warning("Path: " + r.request.path_url)
logger.warning("Headers: ")
logger.warning(r.request.headers)
continue
result=r.json()
if result==None :
continue
# Get most recent timestamp from cache, note that returned results are not sorted on date!
last = cache.Get(type)
dtMax=last
for d in result['data'] :
# Find timestamp in returned event
dt=None
for k,f in datestrings :
try:
dt=datetime.datetime.strptime(d[k],f)
d['TIMESTAMP'] = dt.strftime(standarddateformat)
break
except (KeyError, ValueError) as e:
pass
# Make sure dt is timezone aware, for now assume UTC
if dt != None and (dt.tzinfo == None or dt.tzinfo.utcoffset(dt) == None) :
# Change from timezone unaware to aware, assume UTC
dt=dt.replace(tzinfo=datetime.timezone.utc)
if dt != None and dt > last:
d['eventtype']=type
d['eventdescription']=descr
eventwriter.send(d)
eventcount+=1
if dt > dtMax :
dtMax=dt
if dtMax > last:
cache.Write(type,dtMax)
if eventcount > 0:
logger.info('Tried to submit {} events for log type {}'.format(eventcount,descr))
totalcount+=eventcount
#eventwriter.send({'eventssend':totalcount,'eventdescription':'pandacloudscript'})
def ExtractUnmanagedDevices(baseurl,auth,opts,logger,cache,eventwriter) :
# Extract unmanaged devices, only once a day
last = cache.Get('unmanaged')
logger.info('Last timestamp for list of unmanaged devices: {}'.format(last.strftime(standarddateformat)))
if last < (datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days = 1)):
logger.info('Extract unmanaged devices')
eventcount=0
command='api/{}/accounts/{}/unmanageddevices'.format(opts.version,opts.accountid)
url='{}{}'.format(baseurl,command)
r = RetrievePandaEvents(opts.accountid,opts.apikey,auth,url,{})
if r.status_code != 200:
# Output debug information
logger.warning("\n Return Code: " + str(r.status_code) + " " + r.reason)
logger.warning("Path: " + r.request.path_url)
logger.warning("Headers: ")
logger.warning(r.request.headers)
return
result=r.json()
if result==None :
return
for d in result['data'] :
d['TIMESTAMP'] = datetime.dateime.now(datetime.timezone.utc).strftime(standarddateformat)
d['eventdescription']='unmanaged'
eventwriter.send(d)
eventcount+=1
if eventcount > 0:
logger.info('Tried to submit {} events for log type {}'.format(eventcount,'unmanaged'))
cache.Write('unmanaged',datetime.datetime.now(datetime.timezone.utc))
# Main code
def main():
# Hardcoded items for now
version ='v1'
accountid='ACC-0000000'
apikey='nokey'
accesspassword = 'accesspassword'
accessid = 'access id'
authurl = 'https://api.deu.cloud.watchguard.com/oauth/token'
baseurl = 'https://api.deu.cloud.watchguard.com/rest/'
# Parse command line options
p = optparse.OptionParser("usage: %prog [options]")
p.add_option("-b", "--baseurl", dest="baseurl", default=baseurl, \
help = 'Base URL for api requests, defaults to {}'.format(baseurl))
p.add_option("-a", "--authurl", dest="authurl", default=authurl, \
help = 'url for authentication, defaults to: {}'.format(authurl))
p.add_option("-i", "--accessid", dest="accessid", default=accessid, \
help='access id token for authentication requests, default={}'.format(accessid))
p.add_option("-p", "--accesspassword", dest="accesspassword", default=accesspassword, \
help = 'password for accessid, defaults to: {}'.format(accesspassword))
p.add_option("-k", "--apikey", dest="apikey", default=apikey, \
help='api key, default={}'.format(apikey))
p.add_option("-q", "--endpoint", dest="endpoint", default='https://endpoint1.collection.eu.sumologic.com/receiver/v1/http/', \
help = 'SUMO endpoint, defaults to: {}'.format('https://endpoint1.collection.eu.sumologic.com/receiver/v1/http/'))
p.add_option("-v", "--version", dest="version", default=version, \
help = 'api version in url, defaults to: {}'.format(version))
p.add_option("-c", "--command", dest="command", default='securityevents', \
help = 'Aether api command, defaults to: {}'.format('securityevents'))
p.add_option("-z", "--accountid", dest="accountid", default=accountid, \
help = 'Watchguard account ID, defaults to: {}'.format('accountid'))
p.add_option("-d", "--destination", dest="destination", default='sumo', \
help = 'destination, either sumo,stdout. Defaults to: {}'.format('sumo'))
p.add_option("-r", "--region", dest="region", default='us-east-2', \
help = 'AWS region, defaults to: {}'.format('us-east-2'))
p.add_option("-e", "--period", dest="period", type='int', default=7, \
help = 'Period in days for collecting data, either 7 or 1, defaults to 7')
p.add_option("-l", "--log", dest="loglevel", default='WARNING', \
help = 'loglevel field, defaults to WARNING, options are: CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET')
p.add_option("-n", "--nolambda",
action="store_true", dest="nolambda", default=False,
help="Not running as AWS Lambda function, use local filecache. Default = False")
p.add_option("--authonly",
action="store_true", dest="authonly", default=False,
help="Only request and print authentication token, no commands issued. Default = False")
opts,args = p.parse_args()
# For AWS lambda: try to overwrite some options from environment
for x in ('baseurl','authurl','accessid','accesspassword','apikey','region','version','accountid','command','period','endpoint','loglevel','destination') :
try:
exec('opts.{}=os.environ[\'{}\']'.format(x,x))
except Exception as e:
pass
try:
opts.period = int(opts.period)
except:
pass
# Initiate Logging
numeric_level = getattr(logging, opts.loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % opts.loglevel)
formatter = logging.Formatter('%(asctime)s,Level=%(levelname)s,\
LOGGING=%(message)s', '%m/%d/%Y %H:%M:%S')
sh = logging.StreamHandler(sys.stderr)
sh.setFormatter(formatter)
logger = logging.getLogger('PANDA')
logger.addHandler(sh)
logger.level=numeric_level
# Create output channel
if opts.destination=='stdout':
eventwriter=EventWriterJSONStdOut(mfunc=Marshal, logger=logger)
logger.info('Writing events to stdout')
else:
# Must be sumo
eventwriter=EventWriterJSON(url=opts.endpoint,mfunc=Marshal, logger=logger)
logger.info('Writing events to Sumo: {}'.format(opts.endpoint))
auth=GetAuthentication(opts.authurl,opts.accessid,opts.accesspassword)
if opts.authonly:
sys.stdout.write('Provided authentication token:\n{}\n\n'.format(auth))
sys.exit(0)
# Open up the cache, which remembers per event type what the date/time of the most recent event was
starttime = (datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days = opts.period)).strftime(standarddateformat)
try:
logger.info('Opening cache.....: {}'.format(opts.nolambda))
if opts.nolambda:
logger.info('File cache.....')
cache = CacheFile('lastpandaevents',logger, starttime)
logger.info('Succeeded opening local file cache')
else:
logger.info('AWS parameter cache.....')
cache = CacheAWSParameter(opts.region,logger, starttime)
logger.info('Succeeded opening AWS cache')
except Exception as e:
logger.warning(str(e))
logger.warning('Could not open cache')
return
baseurl='{}aether-endpoint-security/aether-mgmt/'.format(opts.baseurl)
logger.info('Panda URL: {}'.format(baseurl))
# Start extraction from panda portal
ExtractSecurityEvents(baseurl,auth,opts,logger,cache,eventwriter)
# Extract unmanaged devices, only once a day
ExtractUnmanagedDevices(baseurl,auth,opts,logger,cache,eventwriter)
cache.Close()
if __name__ == '__main__':
main()
def lambda_handler(event, context):
main()
return {
'statusCode': 200,
'body': json.dumps('Sumo\'s Panda Lambda returns\n')
}