-
Notifications
You must be signed in to change notification settings - Fork 0
/
portscanner.py
159 lines (122 loc) · 3.95 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sys
import socket
from datetime import datetime
target = input("Enter IP or a Host to scan: ")
tcp = [21, 22, 23, 25, 53, 80, 110, 111, 135, 139, 143, 443, 993, 995, 1723, 3306, 3389, 5900, 8080]
udp = [53, 67, 68, 69, 123, 135, 137, 138, 139, 161, 162, 445, 500, 514, 520, 631, 1434, 1900, 4500, 49152]
if len(target) > 2:
targetIP = socket.gethostbyname(target)
else:
print("\nPlease give a IP or a Host\nExiting script...")
sys.exit()
print('\n')
print("*" * 55)
print("Scanning IP: " + target)
print("Scanning started at: " + str(datetime.now()))
print("*" * 55)
print('\n')
print("Select a option")
input = input(
"""
1) Normal scan only 999 ports \n
2) Scan all 65535 ports\n
3) Scan Typical TCP ports\n
4) Scan Typical UDP ports\n
5) Scan Both TCP and UDP ports\n
""")
print("\n")
if input == '1':
print("Staring a normal scan of 999 ports\n")
try:
for port in range(1, 65535):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((target, port))
if result == 0:
print("Port {} is open".format(port))
s.close()
except KeyboardInterrupt:
print("\n Exiting the script ")
sys.exit()
except socket.gaierror:
print("\n Could not find IP or Host ")
sys.exit()
except socket.error:
print("\n Server do not respond ")
sys.exit()
elif input == '2':
print("Staring scan of all 65535 ports\n")
try:
for port in range(1, 1000):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((target, port))
if result == 0:
print("Port {} is open".format(port))
s.close()
except KeyboardInterrupt:
print("\n Exiting the script ")
sys.exit()
except socket.gaierror:
print("\n Could not find IP or Host ")
sys.exit()
except socket.error:
print("\n Server do not respond ")
sys.exit()
elif input == '3':
print("Starting a TCP port scan\n")
try:
for port in tcp:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((target, port))
if result == 0:
print("Port {} is open".format(port))
s.close()
except KeyboardInterrupt:
print("\n Exiting the script ")
sys.exit()
except socket.gaierror:
print("\n Could not find IP or Host ")
sys.exit()
except socket.error:
print("\n Server do not respond ")
sys.exit()
elif input == '4':
print("Starting a UDP port scan\n")
try:
for port in udp:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((target, port))
if result == 0:
print("Port {} is open".format(port))
s.close()
except KeyboardInterrupt:
print("\n Exiting the script ")
sys.exit()
except socket.gaierror:
print("\n Could not find IP or Host ")
sys.exit()
except socket.error:
print("\n Server do not respond ")
sys.exit()
elif input == '5':
print("Starting both a TCP and UDP port scan\n")
try:
for port in tcp + udp:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((target, port))
if result == 0:
print("Port {} is open".format(port))
s.close()
except KeyboardInterrupt:
print("\n Exiting the script ")
sys.exit()
except socket.gaierror:
print("\n Could not find IP or Host ")
sys.exit()
except socket.error:
print("\n Server do not respond ")
sys.exit()