import socket
import re
import threading
import time
class PortScan:
def __init__(self, domain):
self.domain = domain
self.IP = None
self.portList = [port for port in range(1, 65535)]
self.openPorts = []
self.thread = 65535
def setIP(self):
url = re.sub('(http|https)://', '', self.domain)
IP = socket.gethostbyname(url)
self.IP = IP
def setPortList(self, portlist):
if portlist:
self.portList = portlist
def setThread(self, thread):
if thread:
self.thread = thread
def response(self, port):
s = socket.socket()
s.settimeout(5)
response = s.connect_ex((self.IP, port))
if response == 0:
time.sleep(1)
self.openPorts.append(port)
s.close()
def main(self):
self.setIP()
for port in self.portList:
print(f"Scanning Port: {port}", end="\r")
t = threading.Thread(target=self.response, args=(port, ))
t.start()
if port % self.thread == 0 or port == self.portList[-1]:
t.join()
print("\n\nPORTS\tSTATE")
for openPort in self.openPorts:
print(openPort, "\tOPEN")
if __name__ == '__main__':
domain = input("\nTarget IP : ")
thread = int(input("并行数:"))
portScanner = PortScan(domain)
if thread:
portScanner.setThread(thread)
portScanner.main()