TCPポートスキャナ

TCP 1~65535までのポートスキャンを行う。
高速化のためスレッドを30000作成した。
PCが重たかったらif i%30000のところで調整する。

使用例)
python tcp_scan.py 192.168.x.y

import socket
import sys
import threading

opend_port = []

def port_scan(ip,port):
global opend_port
sd = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sd.settimeout(1)
res = sd.connect_ex((ip,port))
sd.close()
if res == 0:
opend_port.append(port)

def main():
ip = sys.argv[1]

print(f"target is {ip}")

threads = []
max_port = 65535
for i in range(1,max_port+1):
print(f"{i} of {max_port}\r",end="")
port = i
thread = threading.Thread(target=port_scan,args=(ip,port,))
threads.append(thread)
thread.start()
if i%30000 == 0:
for thread in threads:
thread.join()
threads = []

if len(threads):
for thread in threads:
thread.join()

print("opened tcp ports >")
for i in opend_port:
print(i)
return

if __name__ == "__main__":
main()

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です