-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrun.py
executable file
·272 lines (243 loc) · 9.98 KB
/
run.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
#!/bin/env python
# Samuel Jero <[email protected]>
# Top-level testing script
import os
import sys
import time
from datetime import datetime
import argparse
import socket
system_home = os.path.dirname(os.path.realpath(__file__))
scripts_path = os.path.abspath(os.path.join(system_home, 'scripts'))
config_path = os.path.abspath(os.path.join(system_home, 'config'))
sys.path.insert(1,scripts_path)
sys.path.insert(0,config_path)
from scripts.test import *
import config
def main(args):
vms_per_instance = config.controllers_per_instance + 1
standalone = True
loop = False
mode = "w"
#Parse args
argp = argparse.ArgumentParser(description='Test Executor')
argp.add_argument('-p','--port', type=int, default=config.coordinator_port)
argp.add_argument('-c','--coordinator', type=str)
argp.add_argument('-i','--instance', type=int, default=0)
argp.add_argument('-l', '--loop', action='store_true')
args = vars(argp.parse_args(args[1:]))
instance = args['instance']
if args['coordinator'] is not None:
mode = "a"
standalone = False
if args['loop'] is True:
loop = True
standalone = False
print "Running Instance " + str(instance) + "..."
print "Controller: " + config.controller_type
#Open Log file
lg = open(config.logs_loc.format(instance=instance), mode)
lg.write(str(datetime.today()) + "\n")
lg.write("Instance: " + str(instance) + "\n")
lg.write("Controller: " + config.controller_type + "\n")
#Determine VMs
mininet = [instance*vms_per_instance + 1]
controllers = list()
for i in range(1,vms_per_instance):
controllers.append(mininet[0] + i)
lg.write("Mininet: " + str(mininet) + "\n")
lg.write("Controllers: " + str(controllers) + "\n")
lg.flush()
tester = SDNTester(mininet,controllers,lg)
#Start VMs
print "Starting VMs..."
if tester.startVms() == False:
return
#Do Tests
if standalone:
print "Stand alone testing..."
lg.write("Stand alone testing\n")
standalone_tests(tester)
elif loop:
print "Loop testing..."
lg.write("Loop testing\n")
infinite_loop(tester)
else:
print "Coordinated testing..."
lg.write("Coordinated testing\n")
coordinated_tests(tester, instance, lg,(args['coordinator'], args['port']))
#Stop VMs
print "Stopping VMs..."
if not loop:
tester.stopVms()
#Close log
lg.write(str(datetime.today()) + "\n")
lg.close()
def reconnect(addr):
sock = 0
while True:
try:
sock = socket.create_connection(addr)
break
except Exception as e:
print "[%s] Failed to connect to coordinator (%s:%d): %s...retrying" % (str(datetime.today()),addr[0], addr[1], e)
time.sleep(5)
continue
return sock
#Comments on controller<->executor message format:
# Messages are arbitrary strings ending in \n
# use sock.send() to send and sock.makefile.readline() to receive a full message.
# readline() properly handles waiting for a full message before delivering it. On
# error, an empty string is returned.
# Arbitrary python can be passed back and forth with str=repr(data) and data=eval(str)
def coordinated_tests(tester, instance, lg, addr):
num = 1
#Connect
try:
sock = socket.create_connection(addr)
except Exception as e:
print "[%s] Failed to connect to coordinator (%s:%d): %s" % (str(datetime.today()),addr[0], addr[1], e)
return
rf = sock.makefile()
#Loop Testing Strategies
baseline_msg = None
while True:
print "****************"
#Ask for Next Strategy
print "[%s] Asking for Next Strategy..." % (str(datetime.today()))
lg.write("[%s] Asking for Next Strategy...\n" % (str(datetime.today())))
try:
msg = {'msg':'READY','instance':"%s:%d"%(socket.gethostname(),instance)}
sock.send("%s\n" %(repr(msg)))
except Exception as e:
print "Failed to send on socket..."
rf.close()
sock.close()
sock = reconnect(addr)
rf = sock.makefile()
continue
#Get Reply
try:
line = rf.readline()
except Exception as e:
print "Failed to send on socket..."
rf.close()
sock.close()
sock = reconnect(addr)
rf = sock.makefile()
continue
if line=="":
rf.close()
sock.close()
sock = reconnect(addr)
rf = sock.makefile()
continue
try:
msg = eval(line)
except Exception as e:
continue
#Check for finished
if msg['msg']=="DONE":
#Done, shutdown
print "[%s] Finished... Shutting down..." % (str(datetime.today()))
lg.write("[%s] Finished... Shutting down...\n" % (str(datetime.today())))
break
elif msg['msg']=="STRATEGY":
#Test Strategy
strat = msg['data']
print "[%s] Test %d: %s" % (str(datetime.today()), num, str(strat))
lg.write("[%s] Test %d: %s\n" % (str(datetime.today()), num, str(strat)))
res = tester.doTest(strat)
# Check if rebaseline is suggested (0=for any metric).
if res[2] > 0:
s = "[%s] Rebaseline suggested. Value=%d." % (str(datetime.today()), res[2])
print s
lg.write(s + '\n')
tester.baseline(baseline_msg)
res = tester.doTest(strat) # Override the previous result.
num+=1
#Return Result and Feedback
print "[%s] Test Result: %s, Reason: %s" %(str(datetime.today()),str(res[0]), res[1])
lg.write("[%s] Test Result: %s , Reason: %s\n" %(str(datetime.today()),str(res[0]), res[1]))
fb = tester.retrieve_feedback()
try:
msg = {'msg':'RESULT','instance':"%s:%d"%(socket.gethostname(),instance), 'value':res[0], 'reason':res[1], 'feedback':fb, 'rebase': res[2]}
sock.send("%s\n" %(repr(msg)))
except Exception as e:
print "Failed to send on socket..."
sock = reconnect(addr)
continue
elif msg['msg'] == 'BASELINE':
print "[%s] Creating Baseline..." % (str(datetime.today()))
lg.write("[%s] Creating Baseline...\n" % (str(datetime.today())))
baseline_msg = msg['test']
tester.baseline(baseline_msg)
#Return Feedback
fb = tester.retrieve_feedback()
try:
msg = {'msg':'FEEDBACK','instance':"%s:%d"%(socket.gethostname(),instance), 'data':fb}
sock.send("%s\n" %(repr(msg)))
except Exception as e:
print "Failed to send on socket..."
sock = reconnect(addr)
continue
else:
print "Unknown Message: %s" % (msg)
#Cleanup
rf.close()
sock.close()
def standalone_tests(tester):
print "Starting Tests..."
print "Test 1 " + str(datetime.today())
res = tester.doTest({'topo':'/root/test1.py','switch':["*,*,*,*,*,CLEAR,*"],'host':None})
print "Test Result: " + str(res[0])
print "******"
print "Test 2 " + str(datetime.today())
res = tester.doTest({'topo':'/root/test1.py','switch':["{controllers[0]},3,*,of_packet_in,12,CLIE,mfield=12&mval=2&act==&val=1"],'host':None})
print "Test Result: " + str(res[0])
print "******"
print "Test 3 " + str(datetime.today())
res = tester.doTest({'topo':'/root/test1.py','switch':["*,*,*,*,*,CLEAR,*"],'host':None})
print "Test Result: " + str(res[0])
print "******"
print "Test 4 " + str(datetime.today())
res = tester.doTest({'topo':'/root/test1.py','switch':["{controllers[0]},3,*,of_packet_in,12,CDIVERT,mfield=12&mval=3&p=100&sw=2&ctl={controllers[0]}","{controllers[0]},2,*,of_packet_in,12,CDIVERT,mfield=12&mval=3&p=100&sw=3&ctl={controllers[0]}"], 'host':None})
print "Test Result: " + str(res[0])
print "******"
print "Test 5 " + str(datetime.today())
res = tester.doTest({'topo':'/root/test1.py','switch':["{controllers[0]},3,*,of_packet_out,7.1.1,CDIVERT,mfield=7.1.1&mval=2&p=100&sw=1&ctl={controllers[0]}"], 'host':None})
print "Test Result: " + str(res[0])
print "******"
print "Test 6 " + str(datetime.today())
res = tester.doTest({'topo':'/root/test1.py','switch':None, 'host':[{'cmd':'tunnel','h1':1,'h2':3,'filter':'lldp'},{'cmd':'basic'}]})
print "Test Result: " + str(res[0])
print "******"
print "Test 7 " + str(datetime.today())
now = str(time.time()+30)
res = tester.doTest({'topo':'/root/test1.py','switch':None, 'host':[{'cmd':'attack','action':{'module':'arp','command':'inject','type':'is-at','ids':[None,None,None,'vict-ip','mal-mac','vict-ip'],'start':now, 'freq':0.1, 'num':100},'mal':1,'vict':3},{'cmd':'basic'}]})
print "Test Result: " + str(res[0])
print "******"
print "Test 8 " + str(datetime.today())
res = tester.doTest({'topo':'/root/test1.py','switch':None, 'host':None, 'controller':{'action':'kill','time':45}})
print "Test Result: " + str(res[0])
print "******"
def infinite_loop(tester):
print "Starting Tests..."
i = 0
while True:
print "Test " + str(i) + " " + str(datetime.today())
#Do Nothing
res = tester.doTest({'topo':'/root/test1.py','switch':["*,*,*,*,*,CLEAR,*"],'host':[{'cmd':'basic'}]})
#Break Network Quarantine
#res = tester.doTest({'topo':'/root/test1.py','switch':[{'action':'{controllers[0]},2,*,of_packet_in,3,LIE,act==&val=0','time':20}],'host':None,'controller':[{'action':'kill','time':25}],'priority':100})
#DeniableDoS
#res = tester.doTest({'topo':'/root/test1.py','switch':['{controllers[0]},1,*,of_features_reply,11.3.1,LIE,act==&val=65533'],'host':[{'cmd':'attack','action':{'module':'arp','command':'inject','type':'who-has','ids':[None,None,None,None,None,'10.0.0.42'],'freq':0.5,'num':100,'start':time.time()+30},'mal':2},{'cmd':'basic'}],'controller':None,'priority':100})
#res = tester.doTest({'topo':'/root/test1.py','switch':['{controllers[0]},1,*,of_features_reply,11.3.1,LIE,act==&val=65533'],'host':None,'controller':None,'priority':100})
#WebServer Impersonation
#res = tester.doTest({'topo':'/root/test1.py','switch':[{'action':'{controllers[0]},3,*,of_packet_in,*,CDROP,p=100&mfield=12&mval=2','time':40}],'host':[{'cmd':'attack','action':{'module':'arp','command':'inject','type':'is-at','ids':[None,'vict-mac','vict-mac','vict-ip',None,None],'freq':0.1,'num':200,'start':str(time.time()+45)},'mal':1,'vict':3},{'cmd':'hijack'}],'controller':None,'priority':100})
#res = tester.doTest({'topo':'/root/test1.py','switch':None,'host':[{'cmd':'attack','action':{'module':'arp','command':'inject','type':'is-at','ids':[None,'vict-mac','vict-mac','vict-ip',None,None],'freq':0.1,'num':200,'start':str(time.time()+45)},'mal':1,'vict':3},{'cmd':'hijack'}],'controller':None,'priority':100})
print "Test Result: " + str(res[0])
print "******"
i += 1
if __name__ == "__main__":
main(sys.argv)