-
Notifications
You must be signed in to change notification settings - Fork 190
/
__init__.py
1201 lines (1146 loc) · 57.3 KB
/
__init__.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime, os, re, time
from flask import Flask, render_template_string, request, render_template, redirect, jsonify, flash, g
from flask_mail import Mail
from flask_wtf.csrf import CSRFProtect
from flask_wtf.csrf import CSRFError
csrf = CSRFProtect()
from elasticsearch import Elasticsearch
from time import sleep
import json
#Local Scripts
import email
import es
import validators
class ConfigClass(object):
__appSettings__
CSRF_ENABLED = True
def create_app():
""" Flask application factory """
# Setup Flask app and app.config
app = Flask(__name__)
app.config.from_object(__name__+'.ConfigClass')
esService = Elasticsearch()
mail = Mail(app)
try:
recipient=app.config['MAIL_USERNAME']
except:
recipient=None
csrf.init_app(app)
@app.route('/csrf')
def getCsrfToken():
return render_template('csrf.html')
@app.route('/')
def home_page():
#So we can link to kibana
serverIP=re.search(r'^https?://([\w\d\.\-]+)',request.url).groups()
serverIP=serverIP[0]
deviceList=[]
matchAll = {"query": {"match_all": {}}}
allDevices=es.search(esService, matchAll, 'sweet_security', 'devices')
if allDevices is not None:
for host in allDevices['hits']['hits']:
portCount=0
portCountQuery = {"query": {"match_phrase": {"mac": { "query": host['_source']['mac']}}}}
portInfo=es.search(esService, portCountQuery, 'sweet_security', 'ports')
if portInfo is not None:
portCount=len(portInfo['hits']['hits'])
deviceInfo={'hostname': host['_source']['hostname'],
'nickname': host['_source']['nickname'],
'ip': host['_source']['ip4'],
'mac': host['_source']['mac'],
'vendor': host['_source']['vendor'],
'ignore': str(host['_source']['ignore']),
'defaultFwAction': host['_source']['defaultFwAction'],
'firstSeen': host['_source']['firstSeen'],
'lastSeen': host['_source']['lastSeen'],
'openPorts': portCount}
deviceList.append(deviceInfo)
alertQuery = {"query": {"match_phrase": {"addressed": {"query": 0}}}}
allAlerts = es.search(esService, alertQuery, 'sweet_security_alerts', 'alerts')
if allAlerts['hits']['total'] > 0:
flash(u'There are %d new alerts!' % allAlerts['hits']['total'], 'error')
alertCount = allAlerts['hits']['total']
return render_template('index.html', serverIP=serverIP, deviceList=deviceList, alertCount=alertCount)
return render_template('index.html', serverIP=serverIP, deviceList=deviceList)
@app.route('/addDevice', methods=['POST', 'GET'])
def addDevice():
if request.method == 'GET':
return render_template('csrf.html')
hostname = ''
ip = ''
mac = ''
vendor = ''
# ignored='None'
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "key":
apiKey = request.form['key']
if key == "hostname":
hostname = request.form['hostname']
if key == "ip":
ip = request.form['ip']
if key == "macAddress":
mac = request.form['macAddress']
# Convert mac to uppercase alphanumeric
mac = validators.convertMac(mac)
if key == "vendor":
vendor = request.form['vendor']
# if key == "ignored":
# ignored=request.form['ignored']
if len(mac) == 0:
return jsonify(status="Error", reason="Must Supply MAC Address")
if validators.macAddress(mac) == False:
return jsonify(status="Error", reason="Invalid MAC Address")
if len(ip) > 0 and validators.ipAddress(ip) == False:
return jsonify(status="Error", reason="Invalid IP Address")
if len(hostname) > 0 and validators.hostname(hostname) == False:
return jsonify(status="Error", reason="Invalid Hostname")
# if ignored != "None" and validators.ignoreStatus(ignored) == False:
# return jsonify(status="Error", reason="Invalid Ignore Status")
# Get Configuration Settings
matchAll = {"query": {"match_all": {}}}
ssConfig = es.search(esService, matchAll, 'sweet_security', 'configuration')
if ssConfig is None:
print "Error: configuration not found"
return "Error: configuration not found"
elif len(ssConfig['hits']['hits']) == 0:
print "Error: configuration not found"
return "Error: configuration not found"
# configData={'defaultMonitor': 0, 'defaultIsolate': 0, 'defaultFW': 0, 'defaultLogRetention': 0}
# es.write(esService, configData, 'sweet_security', 'configuration')
defaultMonitorAction = 0
defaultIsolateAction = 0
defaultFwAction = 0
# Get Configuration Settings
matchAll = {"query": {"match_all": {}}}
ssConfig = es.search(esService, matchAll, 'sweet_security', 'configuration')
if ssConfig is not None:
for config in ssConfig['hits']['hits']:
defaultMonitorAction = config['_source']['defaultMonitor']
defaultIsolateAction = config['_source']['defaultIsolate']
defaultFwAction = config['_source']['defaultFW']
if defaultFwAction == 1:
defaultFwAction = 'ACCEPT'
else:
defaultFwAction = 'DROP'
newDeviceData = {'hostname': hostname,
'nickname': hostname,
'ip4': ip,
'mac': mac,
'vendor': vendor,
'ignore': defaultMonitorAction,
'defaultFwAction': defaultFwAction,
'isolate': defaultIsolateAction,
'firstSeen': str(int(round(time.time() * 1000))),
'lastSeen': str(int(round(time.time() * 1000)))}
deviceQuery = {"query": {"match_phrase": {"mac": {"query": mac}}}}
deviceInfo = es.search(esService, deviceQuery, 'sweet_security', 'devices')
serverIP = re.search(r'^https?://([\w\d\.\-]+)', request.url).groups()
serverIP = serverIP[0]
if deviceInfo is None:
# First ever device...
es.write(esService, newDeviceData, 'sweet_security', 'devices')
emailBody = render_template('emails/addDevice.html', deviceInfo=newDeviceData, serverIP=serverIP)
email.emailUser(mail, "New Device Found", recipient, emailBody)
return jsonify(status="Success", reason="Device added")
elif len(deviceInfo['hits']['hits']) == 0:
# New Device
es.write(esService, newDeviceData, 'sweet_security', 'devices')
emailBody = render_template('emails/addDevice.html', deviceInfo=newDeviceData, serverIP=serverIP)
email.emailUser(mail, "New Device Found", recipient, emailBody)
return jsonify(status="Success", reason="Device added")
elif len(deviceInfo['hits']['hits']) == 1:
if deviceInfo['hits']['hits'][0]['_source']['hostname'] != newDeviceData['hostname']:
body = {'doc': {'hostname': newDeviceData['hostname']}}
es.update(esService, body, 'sweet_security', 'devices', deviceInfo['hits']['hits'][0]['_id'])
# If user hasn't updated the nickname, update that too
if deviceInfo['hits']['hits'][0]['_source']['hostname'] == deviceInfo['hits']['hits'][0]['_source'][
'nickname']:
body = {'doc': {'nickname': newDeviceData['hostname']}}
es.update(esService, body, 'sweet_security', 'devices', deviceInfo['hits']['hits'][0]['_id'])
if deviceInfo['hits']['hits'][0]['_source']['ip4'] != newDeviceData['ip4']:
body = {'doc': {'ip4': newDeviceData['ip4']}}
es.update(esService, body, 'sweet_security', 'devices', deviceInfo['hits']['hits'][0]['_id'])
body = {'doc': {'lastSeen': str(int(round(time.time() * 1000)))}}
es.update(esService, body, 'sweet_security', 'devices', deviceInfo['hits']['hits'][0]['_id'])
return jsonify(status="Success", reason="Device updated")
else:
es.consolidate(mac, esService, 'devices')
return jsonify(status="Warning", reason="Multiple devices consolidated. Next update will update device")
@app.route('/renameDevice', methods=['POST'])
def renameDevice():
mac=''
nickName=''
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "macAddress":
mac=request.form['macAddress']
if key == "nickName":
nickName=request.form['nickName']
if len(mac)==0:
flash(u'MAC Address Missing For Device', 'error')
return redirect('/')
if len(nickName)==0:
flash(u'New Name Must Actually Have Words', 'error')
return redirect('/')
deviceQuery = {"query": {"match_phrase": {"mac": { "query": mac }}}}
deviceInfo=es.search(esService, deviceQuery, 'sweet_security', 'devices')
#deviceInfo = query_db('SELECT * FROM hosts where mac = ?',[mac],one=True)
if deviceInfo is None:
flash(u'Error renaming device, unknown device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 0:
flash(u'Error renaming device, unknown device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 1:
import cgi
escaped = cgi.escape(nickName)
for hit in deviceInfo['hits']['hits']:
body = {'doc' : {'nickname': escaped}}
es.update(esService, body, 'sweet_security', 'devices', hit['_id'])
#Have to delay the response so the refreshed page shows the new name
sleep(1)
flash(u'Device renamed', 'success')
return redirect('/')
else:
es.consolidate(mac,esService,'devices')
sleep(1)
deviceInfo=es.search(esService, deviceQuery, 'sweet_security', 'devices')
import cgi
escaped = cgi.escape(nickName)
for hit in deviceInfo['hits']['hits']:
body = {'doc' : {'nickname': escaped}}
es.update(esService, body, 'sweet_security', 'devices', hit['_id'])
#Have to delay the response so the refreshed page shows the new name
sleep(1)
flash(u'Device renamed', 'success')
return redirect('/')
@app.route('/ignoreDevice', methods=['POST'])
def ignoreDevice():
mac=''
ignored=''
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "macAddress":
mac=request.form['macAddress']
if key == "ignored":
ignored=request.form['ignored']
if len(mac)==0:
flash(u'MAC Address Missing For Device', 'error')
return redirect('/')
if len(ignored)==0:
flash(u'Missing ignore flag', 'error')
return redirect('/')
deviceQuery = {"query": {"match_phrase": {"mac": { "query": mac }}}}
deviceInfo=es.search(esService, deviceQuery, 'sweet_security', 'devices')
if deviceInfo is None:
flash(u'Error finding device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 0:
flash(u'Error finding device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 1:
serverIP = re.search(r'^https?://([\w\d\.\-]+)', request.url).groups()
serverIP = serverIP[0]
for hit in deviceInfo['hits']['hits']:
body = {'doc' : {'ignore': ignored}}
es.update(esService, body, 'sweet_security', 'devices', hit['_id'])
emailDeviceInfo = {'hostname': deviceInfo['hits']['hits'][0]['_source']['hostname'],
'nickname': deviceInfo['hits']['hits'][0]['_source']['nickname'],
'ip': deviceInfo['hits']['hits'][0]['_source']['ip4'],
'mac': deviceInfo['hits']['hits'][0]['_source']['mac'],
'vendor': deviceInfo['hits']['hits'][0]['_source']['vendor'],
'ignore': ignored
}
emailBody = render_template('emails/ignoreDevice.html', deviceInfo=emailDeviceInfo, serverIP=serverIP)
if int(ignored)==0:
response=email.emailUser(mail,"Device Being Monitored",recipient,emailBody)
else:
response=email.emailUser(mail,"Device Bypassed",recipient,emailBody)
#Have to delay the response so the refreshed page shows the new name
sleep(1)
flash(u'Device modified', 'success')
return redirect('/')
else:
serverIP = re.search(r'^https?://([\w\d\.\-]+)', request.url).groups()
serverIP = serverIP[0]
es.consolidate(mac,esService,'devices')
sleep(1)
deviceInfo=es.search(esService, deviceQuery, 'sweet_security', 'devices')
for hit in deviceInfo['hits']['hits']:
body = {'doc' : {'ignore': ignored}}
es.update(esService, body, 'sweet_security', 'devices', hit['_id'])
emailDeviceInfo = {'hostname': deviceInfo['hits']['hits'][0]['_source']['hostname'],
'nickname': deviceInfo['hits']['hits'][0]['_source']['nickname'],
'ip': deviceInfo['hits']['hits'][0]['_source']['ip4'],
'mac': deviceInfo['hits']['hits'][0]['_source']['mac'],
'vendor': deviceInfo['hits']['hits'][0]['_source']['vendor'],
'ignore': ignored
}
emailBody = render_template('emails/ignoreDevice.html', deviceInfo=emailDeviceInfo, serverIP=serverIP)
if int(ignored)==0:
email.emailUser(mail,"Device Being Monitored",recipient,emailBody)
else:
email.emailUser(mail,"Device Bypassed",recipient,emailBody)
#Have to delay the response so the refreshed page shows the new name
sleep(1)
flash(u'Device modified', 'success')
return redirect('/')
@app.route('/isolateDevice', methods=['POST'])
def isolateDevice():
mac = ''
isolate = ''
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "macAddress":
mac = request.form['macAddress']
if key == "isolate":
isolate = request.form['isolate']
if len(mac) == 0:
flash(u'MAC Address Missing For Device', 'error')
return redirect('/')
if len(isolate) == 0:
flash(u'Missing isolate flag', 'error')
return redirect('/')
deviceQuery = {"query": {"match_phrase": {"mac": {"query": mac}}}}
deviceInfo = es.search(esService, deviceQuery, 'sweet_security', 'devices')
if deviceInfo is None:
flash(u'Error finding device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 0:
flash(u'Error finding device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 1:
for hit in deviceInfo['hits']['hits']:
body = {'doc': {'isolate': isolate}}
es.update(esService, body, 'sweet_security', 'devices', hit['_id'])
sleep(1)
flash(u'Device modified', 'success')
return redirect('/')
@app.route('/deleteDevice', methods=['POST'])
def deleteDevice():
mac=''
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "macAddress":
mac=request.form['macAddress']
if len(mac)==0:
flash(u'MAC Address Missing For Device', 'error')
return redirect('/')
deviceQuery = {"query": {"match_phrase": {"mac": { "query": mac }}}}
deviceInfo=es.search(esService, deviceQuery, 'sweet_security', 'devices')
if deviceInfo is None:
flash(u'Error finding device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 0:
flash(u'Error finding device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) > 0:
for hit in deviceInfo['hits']['hits']:
es.delete(esService, 'sweet_security', 'devices', hit['_id'])
#Have to delay the response so the refreshed page shows the new name
sleep(1)
flash(u'Device deleted', 'success')
return redirect('/')
@app.route('/device/<mac>')
def updateDevice(mac):
serverIP = re.search(r'^https?://([\w\d\.\-]+)', request.url).groups()
serverIP = serverIP[0]
deviceQuery = {"query": {"match_phrase": {"mac": { "query": mac }}}}
deviceInfo=es.search(esService, deviceQuery, 'sweet_security', 'devices')
if deviceInfo is None:
flash(u'Error finding device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 0:
flash(u'Error finding device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 1:
for host in deviceInfo['hits']['hits']:
portList=[]
fwList=[]
firstSeen = float(host['_source']['firstSeen']) / 1000.0
lastSeen = float(host['_source']['lastSeen']) / 1000.0
deviceInfo={'hostname': host['_source']['hostname'],
'nickname': host['_source']['nickname'],
'ip': host['_source']['ip4'],
'mac': host['_source']['mac'],
'vendor': host['_source']['vendor'],
'ignore': str(host['_source']['ignore']),
'isolate': str(host['_source']['isolate']),
'defaultFwAction': host['_source']['defaultFwAction'],
'firstSeen': datetime.datetime.fromtimestamp(firstSeen).strftime('%Y-%m-%d %H:%M:%S'),
'lastSeen': datetime.datetime.fromtimestamp(lastSeen).strftime('%Y-%m-%d %H:%M:%S')}
#portCountQuery = {"sort":[{ "port" : {"order" : "asc"}}],"query": {"match_phrase": {"mac": { "query": host['_source']['mac']}}}}
portCountQuery = {"query": {"match_phrase": {"mac": { "query": host['_source']['mac']}}}}
portInfo=es.search(esService, portCountQuery, 'sweet_security', 'ports')
if portInfo is not None:
for port in portInfo['hits']['hits']:
portInfoTmp = port['_source']
time = datetime.datetime.fromtimestamp(float(port['_source']['lastSeen']) / 1000.)
time = time.strftime('%Y-%m-%d %H:%M')
portInfoTmp['lastSeen'] = time
portInfoTmp['port'] = int(portInfoTmp['port'])
portList.append(portInfoTmp)
sortedPortList = sorted(portList, key=lambda k: k['port'])
deviceInfo['portList']=sortedPortList
lastPortScanQuery = {"sort": [{"@timestamp": {"order": "desc"}}], "query": {"bool": { "must": [{"match": {"ipAddress": host['_source']['ip4']}}, {"match": {"action": "Port scanning"}}]}}}
lastPortScanInfo = es.search(esService, lastPortScanQuery, 'logstash-*', 'logs', 1)
if lastPortScanInfo is not None:
if len(lastPortScanInfo['hits']['hits']) > 0:
lastPortScan = datetime.datetime.strptime(lastPortScanInfo['hits']['hits'][0]['_source']['@timestamp'], '%Y-%m-%dT%H:%M:%S.%fZ')
lastPortScan = lastPortScan.strftime('%Y-%m-%d %H:%M')
else:
lastPortScan = 'Device has not been port scanned'
else:
lastPortScan = 'Device has not been port scanned'
deviceInfo['lastPortScan'] = lastPortScan
fwQuery = {"query": {"match_phrase": {"mac": { "query": host['_source']['mac']}}}}
fwData=es.search(esService, fwQuery, 'sweet_security', 'firewallProfiles')
if fwData is not None:
for entry in fwData['hits']['hits']:
fwList.append(entry['_source'])
deviceInfo['fwList']=fwList
deviceAlertCount = 0
alertQuery = {
"query": {
"bool": {
"must": [
{"match_phrase": {"addressed": 0}},
{"match_phrase": {"mac": deviceInfo['mac']}}
]
}
}
}
allAlerts = es.search(esService, alertQuery, 'sweet_security_alerts', 'alerts')
deviceAlertCount = allAlerts['hits']['total']
for ssAlert in allAlerts['hits']['hits']:
firstSeen = float(ssAlert['_source']['firstSeen']) / 1000.0
humanDate = datetime.datetime.fromtimestamp(firstSeen).strftime('%Y-%m-%d %H:%M:%S')
ssAlert['_source']['firstSeen'] = humanDate
baseline = []
knownHostQuery = {"query": {"match_phrase": {"mac": {"query": deviceInfo['mac']}}}}
knownHostData = es.search(esService, knownHostQuery, 'tardis', 'known_hosts')
for device in knownHostData['hits']['hits']:
baseline.append({'type': 'ip', 'value': device['_source']['ip']})
knownDnsQuery = {"query": {"match_phrase": {"mac": {"query": mac}}}}
knownDnsData = es.search(esService, knownDnsQuery, 'tardis', 'known_dnsqueries')
for query in knownDnsData['hits']['hits']:
baseline.append({'type': 'dns', 'value': query['_source']['query']})
knownHostQuery = {"query": {"match_phrase": {"mac": {"query": mac}}}}
knownHostData = es.search(esService, knownHostQuery, 'tardis', 'known_websites')
for url in knownHostData['hits']['hits']:
baseline.append({'type': 'website', 'value': url['_source']['server_name']})
if deviceAlertCount > 0:
flash(u'There are %d alerts for this device' % deviceAlertCount, 'error')
return render_template('device.html', serverIP=serverIP, deviceInfo=deviceInfo,
alertCount=deviceAlertCount,alerts=allAlerts['hits']['hits'], baseline=baseline)
else:
return render_template('device.html', serverIP=serverIP, deviceInfo=deviceInfo, baseline=baseline)
else:
#This happens when the web component is still booting up and the ES index hasn't initialized
#Sometimes we get two devices, we'll delete the old one and let the sensor send info on it's next update
es.consolidate(mac,esService,'devices')
#sleep for one second, otherwise ES doesn't have enough time to delete the duplicated record
sleep(1)
return redirect('/device/%s' % mac)
@app.route('/updateFW', methods=['POST'])
def updateFW():
mac=''
fwDest=''
fwAction=''
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "macAddress":
mac=request.form['macAddress']
mac=validators.convertMac(mac)
if key == "destination":
fwDest=request.form['destination']
if key == "action":
fwAction=request.form['action']
if not validators.macAddress(mac):
print "invalid mac"
return redirect('/')
if not validators.url(fwDest):
#If it's *, then we'll update the defaultFwAction for the device, bypass validation
if fwDest != "*":
print "invalid destination"
return redirect('/')
if len(fwAction) == 0:
print "invalid action, no action given"
return redirect('/')
if fwAction == "true":
fwAction="ACCEPT"
elif fwAction == "false":
fwAction="DROP"
else:
print "unknown action"
return redirect('/')
if fwDest == '*':
deviceQuery = {"query": {"match_phrase": {"mac": { "query": mac }}}}
deviceInfo=es.search(esService, deviceQuery, 'sweet_security', 'devices')
if deviceInfo is None:
flash(u'Unknown device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 0:
flash(u'Unknown device', 'error')
return redirect('/')
elif len(deviceInfo['hits']['hits']) == 1:
for hit in deviceInfo['hits']['hits']:
body = {'doc' : {'defaultFwAction': fwAction}}
es.update(esService, body, 'sweet_security', 'devices', hit['_id'])
#Have to delay the response so the refreshed page shows the new name
sleep(1)
flash(u'Device updated', 'success')
return redirect('/')
else:
es.consolidate(mac,esService,'devices')
sleep(1)
deviceInfo=es.search(esService, deviceQuery, 'sweet_security', 'devices')
for hit in deviceInfo['hits']['hits']:
body = {'doc' : {'defaultFwAction': fwAction}}
es.update(esService, body, 'sweet_security', 'devices', hit['_id'])
#Have to delay the response so the refreshed page shows the new name
sleep(1)
flash(u'Device updated', 'success')
else:
fwData={'mac': mac,
'destination': fwDest,
'action': fwAction}
fwQuery={"query":{"bool":{"must":[{"match":{"mac": mac }},{ "match": { "destination": fwDest }}]}}}
exists=es.search(esService,fwQuery,'sweet_security','firewallProfiles')
if len(exists['hits']['hits']) == 0:
es.write(esService, fwData, 'sweet_security', 'firewallProfiles')
sleep(1)
return "true"
@app.route('/deleteFW', methods=['POST'])
def deleteFW():
mac=''
fwDest=''
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "macAddress":
mac=request.form['macAddress']
mac=validators.convertMac(mac)
if key == "destination":
fwDest=request.form['destination']
if not validators.macAddress(mac):
print "invalid mac"
return redirect('/')
if not validators.url(fwDest):
print "invalid destination"
return redirect('/')
fwQuery={"query":{"bool":{"must":[{"match":{"mac": mac }},{ "match": { "destination": fwDest }}]}}}
exists=es.search(esService,fwQuery,'sweet_security','firewallProfiles')
if len(exists['hits']['hits']) > 0:
es.delete(esService, 'sweet_security', 'firewallProfiles', exists['hits']['hits'][0]['_id'])
sleep(1)
return "true"
@app.route('/addPort', methods=['POST'])
def addPort():
mac=''
port=''
protocol=''
name=''
product=''
version=''
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "macAddress":
mac=request.form['macAddress']
mac=validators.convertMac(mac)
if key == "port":
port=request.form['port']
if key == "protocol":
protocol=request.form['protocol']
if key == "name":
name=request.form['name']
if key == "version":
version=request.form['version']
if key == "product":
product=request.form['product']
if len(mac)==0:
return jsonify (status='error',reason='missing mac address')
if len(port)==0:
return jsonify (status='error',reason='missing port number')
if len(protocol)==0:
return jsonify (status='error',reason='missing protocol')
if len(name)==0:
return jsonify (status='error',reason='missing port name')
if len(version)==0:
version='Unknown'
if len(product)==0:
product='Unknown'
portQuery = {"query":{"bool":{"must":[{"match":{"mac": mac }},{ "match": { "port": port }}]}}}
portInfo=es.search(esService, portQuery, 'sweet_security', 'ports')
if portInfo is None:
portData={'port': (port),
'protocol': protocol,
'name': name,
'mac': mac,
'product': product,
'version': version,
'lastSeen': str(int(round(time.time() * 1000)))}
es.write(esService, portData, 'sweet_security', 'ports')
return jsonify(status='success',reason='Port added')
elif len(portInfo['hits']['hits']) == 0:
portData={'port': (port),
'protocol': protocol,
'name': name,
'mac': mac,
'product': product,
'version': version,
'lastSeen': str(int(round(time.time() * 1000)))}
es.write(esService, portData, 'sweet_security', 'ports')
return jsonify(status='success',reason='Port added')
elif len(portInfo['hits']['hits']) == 1:
portData={'port': (port),
'protocol': protocol,
'name': name,
'mac': mac,
'product': product,
'version': version}
previousPortData=portInfo['hits']['hits'][0]['_source']
changeName=False
changeProduct=False
changeVersion=False
if portData['name'] != previousPortData['name']:
body = {'doc' : {'name': portData['name']}}
es.update(esService, body, 'sweet_security', 'ports', portInfo['hits']['hits'][0]['_id'])
if portData['product'] != previousPortData['product']:
body = {'doc' : {'product': portData['product']}}
es.update(esService, body, 'sweet_security', 'ports', portInfo['hits']['hits'][0]['_id'])
if portData['version'] != previousPortData['version']:
body = {'doc' : {'version': portData['version']}}
es.update(esService, body, 'sweet_security', 'ports', portInfo['hits']['hits'][0]['_id'])
body = {'doc': {'lastSeen': str(int(round(time.time() * 1000)))}}
es.update(esService, body, 'sweet_security', 'ports', portInfo['hits']['hits'][0]['_id'])
return jsonify(status='success',reason='port information updated')
else:
return jsonify(status='error',reason='duplicate ports found')
return jsonify(status='error', reason="you should not be here")
@app.route('/getConfig', methods=['GET','POST'])
def getConfig():
#Get config for spoofing devices
deviceList=[]
matchAll = {"query": {"match_all": {}}}
allDevices=es.search(esService, matchAll, 'sweet_security', 'devices')
if allDevices is not None:
for host in allDevices['hits']['hits']:
fwQuery = {"query": {"match_phrase": {"mac": { "query": host['_source']['mac']}}}}
fwData=es.search(esService, fwQuery, 'sweet_security', 'firewallProfiles')
fwList=[]
if len(fwData['hits']['hits']) > 0:
for entry in fwData['hits']['hits']:
fwInfo={'action': entry['_source']['action'],'destination': entry['_source']['destination']}
fwList.append(fwInfo)
if host['_source']['defaultFwAction'] == 'ACCEPT':
fwInfo={'action': 'ACCEPT','destination': '*'}
else:
fwInfo={'action': 'DROP','destination': '*'}
fwList.append(fwInfo)
deviceInfo={'hostname': host['_source']['hostname'],
'nickname': host['_source']['nickname'],
'ip': host['_source']['ip4'],
'mac': host['_source']['mac'],
'vendor': host['_source']['vendor'],
'ignore': str(host['_source']['ignore']),
'firewall': fwList,
'isolate': str(host['_source']['isolate']),
'firstSeen': host['_source']['firstSeen'],
'lastSeen': host['_source']['lastSeen']}
deviceList.append(deviceInfo)
return jsonify(deviceList=deviceList)
@app.route('/sensorHealth', methods=['POST'])
def sensorHealth():
sensorMac = ''
sensorHostname = ''
broHealth = ''
logstashHealth = ''
diskUsage = 0
memConsumed = 0
memAvailable = 0
memPercent = 0
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "sensorMac":
sensorMac = request.form['sensorMac']
if key == "sensorName":
sensorName = request.form['sensorName']
if key == "broHealth":
broHealth = request.form['broHealth']
if key == "logstashHealth":
logstashHealth = request.form['logstashHealth']
if key == "diskUsage":
diskUsage = request.form['diskUsage']
if key == "memConsumed":
memConsumed = request.form['memConsumed']
if key == "memAvailable":
memAvailable = request.form['memAvailable']
if key == "memPercent":
memPercent = request.form['memPercent']
if len(sensorMac) == 0:
print "unknown sensor mac"
flash(u'Unknown Sensor MAC', 'error')
return redirect('/')
if len(sensorName) == 0:
print "unknown sensor"
flash(u'Unknown Sensor Name', 'error')
return redirect('/settings')
if len(broHealth) == 0:
print "unknown bro health"
flash(u'Unknown Bro Health', 'error')
return redirect('/settings')
if len(logstashHealth) == 0:
print "unknown logstash health"
flash(u'Unknown Logstash Health', 'error')
return redirect('/settings')
if len(diskUsage) == 0:
print "unknown diskUsage"
flash(u'Unknown Disk Usage', 'error')
return redirect('/settings')
if len(memConsumed) == 0:
print "unknown memConsumed"
flash(u'Unknown Memory Consumed', 'error')
return redirect('/settings')
if len(memAvailable) == 0:
print "unknown memAvailable"
flash(u'Unknown Memory Available', 'error')
return redirect('/settings')
if len(memPercent) == 0:
print "unknown memPercent"
flash(u'Unknown Memory Percent', 'error')
return redirect('/settings')
healthInfo = {'mac': sensorMac,
'sensorName': sensorName,
'broHealth': broHealth,
'logstashHealth': logstashHealth,
'diskUsage': diskUsage,
'memAvailable': memAvailable,
'memConsumed': memConsumed,
'memPercent': memPercent,
'firstSeen': str(int(round(time.time() * 1000))),
'lastSeen': str(int(round(time.time() * 1000)))}
sensorStatus = 'Unknown'
sensorQuery = {"query": {"match_phrase": {"mac": {"query": sensorMac}}}}
sensorInfo = es.search(esService, sensorQuery, 'sweet_security', 'sensors')
if sensorInfo is None:
# First Ever Device
sensorStatus = 'First Ever'
es.write(esService, healthInfo, 'sweet_security', 'sensors')
# emailBody = render_template('emails/addDevice.html', deviceInfo=newDeviceData, serverIP=serverIP)
# email.emailUser(mail,"New Device Found",recipient,emailBody)
elif len(sensorInfo['hits']['hits']) == 0:
# newDevice
sensorStatus = 'New Sensor'
es.write(esService, healthInfo, 'sweet_security', 'sensors')
# emailBody = render_template('emails/addDevice.html', deviceInfo=newDeviceData, serverIP=serverIP)
# email.emailUser(mail,"New Device Found",recipient,emailBody)
elif len(sensorInfo['hits']['hits']) == 1:
# update Sensor
body = {'doc': {'lastSeen': str(int(round(time.time() * 1000))),
'sensorName': sensorName,
'broHealth': broHealth,
'logstashHealth': logstashHealth,
'diskUsage': diskUsage,
'memAvailable': memAvailable,
'memConsumed': memConsumed,
'memPercent': memPercent
}
}
es.update(esService, body, 'sweet_security', 'sensors', sensorInfo['hits']['hits'][0]['_id'])
sensorStatus = 'Update Sensor'
else:
# need to consolidate then update
sensorStatus = 'Consolidate Sensors'
es.consolidate(sensorMac, esService, 'sensors')
return jsonify(healthInfo=healthInfo, sensorStatus=sensorStatus)
@app.route('/settings')
def settings():
# So we can link to kibana
serverIP = re.search(r'^https?://([\w\d\.\-]+)', request.url).groups()
serverIP = serverIP[0]
elasticHealth = os.popen('service elasticsearch status').read()
for line in elasticHealth.splitlines():
if line.lstrip().startswith('Active: '):
if line.lstrip().startswith('Active: active (running)'):
elasticHealth = 'Started'
else:
elasticHealth = 'Stopped'
kibanaHealth = os.popen('service kibana status').read()
for line in kibanaHealth.splitlines():
if line.lstrip().startswith('Active: '):
if line.lstrip().startswith('Active: active (running)'):
kibanaHealth = 'Started'
else:
kibanaHealth = 'Stopped'
diskUsage = 0
diskUsageCommand = os.popen('df -k "/"').read()
for line in diskUsageCommand.splitlines():
line = line.split()
if line[0] != 'Filesystem':
diskUsage = int(line[4][:-1])
memUsage = {'available': 0, 'consumed': 0, 'percentUsed': 0}
memInfo = os.popen('free -t -m').read()
for line in memInfo.splitlines():
if line.rstrip().startswith('Mem:'):
memUsage['available'] = line.split()[1]
memUsage['consumed'] = line.split()[2]
memUsage['percentUsed'] = int(round((float(line.split()[2]) / float(line.split()[1])) * 100, 0))
ssServerStatus = 'Unknown'
serverStatus = os.popen('service sweetsecurity_server status').read()
for line in serverStatus.splitlines():
if line.lstrip().startswith('Active: '):
if line.lstrip().startswith('Active: active (running)'):
ssServerStatus = 'Started'
else:
ssServerStatus = 'Stopped'
defaultMonitor = 0
defaultIsolate = 0
defaultFW = 0
defaultLogRetention = 0
# Get Configuration Settings
matchAll = {"query": {"match_all": {}}}
ssConfig = es.search(esService, matchAll, 'sweet_security', 'configuration')
if ssConfig is not None:
for config in ssConfig['hits']['hits']:
defaultMonitor = config['_source']['defaultMonitor']
defaultIsolate = config['_source']['defaultIsolate']
defaultFW = config['_source']['defaultFW']
defaultLogRetention = config['_source']['defaultLogRetention']
# Get the system status info for every sensor sending data
sensorInfo = []
matchAll = {"query": {"match_all": {}}}
allSensors = es.search(esService, matchAll, 'sweet_security', 'sensors')
if allSensors is not None:
for sensor in allSensors['hits']['hits']:
lastSeenTime = int(sensor['_source']['lastSeen']) / 1000
timeSince = (int(datetime.datetime.now().strftime("%s")) * 1000) - int(sensor['_source']['lastSeen'])
systemInfo = {
'time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(lastSeenTime)),
'timeSince': int(timeSince / 1000) / 60,
'sensorName': sensor['_source']['sensorName'],
'sensorMac': sensor['_source']['mac'],
'logstash': sensor['_source']['logstashHealth'],
'broStatus': sensor['_source']['broHealth'],
'diskUsage': int(sensor['_source']['diskUsage']),
'memInstalled': int(sensor['_source']['memAvailable']),
'memConsumed': int(sensor['_source']['memConsumed']),
'memPercent': int(sensor['_source']['memPercent'])
}
sensorInfo.append(systemInfo)
alertQuery = {"query": {"match_phrase": {"addressed": {"query": 0}}}}
allAlerts = es.search(esService, alertQuery, 'sweet_security_alerts', 'alerts')
if allAlerts['hits']['total'] > 0:
flash(u'There are %d new alerts!' % allAlerts['hits']['total'], 'error')
alertCount = allAlerts['hits']['total']
return render_template('settings.html', serverIP=serverIP, esHealth=elasticHealth, kHealth=kibanaHealth,
diskUsage=diskUsage, memUsage=memUsage, sensorInfo=sensorInfo, defaultFW=defaultFW,
defaultIsolate=defaultIsolate, defaultMonitor=defaultMonitor,
defaultLogRetention=defaultLogRetention, alertCount=alertCount,
ssServerStatus=ssServerStatus)
else:
return render_template('settings.html', serverIP=serverIP, esHealth=elasticHealth, kHealth=kibanaHealth,
diskUsage=diskUsage, memUsage=memUsage, sensorInfo=sensorInfo, defaultFW=defaultFW,
defaultIsolate=defaultIsolate, defaultMonitor=defaultMonitor,
defaultLogRetention=defaultLogRetention, ssServerStatus=ssServerStatus)
@app.route('/settings/modify', methods=['POST'])
def settingsModify():
settingID=''
setting=''
value=''
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "setting":
setting=request.form['setting']
if key == "value":
value=request.form['value']
if setting == 'monitor':
matchAll = {"query": {"match_all": {}}}
ssConfig = es.search(esService, matchAll, 'sweet_security', 'configuration')
if ssConfig is not None:
for config in ssConfig['hits']['hits']:
settingID=config['_id']
if value == 'Yes':
body = {'doc' : {'defaultMonitor': 0}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
elif value == 'No':
body = {'doc' : {'defaultMonitor': 1}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
else:
return "Unknown value"
sleep(1)
return "Default monitor changed"
if setting == 'isolate':
matchAll = {"query": {"match_all": {}}}
ssConfig = es.search(esService, matchAll, 'sweet_security', 'configuration')
if ssConfig is not None:
for config in ssConfig['hits']['hits']:
settingID=config['_id']
if value == 'Yes':
body = {'doc' : {'defaultIsolate': 1}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
elif value == 'No':
body = {'doc' : {'defaultIsolate': 0}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
else:
return "Unknown value"
sleep(1)
return "Default isolate changed"
if setting == 'fw':
matchAll = {"query": {"match_all": {}}}
ssConfig = es.search(esService, matchAll, 'sweet_security', 'configuration')
if ssConfig is not None:
for config in ssConfig['hits']['hits']:
settingID=config['_id']
if value == 'Allow':
body = {'doc' : {'defaultFW': 1}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
elif value == 'Block':
body = {'doc' : {'defaultFW': 0}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
else:
return "Unknown value"
sleep(1)
return "Default fw changed"
if setting == 'logRetention':
matchAll = {"query": {"match_all": {}}}
ssConfig = es.search(esService, matchAll, 'sweet_security', 'configuration')
if ssConfig is not None:
for config in ssConfig['hits']['hits']:
settingID=config['_id']
if value == '7':
body = {'doc' : {'defaultLogRetention': 7}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
elif value == '14':
body = {'doc' : {'defaultLogRetention': 14}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
elif value == '30':
body = {'doc' : {'defaultLogRetention': 30}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
elif value == '90':
body = {'doc' : {'defaultLogRetention': 90}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
elif value == '180':
body = {'doc' : {'defaultLogRetention': 180}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
elif value == '0':
body = {'doc' : {'defaultLogRetention': 0}}
es.update(esService, body, 'sweet_security', 'configuration', settingID)
else:
return "Unknown value"
sleep(1)
else:
return "Unknown setting"
return "Settings Modified"
@app.route('/settings/manageService', methods=['POST'])
def settingsManageService():
serviceName=''
action=''
f = request.form
for key in f.keys():
for value in f.getlist(key):
if key == "serviceName":
serviceName=request.form['serviceName']
if key == "action":
action=request.form['action']