-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
133 lines (115 loc) · 3.62 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
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
# import modules used to create program
import socket
import sys
from queue import Queue
import threading
from datetime import datetime
# target host to scan, also converts to ip from URL
try:
host = socket.gethostbyname(input("Enter your target IP address or URL here: "))
except socket.gaierror as e:
print(f"Invalid hostname: {str(e)}")
sys.exit(1)
# lock thread during print so we can get cleaner outputs
print_lock = threading.Lock()
# custom ports for user to scan later
startPort = 0
endPort = 0
# options for user to scan different ports
print("Select your scan type: ")
print("1: 1 to 1024 port")
print("2: 1 to 65535 port")
print("3: for custom port")
print("4: Exit")
# ask for input scan type
try:
mode = int(input("Select an option: "))
except ValueError:
print("Invalid input. Please enter a number.")
sys.exit(1)
# number of threads are we going to allow for
threadcount = int(input("Thread amount: "))
# if option 3 is selected user can enter the range of ports to start scan and end
if mode == 3:
try:
start_port = int(input("Enter starting port number: "))
end_port = int(input("Enter ending port number: "))
except ValueError:
print("Invalid port number. Please enter a valid integer.")
sys.exit(1)
# display target and sets starting time
print("-" * 50)
print(f"Target IP: {host}")
t1 = datetime.now()
print("Scanning started at: " + str(datetime.now()))
print("-" * 50)
# port scan fucntion
def scan(port):
# creates a new socket and sets timeout to 0.5
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.5)
try:
# tries to connect to host
s.connect((host, port))
# no more sends/receives
s.shutdown(socket.SHUT_RDWR)
with print_lock:
# display port that is open
print(f"Port {port} is OPEN")
except:
# if port is closed, pass instead of returning error
pass
finally:
# close the socket
s.close()
# create the queue and threader
queue = Queue()
def getports(mode):
# scanning port range from 1 to 1024
if mode == 1:
print("\nScanning Started")
for port in range(1, 1024):
queue.put(port)
# scanning port range from 1 to 65535
elif mode == 2:
print("\nScanning Started")
for port in range(1, 65535):
queue.put(port)
# scan custom ports
elif mode == 3:
print("\nScanning Started")
for port in range(startPort, endPort):
queue.put(port)
# exiting
elif mode == 4:
print("Exiting")
sys.exit()
# threader thread pulls a worker
# from a queue and processes it
def worker():
while not queue.empty():
# get a worker from the queue
port = queue.get()
if scan(port):
print("Port {}".format(port))
# Creates, starts and manages our threads
# load ports depending on the mode
def run_scanner(threads, mode):
getports(mode)
# create a list for our threads
thread_list = []
for t in range(threads):
thread = threading.Thread(target=worker)
thread_list.append(thread)
for thread in thread_list:
thread.start()
for thread in thread_list:
# wait until thread terminates
thread.join()
# run the thread count with the amount enter from user input and mode selected
run_scanner(threadcount, mode)
# stop the timer and print how long
print("-" * 50)
t2 = datetime.now()
stop = t2 - t1
print(f"Scanning complete in: {stop}")