def _readradio(self, conf, rtype='Recon'): """ read in the rtype radio configuration from conf and return parsed dict """ # don't bother if specified radio is not present if not conf.get(rtype, 'nic') in wifaces(): raise RuntimeError("Radio %s not present/not wireless" % conf.get(rtype, 'nic')) # get nic and set role setting default antenna config r = { 'nic': conf.get(rtype, 'nic'), 'spoofed': None, 'ant_gain': 0.0, 'ant_loss': 0.0, 'ant_offset': 0.0, 'ant_type': 0.0, 'desc': "unknown", 'scan_start': None, 'role': rtype.lower(), 'antennas': {} } # get optional properties if conf.has_option(rtype, 'spoof'): r['spoofed'] = conf.get(rtype, 'spoof') if conf.has_option(rtype, 'desc'): r['desc'] = conf.get(rtype, 'desc') # process antennas - get the number first try: nA = conf.getint(rtype, 'antennas') if conf.has_option( rtype, 'antennas') else 0 except ValueError: nA = 0 if nA: # antennas has been specified, force correct/valid specifications try: gain = map(float, conf.get(rtype, 'antenna_gain').split(',')) if len(gain) != nA: raise RuntimeError('gain') atype = conf.get(rtype, 'antenna_type').split(',') if len(atype) != nA: raise RuntimeError('type') loss = map(float, conf.get(rtype, 'antenna_loss').split(',')) if len(loss) != nA: raise RuntimeError('loss') rs = conf.get(rtype, 'antenna_xyz').split(',') xyz = [] for t in rs: xyz.append(tuple(map(int, t.split(':')))) if len(xyz) != nA: raise RuntimeError('xyz') except ConfigParser.NoOptionError as e: logging.warn("Antenna %s not specified" % e) #raise DySKTConfException("%s" % e) except ValueError as e: logging.warn("Invalid type for %s antenna configuration - %s") except RuntimeError as e: logging.warn( "Antenna %s has invalid number of specifications" % e) else: r['antennas']['num'] = nA r['antennas']['gain'] = gain r['antennas']['type'] = atype r['antennas']['loss'] = loss r['antennas']['xyz'] = xyz else: # none, set all empty r['antennas']['num'] = 0 r['antennas']['gain'] = [] r['antennas']['type'] = [] r['antennas']['loss'] = [] r['antennas']['xyz'] = [] # get scan pattern r['dwell'] = conf.getfloat(rtype, 'dwell') if r['dwell'] <= 0: raise ValueError("dwell must be > 0") r['scan'] = parsechlist(conf.get(rtype, 'scan'), 'scan') r['pass'] = parsechlist(conf.get(rtype, 'pass'), 'pass') if conf.has_option(rtype, 'scan_start'): try: scanspec = conf.get(rtype, 'scan_start') if ':' in scanspec: (ch, chw) = scanspec.split(':') else: ch = scanspec chw = None ch = int(ch) if ch else r['scan'][0][0] if not chw in iw.IW_CHWS: chw = r['scan'][0][1] r['scan_start'] = (ch, chw) if (ch, chw) in r['scan'] else r['scan'][0] except ValueError: # use default r['scan_start'] = r['scan'][0] else: r['scan_start'] = r['scan'][0] return r
def _setup(self, conf): """ 1) sets radio properties as specified in conf 2) prepares specified nic for monitoring and binds it 3) creates a scan list and compile statistics """ # if the nic specified in conf is present, set it if conf['nic'] not in iwt.wifaces(): raise RuntimeError("%s:iwtools.wifaces:not found" % conf['role']) self._nic = conf['nic'] self._role = conf['role'] # get the phy and associated interfaces try: (self._phy, ifaces) = iw.dev(self._nic) self._mac = ifaces[0]['addr'] except (KeyError, IndexError): raise RuntimeError("%s:iw.dev:error getting interfaces" % self._role) except iw.IWException as e: raise RuntimeError("%s:iw.dev:failed to get phy <%s>" % (self._role, e)) # get properties (the below will return None rather than throw exception) self._chs = iw.chget(self._phy) self._std = iwt.iwconfig(self._nic, 'Standards') self._txpwr = iwt.iwconfig(self._nic, 'Tx-Power') self._driver = iwt.getdriver(self._nic) self._chipset = iwt.getchipset(self._driver) # spoof the mac address ?? if conf['spoofed']: mac = None if conf['spoofed'].lower( ) == 'random' else conf['spoofed'] try: iwt.ifconfig(self._nic, 'down') self._spoofed = iwt.sethwaddr(self._nic, mac) except iwt.IWToolsException as e: raise RuntimeError("%s:iwtools.sethwaddr:%s" % (self._role, e)) # delete all associated interfaces - we want full control for iface in ifaces: iw.devdel(iface['nic']) # determine virtual interface name ns = [] for wiface in iwt.wifaces(): cs = wiface.split('dyskt') try: if len(cs) > 1: ns.append(int(cs[1])) except ValueError: pass n = 0 if not 0 in ns else max(ns) + 1 self._vnic = "dyskt%d" % n # sniffing interface try: iw.phyadd(self._phy, self._vnic, 'monitor') # create a monitor, iwt.ifconfig(self._vnic, 'up') # and turn it on except iw.IWException as e: # never added virtual nic, restore nic errMsg = "%s:iw.phyadd:%s" % (self._role, e) try: iwt.ifconfig(self._nic, 'up') except iwt.IWToolsException: errMsg += " Failed to restore %s" % self._nic raise RuntimeError(errMsg) except iwt.IWToolsException as e: # failed to 'raise' virtual nic, remove vnic and add nic errMsg = "%s:iwtools.ifconfig:%s" % (self._role, e) try: iw.phyadd(self._phy, self._nic, 'managed') iw.devdel(self._vnic) iwt.ifconfig(self._nic, 'up') except (iw.IWException, iwt.IWToolsException): errMsg += " Failed to restore %s" % self._nic raise RuntimeError(errMsg) # wrap remaining in a try block, we must attempt to restore card and # release the socket after any failures ATT self._s = None try: # bind socket w/ a timeout of 5 just in case there is no wireless traffic self._s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003)) self._s.settimeout(5) self._s.bind((self._vnic, 0x0003)) uptime = time.time() # read in antenna details and radio description if conf['antennas']['num'] > 0: self._antenna['num'] = conf['antennas']['num'] self._antenna['type'] = conf['antennas']['type'] self._antenna['gain'] = conf['antennas']['gain'] self._antenna['loss'] = conf['antennas']['loss'] self._antenna['x'] = [v[0] for v in conf['antennas']['xyz']] self._antenna['y'] = [v[1] for v in conf['antennas']['xyz']] self._antenna['z'] = [v[2] for v in conf['antennas']['xyz']] self._desc = conf['desc'] # compile initial scan pattern from config scan = [ t for t in conf['scan'] if str(t[0]) in self._chs and not t in conf['pass'] ] # sum hop times with side effect of removing any invalid channel tuples # i.e. Ch 14, Width HT40+ and channels the card cannot tune to i = 0 hop = 0 while scan: try: t = time.time() iw.chset(self._vnic, str(scan[i][0]), scan[i][1]) hop += (time.time() - t) i += 1 except iw.IWException as e: # if invalid argument, drop the channel otherwise reraise error if iw.ecode(str(e)) == iw.IW_INVALIDARG: del scan[i] else: raise except IndexError: # all channels checked break if not scan: raise ValueError("Empty scan pattern") # calculate avg hop time, and interval time # NOTE: these are not currently used but may be useful later hop /= len(scan) interval = len(scan) * conf['dwell'] + len(scan) * hop # create list of dwell times ds = [conf['dwell']] * len(scan) # get start ch & set the initial channel try: ch_i = scan.index(conf['scan_start']) except ValueError: ch_i = 0 iw.chset(self._vnic, str(scan[ch_i][0]), scan[ch_i][1]) # initialize tuner thread self._qT = Queue() self._tuner = Tuner(self._qT, self._conn, self._vnic, scan, ds, ch_i, conf['paused']) # notify RTO we are good self._icomms.put((self._vnic, uptime, '!UP!', self.radio)) except socket.error as e: try: iw.devdel(self._vnic) iw.phyadd(self._phy, self._nic) iwt.ifconfig(self._nic, 'up') except (iw.IWException, iwt.IWToolsException): pass if self._s: self._s.shutdown(socket.SHUT_RD) self._s.close() raise RuntimeError("%s:Raw Socket:%s" % (self._role, e)) except iw.IWException as e: try: iw.devdel(self._vnic) iw.phyadd(self._phy, self._nic) iwt.ifconfig(self._nic, 'up') except (iw.IWException, iwt.IWToolsException): pass if self._s: self._s.shutdown(socket.SHUT_RD) self._s.close() raise RuntimeError("%s:iw.chset:Failed to set channel: %s" % (self._role, e)) except (ValueError, TypeError) as e: try: iw.devdel(self._vnic) iw.phyadd(self._phy, self._nic) iwt.ifconfig(self._nic, 'up') except (iw.IWException, iwt.IWToolsException): pass if self._s: self._s.shutdown(socket.SHUT_RD) self._s.close() raise RuntimeError("%s:config:%s" % (self._role, e)) except Exception as e: # blanket exception try: iw.devdel(self._vnic) iw.phyadd(self._phy, self._nic) iwt.ifconfig(self._nic, 'up') except (iw.IWException, iwt.IWToolsException): pass if self._s: self._s.shutdown(socket.SHUT_RD) self._s.close() raise RuntimeError("%s:Unknown:%s" % (self._role, e))
def _setup(self,conf): """ 1) sets radio properties as specified in conf 2) prepares specified nic for monitoring and binds it 3) creates a scan list and compile statistics """ # if the nic specified in conf is present, set it if conf['nic'] not in iwt.wifaces(): raise RuntimeError("%s:iwtools.wifaces:not found" % conf['role']) self._nic = conf['nic'] self._role = conf['role'] # get the phy and associated interfaces try: (self._phy,ifaces) = iw.dev(self._nic) self._mac = ifaces[0]['addr'] except (KeyError, IndexError): raise RuntimeError("%s:iw.dev:error getting interfaces" % self._role) except iw.IWException as e: raise RuntimeError("%s:iw.dev:failed to get phy <%s>" % (self._role,e)) # get properties (the below will return None rather than throw exception) self._chs = iw.chget(self._phy) self._std = iwt.iwconfig(self._nic,'Standards') self._txpwr = iwt.iwconfig(self._nic,'Tx-Power') self._driver = iwt.getdriver(self._nic) self._chipset = iwt.getchipset(self._driver) # spoof the mac address ?? if conf['spoofed']: mac = None if conf['spoofed'].lower() == 'random' else conf['spoofed'] try: iwt.ifconfig(self._nic,'down') self._spoofed = iwt.sethwaddr(self._nic,mac) except iwt.IWToolsException as e: raise RuntimeError("%s:iwtools.sethwaddr:%s" % (self._role,e)) # delete all associated interfaces - we want full control for iface in ifaces: iw.devdel(iface['nic']) # determine virtual interface name ns = [] for wiface in iwt.wifaces(): cs = wiface.split('dyskt') try: if len(cs) > 1: ns.append(int(cs[1])) except ValueError: pass n = 0 if not 0 in ns else max(ns)+1 self._vnic = "dyskt%d" % n # sniffing interface try: iw.phyadd(self._phy,self._vnic,'monitor') # create a monitor, iwt.ifconfig(self._vnic,'up') # and turn it on except iw.IWException as e: # never added virtual nic, restore nic errMsg = "%s:iw.phyadd:%s" % (self._role,e) try: iwt.ifconfig(self._nic,'up') except iwt.IWToolsException: errMsg += " Failed to restore %s" % self._nic raise RuntimeError(errMsg) except iwt.IWToolsException as e: # failed to 'raise' virtual nic, remove vnic and add nic errMsg = "%s:iwtools.ifconfig:%s" % (self._role,e) try: iw.phyadd(self._phy,self._nic,'managed') iw.devdel(self._vnic) iwt.ifconfig(self._nic,'up') except (iw.IWException,iwt.IWToolsException): errMsg += " Failed to restore %s" % self._nic raise RuntimeError(errMsg) # wrap remaining in a try block, we must attempt to restore card and # release the socket after any failures ATT self._s = None try: # bind socket w/ a timeout of 5 just in case there is no wireless traffic self._s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003)) self._s.settimeout(5) self._s.bind((self._vnic,0x0003)) uptime = time.time() # read in antenna details and radio description if conf['antennas']['num'] > 0: self._antenna['num'] = conf['antennas']['num'] self._antenna['type'] = conf['antennas']['type'] self._antenna['gain'] = conf['antennas']['gain'] self._antenna['loss'] = conf['antennas']['loss'] self._antenna['x'] = [v[0] for v in conf['antennas']['xyz']] self._antenna['y'] = [v[1] for v in conf['antennas']['xyz']] self._antenna['z'] = [v[2] for v in conf['antennas']['xyz']] self._desc = conf['desc'] # compile initial scan pattern from config scan = [t for t in conf['scan'] if str(t[0]) in self._chs and not t in conf['pass']] # sum hop times with side effect of removing any invalid channel tuples # i.e. Ch 14, Width HT40+ and channels the card cannot tune to i = 0 hop = 0 while scan: try: t = time.time() iw.chset(self._vnic,str(scan[i][0]),scan[i][1]) hop += (time.time() - t) i += 1 except iw.IWException as e: # if invalid argument, drop the channel otherwise reraise error if iw.ecode(str(e)) == iw.IW_INVALIDARG: del scan[i] else: raise except IndexError: # all channels checked break if not scan: raise ValueError("Empty scan pattern") # calculate avg hop time, and interval time # NOTE: these are not currently used but may be useful later hop /= len(scan) interval = len(scan) * conf['dwell'] + len(scan) * hop # create list of dwell times ds = [conf['dwell']] * len(scan) # get start ch & set the initial channel try: ch_i = scan.index(conf['scan_start']) except ValueError: ch_i = 0 iw.chset(self._vnic,str(scan[ch_i][0]),scan[ch_i][1]) # initialize tuner thread self._qT = Queue() self._tuner = Tuner(self._qT,self._conn,self._vnic,scan,ds,ch_i,conf['paused']) # notify RTO we are good self._icomms.put((self._vnic,uptime,'!UP!',self.radio)) except socket.error as e: try: iw.devdel(self._vnic) iw.phyadd(self._phy,self._nic) iwt.ifconfig(self._nic,'up') except (iw.IWException,iwt.IWToolsException): pass if self._s: self._s.shutdown(socket.SHUT_RD) self._s.close() raise RuntimeError("%s:Raw Socket:%s" % (self._role,e)) except iw.IWException as e: try: iw.devdel(self._vnic) iw.phyadd(self._phy,self._nic) iwt.ifconfig(self._nic,'up') except (iw.IWException,iwt.IWToolsException): pass if self._s: self._s.shutdown(socket.SHUT_RD) self._s.close() raise RuntimeError("%s:iw.chset:Failed to set channel: %s" % (self._role,e)) except (ValueError,TypeError) as e: try: iw.devdel(self._vnic) iw.phyadd(self._phy,self._nic) iwt.ifconfig(self._nic,'up') except (iw.IWException,iwt.IWToolsException): pass if self._s: self._s.shutdown(socket.SHUT_RD) self._s.close() raise RuntimeError("%s:config:%s" % (self._role,e)) except Exception as e: # blanket exception try: iw.devdel(self._vnic) iw.phyadd(self._phy,self._nic) iwt.ifconfig(self._nic,'up') except (iw.IWException,iwt.IWToolsException): pass if self._s: self._s.shutdown(socket.SHUT_RD) self._s.close() raise RuntimeError("%s:Unknown:%s" % (self._role,e))
def _readradio(self,conf,rtype='Recon'): """ read in the rtype radio configuration from conf and return parsed dict """ # don't bother if specified radio is not present if not conf.get(rtype,'nic') in wifaces(): raise RuntimeError("Radio %s not present/not wireless" % conf.get(rtype,'nic')) # get nic and set role setting default antenna config r = {'nic':conf.get(rtype,'nic'), 'spoofed':None, 'ant_gain':0.0, 'ant_loss':0.0, 'ant_offset':0.0, 'ant_type':0.0, 'desc':"unknown", 'scan_start':None, 'role':rtype.lower(), 'antennas':{}} # get optional properties if conf.has_option(rtype,'spoof'): r['spoofed'] = conf.get(rtype,'spoof') if conf.has_option(rtype,'desc'): r['desc'] = conf.get(rtype,'desc') # process antennas - get the number first try: nA = conf.getint(rtype,'antennas') if conf.has_option(rtype,'antennas') else 0 except ValueError: nA = 0 if nA: # antennas has been specified, force correct/valid specifications try: gain = map(float,conf.get(rtype,'antenna_gain').split(',')) if len(gain) != nA: raise RuntimeError('gain') atype = conf.get(rtype,'antenna_type').split(',') if len(atype) != nA: raise RuntimeError('type') loss = map(float,conf.get(rtype,'antenna_loss').split(',')) if len(loss) != nA: raise RuntimeError('loss') rs = conf.get(rtype,'antenna_xyz').split(',') xyz = [] for t in rs: xyz.append(tuple(map(int,t.split(':')))) if len(xyz) != nA: raise RuntimeError('xyz') except ConfigParser.NoOptionError as e: logging.warn("Antenna %s not specified" % e) #raise DySKTConfException("%s" % e) except ValueError as e: logging.warn("Invalid type for %s antenna configuration - %s") except RuntimeError as e: logging.warn("Antenna %s has invalid number of specifications" % e) else: r['antennas']['num'] = nA r['antennas']['gain'] = gain r['antennas']['type'] = atype r['antennas']['loss'] = loss r['antennas']['xyz'] = xyz else: # none, set all empty r['antennas']['num'] = 0 r['antennas']['gain'] = [] r['antennas']['type'] = [] r['antennas']['loss'] = [] r['antennas']['xyz'] = [] # get scan pattern r['dwell'] = conf.getfloat(rtype,'dwell') if r['dwell'] <= 0: raise ValueError("dwell must be > 0") r['scan'] = parsechlist(conf.get(rtype,'scan'),'scan') r['pass'] = parsechlist(conf.get(rtype,'pass'),'pass') if conf.has_option(rtype,'scan_start'): try: scanspec = conf.get(rtype,'scan_start') if ':' in scanspec: (ch,chw) = scanspec.split(':') else: ch = scanspec chw = None ch = int(ch) if ch else r['scan'][0][0] if not chw in iw.IW_CHWS: chw = r['scan'][0][1] r['scan_start'] = (ch,chw) if (ch,chw) in r['scan'] else r['scan'][0] except ValueError: # use default r['scan_start'] = r['scan'][0] else: r['scan_start'] = r['scan'][0] return r