-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
117 lines (93 loc) · 2.6 KB
/
main.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
import os
from re import U
from time import sleep
import pandas as pd
from IDSOrchestrator import IDSOrchestrator
from CICFLowMeter import CICFlowMeter
from Model import Model
import csv
base_path = os.path.dirname(os.path.realpath(__file__))
models_directory_path = base_path + '/models/'
pcap_dir = base_path + '/data/pcaps'
csv_dir = base_path + '/data/ids_input/'
class Main:
def __init__(self):
self.kill_switch = False
self.ids = IDSOrchestrator()
def main(self):
self.kill_switch = False
self.ids = None
try:
while not self.kill_switch:
u_input = input('IDS-Agent: ')
if u_input == 'run':
self.ids.start()
elif u_input == 'see queue':
if not self.ids: continue
exit_queue = False
try:
while not exit_queue:
print(self.ids.classify_pcap_thread_pool._work_queue.qsize())
sleep(1)
except KeyboardInterrupt:
exit_queue = True
elif u_input == 'submit':
submit_ids = IDSOrchestrator()
return_flag = False
while not return_flag:
pcap_path = input('Pcap path: ')
if pcap_path == '':
continue
if pcap_path == 'back':
return_flag = True
continue
# if directory, submit pcaps within
if os.path.isdir(pcap_path):
for pcap in os.listdir(pcap_path):
submit_ids.classify_pcap(pcap_path + pcap, print_output=True)
# submit pcap
submit_ids.classify_pcap(pcap_path, print_output=True)
# pcap submission exited
submit_ids.kill()
elif u_input == 'kill':
self.kill()
except KeyboardInterrupt:
print('\n-- KeyboardInterrupt --')
self.kill()
def kill(self):
print('Exiting IDS...')
if self.ids: self.ids.kill()
self.kill_switch = True
def report_metrics(self, model_path, pcap_path, csv_path, pcap_conversion_time, prediction_time):
report_path = 'reports/indv_pcap_performance.csv'
# if the report doesn't already exists, write the title row first
if os.path.isfile(report_path):
report_headers = None
else:
report_headers = [
'model_path',
'pcap_path',
'csv_path',
'pcap_filesize',
'csv_filesize',
'pcap_conversion_time',
'prediction_time'
]
row = [
model_path,
pcap_path,
csv_path,
os.path.getsize(pcap_path),
os.path.getsize(csv_path),
pcap_conversion_time,
prediction_time
]
with open(report_path, 'a') as f:
writer = csv.writer(f)
# if the report doesn't already exists, write the title row first
if report_headers:
writer.writerow(report_headers)
writer.writerow(row)
if __name__ == '__main__':
main = Main()
main.main()