-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate.py
More file actions
234 lines (202 loc) · 7.83 KB
/
aggregate.py
File metadata and controls
234 lines (202 loc) · 7.83 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
dirs = {
"Brute_Force": 0,
"Home_Vacation": 1,
"Home_Weekday": 1,
"Home_Weekend": 1,
"Hping3": 0,
"Infrastructure": 1,
"Large_Ping": 0,
"Ping_Flood": 0,
"Scanning": 0
}
output_file = open("aggregated_data.txt", "w")
# Processes
for dir in dirs:
for run in range(1, 4):
traffic_type = "Attack" if dirs[dir] == 0 else "Normal"
ps_file = open(dir + "/run" + str(run) + "/ps_data_webcam.txt")
num_captures = 0
num_processes = 0
for line in ps_file:
if "PID Uid" in line:
num_captures += 1
if not line.split()[2] == "VSZ" and not line.split()[2] == "0":
num_processes += 1
avg_processes = num_processes / num_captures
output_file.write(dir + " Run " + str(run) + ",")
output_file.write(traffic_type + ",")
output_file.write("# Running Processes,")
output_file.write(str(avg_processes) + "\n")
output_file.write("\n")
print("Processes Done")
# Mem Usage
for dir in dirs:
for run in range(1, 4):
traffic_type = "Attack" if dirs[dir] == 0 else "Normal"
top_file = open(dir + "/run" + str(run) + "/top_data_webcam.txt")
num_captures = 0
data_used = 0
data_free = 0
for line in top_file:
if "Mem: " in line:
num_captures += 1
data_used += int(line.split()[1][:-1])
data_free += int(line.split()[3][:-1])
avg_data = 100 * data_used / (data_used + data_free)
output_file.write(dir + " Run " + str(run) + ",")
output_file.write(traffic_type + ",")
output_file.write("% Memory Used,")
output_file.write(str(avg_data) + "\n")
output_file.write("\n")
print("Mem Done")
# Usr CPU Usage
for dir in dirs:
for run in range(1, 4):
traffic_type = "Attack" if dirs[dir] == 0 else "Normal"
top_file = open(dir + "/run" + str(run) + "/top_data_webcam.txt")
num_captures = 0
cpu_usr = 0
for line in top_file:
if "Mem: " in line:
num_captures += 1
if "CPU: " in line:
cpu_usr += float(line.split()[1][:-1])
avg_usr = cpu_usr / num_captures
output_file.write(dir + " Run " + str(run) + ",")
output_file.write(traffic_type + ",")
output_file.write("% Usr CPU Usage,")
output_file.write(str(avg_usr) + "\n")
output_file.write("\n")
print("Usr CPU Done")
# Sys CPU Usage
for dir in dirs:
for run in range(1, 4):
traffic_type = "Attack" if dirs[dir] == 0 else "Normal"
top_file = open(dir + "/run" + str(run) + "/top_data_webcam.txt")
num_captures = 0
cpu_sys = 0
for line in top_file:
if "Mem: " in line:
num_captures += 1
if "CPU: " in line:
cpu_sys += float(line.split()[3][:-1])
avg_sys = cpu_sys / num_captures
output_file.write(dir + " Run " + str(run) + ",")
output_file.write(traffic_type + ",")
output_file.write("% Sys CPU Usage,")
output_file.write(str(avg_sys) + "\n")
output_file.write("\n")
print("Sys CPU Done")
# Idle CPU Usage
for dir in dirs:
for run in range(1, 4):
traffic_type = "Attack" if dirs[dir] == 0 else "Normal"
top_file = open(dir + "/run" + str(run) + "/top_data_webcam.txt")
num_captures = 0
cpu_idle = 0
for line in top_file:
if "Mem: " in line:
num_captures += 1
if "CPU: " in line:
cpu_idle += float(line.split()[7][:-1])
avg_idle = cpu_idle / num_captures
output_file.write(dir + " Run " + str(run) + ",")
output_file.write(traffic_type + ",")
output_file.write("% Idle CPU Usage,")
output_file.write(str(avg_idle) + "\n")
output_file.write("\n")
print("Idle CPU Done")
# Connections
for dir in dirs:
for run in range(1, 4):
traffic_type = "Attack" if dirs[dir] == 0 else "Normal"
netstat_file = open(dir + "/run" + str(run) + "/netstat_data_webcam.txt")
num_captures = 0
open_connections = 0
for line in netstat_file:
if "Active Internet" in line:
num_captures += 1
if "LISTEN" in line or "ESTABLISHED" in line:
open_connections += 1
avg_conn = open_connections / num_captures
output_file.write(dir + " Run " + str(run) + ",")
output_file.write(traffic_type + ",")
output_file.write("# Open Connections,")
output_file.write(str(avg_conn) + "\n")
output_file.write("\n")
print("Connections Done")
# Bytes Out Per Second
for dir in dirs:
for run in range(1, 4):
traffic_type = "Attack" if dirs[dir] == 0 else "Normal"
wireshark_file = open(dir + "/run" + str(run) + "/wireshark_data_host.csv")
bytes = 0
initial_time = 0
final_time = 0
for line in wireshark_file:
if line.split()[5] == "a0:04:60:86:d3:e8":
bytes += int(line.split()[9])
curr_time = 0
a = line.split()[2]
curr_time += 3600 * float(a[:a.index(":")])
a = a[a.index(":") + 1:]
curr_time += 60 * float(a[:a.index(":")])
a = a[a.index(":") + 1:]
curr_time += float(a)
if initial_time == 0:
initial_time = curr_time
if final_time < curr_time:
final_time = curr_time
bytes_out = bytes / (final_time - initial_time)
output_file.write(dir + " Run " + str(run) + ",")
output_file.write(traffic_type + ",")
output_file.write("# Bytes Out / s,")
output_file.write(str(bytes_out) + "\n")
output_file.write("\n")
print("Bytes Out Done")
# Bytes In Per Second
for dir in dirs:
for run in range(1, 4):
traffic_type = "Attack" if dirs[dir] == 0 else "Normal"
wireshark_file = open(dir + "/run" + str(run) + "/wireshark_data_host.csv")
bytes = 0
initial_time = 0
final_time = 0
for line in wireshark_file:
if line.split()[7] == "a0:04:60:86:d3:e8":
bytes += int(line.split()[9])
curr_time = 0
a = line.split()[2]
curr_time += 3600 * float(a[:a.index(":")])
a = a[a.index(":") + 1:]
curr_time += 60 * float(a[:a.index(":")])
a = a[a.index(":") + 1:]
curr_time += float(a)
if initial_time == 0:
initial_time = curr_time
if final_time < curr_time:
final_time = curr_time
bytes_in = bytes / (final_time - initial_time)
output_file.write(dir + " Run " + str(run) + ",")
output_file.write(traffic_type + ",")
output_file.write("# Bytes In / s,")
output_file.write(str(bytes_in) + "\n")
output_file.write("\n")
print("Bytes In Done")
# Protocols
for dir in dirs:
for run in range(1, 4):
traffic_type = "Attack" if dirs[dir] == 0 else "Normal"
wireshark_file = open(dir + "/run" + str(run) + "/wireshark_data_host.csv")
protocols = []
protocol_count = 0
for line in wireshark_file:
if line.split()[8] not in protocols:
protocols.append(line.split()[8])
protocol_count += 1
output_file.write(dir + " Run " + str(run) + ",")
output_file.write(traffic_type + ",")
output_file.write("# Protocols,")
output_file.write(str(protocol_count) + "\n")
output_file.write("\n")
print("Protocols Done")