def scan_button(self): # Check if files had been closed previously if self.trust_file.closed: self.open_files() # Run code to remove devices currently in frame (loop through devices, destroy) for child in self.device_frame.winfo_children(): child.destroy() # Run Quick Scan to get available hosts nm = nmap.PortScanner() quick_scan = nm.scan(hosts='192.168.0.1/24', arguments='-F') row = 0 hosts = nm.all_hosts() for host in hosts: # OS Scan os_scan = nm.scan(hosts=host, arguments='-O -F') # Try Getting Mac try: mac = os_scan['scan'][host]['addresses']['mac'] except KeyError: # Error if the dictionary key is not available mac = "Mac Address not Available" # Try Getting Manufacturer try: manu = os_scan['scan'][host]['vendor'][mac] except KeyError: manu = "Manufacturer not Available" # Try Getting OS try: op_sys = os_scan['scan'][host]['osmatch'][0]['name'] except KeyError: op_sys = "OS not Available" except IndexError: op_sys = "OS not Available" # Creating Device Object curr_device = Device(host, mac, manu, op_sys) print("Current MAC" + curr_device.get_mac()) # Checking if device is already known device_known = False # Checking Trusted Devices List for device in self.trusted_devices: if curr_device.get_mac() == device[0]: print(curr_device.get_mac() + " is trusted") curr_device.set_type("trusted") frame_bg = '#adebad' # Turn Device's frame background green device_known = True break # Exit loop, unnecessary to loop any more # Checking Suspected Devices List - Only run if device_known is false if not device_known: for device in self.suspect_devices: if curr_device.get_mac() == device[0]: curr_device.set_type("suspicious") print(curr_device.get_mac() + " is suspicious") # Show message box alerting User tkinter.messagebox.showwarning( "Suspicious Device Found", "A Device that was flagged previously as suspicious is on " + "the network") frame_bg = "#ffb3b3" device_known = True break # If device isn't known run code if not device_known: # Creating Pop-Up asking User if they trust device pop_message = "Do you trust this device:\n" +\ "MAC Address: " + curr_device.get_mac() + "\n" +\ "IP Address: " + curr_device.get_ip() + "\n" +\ "Manufacturer: " + curr_device.get_manufacturer() + "\n" +\ "OS: " + curr_device.get_os() user_input = tkinter.messagebox.askquestion( "New Device Found", pop_message) if user_input == "yes": curr_device.set_type("trusted") self.trust_file.write(curr_device.get_mac() + "," + host + "," + curr_device.get_manufacturer() + "," + curr_device.get_os() + "\n") self.trust_file.flush() frame_bg = "#adebad" # If User answers yes, frame bg is green elif user_input == "no": curr_device.set_type("suspicious") self.sus_file.write(curr_device.get_mac() + "," + host + "," + curr_device.get_manufacturer() + "," + curr_device.get_os() + "\n") self.sus_file.flush() frame_bg = "#ffb3b3" # If User answers no, frame bg is red # Creating Frame for device frame = Frame(self.device_frame, bg=frame_bg, highlightbackground="black", highlightthickness=1) frame.grid(row=row, sticky="WE") # "WE" stretches the frames # Elements inside this frame host_lbl = Label(frame, text=host, bg=frame_bg).grid(row=0, column=0, sticky="W") manu_lbl = Label(frame, text=op_sys, bg=frame_bg).grid(row=1, column=0, sticky="W") frame.grid_columnconfigure(0, weight=9) frame.grid_columnconfigure(1, weight=1) info_btn = Button(frame, text="Info", command=lambda curr_device=curr_device: self. info_button(curr_device)) info_btn.grid(row=0, column=1, rowspan=2, padx=2, pady=5, sticky="NESW") row += 1 # Close Files after loop finishes tkinter.messagebox.showinfo( "Scan Finished", "Scan Finished") # Message to let User know scan loop has ended # Closing Trust file self.trust_file.flush( ) # Flush and fsync, writes lists without waiting for buffer os.fsync(self.trust_file.fileno()) self.trust_file.close() # Closing Sus file self.sus_file.flush() os.fsync(self.sus_file.fileno()) self.sus_file.close()