Esempio n. 1
0
    def test_to_file(self):
        dump_file = tempfile.NamedTemporaryFile(mode="w")
        amount = 5

        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"),
                                    TCP(srcport=99)), amount)
        th.start()
        try:
            umit.umpa.sniffing.to_file(dump_file.name, amount,
                    "src host 1.2.3.4 and src port 99", "any")
        except UMPASniffingException:
            py.test.skip("no suitable devices for sniffing found. "
                        "propably not sufficent priviliges.")
        finally:
            th.join()

        result = umit.umpa.sniffing.from_file(dump_file.name)
        assert len(result) == amount
        for packet in result:
            assert packet.ip.src == "1.2.3.4"
            assert packet.tcp.srcport == 99

        result = umit.umpa.sniffing.from_file(dump_file.name, 2)
        assert len(result) == 2
        for packet in result:
            assert packet.ip.src == "1.2.3.4"
            assert packet.tcp.srcport == 99
Esempio n. 2
0
 def test_dump(self):
     amount = 5
     dump_file = tempfile.NamedTemporaryFile(mode="w")
     th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"),
                                 TCP(srcport=99)), amount)
     th.start()
     try:
         p = pypcap.open_pcap("any", to_ms=100)
         p.setfilter("src host 1.2.3.4 and src port 99")
         d = pypcap.dumper()
         d.open(p, dump_file.name)
         pkts = []
         for i in xrange(amount):
             pkts.append(p.next())
             d.dump()
     except UMPASniffingException:
         py.test.skip("no suitable devices for sniffing found. "
                     "propably not sufficent priviliges.")
     finally:
         th.join()
     d.close()
     p = pypcap.open_pcap(dump_file.name)
     p.setfilter("src host 1.2.3.4 and src port 99")
     for i, pkt in enumerate(p):
         assert pkt[0] == pkts[i][0]
         # not sure if the below should be equivalent
         assert str(pkt[1]) == str(pkts[i][1])
Esempio n. 3
0
    def test_to_file(self):
        dump_file = tempfile.NamedTemporaryFile(mode="w")
        amount = 5

        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99)),
                        amount)
        th.start()
        try:
            umit.umpa.sniffing.to_file(dump_file.name, amount,
                                       "src host 1.2.3.4 and src port 99",
                                       "any")
        except UMPASniffingException:
            py.test.skip("no suitable devices for sniffing found. "
                         "propably not sufficent priviliges.")
        finally:
            th.join()

        result = umit.umpa.sniffing.from_file(dump_file.name)
        assert len(result) == amount
        for packet in result:
            assert packet.ip.src == "1.2.3.4"
            assert packet.tcp.srcport == 99

        result = umit.umpa.sniffing.from_file(dump_file.name, 2)
        assert len(result) == 2
        for packet in result:
            assert packet.ip.src == "1.2.3.4"
            assert packet.tcp.srcport == 99
Esempio n. 4
0
    def test_from_file_loop(self):
        global idx
        idx = 0
        def cbk(ts, pkt, *args):
            global idx
            assert pkt.ip.src == "1.2.3.6"
            assert pkt.tcp.srcport == 99
            assert ts > 0
            assert len(args) == 1
            idx += 1

        dump_file = tempfile.NamedTemporaryFile(mode="w")
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"),
                                    TCP(srcport=99)), 3)
        th.start()
        umit.umpa.sniffing.sniff(3, device="any", dump=dump_file.name,
                            filter="src host 1.2.3.6 and src port 99")
        th.join()

        idx = 0
        amount = 3
        umit.umpa.sniffing.from_file_loop(dump_file.name, callback=cbk,
                                    callback_args=[amount,])
        assert idx == amount

        idx = 0
        amount = 2
        umit.umpa.sniffing.from_file_loop(dump_file.name, 2, callback=cbk,
                                            callback_args=[amount,])
        assert idx == amount
Esempio n. 5
0
    def test_sniff_loop(self):
        def cbk(ts, pkt, *args):
            assert pkt.ip.src == "1.2.3.6"
            assert pkt.tcp.srcport == 99
            assert ts > 0
            assert len(args) == 2
            if args[0] > args[1]:
                raise UMPASniffingException("test")

        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"), TCP(srcport=99)),
                        2)
        th.start()
        umit.umpa.sniffing.sniff_loop(1,
                                      filter="src 1.2.3.6",
                                      device='any',
                                      callback=cbk,
                                      callback_args=[1, 2])
        th.join()

        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"), TCP(srcport=99)))
        th.start()
        py.test.raises(UMPASniffingException,
                       umit.umpa.sniffing.sniff_loop,
                       1,
                       filter="src 1.2.3.6",
                       device='any',
                       callback=cbk,
                       callback_args=[2, 1])
        th.join()

        py.test.raises(UMPASniffingException, umit.umpa.sniffing.sniff_loop, 1)
Esempio n. 6
0
    def test_sniff_loop(self):
        def cbk(ts, pkt, *args):
            assert pkt.ip.src == "1.2.3.6"
            assert pkt.tcp.srcport == 99
            assert ts > 0
            assert len(args) == 2
            if args[0] > args[1]:
                raise UMPASniffingException("test")

        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"),
                                    TCP(srcport=99)), 2)
        th.start()
        umit.umpa.sniffing.sniff_loop(1, filter="src 1.2.3.6", device='any',
                                            callback=cbk, callback_args=[1,2])
        th.join()

        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"),
                                    TCP(srcport=99)))
        th.start()
        py.test.raises(UMPASniffingException, umit.umpa.sniffing.sniff_loop, 1,
                        filter="src 1.2.3.6", device='any', callback=cbk,
                        callback_args=[2,1] )
        th.join()

        py.test.raises(UMPASniffingException, umit.umpa.sniffing.sniff_loop, 1)
Esempio n. 7
0
    def test_sniff(self):
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"),
                                    TCP(srcport=99)))
        th.start()
        result = umit.umpa.sniffing.sniff(1, device='any', filter="src port 99")
        th.join()

        assert len(result) == 1
        assert result[0].ip.src == '1.2.3.4'
        assert result[0].tcp.srcport == 99
Esempio n. 8
0
    def test_sniff(self):
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99)))
        th.start()
        result = umit.umpa.sniffing.sniff(1,
                                          device='any',
                                          filter="src port 99")
        th.join()

        assert len(result) == 1
        assert result[0].ip.src == '1.2.3.4'
        assert result[0].tcp.srcport == 99
Esempio n. 9
0
    def test_sndout(self):
        packet = umit.umpa.Packet(IP(src="1.2.3.4", dst="67.205.14.183"),
                            TCP(srcport=81, dstport=80))
        th = SendPacket(packet)
        th.start()
        received = umit.umpa.sniffing.sniff_next(filter="dst 67.205.14.183",
                                            device="any")
        th.join()

        assert received.ip.src == packet.ip.src
        assert received.ip.dst == packet.ip.dst
        assert received.tcp.srcport == packet.tcp.srcport
        assert received.tcp.dstport == packet.tcp.dstport
Esempio n. 10
0
    def test_sndout(self):
        packet = umit.umpa.Packet(IP(src="1.2.3.4", dst="67.205.14.183"),
                                  TCP(srcport=81, dstport=80))
        th = SendPacket(packet)
        th.start()
        received = umit.umpa.sniffing.sniff_next(filter="dst 67.205.14.183",
                                                 device="any")
        th.join()

        assert received.ip.src == packet.ip.src
        assert received.ip.dst == packet.ip.dst
        assert received.tcp.srcport == packet.tcp.srcport
        assert received.tcp.dstport == packet.tcp.dstport
Esempio n. 11
0
    def test_sndrcv_loopback(self):
        packet = umit.umpa.Packet(IP(src="1.2.3.4", dst="127.0.0.1"),
                            TCP(srcport=99), Payload(data="foo bar"))
        th = SendPacket(packet)
        th.start()
        received = umit.umpa.sniffing.sniff_next(filter="src 1.2.3.4", device="lo")
        th.join()

        assert received.ip.src == packet.ip.src
        assert received.ip.dst == packet.ip.dst
        assert received.tcp.srcport == packet.tcp.srcport
        assert received.tcp.dstport == packet.tcp.dstport
        assert received.payload.data == packet.payload.data
Esempio n. 12
0
    def test_sndrcv_loopback(self):
        packet = umit.umpa.Packet(IP(src="1.2.3.4", dst="127.0.0.1"),
                                  TCP(srcport=99), Payload(data="foo bar"))
        th = SendPacket(packet)
        th.start()
        received = umit.umpa.sniffing.sniff_next(filter="src 1.2.3.4",
                                                 device="lo")
        th.join()

        assert received.ip.src == packet.ip.src
        assert received.ip.dst == packet.ip.dst
        assert received.tcp.srcport == packet.tcp.srcport
        assert received.tcp.dstport == packet.tcp.dstport
        assert received.payload.data == packet.payload.data
Esempio n. 13
0
 def test_next(self):
     # can't test iterable of the object
     # because pypcap doesn't raise StopIteration
     amount = 5
     th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"),
                                 TCP(srcport=99)), amount)
     th.start()
     try:
         p = pypcap.open_pcap("any", to_ms=100)
         p.setfilter("src host 1.2.3.4 and src port 99")
         for i in xrange(amount):
             packet = p.next()
     except UMPASniffingException:
         py.test.skip("no suitable devices for sniffing found. "
                     "propably not sufficent priviliges.")
     finally:
         th.join()
Esempio n. 14
0
    def test_revports_udp(self):
        # use queue to communicate between threads
        # py.test doesn't catch assertions from threads by itself
        queue = Queue.Queue()

        th = SendPacket(umit.umpa.Packet(IP(), UDP(srcport=80, dstport=0)))
        th.start()
        
        th2 = RevPortsUDPThread("host 127.0.0.1 and port 80", "lo", queue)
        th2.start()
        models.react(1, filter="host 127.0.0.1 and port 80", device="lo",
                    revports=True)
        th.join()
        th2.join()
        if not queue.empty():
            err = queue.get()
            raise AssertionError(err)
        queue.join()
Esempio n. 15
0
    def test_revhosts(self):
        # use queue to communicate between threads
        # py.test doesn't catch assertions from threads by itself
        queue = Queue.Queue()

        th = SendPacket(umit.umpa.Packet(IP(src="67.205.14.183",
                                            dst="127.0.0.1")))
        th.start()
        
        th2 = RevHostsThread("host 67.205.14.183", "any", queue)
        th2.start()
        models.react(1, filter="host 67.205.14.183", device="any",
                    revhosts=True)
        th.join()
        th2.join()
        if not queue.empty():
            err = queue.get()
            raise AssertionError(err)
        queue.join()
Esempio n. 16
0
    def test_sniff_next(self):
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"),
                                    TCP(srcport=99)))
        th.start()
        result = umit.umpa.sniffing.sniff_next(device='any', filter="src port 99")
        th.join()

        assert result.ip.src == '1.2.3.4'
        assert result.tcp.srcport == 99

        # send more, sniff one
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"),
                                    TCP(srcport=99)), 5)
        th.start()
        result = umit.umpa.sniffing.sniff_next(device='any', filter="src port 99")
        th.join()

        assert result.ip.src == '1.2.3.4'
        assert result.tcp.srcport == 99
Esempio n. 17
0
    def test_sniff_next(self):
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99)))
        th.start()
        result = umit.umpa.sniffing.sniff_next(device='any',
                                               filter="src port 99")
        th.join()

        assert result.ip.src == '1.2.3.4'
        assert result.tcp.srcport == 99

        # send more, sniff one
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"), TCP(srcport=99)),
                        5)
        th.start()
        result = umit.umpa.sniffing.sniff_next(device='any',
                                               filter="src port 99")
        th.join()

        assert result.ip.src == '1.2.3.4'
        assert result.tcp.srcport == 99
Esempio n. 18
0
    def test_from_file(self):
        dump_file = tempfile.NamedTemporaryFile(mode="w")
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"),
                                    TCP(srcport=99)), 3)
        th.start()
        umit.umpa.sniffing.sniff(3, device="any", dump=dump_file.name,
                            filter="src host 1.2.3.6 and src port 99")
        th.join()

        result = umit.umpa.sniffing.from_file(dump_file.name)

        assert len(result) == 3
        for packet in result:
            assert packet.ip.src == "1.2.3.6"
            assert packet.tcp.srcport == 99

        result = umit.umpa.sniffing.from_file(dump_file.name, 2)
        assert len(result) == 2
        for packet in result:
            assert packet.ip.src == "1.2.3.6"
            assert packet.tcp.srcport == 99
Esempio n. 19
0
    def test_from_file_loop(self):
        global idx
        idx = 0

        def cbk(ts, pkt, *args):
            global idx
            assert pkt.ip.src == "1.2.3.6"
            assert pkt.tcp.srcport == 99
            assert ts > 0
            assert len(args) == 1
            idx += 1

        dump_file = tempfile.NamedTemporaryFile(mode="w")
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"), TCP(srcport=99)),
                        3)
        th.start()
        umit.umpa.sniffing.sniff(3,
                                 device="any",
                                 dump=dump_file.name,
                                 filter="src host 1.2.3.6 and src port 99")
        th.join()

        idx = 0
        amount = 3
        umit.umpa.sniffing.from_file_loop(dump_file.name,
                                          callback=cbk,
                                          callback_args=[
                                              amount,
                                          ])
        assert idx == amount

        idx = 0
        amount = 2
        umit.umpa.sniffing.from_file_loop(dump_file.name,
                                          2,
                                          callback=cbk,
                                          callback_args=[
                                              amount,
                                          ])
        assert idx == amount
Esempio n. 20
0
    def test_forward(self):
        # use queue to communicate between threads
        # py.test doesn't catch assertions from threads by itself
        queue = Queue.Queue()

        th = SendPacket(umit.umpa.Packet(IP(src="127.0.0.1", 
                        dst="67.205.14.183"), TCP(srcport=888)))
        th.start()
        
        th2 = ForwardThread(
                "host 127.0.0.1 and port 888 and tcp[tcpflags] == 0x0",
                "any", queue)
        th2.start()
        models.react(1,
            filter="host 67.205.14.183 and port 888 and tcp[tcpflags] == 0x0",
            device="any", forward="127.0.0.1")
        th.join()
        th2.join()
        if not queue.empty():
            err = queue.get()
            raise AssertionError(err)
        queue.join()
Esempio n. 21
0
    def test_loop_and_filter(self):
        # XXX it would rather looped than failing
        def cbk(timestamp, pkt, *args):
            assert args[0] == "foobar" # stupid isn't it? :)

        try:
            p = pypcap.open_pcap("any", to_ms=100)
            p.setfilter("src host 1.2.3.4 and src port 99")
            th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"),
                                        TCP(srcport=99)))
            th.start()
            p.loop(1, cbk, "foobar")
            th.join()

            th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.4"),
                                        TCP(srcport=99)), 5)
            th.start()
            p.loop(5, cbk, "foobar")
            th.join()
        except UMPASniffingException:
            py.test.skip("no suitable devices for sniffing found. "
                        "propably not sufficent priviliges.")
Esempio n. 22
0
        def _func(ttl, decrement):
            # use queue to communicate between threads
            # py.test doesn't catch assertions from threads by itself
            queue = Queue.Queue()

            th = SendPacket(umit.umpa.Packet(IP(src="127.0.0.1", dst="127.0.0.1", ttl=ttl),
                            TCP(srcport=888)))
            th.start()
            
            th2 = TTLThread(
                    "host 127.0.0.1 and port 888 and tcp[tcpflags] == 0x0",
                    "any", queue, begin=ttl, decrement=decrement)
            th2.start()
            models.react(1,
                filter="host 127.0.0.1 and port 888 and tcp[tcpflags] == 0x0",
                device="any",
                ttl=decrement)
            th.join()
            th2.join()
            if not queue.empty():
                err = queue.get()
                raise AssertionError(err)
            queue.join()
Esempio n. 23
0
    def test_from_file(self):
        dump_file = tempfile.NamedTemporaryFile(mode="w")
        th = SendPacket(umit.umpa.Packet(IP(src="1.2.3.6"), TCP(srcport=99)),
                        3)
        th.start()
        umit.umpa.sniffing.sniff(3,
                                 device="any",
                                 dump=dump_file.name,
                                 filter="src host 1.2.3.6 and src port 99")
        th.join()

        result = umit.umpa.sniffing.from_file(dump_file.name)

        assert len(result) == 3
        for packet in result:
            assert packet.ip.src == "1.2.3.6"
            assert packet.tcp.srcport == 99

        result = umit.umpa.sniffing.from_file(dump_file.name, 2)
        assert len(result) == 2
        for packet in result:
            assert packet.ip.src == "1.2.3.6"
            assert packet.tcp.srcport == 99