1. 程式人生 > 其它 >利用Python實現批量獲取埠Banner的工具

利用Python實現批量獲取埠Banner的工具

  主要用到的模組包括:

  Socket

  threading多執行緒模組

import socket
import sys
import optparse
import ipaddress
import threading



class BannerGrab:
    def __init__(self) -> None:
        self.target = self.get_params()

    def get_params(self):
        parser = optparse.OptionParser('Usage: <Program> -t target 
') parser.add_option('-t','--target', dest='target', type='string', help='Specify target IP address') options, args = parser.parse_args() if options.target is None: print(parser.usage) sys.exit(0) try: ipaddress.ip_address(options.target)
except: print('[-] Please enter IP address') sys.exit(0) return options.target def banner_grab(self, port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(1) s.connect((self.target, port)) banner
= s.recv(1024).decode('utf-8') print("Port %d is open \t %s" % (port, banner)) except: pass def run(self): try: for port in range(1,65535): t = threading.Thread(target=self.banner_grab, args=(port, )) t.start() except KeyboardInterrupt: print("[-] Exit the program now!") sys.exit(0) if __name__ == "__main__": banner = BannerGrab() banner.run()