-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (40 loc) · 1.38 KB
/
main.py
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
# scan open ports to avoid security gaps || to find open ports in target mahcines [illegal]
import socket
import threading
from queue import Queue
target='127.0.0.1'
openPorts=[]
queue=Queue()
def portScan(port):
try: # try to connect
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #AF_INET=internet socket not a unix socket , SOCK_STREAM= using TCP not UDP
sock.connect((target,port)) # connect to the target on this port
return True # the port is open
except :
return False
# ============SimplePortCheck==============
# for port in range(1,1024):
# result=portScan(port)
# if result :
# print("port {} is open!".format(port))
# we gone use queuse with threading to avoid chking the same port more then once
def fill_queue(portList):
for port in portList:
queue.put(port)
def worker():
while not queue.empty():
port=queue.get()
if portScan(port):
print("port {} is open!".format(port))
openPorts.append(port)
portList=range(1,1024)
fill_queue(portList)
threadList=[]
for i in range(10):
thread=threading.Thread(target=worker) # target =target function
threadList.append(thread)
for i in threadList :
i.start()
for i in threadList :
thread.join()
print(openPorts) # we want t print thisonly when all the ports are don that's why we made a join