-
Notifications
You must be signed in to change notification settings - Fork 0
/
mininet_config.py
88 lines (76 loc) · 3.1 KB
/
mininet_config.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
from mininet.net import Mininet
from mininet.topo import LinearTopo
from mininet.topolib import TreeTopo
from mininet.node import Controller, RemoteController, Node
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from parameters import TABLE_CAPACITY
import sys
import benign_traffic
import attack_sim
from time import sleep
class MininetTopo():
def __init__(self, topology = 'linear', number_of_switch = 2 , number_of_host_per_switch = 1):
if topology == 'linear':
Linear = LinearTopo(number_of_switch, number_of_host_per_switch)
self.net = Mininet(topo=Linear, controller=RemoteController)
elif topology == 'tree':
Tree = TreeTopo(number_of_switch, number_of_host_per_switch)
self.net = Mininet(topo=Tree, controller=RemoteController)
else:
print('Invalid topology')
sys.exit(1)
self.master_node = Node('master')
def start(self):
self.net.start()
def config_switch(self):
for switch in self.net.switches:
print(switch)
self.master_node.cmdPrint('ovs-vsctl set bridge %s protocols=OpenFlow13' %switch)
self.master_node.cmdPrint(f'ovs-vsctl -- --id=@{switch} create Flow_Table flow_limit={TABLE_CAPACITY} overflow_policy=refuse -- set Bridge {switch} flow_tables=0=@{switch}')
def pingAll(self):
self.net.pingAll()
def stop(self):
self.net.stop()
def run_real_data(topo):
# for each hosts in the network run the real data coming from uni1_1.pcap
hosts = topo.net.hosts
for host in hosts:
interface = host.intf()
host.cmd(f'tcpreplay -i {interface} -tK univ1_pt1 ')
def delayed_detection(malicious, hosts):
### important parameter
sleep(2)
print('attackkk')
malicious.attack_protocol_change(3, 60 ,hosts, 10)
if __name__ == '__main__':
arguments = sys.argv
topology = arguments[1] if len(arguments) > 1 else 'linear'
number_of_switch = int(arguments[2]) if len(arguments) > 2 else 2
number_of_host_per_switch = int(arguments[3]) if len(arguments) > 3 else 1
topo = MininetTopo(topology, number_of_switch, number_of_host_per_switch)
try:
setLogLevel( 'debug' )
print('Topology created')
topo.start()
topo.config_switch()
a = topo.net.hosts
host = a[0]
switch = a[1]
from threading import Thread
malicious = attack_sim.malicious_host('h1s1',host,10)
benign_thread = Thread(target=benign_traffic.traffic, args=(topo.net, number_of_host_per_switch*number_of_switch))
malicious_thread = Thread(target=delayed_detection, args=(malicious, number_of_host_per_switch*number_of_switch,))
# benign_thread.start()
malicious_thread.start()
# benign_thread.join()
malicious_thread.join()
## run tcreplay
# run_real_data(topo)
# use
CLI( topo.net )
print('CLI opened')
topo.stop()
except Exception as e:
print('ERROR:',e)
topo.stop()