티스토리 뷰

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import gc
import errno
import socket
import optparse
from threading import Thread
from Queue import Queue
 
socket_timeout = 0.1
thread_size = 100
default_max_port = 65535
gc_cycle = 1000
max_retries = 0
 
in_queue = Queue()
out_queue = Queue()
 
def test_conn(host, port):
    try:
        for _ in range(max_retries+1):
            try:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.settimeout(socket_timeout)
                sock.connect((host, port))
                if sock:
                    sock.close()
                    return True
            except socket.timeout:
                pass
    except socket.error as e:
        if e.errno != errno.ECONNREFUSED:
            raise e
    return False
 
def set_scan_ports(ports):
    for port in ports:
        if port:
            in_queue.put(port)
    for i in range(thread_size):
        in_queue.put(None)
 
def thread_scan_port(host):
    count = 0
    while True:
        port = in_queue.get()
        if port is None:
            in_queue.task_done()
            break
 
        out = test_conn(host, port)
        data = {
            'port': port,
            'return': out
        }
        out_queue.put(data)
        in_queue.task_done()
 
        count += 1
        if count == gc_cycle:
            gc.collect()
            count = 0
 
def scan_port(host, ports=None, thread=True):
    #socket.setdefaulttimeout(socket_timeout)
    if not ports:
        ports = range(1, default_max_port+1)
    set_scan_ports(ports)
    opened_ports = []
    if thread:
        ts = []
        for i in range(thread_size):
            t = Thread(target=thread_scan_port, args=(host,))
            t.start()
            ts.append(t)
 
        for t in ts:
            t.join()
 
        while not out_queue.empty():
            out = out_queue.get()
            if out['return']:
                opened_ports.append(out['port'])
    else:
        for port in ports:
            if test_conn(host, port):
                opened_ports.append(port)
    return opened_ports
 
# Main Function Area
def main():
    parser = optparse.OptionParser(usage='Usage %Prog -H <Target Host> -p <Target Port>')
    parser.add_option('-H', dest = 'host', type ='string', help = 'Specify Target IP')
    parser.add_option('-p', dest = 'ports', type ='string', help = 'Specify Target Port')
    (options, args) = parser.parse_args()
 
    host = options.host
    ports = options.ports
    if ports:
        ports = str(ports).split(',')
        _ports = []
        for port in ports:
            if port.count("-"):
                start, end = port.split("-"1)
                _ports += range(int(start), int(end)+1)
            else:
                _ports.append(int(port))
        ports = _ports
 
    if host == None:
        print parser.usage
        os._exit(1)
    print scan_port(host, ports)
 
if __name__ == '__main__':
    main()
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
페이지
«   2024/11   »
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
글 보관함