-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_network
123 lines (112 loc) · 3.89 KB
/
create_network
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created A Mininet Based Network For The Botnet
'''
import sys
import time
import numpy as np
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import OVSSwitch, RemoteController
from mininet.log import setLogLevel, info
from mininet.cli import CLI
from mininet.link import TCLink
class ForkTopo(Topo):
"A fork shaped network"
def build(self, **opts):
"Build the topology"
botnet_switch = self.addSwitch("s2")
add_hosts(self, botnet_switch, opts['num_bots'] + 1, "b")
user_switch = self.addSwitch("s3")
add_hosts(self, user_switch, 1, "u")
sdn_switch = self.addSwitch(f"s1")
self.addLink(sdn_switch, botnet_switch)
self.addLink(sdn_switch, user_switch)
add_hosts(
self,
sdn_switch,
1,
"t",
{
"bw": 0.1,
"delay": "5ms",
"loss": 0,
"max_queue_size": 1000,
"use_htb": True
}
)
def add_hosts(topo, switch, num_nodes, id_tag, opts=None):
'''
A hosts to the topology all connected in a star to the switch
:param topo The network topology object
:param switch Switch to attach the hosts to
:param id_tag characters to identify the hosts
:param opts extra arguments to pass to the link between the switch and hosts
'''
for i in range(num_nodes):
if opts:
topo.addLink(topo.addHost(f"{id_tag}{i}"), switch, **opts)
else:
topo.addLink(topo.addHost(f"{id_tag}{i}"), switch)
def run_network(num_bots):
'''
Run the DDoS attack on the target of the network, also have the user
request a web service from the target
:param num_bots Amount of bots in the botnet for the DDoS
'''
topo = ForkTopo(num_bots=num_bots)
net = Mininet(
topo=topo,
link=TCLink,
switch=OVSSwitch,
controller=RemoteController
)
net.start()
finish_time = time.time() + 3_600
if "--train" in sys.argv:
for host in net.hosts:
host.cmdPrint("export FLASK_APP=WebServer.py && flask run --host=0.0.0.0 &")
time.sleep(3)
print(f"Training will finish at {time.ctime(finish_time)}")
if "--attack" in sys.argv:
info("*** Starting botnet controller\n")
net['b0'].cmdPrint(f"./botnet_controller -n {num_bots} -t {net['t0'].IP()} &")
time.sleep(1)
info("*** Starting botnet attack on the target\n")
for i in range(1, num_bots + 1):
net[f"b{i}"].cmd(f"./bot -c {net['b0'].IP()} &")
if "--train" in sys.argv:
info("*** Waiting for training to finish")
time.sleep(finish_time - time.time())
elif "--normal" in sys.argv:
info("*** Normal activity\n")
tcp = 0
icmp = 0
while time.time() < finish_time:
host = net.hosts[
int(np.round(np.random.uniform(len(net.hosts)))) - 1
]
random_host_ip = net.hosts[
int(np.round(np.random.uniform(len(net.hosts)))) - 1
].IP()
if np.random.choice(range(1, 100)) < 95:
tcp += 1
host.cmd(f"curl {random_host_ip}:5000")
else:
icmp += 1
host.cmd(f"ping -c1 {random_host_ip}")
print(f"\rTCP: {tcp}, ICMP: {icmp}", end="")
time.sleep(np.random.uniform(0.25, 5))
else:
info("*** Starting web server on target\n")
net['t0'].cmdPrint("export FLASK_APP=WebServer.py && flask run --host=0.0.0.0 &")
time.sleep(1)
info("*** User browsing web service\n")
net['u0'].cmdPrint(f"netsurf http://{net['t0'].IP()}:5000/ &")
if "--cli" in sys.argv:
CLI(net)
net.stop()
if __name__ == '__main__':
setLogLevel('info')
run_network(50)