Ejemplo n.º 1
0
    def test_timeout(self):
        dest_ip = socket.gethostbyname('google.com')
        dest_port = 80
        scanner = PortScanner(dest_ip, dest_port)
        timeout = 0.5
        number = 3

        with socket.socket(socket.AF_INET, socket.SOCK_RAW,
                           socket.IPPROTO_TCP) as sock:
            sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
            sock.settimeout(timeout)

            for n in range(number):
                scanner.send_packet(sock, timeout)

        for t in scanner.conn_time:
            self.assertTrue(t <= timeout * 1000)
Ejemplo n.º 2
0
    def test_received_packets_correct(self):
        dest_ip = socket.gethostbyname('google.com')
        dest_port = 80
        scanner = PortScanner(dest_ip, dest_port)
        timeout = 5
        number = 3

        with socket.socket(socket.AF_INET, socket.SOCK_RAW,
                           socket.IPPROTO_TCP) as sock:
            sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
            sock.settimeout(timeout)

            for n in range(number):
                packet, _ = scanner.send_packet(sock, timeout)
                self.assertTrue(scanner.packet_is_correct(packet))
Ejemplo n.º 3
0
    def test_send_packet_without_response(self):
        dest_ip = socket.gethostbyname('google.com')
        dest_port = 1234
        scanner = PortScanner(dest_ip, dest_port)
        timeout = 2
        number = 3

        with socket.socket(socket.AF_INET, socket.SOCK_RAW,
                           socket.IPPROTO_TCP) as sock:
            sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
            sock.settimeout(timeout)

            for n in range(number):
                response = scanner.send_packet(sock, timeout)
                self.assertEqual(response, (None, timeout * 1000))

        self.assertEqual(scanner.received, [0, 0, 0])
Ejemplo n.º 4
0
    def test_send_packets_to_closed_port(self):
        dest_ip = socket.gethostbyname('localhost')
        dest_port = 1234
        scanner = PortScanner(dest_ip, dest_port)
        timeout = 5
        number = 3

        with socket.socket(socket.AF_INET, socket.SOCK_RAW,
                           socket.IPPROTO_TCP) as sock:
            sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
            sock.settimeout(timeout)

            for n in range(number):
                received_packet, _ = scanner.send_packet(sock, timeout)
                self.assertEqual(received_packet.flags, 'SYN, RST')

        self.assertEqual(scanner.received, [1, 1, 1])