-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkippo_generate_graphs.py
677 lines (623 loc) · 25.1 KB
/
kippo_generate_graphs.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
import kippo_update_config as conf
import matplotlib.pyplot as plt
import pymysql
import sys
import pwd
import grp
import os
def noGraphGenerated(name):
try:
from PIL import Image, ImageDraw, ImageFont
drawWidth = 8 * conf.KIPPO_GRAPH_RES
drawHeight = 6 * conf.KIPPO_GRAPH_RES
img = Image.new('RGB', (drawWidth, drawHeight), (255, 255, 255))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(conf.KIPPO_FONT, conf.KIPPO_FONT_SIZE)
textWidth, textHeight = draw.textsize('No Data Available', font)
draw.text(((drawWidth/2) - (textWidth/2), (drawHeight/2) - (textHeight/2)), "No Data Available", (0, 0, 0), font=font)
img.save(conf.KIPPO_GRAPH_PATH + name + '_th' + '.' + conf.KIPPO_GRAPH_FORMAT)
img.save(conf.KIPPO_GRAPH_PATH + name + '.' + conf.KIPPO_GRAPH_FORMAT)
os.chown(conf.KIPPO_GRAPH_PATH + name + '.' + conf.KIPPO_GRAPH_FORMAT, pwd.getpwnam("www-data").pw_uid, grp.getgrnam("www-data").gr_gid)
os.chown(conf.KIPPO_GRAPH_PATH + name + '_th' + '.' + conf.KIPPO_GRAPH_FORMAT, pwd.getpwnam("www-data").pw_uid, grp.getgrnam("www-data").gr_gid)
except:
print 'No enough data to generate the graph %s but no file generated because PIL Library was not found' % name
def getMySQLKippoData():
"""Function to connect to the MySQL db and retrieve the kippo databases already created"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SHOW DATABASES"
cur.execute(sql)
kippoDbs = []
for (db,) in cur:
if db.startswith('kippo'):
kippoDbs.append(db)
cur.close()
conn.close()
return kippoDbs
def autolabel(rects):
"""Function to generate the data labels in the Bar Charts"""
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width()/2., height*1.05, '%d' % int(height), ha='center', va='bottom', rotation=50)
def generateBarGraph(x, y, title, graphName):
"""Function to generate the Bar Graphs with matplotlib"""
plt.style.use(conf.KIPPO_STYLE)
plt.title(title)
rects = plt.bar(range(len(x)), y, align='center')
plt.xticks(range(len(x)), x, rotation=50, size='small', ha='right')
plt.axes().set_ylim([0, max(y)*1.2])
plt.axes().yaxis.grid(True, lw=1, ls='--', c='.75')
autolabel(rects)
#plt.show()
plt.savefig(conf.KIPPO_GRAPH_PATH + graphName + '.' + conf.KIPPO_GRAPH_FORMAT,
dpi=conf.KIPPO_GRAPH_RES,
format=conf.KIPPO_GRAPH_FORMAT,
bbox_inches='tight')
plt.savefig(conf.KIPPO_GRAPH_PATH + graphName + '_th' + '.' + conf.KIPPO_GRAPH_FORMAT,
dpi=conf.KIPPO_GRAPH_RES/conf.KIPPO_THUMB_FACTOR,
format=conf.KIPPO_GRAPH_FORMAT,
bbox_inches='tight')
os.chown(conf.KIPPO_GRAPH_PATH + graphName + '_th' + '.' + conf.KIPPO_GRAPH_FORMAT, pwd.getpwnam("www-data").pw_uid, grp.getgrnam("www-data").gr_gid)
os.chown(conf.KIPPO_GRAPH_PATH + graphName + '.' + conf.KIPPO_GRAPH_FORMAT, pwd.getpwnam("www-data").pw_uid, grp.getgrnam("www-data").gr_gid)
plt.clf()
def generateLineGraph(x, y, title, graphName):
"""Function to generate the Line Graphs with matplotlib"""
plt.style.use(conf.KIPPO_STYLE)
plt.title(title)
plt.plot(range(len(x)), y, marker='o', markersize=5)
plt.xticks(range(len(x)), x, rotation=50, size='small', ha='right')
plt.axes().set_ylim([0, max(y)*1.2])
plt.axes().yaxis.grid(True, lw=1, ls='--', c='.75')
plt.axes().xaxis.grid(True, lw=1, ls='--', c='.75')
#plt.show()
plt.savefig(conf.KIPPO_GRAPH_PATH + graphName + '.' + conf.KIPPO_GRAPH_FORMAT,
dpi=conf.KIPPO_GRAPH_RES,
format=conf.KIPPO_GRAPH_FORMAT,
bbox_inches='tight')
plt.savefig(conf.KIPPO_GRAPH_PATH + graphName + '_th' + '.' + conf.KIPPO_GRAPH_FORMAT,
dpi=conf.KIPPO_GRAPH_RES/conf.KIPPO_THUMB_FACTOR,
format=conf.KIPPO_GRAPH_FORMAT,
bbox_inches='tight')
os.chown(conf.KIPPO_GRAPH_PATH + graphName + '_th' + '.' + conf.KIPPO_GRAPH_FORMAT, pwd.getpwnam("www-data").pw_uid, grp.getgrnam("www-data").gr_gid)
os.chown(conf.KIPPO_GRAPH_PATH + graphName + '.' + conf.KIPPO_GRAPH_FORMAT, pwd.getpwnam("www-data").pw_uid, grp.getgrnam("www-data").gr_gid)
plt.clf()
def generateTop10Passwords(database):
"""Generate the Top 10 Password from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT password, COUNT(password) " \
"FROM auth " \
"WHERE password <> '' " \
"GROUP BY password " \
"ORDER BY COUNT(password) " \
"DESC LIMIT 10"
cur.execute(sql)
if cur.rowcount != 0:
password = []
countPassword = []
for (pwd, countPwd) in cur.fetchall():
password.append(pwd)
countPassword.append(countPwd)
cur.close()
conn.close()
title = 'Top 10 Passwords Attempted'
generateBarGraph(password, countPassword, title, 'top_10_passwords_' + database)
else:
noGraphGenerated('top_10_passwords_' + database)
def generateTop10Usernames(database):
"""Generate the Top 10 Usernames from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT username, COUNT(username) " \
"FROM auth " \
"WHERE username <> '' " \
"GROUP BY username " \
"ORDER BY COUNT(username) " \
"DESC LIMIT 10"
cur.execute(sql)
if cur.rowcount != 0:
username = []
countUsername = []
for (user, countUser) in cur.fetchall():
username.append(user)
countUsername.append(countUser)
cur.close()
conn.close()
title = 'Top 10 Usernames Attempted'
generateBarGraph(username, countUsername, title, 'top_10_usernames_' + database)
else:
noGraphGenerated('top_10_usernames_' + database)
def generateTop10Combinations(database):
"""Generate the Top 10 Usernames-Password Combinations from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT username, password, COUNT(username) " \
"FROM auth " \
"WHERE username <> '' AND password <> '' " \
"GROUP BY username, password " \
"ORDER BY COUNT(username) " \
"DESC LIMIT 10"
cur.execute(sql)
if cur.rowcount != 0:
combination = []
countCombination = []
for (user, pwd, countComb) in cur.fetchall():
combination.append(user + '/' + pwd)
countCombination.append(countComb)
cur.close()
conn.close()
title = 'Top 10 Username-Password Combinations'
generateBarGraph(combination, countCombination, title, 'top_10_combinations_' + database)
else:
noGraphGenerated('top_10_combinations_' + database)
def generateSuccessRatio(database):
"""Generate the Authentication Success Ratio from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT success, COUNT(success) " \
"FROM auth " \
"GROUP BY success " \
"ORDER BY success"
cur.execute(sql)
if cur.rowcount != 0:
countSuccess = []
Success=[]
for (success, countSuc) in cur.fetchall():
if success == 0:
Success.append('Failure')
elif success == 1:
Success.append('Success')
countSuccess.append(countSuc)
cur.close()
conn.close()
title = 'Overall Success Ratio'
generateBarGraph(Success, countSuccess, title, 'success_ratio_' + database)
else:
noGraphGenerated('success_ratio_' + database)
def generateNumberOfConnectionsPerIP(database):
"""Generate the Number of Connections per IP from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT ip, COUNT(ip) " \
"FROM sessions " \
"GROUP BY ip " \
"ORDER BY COUNT(ip) DESC " \
"LIMIT 10"
cur.execute(sql)
if cur.rowcount != 0:
ip = []
countIp = []
for (ipAddr, countIpAddr) in cur.fetchall():
ip.append(ipAddr)
countIp.append(countIpAddr)
cur.close()
conn.close()
title = 'Number of Connections per Unique IP (Top 10)'
generateBarGraph(ip, countIp, title, 'number_connections_per_ip_' + database)
else:
noGraphGenerated('number_connections_per_ip_' + database)
def generateSuccessfulLoginsFromSameIP(database):
"""Generate the Successful Logins from same IP from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT sessions.ip, COUNT(sessions.ip) " \
"FROM sessions INNER JOIN auth ON sessions.id = auth.session " \
"WHERE auth.success = 1 " \
"GROUP BY sessions.ip " \
"ORDER BY COUNT(sessions.ip) DESC " \
"LIMIT 20"
cur.execute(sql)
if cur.rowcount != 0:
ip = []
countSessionsIp = []
for (ipAddr, countSessIpAddr) in cur.fetchall():
ip.append(ipAddr)
countSessionsIp.append(countSessIpAddr)
cur.close()
conn.close()
title = 'Successful Logins from Same IP (Top 20)'
generateBarGraph(ip, countSessionsIp, title, 'successful_logins_from_same_ip_' + database)
else:
noGraphGenerated('successful_logins_from_same_ip_' + database)
def generateTop10SSHClients(database):
"""Generate the Top 10 SSH Clients used to connect from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT clients.version, COUNT(client) " \
"FROM sessions INNER JOIN clients ON sessions.client = clients.id " \
"GROUP BY sessions.client " \
"ORDER BY COUNT(client) DESC " \
"LIMIT 10"
cur.execute(sql)
if cur.rowcount != 0:
version = []
countVersion = []
for (ver, countVer) in cur.fetchall():
version.append(ver)
countVersion.append(countVer)
cur.close()
conn.close()
title = 'Top 10 SSH Clients'
generateBarGraph(version, countVersion, title, 'top_10_ssh_clients_' + database)
else:
noGraphGenerated('top_10_ssh_clients_' + database)
def generateTop10OverallInput(database):
"""Generate the Top 10 Overall Input commands issued in the sensor from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT input, COUNT(input) " \
"FROM input " \
"GROUP BY input " \
"ORDER BY COUNT(input) DESC " \
"LIMIT 10"
cur.execute(sql)
if cur.rowcount != 0:
input = []
countInput = []
for (inp, countInp) in cur.fetchall():
input.append(inp)
countInput.append(countInp)
cur.close()
conn.close()
title = 'Top 10 Input (Overall)'
generateBarGraph(input, countInput, title, 'top_10_overall_input_' + database)
else:
noGraphGenerated('top_10_overall_input_' + database)
def generateTop10SuccessfulInput(database):
"""Generate the Top 10 Successful Input commands issued in the sensor from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT input, COUNT(input) " \
"FROM input " \
"WHERE success = 1 " \
"GROUP BY input " \
"ORDER BY COUNT(input) DESC " \
"LIMIT 10"
cur.execute(sql)
if cur.rowcount != 0:
input = []
countInput = []
for (inp, countInp) in cur.fetchall():
input.append(inp)
countInput.append(countInp)
cur.close()
conn.close()
title = 'Top 10 Successful Input'
generateBarGraph(input, countInput, title, 'top_10_successful_input_' + database)
else:
noGraphGenerated('top_10_successful_input_' + database)
def generateTop10FailedInput(database):
"""Generate the Top 10 Failed Input commands issued in the sensor from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT input, COUNT(input) " \
"FROM input " \
"WHERE success = 0 " \
"GROUP BY input " \
"ORDER BY COUNT(input) DESC " \
"LIMIT 10"
cur.execute(sql)
if cur.rowcount != 0:
input = []
countInput = []
for (inp, countInp) in cur.fetchall():
input.append(inp)
countInput.append(countInp)
cur.close()
conn.close()
title = 'Top 10 Failed Input'
generateBarGraph(input, countInput, title, 'top_10_failed_input_' + database)
else:
noGraphGenerated('top_10_failed_input_' + database)
def generateMostSuccessfulLoginsPerDay(database):
"""Generate the Top 20 most successful logins per day from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT timestamp, COUNT(session) " \
"FROM auth " \
"WHERE success = 1 " \
"GROUP BY DAYOFYEAR(timestamp) " \
"ORDER BY COUNT(session) DESC " \
"LIMIT 20"
cur.execute(sql)
if cur.rowcount != 0:
timestamp = []
countSession = []
for (dt, countSess) in cur.fetchall():
timestamp.append(dt.strftime('%m-%d-%Y'))
countSession.append(countSess)
cur.close()
conn.close()
title = 'Most Successful Logins per day (Top 20)'
generateBarGraph(timestamp, countSession, title, 'most_successful_logins_per_day_' + database)
else:
noGraphGenerated('most_successful_logins_per_day_' + database)
def generateSuccessesPerDay(database):
"""Generate the Successes per day from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT timestamp, COUNT(session) " \
"FROM auth " \
"WHERE success = 1 " \
"GROUP BY DAYOFYEAR(timestamp) " \
"ORDER BY timestamp ASC"
cur.execute(sql)
if cur.rowcount != 0:
timestamp = []
countSession = []
for (dt, countSess) in cur.fetchall():
timestamp.append(dt.strftime('%m-%d-%Y'))
countSession.append(countSess)
cur.close()
conn.close()
title = 'Successes per day'
generateLineGraph(timestamp, countSession, title, 'successes_per_day_' + database)
else:
noGraphGenerated('successes_per_day_' + database)
def generateSuccessesPerWeek(database):
"""Generate the Successes per Week from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT COUNT(session), " \
"MAKEDATE(CASE WHEN WEEKOFYEAR(timestamp) = 52 " \
"THEN YEAR(timestamp)-1 " \
"ELSE YEAR(timestamp) " \
"END, (WEEKOFYEAR(timestamp) * 7)-4) AS DateOfWeek_Value " \
"FROM auth " \
"WHERE success = 1 " \
"GROUP BY WEEKOFYEAR(timestamp) " \
"ORDER BY timestamp ASC"
cur.execute(sql)
if cur.rowcount != 0:
timestamp = []
countSession = []
for (countSess, dt) in cur.fetchall():
timestamp.append(dt.strftime('%m-%d-%Y'))
countSession.append(countSess)
cur.close()
conn.close()
title = 'Successes per Week'
generateLineGraph(timestamp, countSession, title, 'successes_per_week_' + database)
else:
noGraphGenerated('successes_per_week_' + database)
def generateMostProbesPerDay(database):
"""Generate the Top 20 most probes per day from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT COUNT(session), timestamp " \
"FROM auth " \
"GROUP BY DAYOFYEAR(timestamp) " \
"ORDER BY COUNT(session) DESC " \
"LIMIT 20"
cur.execute(sql)
if cur.rowcount != 0:
timestamp = []
countSession = []
for (countSess, dt) in cur.fetchall():
timestamp.append(dt.strftime('%m-%d-%Y'))
countSession.append(countSess)
cur.close()
conn.close()
title = 'Most Probes per day (Top 20)'
generateBarGraph(timestamp, countSession, title, 'most_probes_per_day_' + database)
else:
noGraphGenerated('most_probes_per_day_' + database)
def generateProbesPerDay(database):
"""Generate the Probes per day from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT COUNT(session), timestamp " \
"FROM auth " \
"GROUP BY DAYOFYEAR(timestamp) " \
"ORDER BY timestamp ASC"
cur.execute(sql)
if cur.rowcount != 0:
timestamp = []
countSession = []
for (countSess, dt) in cur.fetchall():
timestamp.append(dt.strftime('%m-%d-%Y'))
countSession.append(countSess)
cur.close()
conn.close()
title = 'Probes per day'
generateLineGraph(timestamp, countSession, title, 'probes_per_day_' + database)
else:
noGraphGenerated('probes_per_day_' + database)
def generateProbesPerWeek(database):
"""Generate the Probes per Week from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT COUNT(session), " \
"MAKEDATE(CASE WHEN WEEKOFYEAR(timestamp) = 52 " \
"THEN YEAR(timestamp)-1 " \
"ELSE YEAR(timestamp) " \
"END, (WEEKOFYEAR(timestamp) * 7)-4) AS DateOfWeek_Value " \
"FROM auth " \
"GROUP BY WEEKOFYEAR(timestamp) " \
"ORDER BY timestamp ASC"
cur.execute(sql)
if cur.rowcount != 0:
timestamp = []
countSession = []
for (countSess, dt) in cur.fetchall():
timestamp.append(dt.strftime('%m-%d-%Y'))
countSession.append(countSess)
cur.close()
conn.close()
title = 'Probes per Week'
generateLineGraph(timestamp, countSession, title, 'probes_per_week_' + database)
else:
noGraphGenerated('probes_per_week_' + database)
def generateHumanActivityBusiestDays(database):
"""Generate the Top 20 days with most human activity from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT COUNT(input), timestamp " \
"FROM input " \
"GROUP BY DAYOFYEAR(timestamp) " \
"ORDER BY COUNT(input) DESC " \
"LIMIT 20"
cur.execute(sql)
if cur.rowcount != 0:
timestamp = []
countSession = []
for (countSess, dt) in cur.fetchall():
timestamp.append(dt.strftime('%m-%d-%Y'))
countSession.append(countSess)
cur.close()
conn.close()
title = 'Human Activity busiest days (Top 20)'
generateBarGraph(timestamp, countSession, title, 'human_activity_busiest_days_' + database)
else:
noGraphGenerated('human_activity_busiest_days_' + database)
def generateHumanActivityPerDay(database):
"""Generate the Human Activity per day from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT COUNT(input), timestamp " \
"FROM input " \
"GROUP BY DAYOFYEAR(timestamp) " \
"ORDER BY timestamp ASC"
cur.execute(sql)
if cur.rowcount != 0:
timestamp = []
countSession = []
for (countSess, dt) in cur.fetchall():
timestamp.append(dt.strftime('%m-%d-%Y'))
countSession.append(countSess)
cur.close()
conn.close()
title = 'Human Activity per day'
generateLineGraph(timestamp, countSession, title, 'human_activity_per_day_' + database)
else:
noGraphGenerated('human_activity_per_day_' + database)
def generateHumanActivityPerWeek(database):
"""Generate the Human activity per Week from the MySQL Database"""
try:
conn = pymysql.connect(host=conf.MYSQL_HOST, port=conf.MYSQL_PORT, user=conf.MYSQL_USER, passwd=conf.MYSQL_PWD, db=database)
except pymysql.MySQLError as e:
print e.args[1]
sys.exit(1)
cur = conn.cursor()
sql = "SELECT COUNT(input), " \
"MAKEDATE( CASE WHEN WEEKOFYEAR(timestamp) = 52 " \
"THEN YEAR(timestamp)-1 " \
"ELSE YEAR(timestamp) END, (WEEKOFYEAR(timestamp) * 7)-4) AS DateOfWeek_Value " \
"FROM input " \
"GROUP BY WEEKOFYEAR(timestamp) " \
"ORDER BY timestamp ASC"
cur.execute(sql)
if cur.rowcount != 0:
timestamp = []
countSession = []
for (countSess, dt) in cur.fetchall():
timestamp.append(dt.strftime('%m-%d-%Y'))
countSession.append(countSess)
cur.close()
conn.close()
title = 'Human Activity per Week'
generateLineGraph(timestamp, countSession, title, 'human_activity_per_week_' + database)
else:
noGraphGenerated('human_activity_per_week_' + database)
def main():
"""Main Function"""
kippoDbs = getMySQLKippoData()
for db in kippoDbs:
generateTop10Passwords(db)
generateTop10Usernames(db)
generateTop10Combinations(db)
generateSuccessRatio(db)
generateNumberOfConnectionsPerIP(db)
generateSuccessfulLoginsFromSameIP(db)
generateTop10SSHClients(db)
generateTop10OverallInput(db)
generateTop10SuccessfulInput(db)
generateTop10FailedInput(db)
generateMostSuccessfulLoginsPerDay(db)
generateSuccessesPerDay(db)
generateSuccessesPerWeek(db)
generateMostProbesPerDay(db)
generateProbesPerDay(db)
generateProbesPerWeek(db)
generateHumanActivityBusiestDays(db)
generateHumanActivityPerDay(db)
generateHumanActivityPerWeek(db)
if __name__ == "__main__":
main()
__author__ = 'mercolino'