Пример #1
0
    def test_sniff_host_packet(self):
        tcpd = TCPDump()
        tcps = TCPSender()
        try:

            out = LinuxCLI().cmd('ip l | grep "LOOPBACK" | cut -f 2 -d " "| cut -f 1 -d ":"')
            lo_iface = out.split()[0].rstrip()
            tcpd.start_capture(interface=lo_iface, count=1)
            tcps.start_send(
                interface=lo_iface,
                packet_type="tcp",
                count=1,
                source_ip="127.0.0.1",
                dest_ip="127.0.0.1",
                dest_port=6055,
                source_port=6015,
            )

            ret = tcpd.wait_for_packets(count=1, timeout=3)

            self.assertEquals(1, len(ret))

            self.assertTrue("ethernet" in ret[0])

        finally:
            tcpd.stop_capture()
Пример #2
0
def send_packet():
    time.sleep(5)
    tcps = TCPSender()
    out = LinuxCLI().cmd('ip l | grep "LOOPBACK" | cut -f 2 -d " "| cut -f 1 -d ":"')
    lo_iface = out.split()[0].rstrip()
    tcps.start_send(
        interface=lo_iface,
        packet_type="tcp",
        count=3,
        source_ip="127.0.0.1",
        dest_ip="127.0.0.1",
        dest_port=6055,
        source_port=6015,
    )
Пример #3
0
 def send_arp_packet(self, iface, dest_ip, source_ip=None, command='request',
                     source_mac=None, dest_mac=None, packet_options=None, count=1):
     tcps = TCPSender()
     opt_map = {'command': command}
     if source_mac is not None:
         opt_map = {'smac': source_mac}
     if dest_mac is not None:
         opt_map = {'tmac': dest_mac}
     if source_ip is not None:
         opt_map = {'sip': source_ip}
     if dest_ip is not None:
         opt_map = {'tip': dest_ip}
     opt_map += packet_options
     return tcps.send_packet(self.cli, interface=iface, dest_ip=dest_ip, packet_type='arp',
                             packet_options=opt_map, count=count)
Пример #4
0
    def test_callback(self):
        tcpd = TCPDump()
        tcps = TCPSender()

        out = LinuxCLI().cmd('ip l | grep "LOOPBACK" | cut -f 2 -d " "| cut -f 1 -d ":"')
        lo_iface = out.split()[0].rstrip()

        LinuxCLI().rm("tmp.file")

        tcpd.start_capture(
            interface=lo_iface,
            count=3,
            pcap_filter=PCAP_And(
                [
                    PCAP_Host("localhost", proto="ip", source=True, dest=True),
                    PCAP_Port(6015, proto="tcp", source=True),
                    PCAP_Port(6055, proto="tcp", dest=True),
                ]
            ),
            callback=packet_callback,
            callback_args=("tmp.file",),
            save_dump_file=True,
            save_dump_filename="tcp.callback.out",
        )

        tcps.start_send(
            interface=lo_iface,
            packet_type="tcp",
            count=3,
            source_ip="127.0.0.1",
            dest_ip="127.0.0.1",
            dest_port=6055,
            source_port=6015,
        )

        ret = tcpd.wait_for_packets(count=3)
        tcpd.stop_capture()
        time.sleep(2)

        self.assertEquals(3, len(ret))

        self.assertTrue(LinuxCLI().exists("tmp.file"))
        file_str = LinuxCLI().read_from_file("tmp.file")
        LinuxCLI().rm("tmp.file")

        self.assertEquals(3, file_str.count("END PACKET time["))
Пример #5
0
    def test_sniff_specific_host_packet(self):
        tcpd = TCPDump()
        tcps = TCPSender()

        out = LinuxCLI().cmd('ip l | grep "LOOPBACK" | cut -f 2 -d " "| cut -f 1 -d ":"')
        iface = out.split()[0].rstrip()

        tcpd.start_capture(
            timeout=10,
            interface=iface,
            count=1,
            pcap_filter=PCAP_Or(
                [
                    PCAP_And(
                        [
                            PCAP_Host("localhost", proto="ip", source=True, dest=True),
                            PCAP_Port(6015, proto="tcp", source=True),
                            PCAP_Port(6055, proto="tcp", dest=True),
                            PCAP_LessThanEqual("len", 1500),
                        ]
                    ),
                    PCAP_And(
                        [
                            PCAP_Port(80, proto="tcp", dest=True),
                            PCAP_PortRange(8000, 8500, proto="tcp", source=True),
                            PCAP_LessThanEqual("len", 1500),
                        ]
                    ),
                ]
            ),
        )

        tcps.start_send(
            interface=iface,
            packet_type="tcp",
            count=1,
            source_ip="127.0.0.1",
            dest_ip="127.0.0.1",
            dest_port=6055,
            source_port=6015,
        )

        ret = tcpd.wait_for_packets(count=1, timeout=3)
        tcpd.stop_capture()

        self.assertEquals(1, len(ret))
Пример #6
0
    def test_early_stop(self):
        tcpd = TCPDump()
        tcps = TCPSender()

        out = LinuxCLI().cmd('ip l | grep "LOOPBACK" | cut -f 2 -d " "| cut -f 1 -d ":"')
        lo_iface = out.split()[0].rstrip()

        tcpd.start_capture(
            interface=lo_iface,
            count=0,
            pcap_filter=PCAP_And(
                [
                    PCAP_Host("localhost", proto="ip", source=True, dest=True),
                    PCAP_Port(6015, proto="tcp", source=True),
                    PCAP_Port(6055, proto="tcp", dest=True),
                ]
            ),
        )

        tcps.start_send(
            interface=lo_iface,
            packet_type="tcp",
            count=5,
            source_ip="127.0.0.1",
            dest_ip="127.0.0.1",
            dest_port=6055,
            source_port=6015,
        )

        ret = tcpd.wait_for_packets(count=3, timeout=3)
        self.assertEqual(3, len(ret))
        ret = tcpd.wait_for_packets(count=1, timeout=3)
        self.assertEqual(1, len(ret))

        proc = tcpd.stop_capture()

        ret = tcpd.wait_for_packets(count=1, timeout=3)
        self.assertEqual(1, len(ret))

        self.assertTrue(not proc.is_alive())
Пример #7
0
 def send_tcp_packet(self, iface, dest_ip, source_port, dest_port, packet_options=None, count=1):
     tcps = TCPSender()
     return tcps.send_packet(self.cli, interface=iface, dest_ip=dest_ip, packet_type='tcp',
                             source_port=source_port, dest_port=dest_port,
                             packet_options=packet_options, count=count)
Пример #8
0
 def send_custom_packet(self, iface, **kwargs):
     tcps = TCPSender()
     return tcps.send_packet(self.cli, interface=iface, **kwargs)