-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui_bro.py
268 lines (204 loc) · 8.19 KB
/
gui_bro.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
import Tkinter as tk
import tkFont as tkfont
import random
import threading
import time
from Config import config, DNP3PORT
from utils.Connection import Connection
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
# Flags
starting = False
dumpAttack = False
smartAttack = False
# Default delay cycles
delayCycles = 3
# Display trace
generated_trace = []
generated_seq = []
send_trace = []
send_seq = []
attack_trace = [[]]
attack_seq = [[]]
# Attack Counter
smartAttackCounter = 0
dumpAttackCounter = 0
normalDelayPoints = []
smartDelayPoints = []
f = Figure(figsize=(5, 5), dpi=100)
def animate(i):
a = f.add_subplot(211)
a.clear()
a.set_ylim(-1, 1)
a.plot(generated_seq, generated_trace, linestyle=':', marker='o', color='gray')
a.set_title("Generated Trace:")
b = f.add_subplot(212)
b.clear()
b.set_ylim(-1, 1)
b.set_title("Trace sent: ")
b.plot(send_seq, send_trace, linestyle=':', marker='o', color='gray')
for j in range(len(attack_seq)):
b.plot(attack_seq[j], attack_trace[j], linestyle=':', marker='o', color='red')
if len(smartDelayPoints) > 0:
for point in smartDelayPoints:
b.axvline(point, linewidth=4, color='r')
b.text(point + 0.1, 0.3, r'$\leftarrow\ Attack$', fontsize=20, color='red')
if len(normalDelayPoints) > 0:
for point in normalDelayPoints:
b.axvline(point, linewidth=4, color='b')
b.text(point + 0.1, 0.5, 'Missing Packets', fontsize=20, color='blue')
if len(generated_seq) < 40:
a.set_xlim(0, 40)
b.set_xlim(0, 40)
b.text(1, 0.75, 'Delay Cycles:' + str(delayCycles), fontsize=16, color='red')
else:
a.set_xlim(len(generated_seq) - 40, len(generated_seq))
b.set_xlim(len(generated_seq) - 40, len(generated_seq))
b.text(len(generated_seq) - 40 + 1, 0.75, 'Delay Cycles:' + str(delayCycles), fontsize=16, color='red')
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
global trace_str
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
trace_str = tk.StringVar()
self.frames = {}
for F in (StartPage, PageOne):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("PageOne")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
number_label = tk.Label(self, text="Number of packets", font=('Arial', 28), width=40, height=2)
number_label.pack()
number_entry = tk.Entry(self, show=None)
number_entry.pack()
cycle_label = tk.Label(self, text="how many cycles you want to delay: ", font=('Arial', 28),
width=40, height=2)
cycle_label.pack()
cycle_entry = tk.Entry(self, show=None)
cycle_entry.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
global trace_seq
global trace_show
canvas = FigureCanvasTkAgg(f, self)
canvas.show()
toolbar = NavigationToolbar2TkAgg(canvas, self)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
####################### Set Delay ############################
cycle_entry = tk.Entry(self, show=None)
cycle_entry.pack(pady=5)
def set():
global delayCycles
var = cycle_entry.get()
if var.isdigit():
delayCycles = int(var)
print "set delay cycle to " + var
delayCyclesButton = tk.Button(self, text="Set Delay Cycles", font=('Arial', 20),
width=40, height=2, command=set)
delayCyclesButton.pack()
start_button = tk.Button(self, text='Start sending packets', bg='green', font=('Arial', 20), width=30,
height=40,
command=self.startSendPackets)
start_button.pack(padx=40, side=tk.LEFT)
normal_delay = tk.Button(self, text='Launch Normal Delay', bg='green', font=('Arial', 20), width=30, height=40,
command=self.normal_delay)
normal_delay.pack(padx=40, side=tk.LEFT)
smart_delay = tk.Button(self, text='Launch Smart Delay', bg='green', font=('Arial', 20), width=30, height=40,
command=self.smart_delay)
smart_delay.pack(padx=40, side=tk.LEFT)
t = threading.Thread(target=self.sendPackets, args=())
t.start()
def sendPackets(self):
global starting
global dumpAttack
global smartAttack
global generated_trace
global generated_seq
global send_trace
global send_seq
global attack_trace
global attack_seq
global dumpAttackCounter
global smartAttackCounter
while (starting is False):
time.sleep(0.3)
continue
# Create connection
connection = Connection(config.ipdst, DNP3PORT)
connection.createConnection()
i = 0
while True:
# Generate random packets
dnp3 = config.dnp3Packets[random.randint(0, len(config.dnp3Packets) - 1)]
generated_trace.append(random.randint(0, 20) / 10.0 - 1)
generated_seq.append(i)
if dumpAttackCounter > 0:
dumpAttackCounter = dumpAttackCounter - 1
elif smartAttackCounter > 0:
maliciousPacket = generated_trace[smartDelayPoints[len(smartDelayPoints) - 1] - smartAttackCounter]
smartAttackCounter = smartAttackCounter - 1
send_seq.append(generated_seq[i])
send_trace.append(maliciousPacket)
attack_seq[len(attack_trace) - 1].append(generated_seq[i])
attack_trace[len(attack_trace) - 1].append(maliciousPacket)
if smartAttackCounter == 0:
attack_seq.append([])
attack_trace.append([])
# Send packets
connection.sendPacket(dnp3)
else:
send_trace.append(generated_trace[i])
send_seq.append(generated_seq[i])
# Send packets
connection.sendPacket(dnp3)
i = i + 1
time.sleep(config.normalTime)
def startSendPackets(self):
global starting
starting = True
def normal_delay(self):
global dumpAttack
global dumpAttackCounter
global normalDelayPoints
dumpAttack = True
dumpAttackCounter = delayCycles
normalDelayPoints.append(len(generated_trace) - 1)
def smart_delay(self):
global smartAttack
global smartAttackCounter
global generated_trace
global smartDelayPoints
smartAttack = True
smartAttackCounter = delayCycles
smartDelayPoints.append(len(generated_trace) - 1)
if __name__ == "__main__":
dumpAttack = False
smartAttack = False
app = SampleApp()
app.title("Delay Attack Demo")
app.geometry('1400x900')
ani = animation.FuncAnimation(f, animate, interval=1000)
app.mainloop()