-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWithGUIPythonProject.py
146 lines (122 loc) · 4.5 KB
/
WithGUIPythonProject.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
import tkinter as tk
from tkinter import ttk, messagebox
import socket
import scapy.all as scapy
from colorama import Fore, Style
import json
# Function to scan multiple devices in a network
def scanMultiple(ip):
arp_req_frame = scapy.ARP(pdst=ip)
broadcast_ether_frame = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
broadcast_ether_arp_req_frame = broadcast_ether_frame / arp_req_frame
answered_list = scapy.srp(broadcast_ether_arp_req_frame, timeout=1, verbose=False)[
0
]
result = []
for element in answered_list:
ip = element[1].psrc
mac = element[1].hwsrc
result.append({"ip": ip, "mac": mac})
return result
# Function to scan a single IP
def scanSingle(ip):
arp_req_frame = scapy.ARP(pdst=ip)
broadcast_ether_frame = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
broadcast_ether_arp_req_frame = broadcast_ether_frame / arp_req_frame
answered_list = scapy.srp(broadcast_ether_arp_req_frame, timeout=1, verbose=False)[
0
]
result = []
for element in answered_list:
ip = element[1].psrc
mac = element[1].hwsrc
hostname = "Unknown"
try:
hostname = socket.gethostbyaddr(ip)[0]
except socket.herror:
hostname = "Unknown"
result.append({"ip": ip, "mac": mac, "hostname": hostname})
return result
# Function to scan ports
def scan_ports(ip, ports):
open_ports = []
for port in ports:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.5)
if s.connect_ex((ip, port)) == 0:
open_ports.append(port)
except Exception:
pass
return open_ports
# Function to display results in a new window
def display_results(results, port_results=None):
result_window = tk.Toplevel()
result_window.title("Scan Results")
result_window.geometry("600x400")
text_area = tk.Text(result_window, wrap=tk.WORD)
text_area.pack(fill=tk.BOTH, expand=True)
if isinstance(results, list): # Multiple results
for device in results:
text_area.insert(tk.END, f"IP: {device['ip']}, \nMAC: {device['mac']}\n")
else: # Single result
text_area.insert(
tk.END,
f"IP: {results['ip']}, \nMAC: {results['mac']}, \nHostname: {results['hostname']}\n",
)
if port_results:
text_area.insert(tk.END, "\nOpen Ports:\n")
for port in port_results:
text_area.insert(tk.END, f"Port: {port}\n")
text_area.configure(state=tk.DISABLED)
# Main GUI Application
def main_gui():
def scan_network():
network = network_entry.get()
if not network:
messagebox.showerror("Error", "Please enter a network range!")
return
results = scanMultiple(network)
if results:
display_results(results)
save_results(results)
else:
messagebox.showinfo("No Results", "No devices found.")
def scan_single_ip():
ip = ip_entry.get()
if not ip:
messagebox.showerror("Error", "Please enter an IP address!")
return
results = scanSingle(ip)
if results:
ports = [20, 21, 22, 23, 25, 80, 443, 8080]
open_ports = []
for device in results:
open_ports += scan_ports(device["ip"], ports)
display_results(results[0], open_ports)
save_results(results)
else:
messagebox.showinfo("No Results", "No devices found.")
def save_results(results, filename="scan_results.json"):
with open(filename, "w") as f:
json.dump(results, f, indent=4)
messagebox.showinfo("Saved", f"Results saved to {filename}")
# Create main window
root = tk.Tk()
root.title("Network Scanner")
root.geometry("400x300")
# Network range scanning section
ttk.Label(root, text="Network Range (e.g., 192.168.1.0/24):").pack(pady=5)
network_entry = ttk.Entry(root, width=40)
network_entry.pack(pady=5)
ttk.Button(root, text="Scan Network", command=scan_network).pack(pady=5)
# Single IP scanning section
ttk.Label(root, text="Single IP (e.g., 192.168.1.1):").pack(pady=5)
ip_entry = ttk.Entry(root, width=40)
ip_entry.pack(pady=5)
ttk.Button(root, text="Scan Single IP", command=scan_single_ip).pack(pady=5)
# Exit button
ttk.Button(root, text="Exit", command=root.quit).pack(pady=20)
root.mainloop()
if __name__ == "__main__":
main_gui()