-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportscanner.py
136 lines (92 loc) · 2.71 KB
/
portscanner.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pyfiglet
import platform
import os
import sys
import socket
import threading
from time import sleep
from queue import Queue
def banner():
banner = pyfiglet.figlet_format('PORT SCANNER')
print('\033[93m'+banner+'\033[m')
ports = Queue()
first_port = 1
last_port = 65535
for i in range(first_port, last_port+1):
ports.put(i)
def clear_screen():
if platform.system() == 'Windows':
os.system('cls')
elif platform.system() == 'Linux':
os.system('clear')
def validate_ip(ip):
splited_ip = ip.split('.')
if len(splited_ip) == 4:
for ip_part in splited_ip:
if len(ip_part) <= 3 and len(ip_part) > 0:
return True
else:
return False
else:
return False
def scan(port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
res = sock.connect_ex((target, port))
if res == 0:
print(f'Port {port} is open!')
sock.close()
except:
pass
def worker():
while not ports.empty():
port = ports.get()
scan(port)
ports.task_done()
def start_workers():
threads = []
for i in range(500):
thread = threading.Thread(target=worker, daemon=True)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
def start_scanning():
clear_screen()
banner()
sleep(1)
print(f'Scanning the ports from {first_port} to {last_port}...\n')
start_workers()
banner()
try:
target = sys.argv[1]
except IndexError:
raise ValueError('\033[91mYou need to pass an IP address as an argument! e.g.'+
'python portscanner.py an.ip.address.here\033[m')
print('\033[93m'+
'--------------------Welcome to the port scanner!--------------------'+
'\033[m')
print(f'\n\nThis program will scan now the ports of the address {target}')
while True:
ip_is_okay = input('Is this okay? [Y/N]')
if ip_is_okay.strip().upper() == 'Y':
break
elif ip_is_okay.strip().upper() == 'N':
while True:
new_ip = input('Enter a new IP: ')
if validate_ip(new_ip):
target = new_ip
print('\033[34mYou succefully changed the target IP adress.\033[m')
sleep(1.5)
break
else:
print('''\033[91mERROR! The IP you writed is invalid. Please write
an valid ip address, in the format: 000.000.000.000\033[m \n''')
sleep(2)
continue
break
else:
continue
start_scanning()
print('\n\033[93mAll the ports were scanned. Goodbye!\033[m')