def watch(self, vnic): """ creates a monitor interface name vnic on this Radio, deleting all other associated interfaces to avoid conflicts with external processes & the system :param vnic: name of virtual interface to create in monitor mode """ # get all associated interfaces and delete them for iface in self.ifaces: iw.devdel(iface['nic']) try: iw.phyadd(self._phy, vnic, 'monitor') # add monitor mode vnic iwt.ifconfig(vnic, 'up') # & bring it up self._vnic = vnic except iwt.IWToolsException as e: raise RadioException(e) except iw.IWException as e: ecode = iw.ecode(e) if ecode == RDO_PERMISSION: raise RadioNoPermission(e) elif ecode == RDO_BUSY: raise RadioBusyDevice(e) elif ecode == RDO_NONIC: raise RadioNicNotFound(e) elif ecode == RDO_INVALIDARG: raise RadioInvalidArg(e) elif ecode == RDO_EXCEED: raise RadioExceedOpenFiles(e) elif ecode == RDO_OPERATION: raise RadioOpNotPermitted(e) else: raise RadioException(e)
def txpwr(self): """ :returns: current transmit power (in dBm) """ nic = self._vnic if self._vnic else self._nic if nic is None: raise RadioNotInitialized try: return iw.txpwrget(nic) except iw.IWException as e: ecode = iw.ecode(e) if ecode == RDO_PERMISSION: raise RadioNoPermission(e) elif ecode == RDO_BUSY: raise RadioBusyDevice(e) elif ecode == RDO_NONIC: raise RadioNicNotFound(e) elif ecode == RDO_INVALIDARG: raise RadioInvalidArg(e) elif ecode == RDO_EXCEED: raise RadioExceedOpenFiles(e) elif ecode == RDO_OPERATION: raise RadioOpNotPermitted(e) else: raise RadioException(e)
def settxpwr(self, pwr, option='fixed'): """ set tx power :param pwr: desired txpower in dBm :param option: desired option oneof {'fixed'|'limit'|'auto'} """ # this will not work for all cards try: iw.txpwrset(self._phy, pwr, option) except iw.IWException as e: ecode = iw.ecode(e) if ecode == RDO_PERMISSION: raise RadioNoPermission(e) elif ecode == RDO_BUSY: raise RadioBusyDevice(e) elif ecode == RDO_NONIC: raise RadioNicNotFound(e) elif ecode == RDO_INVALIDARG: raise RadioInvalidArg(e) elif ecode == RDO_EXCEED: raise RadioExceedOpenFiles(e) elif ecode == RDO_OPERATION: raise RadioOpNotPermitted(e) else: raise RadioException(e)
def _setup(self, nic, vnic=None, spoofed=None): """ initialize immutable properties from network interface card :param nic: name of nic to initialize :param vnic: name of virtual interface to create in monitor mode :param spoofed: spoofed hwaddress if desired (can be 'random') """ try: interface = None self._phy, ifaces = iw.dev(nic) for iface in ifaces: if iface['nic'] == nic: interface = iface self._nic = nic self._nicmode = interface['type'] self._hwaddr = self._gethwaddr() self._ifindex = self._getifindex( ) # see /sys/class/net/<nic>/ifindex self._channels = iw.chget(self._phy) self._standards = iwt.iwconfig(nic, 'Standards') # iw self._txpwr = iwt.iwconfig(nic, 'Tx-Power') self._driver = driver(nic) self._chipset = chipset(self._driver) except TypeError: raise RadioException("Unexpected error") except iwt.IWToolsException as e: raise RadioException(e) except iw.IWException as e: ecode = iw.ecode(e) if ecode == RDO_PERMISSION: raise RadioNoPermission(e) elif ecode == RDO_BUSY: raise RadioBusyDevice(e) elif ecode == RDO_NONIC: raise RadioNicNotFound(e) elif ecode == RDO_INVALIDARG: raise RadioInvalidArg(e) elif ecode == RDO_EXCEED: raise RadioExceedOpenFiles(e) elif ecode == RDO_OPERATION: raise RadioOpNotPermitted(e) else: raise RadioException(e) # create a spoofed address? if spoofed: mac = None if spoofed.lower() == 'random' else spoofed if mac: self.cloak(mac) # create in montor mode? if vnic: self.watch(vnic)
def mode(self): """ :returns the mode of this Radio """ # does not work nic = self._vnic if self._vnic else self._nic if nic is None: raise RadioNotInitialized try: _, ifaces = iw.dev(nic) for iface in ifaces: if iface['nic'] == nic: return iface['type'] return None except iw.IWException as e: ecode = iw.ecode(e) if ecode == RDO_PERMISSION: raise RadioNoPermission(e) elif ecode == RDO_BUSY: raise RadioBusyDevice(e) elif ecode == RDO_NONIC: raise RadioNicNotFound(e) elif ecode == RDO_INVALIDARG: raise RadioInvalidArg(e) elif ecode == RDO_EXCEED: raise RadioExceedOpenFiles(e) elif ecode == RDO_OPERATION: raise RadioOpNotPermitted(e) else: raise RadioException(e)
def ifaces(self): """ current interfaces belonging to this Radio :returns: all (current) interfaces on this Radio as a list of dicts """ if not self._phy: raise RadioNotInitialized try: return iw.dev()[self._phy] except iw.IWException as e: ecode = iw.ecode(e) if ecode == RDO_PERMISSION: raise RadioNoPermission(e) elif ecode == RDO_BUSY: raise RadioBusyDevice(e) elif ecode == RDO_NONIC: raise RadioNicNotFound(e) elif ecode == RDO_INVALIDARG: raise RadioInvalidArg(e) elif ecode == RDO_EXCEED: raise RadioExceedOpenFiles(e) elif ecode == RDO_OPERATION: raise RadioOpNotPermitted(e) else: raise RadioException(e) except KeyError: raise RadioNicNotFound("No such device (-19)")
def setch(self, ch, chw=None): """ sets channel :param ch: channel (number) to set card to :param chw: channel width to set to default is None """ if not self._phy: raise RadioNotInitialized if not chw in RDO_CHWS: raise RadioInvalidArg("Channel width is invalid (-19)") try: iw.chset(self._phy, ch, chw) except iw.IWException as e: ecode = iw.ecode(e) if ecode == RDO_PERMISSION: raise RadioNoPermission(e) elif ecode == RDO_BUSY: raise RadioBusyDevice(e) elif ecode == RDO_NONIC: raise RadioNicNotFound(e) elif ecode == RDO_INVALIDARG: raise RadioInvalidArg(e) elif ecode == RDO_EXCEED: raise RadioExceedOpenFiles(e) elif ecode == RDO_OPERATION: raise RadioOpNotPermitted(e) else: raise RadioException(e)
def unwatch(self): """ removes the virtual interface this Radio (and adds the original nic back to it's orginal state) NOTE: this does not restore all interfaces that were deleted by watch """ if not self._vnic: raise RadioException("Radio is not in monitor mode") try: iw.devdel(self._vnic) iw.phyadd(self._phy, self._nic, self._nicmode) iwt.ifconfig(self._nic, 'up') self._vnic = None except iwt.IWToolsException as e: raise RadioException(e) except iw.IWException as e: ecode = iw.ecode(e) if ecode == RDO_PERMISSION: raise RadioNoPermission(e) elif ecode == RDO_BUSY: raise RadioBusyDevice(e) elif ecode == RDO_NONIC: raise RadioNicNotFound(e) elif ecode == RDO_INVALIDARG: raise RadioInvalidArg(e) elif ecode == RDO_EXCEED: raise RadioExceedOpenFiles(e) elif ecode == RDO_OPERATION: raise RadioOpNotPermitted(e) else: raise RadioException(e)