-
Notifications
You must be signed in to change notification settings - Fork 0
/
PortScan.py
120 lines (98 loc) · 3.67 KB
/
PortScan.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
import socket
import os
import threading
import time
from pystyle import Colors, Colorate
from datetime import datetime
os.system("cls" if os.name == "nt" else "clear")
print(
Colorate.Vertical(
Colors.blue_to_red,
"""
██████╗ ██████╗ ██████╗ ████████╗███████╗ ██████╗ █████╗ ███╗ ██╗
██╔══██╗██╔═══██╗██╔══██╗╚══██╔══╝██╔════╝██╔════╝██╔══██╗████╗ ██║
██████╔╝██║ ██║██████╔╝ ██║ ███████╗██║ ███████║██╔██╗ ██║
██╔═══╝ ██║ ██║██╔══██╗ ██║ ╚════██║██║ ██╔══██║██║╚██╗██║
██║ ╚██████╔╝██║ ██║ ██║ ███████║╚██████╗██║ ██║██║ ╚████║
╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═══╝
github.com/dimxz
""",
3,
)
)
def get_time():
now = datetime.now()
time = now.strftime("%H:%M:%S")
return time
start = datetime.now().strftime("%d-%m-%Y-%H%M%S")
def scan_port(host, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((host, port))
if result == 0:
print("[+]", Colorate.Color(Colors.green, f"Port {port}: Open"))
return True
else:
print("[-]", Colorate.Color(Colors.red, f"Port {port}: Closed"))
return False
except Exception as e:
print(
"[!]", Colorate.Color(Colors.red, f"Error while scanning port {port}: {e}")
)
return False
finally:
sock.close()
def multi(host, port, open_ports):
if scan_port(host, port):
open_ports.append(port)
def main():
timeNow = get_time()
host = input(
"[{}] ".format(timeNow)
+ Colorate.Horizontal(Colors.blue_to_red, "[?] Enter target host: ")
)
timeNow = get_time()
start_port = int(
input(
"[{}] ".format(timeNow)
+ Colorate.Horizontal(Colors.blue_to_red, "[?] Enter start port: ")
)
)
timeNow = get_time()
end_port = int(
input(
"[{}] ".format(timeNow)
+ Colorate.Horizontal(Colors.blue_to_red, "[?] Enter end port: ")
)
)
timeNow = get_time()
print(
"[{}] ".format(timeNow)
+ Colorate.Horizontal(
Colors.blue_to_red,
f"[+] Scanning ports {start_port} to {end_port} on {host} .... ",
)
)
open_ports = []
threads = []
for port in range(start_port, end_port + 1):
t = threading.Thread(target=multi, args=(host, port, open_ports))
threads.append(t)
t.start()
for t in threads:
t.join()
if open_ports:
filename = f"{host}_{start}.txt"
with open(filename, "w") as file:
file.write(f"Open ports on {host}:\n")
for port in open_ports:
file.write(f"{port}\n")
print(
"[{}] ".format(timeNow)
+ Colorate.Horizontal(
Colors.blue_to_red, f"[+] All open ports saved to : {filename}"
)
)
if __name__ == "__main__":
main()