summaryrefslogtreecommitdiff
path: root/lib/scanner.py
blob: f78e4f6010ed2a37ed275f092a6ad04c86756178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import struct
import socket
import threading
import queue
import logging

from enum import Enum, auto

logger = logging.getLogger(__name__)


class PortState(Enum):
    OPEN = auto()
    CLOSED = auto()
    FILTERED = auto()


class TCPScanner:
    def __init__(self, host, ports, timeout=5):
        self.host = host
        self.ports = ports
        self.timeout = timeout
        self.results = []
        self.q = queue.SimpleQueue()
        self.failed = False

    def scan(self, num_threads=5):
        for port in self.ports:
            self.q.put(port)

        threads = []
        for i in range(num_threads):
            t = threading.Thread(target=self.run)
            t.start()
            threads.append(t)

        for t in threads:
            t.join()

    def run(self):
        while True:
            if self.failed:
                break

            try:
                port = self.q.get(block=False)
            except queue.Empty:
                break

            try:
                self._scan(port)
            except Exception as e:
                logger.exception(e)
                self.failed = True
                break

    def _scan(self, port):
        try:
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
            conn.settimeout(self.timeout)

            ret = conn.connect_ex((self.host, port))

            # DATA RECEIVED - SYN ACK
            if ret == 0:
                logger.debug('%s:%d - tcp open (SYN-ACK packet)' % (self.host, port))
                self.results.append((port, PortState.OPEN))

            # RST RECEIVED - PORT CLOSED
            elif ret == 111:
                logger.debug('%s:%d - tcp closed (RST packet)' % (self.host, port))
                self.results.append((port, PortState.CLOSED))

            # ERR CODE 11 - TIMEOUT
            elif ret == 11:
                self.results.append((port, PortState.FILTERED))

            else:
                logger.debug('%s:%d - code %d' % (self.host, port, ret))

            conn.close()

        except socket.timeout:
            self.results.append((port, PortState.FILTERED))