def get_connected_devices(self): print("Connected-devices that are new") for device in self.context.list_devices(subsystem='usb', DEVTYPE='usb_device'): if device.find_parent(subsystem='usb', device_type='usb_device') != None: bus_id = device.sys_name devnum = int(device.attributes.get("devnum")) busnum = int(device.attributes.get("busnum")) dev = usb.core.find(address=devnum, bus=busnum) dev_id = read_device.get_descriptors(dev) dev_name = read_device.get_device_name(device.attributes) self.add_connected_device(bus_id, dev_id, dev_name, dev) if self.running_mode == RunningMode.CLI: if dev_id not in self.known_devices.keys(): self.ask_user(dev, dev_name, bus_id, dev_id)
def start_monitor(self, device): bus_id = device.sys_name action = device.action # If a new device is added: # * add it to the connected device dict # * check if the flag for known_devices is on and the devic # is in the known devices dict if action == 'add': devnum = int(device.attributes.get("devnum")) busnum = int(device.attributes.get("busnum")) dev = usb.core.find(address=devnum, bus=busnum) dev_id = read_device.get_descriptors(dev) dev_name = read_device.get_device_name(device.attributes) self.add_connected_device(bus_id, dev_name, dev_id) print("Device added!") print("Device name {}, dev_id {} and bus_id {}".format( dev_name, dev_id, bus_id)) # If a device is removed, simply remove it from the # connected device dict if action == 'remove': print("Device removed!") print("Device bus {}".format(bus_id)) self.remove_connected_device(bus_id)
def usb_monitor_start(self): self.monitor.start() for action, device in self.monitor: bus_id = device.sys_name # Device has been added if action == 'add': devnum = int(device.attributess.get("devnum")) busnum = int(device.attributes.get("busnum")) dev = usb.core.find(address=devnum, bus=busnum) print("A device has been added bus_id {}".format(bus_id)) dev_id = read_device.get_descriptors(dev) dev_name = read_device.get_device_name(device.attributes) self.add_connected_device(bus_id, dev_id, dev_name, dev) if key not in self.known_devices.keys(): self.ask_user(dev_name, key, dev) else: print("It is a trusted device") # Device has been removed if action == 'remove': print( "A device has been removed from the bus_id {}".format(dev)) self.remove_connected_device(dev)
def remove_from_known_devices(self, button): treeselection = self.treeview.get_selection() model, treeiter = treeselection.get_selected() device = {} if treeiter != None: if model[treeiter][0] == Gtk.STOCK_NO: return if model[treeiter][3]: device["Manufacturer"] = model[treeiter][2] if model[treeiter][4]: device["Product"] = model[treeiter][3] busnum, devnum = model[treeiter][2].split("\n")[0].split("Bus")[1].split("Address") devnum = devnum.split()[0] dev = usb.core.find(address=int(devnum), bus=int(busnum)) dev_id = read_device.get_descriptors(dev) self.device_monitor.known_devices.pop(dev_id) model.set_value(treeiter, 0, Gtk.STOCK_NO) else: dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CANCEL, "A USB device must be selected!") dialog.format_secondary_text("The selected USB device was removed") dialog.run()
def usb_monitor_start(self): self.monitor.start() for action, device in self.monitor: bus_id = device.sys_name # Device has been added if action == 'add': devnum = int(device.attributess.get("devnum")) busnum = int(device.attributes.get("busnum")) dev = usb.core.find(address=devnum, bus=busnum) print("A device has been added bus_id {}".format(bus_id)) dev_id = read_device.get_descriptors(dev) dev_name = read_device.get_device_name(device.attributes) self.add_connected_device(bus_id, dev_id, dev_name, dev) if key not in self.known_devices.keys(): self.ask_user(dev_name, key, dev) else: print("It is a trusted device") # Device has been removed if action == 'remove': print("A device has been removed from the bus_id {}".format(dev)) self.remove_connected_device(dev)
def write_to_known_devices(self, button): treeselection = self.treeview.get_selection() model, treeiter = treeselection.get_selected() device = {} if treeiter != None: if model[treeiter][0] == Gtk.STOCK_YES: return if model[treeiter][3]: device["Manufacturer"] = model[treeiter][3] if model[treeiter][4]: device["Product"] = model[treeiter][4] print(device["Product"]) print(device["Manufacturer"]) busnum, devnum = model[treeiter][2].split("\n")[0].split("Bus")[1].split("Address") devnum = devnum.split()[0] dev = usb.core.find(address=int(devnum), bus=int(busnum)) dev_id = read_device.get_descriptors(dev) self.device_monitor.add_to_known_device(dev_id, device, dev) model.set_value(treeiter, 0, Gtk.STOCK_YES) else: dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CANCEL, "A USB device must be selected!") dialog.format_secondary_text("The selected USB device will be written to a 'know_hosts' file") dialog.run()
def start_monitor(self, device): bus_id = device.sys_name action = device.action # If a new device is added: # * add it to the connected device dict # * check if the flag for known_devices is on and the devic # is in the known devices dict if action == 'add': devnum = int(device.attributes.get("devnum")) busnum = int(device.attributes.get("busnum")) dev = usb.core.find(address=devnum, bus=busnum) dev_id = read_device.get_descriptors(dev) dev_name = read_device.get_device_name(device.attributes) self.add_connected_device(bus_id, dev_name, dev_id) print("Device added!") print("Device name {}, dev_id {} and bus_id {}".format(dev_name, dev_id, bus_id)) # If a device is removed, simply remove it from the # connected device dict if action == 'remove': print("Device removed!") print("Device bus {}".format(bus_id)) self.remove_connected_device(bus_id)
def device_detected(self, device): import usb.core bus_id = device.sys_name action = device.action # Check only new connected devices to see if they are on an allowed list if action == 'add': print("Device attached") devnum = int(device.attributes.get("devnum")) busnum = int(device.attributes.get("busnum")) dev = usb.core.find(address=devnum, bus=busnum) # Get the device unique id dev_id = read_device.get_descriptors(dev) if dev_id in self.trusted_devices: self.usb_blocker.get_dbus_method( 'enable_device', 'org.gnome.USBBlocker.device')(bus_id, dev_id) return if read_device.find_device( dev, list( usb.core.find(find_all=True, custom_match=read_device.custom_search( self.allowed_devices)))): print("Device found on the non blocking list -- session") if self.usb_blocker.get_dbus_method( 'enable_device', 'org.gnome.USBBlocker.device')(bus_id, dev_id): self.trusted_devices.append(dev_id) self.write_trusted_devices() return id_notification = self.notification( "USB-inhibitor", "0", "", "Unknown device connected", "An unknown device has been connected to the system. Do you want to allow it to connect?", ["Allow", "Connect", "Block", "Block"], {}, 5) self.number_notifications[id_notification] = [bus_id, dev_id]
def device_detected(self, device): import usb.core bus_id = device.sys_name action = device.action # Check only new connected devices to see if they are on an allowed list if action == 'add': print ("Device attached") devnum = int(device.attributes.get("devnum")) busnum = int(device.attributes.get("busnum")) dev = usb.core.find(address=devnum, bus=busnum) # Get the device unique id dev_id = read_device.get_descriptors(dev) if dev_id in self.trusted_devices: self.usb_blocker.get_dbus_method('enable_device', 'org.gnome.USBBlocker.device')(bus_id, dev_id) return if read_device.find_device(dev, list(usb.core.find(find_all = True, custom_match = read_device.custom_search(self.allowed_devices)))): print ("Device found on the non blocking list -- session") if self.usb_blocker.get_dbus_method('enable_device', 'org.gnome.USBBlocker.device')(bus_id, dev_id): self.trusted_devices.append(dev_id) self.write_trusted_devices() return id_notification = self.notification("USB-inhibitor", "0", "", "Unknown device connected", "An unknown device has been connected to the system. Do you want to allow it to connect?", ["Allow", "Connect", "Block", "Block"], {}, 5) self.number_notifications[id_notification] = [bus_id, dev_id]
def refresh(self, device): treeselection = self.treeview.get_selection() model, treeiter = treeselection.get_selected() if treeiter != None: index = (model.get_path(treeiter)).get_indices()[0] action = device.action bus_id = device.sys_name if action == 'add': devnum = int(device.attributes.get("devnum")) busnum = int(device.attributes.get("busnum")) dev = usb.core.find(address=devnum, bus=busnum) dev_id = read_device.get_descriptors(dev) dev_name = read_device.get_device_name(device.attributes) self.device_monitor.add_connected_device(bus_id, dev_id, dev_name, dev) if dev_id not in self.device_monitor.known_devices.keys(): self.usb_list.append([Gtk.STOCK_NO, True, str(dev), dev_name["Manufacturer"], dev_name["Product"]]) else: self.usb_list.append([Gtk.STOCK_YES, True, str(dev), dev_name["Manufacturer"], dev_name["Product"]]) if action == 'remove': bus_id = self.device_monitor.remove_connected_device(dev) self.remove_from_usb_list(bus_id) if treeiter != None: self.treeview.set_cursor(index) return True