示例#1
0
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():
    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()
示例#3
0
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='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()
    """