多线程实现TCP SYN端口扫描

 1  2 from scapy.all import *
 3 import optparse
 4 import sys
 5 import ipaddress
 6 import threading
 7 import queue
 8 
 9 class SynScanner:
10     def __init__(self) -> None:
11         self.target = self.get_params()[0]
12         self.port_list = self.get_params()[1]
13         self.q = queue.Queue()
14         self.open_ports = []
15 
16     def get_params(self):
17         parser = optparse.OptionParser('Usage: <Program> -t target -p port or ports or port range')
18         parser.add_option('-t', '--target', dest='target', type='string', help='Specify target to scan ')
19         parser.add_option('-p', '--ports', dest='ports', type='string', help="Specify port or ports separated by comma or port range")
20         options, args = parser.parse_args()
21         if options.target is None or options.ports is None:
22             print(parser.usage)
23             sys.exit(0)
24         
25         self.check_ip_validity(options.target)
26         return options.target, self.get_ports_list(options.ports)
27     
28     def check_ip_validity(self,ip):
29         try:
30             ipaddress.ip_address(ip)
31             return True
32         except:
33             print("IP address is incorrect")
34             sys.exit(0)
35     
36     def get_ports_list(self, ports):
37         port_list = []
38         if '-' in ports:
39             start_port = int(ports.split('-')[0])
40             end_port = int(ports.split('-')[1])
41             for port in range(start_port, end_port+1):
42                 port_list.append(port)
43             return port_list
44         elif ',' in ports:
45             temp = ports.split(',')
46             for each in temp:
47                 port_list.append(int(each))
48         else:
49             try:
50                 port_list.append(int(ports))
51                 return port_list
52             except:
53                 print("You inputted wrong ports")
54                 sys.exit(0)
55     
56     def port_scan(self, port):
57         try:
58             packet = IP(dst=self.target)/TCP(dport=port, flags='S')
59             ans, unans = sr(packet, timeout=1, verbose=False)
60             if ans:
61                 response_flag = ans[0][1]['TCP'].flags
62                 if response_flag == 18:
63                     self.q.put(port)
64         except:
65             pass
66     
67     def run(self):
68         for port in self.port_list:
69             t = threading.Thread(target=self.port_scan, args=(port, ))
70             t.start()
71         
72         while not self.q.empty():
73             self.open_ports.append(self.q.get())
74         
75         print("Scan result for %s" % self.target)
76         
77         for port in sorted(self.open_ports):
78             print("\tPort \t%d \tis open" % port)
79 
80 if __name__ == '__main__':
81     synscanner = SynScanner()
82     synscanner.run()
83         
84 
85     

  实现效果如下图所示:

 

posted @ 2022-05-26 11:05  Jason_huawen  阅读(231)  评论(0编辑  收藏  举报