-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_scanner.py
80 lines (64 loc) · 2.4 KB
/
port_scanner.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
#!/usr/bin/env python3
import socket
import threading
import time
import json
def main():
while True:
target = socket.gethostbyname(input('Enter target IP: '))
print('1. Load custom lists')
print('2. Specify range')
print('3. Specify port')
option = input('Option: ')
if option == '1':
print('\nAvaliable lists:')
with open('custom_lists.json') as custom_lists:
custom_lists = json.load(custom_lists)
for i, k in enumerate(custom_lists):
print(f'{i + 1}. {k}')
option = int(input('Option: '))
if option > 0 and option <= len(custom_lists):
for index, category in enumerate(custom_lists):
if (option - 1) == index:
custom_list = custom_lists[category]
choise = category
break
else:
print(f'There is no category with index {option}.\n')
time.sleep(2)
main()
elif option == '2':
start_from = int(input('From: '))
stop_on = int(input('To: '))
custom_list = range(start_from, stop_on)
choise = f'Custom range FROM: {start_from} TO: {stop_on}'
elif option == '3':
port = int(input('Port: '))
custom_list = [port]
choise = f'Check port {port}'
else:
print(f'Invalid option {option}!\n')
time.sleep(1)
main()
clock_start = time.time()
print(f'\n\nStarting port scan for {target} | OPTION: {choise}')
print('========================================================')
print('PORT STATE SERVICE')
for port in custom_list:
t = threading.Thread(target=port_scan, args=(target, port))
t.start()
t.join()
print('========================================================')
print(f'Scan done in {round((time.time() - clock_start), 2)}s\n\n')
def port_scan(ip, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((ip, port))
sock.close()
state = 'open'
service = socket.getservbyport(port)
except:
return
print(f'{port}{" " * (8 - len(str(port)))}{state}{" " * (7 - len(state))}{service}')
if __name__ == '__main__':
main()