def simpleTest(hosts): net = TreeNet( depth=1, fanout=hosts, autoSetMacs=True, controller=partial(RemoteController, ip='127.0.0.1', port=6633 )) net.items() #print(net.items()) host=net.items()[0][0] #print(net.get(host).MAC()) for i in net.items(): string1 = i[0] string2 = 'h' if cmp(string1[0:1],string2[0:1]) == 0: db = MySQLdb.connect("localhost","root","44123429","mininet" ) cursor = db.cursor() sql = "INSERT IGNORE INTO HOSTS(MAC, TEAM) VALUES ('%s', '%s')" % (net.get(i[0]).MAC(), 0) #try: cursor.execute(sql) db.commit() #except: #db.rollback() db.close() print(net.get(i[0]).MAC()) #print(net.get(host).MAC()) # Configure and start NATted connectivity rootnode = connectToInternet( net ) print "*** Hosts are running and should have internet connectivity" print "*** Type 'exit' or control-D to shut down network" CLI( net ) # Shut down NAT stopNAT( rootnode ) net.stop()
def simpleTest(hosts): net = TreeNet(depth=1, fanout=hosts, autoSetMacs=True, controller=partial(RemoteController, ip='127.0.0.1', port=6633)) net.items() #print(net.items()) host = net.items()[0][0] #print(net.get(host).MAC()) for i in net.items(): string1 = i[0] string2 = 'h' if cmp(string1[0:1], string2[0:1]) == 0: db = MySQLdb.connect("localhost", "root", "44123429", "mininet") cursor = db.cursor() sql = "INSERT IGNORE INTO HOSTS(MAC, TEAM) VALUES ('%s', '%s')" % ( net.get(i[0]).MAC(), 0) #try: cursor.execute(sql) db.commit() #except: #db.rollback() db.close() print(net.get(i[0]).MAC()) #print(net.get(host).MAC()) # Configure and start NATted connectivity rootnode = connectToInternet(net) print "*** Hosts are running and should have internet connectivity" print "*** Type 'exit' or control-D to shut down network" CLI(net) # Shut down NAT stopNAT(rootnode) net.stop()
def simpleTest(): print("infection hosts rate:") inf_rate = eval(input()) print("amplituda (packets per second):") max = eval(input()) print("period T:") grow_time = eval(input()) / 2 atck_time = 40 test_time = 50 controller = RemoteController('Runos', ip='127.0.0.1', port=6653) switch = partial(OVSKernelSwitch, protocols='OpenFlow13') net = TreeNet(depth=2, fanout=3, controller=controller, switch=switch, cleanup=True) net.start() sleep(3) hosts = net.topo.hosts() inf_amount = round(inf_rate * len(hosts)) inf_hosts = [] while inf_amount: num = randint(0, 8) if inf_amount and not (hosts[num] in inf_hosts): inf_hosts.append(hosts[num]) inf_amount -= 1 hosts = net.topo.hosts() general_capacity_per_sec = max / len(inf_hosts) num_intervals = round(grow_time / 2) offset_delay = round((1000 / general_capacity_per_sec) / num_intervals) print(hosts) start = monotonic() childs = [] for i in range(len(hosts)): try: pid = os.fork() except: print('error: create child process') sys.exit(33) if pid == 0: host = net.get(hosts[i]) if hosts[i] in inf_hosts: ping_pair = net.get(hosts[i - 1]) curr_time = monotonic() - start interval = (2.5 - curr_time) if (2.5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) ping_pair = net.get(hosts[i - 2]) curr_time = monotonic() - start interval = (5 - curr_time) if (5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) k = 0 while curr_time < atck_time + 5: curr_time = monotonic() - start sleep_time = grow_time * 2 * k + 5 - curr_time if sleep_time > 0: sleep(sleep_time) send_delay = 0 for j in range(num_intervals): curr_time = monotonic() - start send_delay += offset_delay if (curr_time + 2 < atck_time + 5): print("curr_time=%s :: %s start attack" % (curr_time, host.IP())) host.cmd( "./runos/src/apps/ddos-defender/scripts/atckWithOpts.sh %s %s %s %s" % (host.MAC(), host.IP(), 2, send_delay)) for j in range(num_intervals): curr_time = monotonic() - start if (curr_time + 2 < atck_time + 5): print("curr_time=%s :: %s start attack" % (curr_time, host.IP())) host.cmd( "./runos/src/apps/ddos-defender/scripts/atckWithOpts.sh %s %s %s %s" % (host.MAC(), host.IP(), 2, send_delay)) send_delay -= offset_delay k += 1 while curr_time < test_time + 5: curr_time = monotonic() - start interval = ( 5 + test_time - curr_time) if (5 + test_time - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) sleep(0.5) file_name = "atck_duration_" + str(i) + ".txt" handle = open(file_name, "w") handle.write("'%s' : [%s, %s]" % (net.get(hosts[i]).IP(), 5, 45)) handle.close() else: ping_pair = net.get(hosts[i - 1]) curr_time = monotonic() - start interval = (5 - curr_time) if (5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) ping_pair = net.get(hosts[i - 2]) curr_time = monotonic() - start interval = (25 - curr_time) if (25 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) ping_pair = net.get(hosts[i - 3]) curr_time = monotonic() - start interval = ( test_time + 5 - curr_time) if (test_time + 5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) sys.exit(3) if pid > 0: childs.append(pid) for p in childs: pid, status = os.wait() print('childs end') host = net.get(hosts[0]) handleResult = open("runos/src/apps/ddos-defender/tests/infectedMACs.txt", "w") handleResult.write("{") for i in range(len(hosts)): if hosts[i] in inf_hosts: file_name = "atck_duration_" + str(i) + ".txt" handle = open(file_name, "r") result = handle.read() handleResult.write(result) if i < (len(hosts) - 1): handleResult.write(", ") handle.close() cmnd = "sudo rm " + file_name host.cmd(cmnd) handleResult.write("}") handleResult.close() CLI(net) net.stop()
def simpleTest(): print("infection hosts rate:") inf_rate = eval(input()) print("k:") k = eval(input()) grow_time = 20 atck_time = 40 test_time = 50 max = grow_time * k controller = RemoteController('Runos', ip='127.0.0.1', port=6653) switch = partial(OVSKernelSwitch, protocols='OpenFlow13') net = TreeNet(depth=2, fanout=3, controller=controller, switch=switch, cleanup=True) net.start() sleep(3) hosts = net.topo.hosts() inf_amount = round(inf_rate * len(hosts)) inf_hosts = [] while inf_amount: num = randint(0, 8) if inf_amount and not (hosts[num] in inf_hosts): inf_hosts.append(hosts[num]) inf_amount -= 1 print(inf_hosts) general_capacity_per_sec = max / len(inf_hosts) connection_interval = grow_time / len(inf_hosts) send_delay_for_host = round(1000 / general_capacity_per_sec) print(send_delay_for_host) infection_sequence = numpy.arange(len(inf_hosts)) numpy.random.shuffle(infection_sequence) start = monotonic() childs = [] for i in range(len(hosts)): try: pid = os.fork() except: print('error: create child process') sys.exit(33) if pid == 0: host = net.get(hosts[i]) if hosts[i] in inf_hosts: index = inf_hosts.index(hosts[i]) host_turn = connection_interval * infection_sequence[index] ping_pair = net.get(hosts[i - 1]) curr_time = monotonic() - start interval = ( 2.5 + host_turn - curr_time) if (2.5 + host_turn - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) ping_pair = net.get(hosts[i - 2]) curr_time = monotonic() - start interval = ( 5 + host_turn - curr_time) if (5 + host_turn - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) curr_time = monotonic() - start sleep_time = 5 + host_turn - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = 5 + atck_time - curr_time if (5 + atck_time - curr_time) >= 1 else 1 print("curr_time=%s :: %s start attack" % (curr_time, host.IP())) host.cmd( "./runos/src/apps/ddos-defender/scripts/atckWithOpts.sh %s %s %s %s" % (host.MAC(), host.IP(), interval, send_delay_for_host)) while curr_time < test_time + 5: curr_time = monotonic() - start interval = ( 5 + test_time - curr_time) if (5 + test_time - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) sleep(0.5) file_name = "atck_duration_" + str(i) + ".txt" handle = open(file_name, "w") handle.write("'%s' : [%s, %s]" % (net.get( hosts[i]).IP(), 5 + host_turn + 6, 5 + atck_time + 6)) handle.close() else: ping_pair = net.get(hosts[i - 1]) curr_time = monotonic() - start interval = (5 - curr_time) if (5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) ping_pair = net.get(hosts[i - 2]) curr_time = monotonic() - start interval = (25 - curr_time) if (25 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) ping_pair = net.get(hosts[i - 3]) curr_time = monotonic() - start interval = ( test_time + 5 - curr_time) if (test_time + 5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), ping_pair.IP(), interval)) host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (ping_pair.IP(), interval)) sys.exit(3) if pid > 0: childs.append(pid) for p in childs: pid, status = os.wait() print('childs end') host = net.get(hosts[0]) handleResult = open("runos/src/apps/ddos-defender/tests/infectedMACs.txt", "w") handleResult.write("{") for i in range(len(hosts)): if hosts[i] in inf_hosts: file_name = "atck_duration_" + str(i) + ".txt" handle = open(file_name, "r") result = handle.read() handleResult.write(result) if i < (len(hosts) - 1): handleResult.write(", ") handle.close() cmnd = "sudo rm " + file_name host.cmd(cmnd) handleResult.write("}") handleResult.close() CLI(net) net.stop()
def simpleTest(): controller = RemoteController('Runos', ip='127.0.0.1', port=6653) switch = partial(OVSKernelSwitch, protocols='OpenFlow13') net = TreeNet(depth=2, fanout=3, controller=controller, switch=switch, cleanup=True) net.start() sleep(3) start = monotonic() attack_hosts = ["h1", "h2", "h5"] childs = [] for hostName in net.topo.hosts(): try: pid = os.fork() except: print('error: create child process') sys.exit(33) if pid == 0: host = net.get(hostName) if hostName == "h1": pingPair = net.get("h4") curr_time = monotonic() - start interval = (12 - curr_time) if (12 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 12.5 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print("curr_time=%s :: %s start attack" % (curr_time, host.IP())) start_attack = curr_time result = host.cmd( "./runos/src/apps/ddos-defender/scripts/atck.sh %s %s 25" % (host.MAC(), host.IP())) curr_time = monotonic() - start end_attack = curr_time print("curr_time=%s :: %s end attack" % (curr_time, host.IP())) pingPair = net.get("h3") pingPair.setIP("10.0.0.20") while (curr_time < 50): curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) sleep(0.5) file_name = "atck_duration_" + attack_hosts[0] + ".txt" handle = open(file_name, "w") handle.write("'%s' : [%s, %s]" % (host.IP(), start_attack, end_attack)) handle.close() if hostName == "h2": pingPair = net.get("h1") curr_time = monotonic() - start interval = (4 - curr_time) if (4 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start print('curr_time=%s :: %s changed IP with DHCP' % (curr_time, host.IP())) host.cmd('dhclient -r') host.cmd('dhclient') sleep(0.5) newIP = host.cmd( "ifconfig | grep 'inet 10.' | cut -f2 -d't' | cut -f1 -d'n'" ) splitnewIP = newIP.split(' ') host.setIP(splitnewIP[1]) pingPair = net.get("h3") pingPair.setIP("10.0.0.20") curr_time = monotonic() - start sleep_time = 6 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = (12 - curr_time) if (12 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 12.5 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print("curr_time=%s :: %s start attack" % (curr_time, host.IP())) start_attack = curr_time result = host.cmd( "./runos/src/apps/ddos-defender/scripts/atck.sh %s %s 35" % (host.MAC(), host.IP())) curr_time = monotonic() - start end_attack = curr_time print("curr_time=%s :: %s end attack" % (curr_time, host.IP())) pingPair = net.get("h7") while curr_time < 50: curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) sleep(0.5) file_name = "atck_duration_" + attack_hosts[1] + ".txt" handle = open(file_name, "w") handle.write("'%s' : [%s, %s]" % (host.IP(), start_attack, end_attack)) handle.close() if hostName == "h3": pingPair = net.get("h4") curr_time = monotonic() - start interval = (4 - curr_time) if (4 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start print( 'curr_time=%s :: %s changed IP STATIC (%s -> 10.0.0.20)' % (curr_time, host.IP(), host.IP())) host.cmd('ifconfig h3-eth0 10.0.0.20 netmask 255:0:0:0') host.setIP("10.0.0.20") sleep(0.5) pingPair = net.get("h4") curr_time = monotonic() - start sleep_time = 6 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = (25 - curr_time) if (25 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h7") curr_time = monotonic() - start interval = (40 - curr_time) if (40 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h6") curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h4": pingPair = net.get("h5") curr_time = monotonic() - start interval = (12.5 - curr_time) if (12.5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h9") curr_time = monotonic() - start interval = (25 - curr_time) if (25 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start print('curr_time=%s :: %s changed IP with DHCP' % (curr_time, host.IP())) host.cmd('dhclient -r') host.cmd('dhclient') sleep(0.5) newIP = host.cmd( "ifconfig | grep 'inet 10.' | cut -f2 -d't' | cut -f1 -d'n'" ) splitnewIP = newIP.split(' ') host.setIP(splitnewIP[1]) pingPair = net.get("h6") curr_time = monotonic() - start sleep_time = 27.5 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = (40 - curr_time) if (40 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 45 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h5": pingPair = net.get("h6") curr_time = monotonic() - start interval = (6 - curr_time) if (6 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h8") curr_time = monotonic() - start interval = (12 - curr_time) if (12 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 12.5 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print("curr_time=%s :: %s start attack" % (curr_time, host.IP())) start_attack = curr_time result = host.cmd( "./runos/src/apps/ddos-defender/scripts/atck.sh %s %s 25" % (host.MAC(), host.IP())) curr_time = monotonic() - start end_attack = curr_time print("curr_time=%s :: %s end attack" % (curr_time, host.IP())) pingPair = net.get("h8") curr_time = monotonic() - start interval = (40 - curr_time) if (40 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 46 - curr_time if sleep_time > 0: sleep(sleep_time) while curr_time < 50: curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) sleep(0.5) file_name = "atck_duration_" + attack_hosts[2] + ".txt" handle = open(file_name, "w") handle.write("'%s' : [%s, %s]" % (host.IP(), start_attack, end_attack)) handle.close() if hostName == "h6": pingPair = net.get("h7") curr_time = monotonic() - start interval = (6 - curr_time) if (6 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h8") curr_time = monotonic() - start interval = (27.5 - curr_time) if (27.5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h7") curr_time = monotonic() - start interval = (40 - curr_time) if (40 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 45 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h7": pingPair = net.get("h9") curr_time = monotonic() - start interval = (6 - curr_time) if (6 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 10 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print('curr_time=%s :: %s move from s4 to s2' % (curr_time, host.IP())) hostIP = host.IP() moveHost(host, net.get('s4'), net.get('s2'), None, net) host = net.get(hostName) host.setIP(hostIP) curr_time = monotonic() - start sleep_time = 17.5 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h8": pingPair = net.get("h9") curr_time = monotonic() - start interval = (15 - curr_time) if (15 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start interval = (27.5 - curr_time) if (27.5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping 10.0.0.20 for %s sec" % (curr_time, host.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh 10.0.0.20 %s" % interval)) curr_time = monotonic() - start sleep_time = 30 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print('curr_time=%s :: %s move from s4 to s1' % (curr_time, host.IP())) hostIP = host.IP() moveHost(host, net.get('s4'), net.get('s1'), None, net) host = net.get(hostName) host.setIP(hostIP) curr_time = monotonic() - start sleep_time = 37.5 - curr_time if sleep_time > 0: sleep(sleep_time) pingPair = net.get("h9") curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h9": pingPair = net.get("h6") curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print( hostName, host.cmd( "./runos/src/apps/ddos-defender/scripts/png.sh %s %s" % (pingPair.IP(), interval))) sys.exit(3) if pid > 0: childs.append(pid) for p in childs: pid, status = os.wait() curr_time = monotonic() - start print("curr_time=%s :: childs end" % curr_time) handleResult = open("runos/src/apps/ddos-defender/tests/infectedMACs.txt", "w") handleResult.write("{") for i in range(len(attack_hosts)): host = net.get(attack_hosts[i]) file_name = "atck_duration_" + attack_hosts[i] + ".txt" handle = open(file_name, "r") result = handle.read() handleResult.write(result) if i < (len(attack_hosts) - 1): handleResult.write(", ") handle.close() cmnd = "sudo rm " + file_name host.cmd(cmnd) handleResult.write("}") handleResult.close() CLI(net) net.stop()
def main(): lg.setLogLevel('info') net = TreeNet(depth=2, fanout=4) # Add NAT connectivity net.addNAT().configDefault() net.start() h1 = net.get('h1') h2 = net.get('h2') h3 = net.get('h3') h4 = net.get('h4') h5 = net.get('h5') h6 = net.get('h6') h7 = net.get('h7') h8 = net.get('h8') h9 = net.get('h9') h10 = net.get('h10') h11 = net.get('h11') h12 = net.get('h12') h13 = net.get('h13') h14 = net.get('h14') h15 = net.get('h15') h16 = net.get('h16') p1 = h1.popen('node ../server/server.js %s & ' % h1.IP()) p16 = h16.popen('node ../server/server.js %s & ' % h16.IP()) sleep(2) p2 = h2.popen('python3 ../mininet/test_company.py 1 %s &' % h1.IP()) p3 = h3.popen('python3 ../mininet/test_company.py 2 %s &' % h1.IP()) p4 = h4.popen('python3 ../mininet/test_company.py 3 %s &' % h1.IP()) p5 = h5.popen('python3 ../mininet/test_company.py 4 %s &' % h1.IP()) p6 = h6.popen('python3 ../mininet/test_company.py 5 %s &' % h16.IP()) p7 = h7.popen('python3 ../mininet/test_company.py 6 %s &' % h16.IP()) p8 = h8.popen('python3 ../mininet/test_company.py 7 %s &' % h1.IP()) p9 = h9.popen('python3 ../mininet/test_company.py 8 %s &' % h1.IP()) p10 = h10.popen('python3 ../mininet/test_company.py 9 %s &' % h1.IP()) p11 = h11.popen('python3 ../mininet/test_company.py 10 %s &' % h16.IP()) p12 = h12.popen('python3 ../mininet/test_company.py 11 %s &' % h16.IP()) p13 = h13.popen('python3 ../mininet/test_company.py 12 %s &' % h16.IP()) p14 = h14.popen('python3 ../mininet/test_company.py 13 %s &' % h16.IP()) p15 = h15.popen('python3 ../mininet/test_company.py 14 %s &' % h16.IP()) process2 = {h2: p2} process3 = {h3: p3} process4 = {h4: p4} process5 = {h5: p5} process6 = {h6: p6} process7 = {h7: p7} process8 = {h8: p8} process9 = {h9: p9} process10 = {h10: p10} process11 = {h11: p11} process12 = {h12: p12} process13 = {h13: p13} process14 = {h14: p14} process15 = {h15: p15} printToConsole(process2) p2.terminate() printToConsole(process3) p3.terminate() printToConsole(process4) p4.terminate() printToConsole(process5) p5.terminate() printToConsole(process6) p6.terminate() printToConsole(process7) p7.terminate() printToConsole(process8) p8.terminate() printToConsole(process9) p9.terminate() printToConsole(process10) p10.terminate() printToConsole(process11) p11.terminate() printToConsole(process12) p12.terminate() printToConsole(process13) p13.terminate() printToConsole(process14) p14.terminate() printToConsole(process15) p15.terminate() p1.terminate() p16.terminate() net.stop()
def simpleTest(): controller = RemoteController('Runos', ip='192.168.56.102', port=6653) switch = partial(OVSKernelSwitch, protocols='OpenFlow13') net = TreeNet(depth=2, fanout=3, controller=controller, switch=switch, cleanup=True) net.start() sleep(3) start = monotonic() results = [] childs = [] for hostName in net.topo.hosts(): try: pid = os.fork() except: print('error: create child process') sys.exit(33) if pid == 0: host = net.get(hostName) if hostName == "h1": pingPair = net.get("h4") curr_time = monotonic() - start interval = (12 - curr_time) if (12 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 12.5 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print("curr_time=%s :: %s start attack" % (curr_time, host.IP())) result = host.cmd("./atck.sh %s %s 25" % (host.MAC(), host.IP())) print("%s end attack" % host.IP()) if hostName == "h2": pingPair = net.get("h1") curr_time = monotonic() - start interval = (4 - curr_time) if (4 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start print('curr_time=%s :: %s changed IP with DHCP' % (curr_time, host.IP())) host.cmd('dhclient -r') host.cmd('dhclient') sleep(0.5) newIP = host.cmd( "ifconfig | grep 'inet 10.' | cut -f2 -d't' | cut -f1 -d'n'" ) splitnewIP = newIP.split(' ') host.setIP(splitnewIP[1]) pingPair = net.get("h3") pingPair.setIP("10.0.0.20") curr_time = monotonic() - start sleep_time = 6 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = (12 - curr_time) if (12 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 12.5 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print("curr_time=%s :: %s start attack" % (curr_time, host.IP())) result = host.cmd("./atck.sh %s %s 25" % (host.MAC(), host.IP())) print("%s end attack" % host.IP()) if hostName == "h3": pingPair = net.get("h9") curr_time = monotonic() - start interval = (4 - curr_time) if (4 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start print( 'curr_time=%s :: %s changed IP STATIC (%s -> 10.0.0.20)' % (curr_time, host.IP(), host.IP())) host.cmd('ifconfig h3-eth0 10.0.0.20 netmask 255:0:0:0') host.setIP("10.0.0.20") sleep(0.5) pingPair = net.get("h9") curr_time = monotonic() - start sleep_time = 6 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = (17 - curr_time) if (17 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h4") curr_time = monotonic() - start interval = (25 - curr_time) if (25 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) result = host.cmd("./png.sh %s %s" % (pingPair.IP(), interval)) handle = open("outputH3-H4.txt", "w") handle.write(result) handle.close() pingPair = net.get("h7") curr_time = monotonic() - start interval = (40 - curr_time) if (40 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h6") curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h4": pingPair = net.get("h5") curr_time = monotonic() - start interval = (17 - curr_time) if (17 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h6") curr_time = monotonic() - start interval = (25 - curr_time) if (25 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) result = host.cmd("./png.sh %s %s" % (pingPair.IP(), interval)) handle = open("outputH4-H6.txt", "w") handle.write(result) handle.close() curr_time = monotonic() - start print('curr_time=%s :: %s changed IP with DHCP' % (curr_time, host.IP())) host.cmd('dhclient -r') host.cmd('dhclient') sleep(0.5) newIP = host.cmd( "ifconfig | grep 'inet 10.' | cut -f2 -d't' | cut -f1 -d'n'" ) splitnewIP = newIP.split(' ') host.setIP(splitnewIP[1]) pingPair = net.get("h6") curr_time = monotonic() - start sleep_time = 27.5 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h5": pingPair = net.get("h6") curr_time = monotonic() - start interval = (6 - curr_time) if (6 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h8") curr_time = monotonic() - start interval = (12 - curr_time) if (12 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 12.5 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print("curr_time=%s :: %s start attack" % (curr_time, host.IP())) result = host.cmd("./atck.sh %s %s 35" % (host.MAC(), host.IP())) print("%s end attack" % host.IP()) if hostName == "h6": pingPair = net.get("h7") curr_time = monotonic() - start interval = (6 - curr_time) if (6 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h8") curr_time = monotonic() - start interval = (27.5 - curr_time) if (27.5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) pingPair = net.get("h7") curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h7": pingPair = net.get("h9") curr_time = monotonic() - start interval = (6 - curr_time) if (6 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start sleep_time = 10 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print('curr_time=%s :: %s move from s4 to s2' % (curr_time, host.IP())) hostIP = host.IP() moveHost(host, net.get('s4'), net.get('s2'), None, net) host = net.get(hostName) host.setIP(hostIP) curr_time = monotonic() - start sleep_time = 17 - curr_time if sleep_time > 0: sleep(sleep_time) pingPair = net.get("h8") curr_time = monotonic() - start interval = (27.5 - curr_time) if (27.5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) result = host.cmd("./png.sh %s %s" % (pingPair.IP(), interval)) handle = open("outputH7-H8.txt", "w") handle.write(result) handle.close() pingPair = net.get("h9") curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h8": pingPair = net.get("h9") curr_time = monotonic() - start interval = (15 - curr_time) if (15 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) curr_time = monotonic() - start interval = (27.5 - curr_time) if (27.5 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping 10.0.0.20 for %s sec" % (curr_time, host.IP(), interval)) print(hostName, host.cmd("./png.sh 10.0.0.20 %s" % interval)) curr_time = monotonic() - start sleep_time = 30 - curr_time if sleep_time > 0: sleep(sleep_time) curr_time = monotonic() - start print('curr_time=%s :: %s move from s4 to s1' % (curr_time, host.IP())) hostIP = host.IP() moveHost(host, net.get('s4'), net.get('s1'), None, net) host = net.get(hostName) host.setIP(hostIP) curr_time = monotonic() - start sleep_time = 37.5 - curr_time if sleep_time > 0: sleep(sleep_time) pingPair = net.get("h9") curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) if hostName == "h9": pingPair = net.get("h6") curr_time = monotonic() - start interval = (50 - curr_time) if (50 - curr_time) >= 1 else 1 print("curr_time=%s :: %s start ping %s for %s sec" % (curr_time, host.IP(), pingPair.IP(), interval)) print(hostName, host.cmd("./png.sh %s %s" % (pingPair.IP(), interval))) sys.exit(3) if pid > 0: childs.append(pid) curr_time = monotonic() - start sleep_time = 27.5 - curr_time if sleep_time > 0: sleep(sleep_time) print('"curr_time=%s :: switch s2 off' % curr_time) SwitchOff(net.get('s2'), net) curr_time = monotonic() - start sleep_time = 40 - curr_time if sleep_time > 0: sleep(sleep_time) print('"curr_time=%s :: switch s2 up' % curr_time) SwitchUp(net.get('s2'), net) for p in childs: pid, status = os.wait() print('childs end') handleResult = open("outputRTT.txt", "w") handle = open("outputH3-H4.txt", "r") result = handle.read() handleResult.write(result) handle.close() handle = open("outputH4-H6.txt", "r") result = handle.read() handleResult.write(result) handle.close() handle = open("outputH7-H8.txt", "r") result = handle.read() handleResult.write(result) handle.close() handleResult.close() try: pid = os.fork() except: print('error: create child process') sys.exit(33) if pid == 0: host = net.get("h1") host.cmd("sudo rm outputH3-H4.txt outputH4-H6.txt outputH7-H9.txt") sys.exit(3) pid, status = os.wait() CLI(net) net.stop() """
def emulated_web(parsed_objects='./parsed-objects', url_list='./url_list'): ''' parsed_objects: a directory storing objects under 'hostname' subfolder url_list: each line is a url to visit ''' hosts = [] objects_for_hosts_dict = {} source_files = {} # get hostnames and hostname:objects pairs in dict for i, (root, folders, files) in enumerate(os.walk(parsed_objects)): if i == 0: hosts = folders else: objects_for_hosts_dict[hosts[i - 1]] = files source_files[root] = files # print(hosts, objects_for_hosts_dict) # list servable-content if not os.path.exists("./servable-content/"): os.mkdir("./servable-content/") # make dir for host, objects_list in objects_for_hosts_dict.iteritems(): with open("./servable-content/" + host, "w+") as f: for i in objects_list: f.write(i + '\n') # each line contains one object # to simply the topo, use default method of tree structure with one level, n leaves server_num = len(hosts) host_server_match = {} for i in range(server_num): host_server_match['h' + str(i + 2)] = hosts[i] # one client as 'h1', with server_num of servers fron 'h2' to 'hn' net = TreeNet(depth=1, fanout=server_num + 1) # Add NAT connectivity net.addNAT().configDefault() net.start() # test this net dumpNodeConnections(net.hosts) # see connectivity print(net.pingAll()) # ping to test connectivity # set up simple http server for num, host in host_server_match.iteritems(): print(str(os.path.join(parsed_objects, host))) net.get(num).cmd('cd %s' % str(os.path.join(parsed_objects, host))) net.get(num).cmd('python -m SimpleHTTPServer 80 &') # test each server http client = net.get('h1') time.sleep(2) # magic, don't delete, otherwise wget doesn't work for num, host in host_server_match.iteritems(): # test for LAN # print(client.cmd('ping -c1 %s' % net.get(num).IP())) print(client.cmd('ping -c1 8.8.8.8')) # test for real print(client.cmd('wget -o - %s' % net.get(num).IP())) # print(client.cmd('wget -o - http://www.google.com')) time.sleep(1) # magic, don't delete, otherwise wget doesn't work if not os.path.exists("./get-responses/"): os.mkdir("./get-responses/") # make dir pass # not finished here