def update_local(): logging.info(u'Updating local database') remote = remote_db() local = local_db() for remote_net in remote.find(Network): if remote_net.bssid.startswith('_'): local_net = local.find(Network, essid=remote_net.essid, key=remote_net.key).one() if local_net and not local_net.bssid.startswith('_'): logging.info(u'Updating MAC address of %s with local data', local_net) remote_net.bssid = local_net.bssid remote.commit() local_net = local.get(Network, remote_net.bssid) if local_net and local_net != remote_net: logging.warn(u'Overriding local %s with remote data', local_net) local_net.bssid = remote_net.bssid local_net.essid = remote_net.essid local_net.key = remote_net.key local_net.on_remote = True elif not local_net: logging.info(u'Retrieved %s from remote database', remote_net) local_net = Network(remote_net.bssid, remote_net.essid, remote_net.key, True) local.add(local_net) local.commit()
def scan(self): powers = {} raw_scan = iw('dev', self.dev, 'scan') for i in range(4, -1, -1): raw_scan = raw_scan.replace('\t' * i + '\n', '<tab{}>'.format(i)) for network in raw_scan.split('<tab0>'): essid = None power = None for param in network.split('<tab1>'): if not ': ' in param: continue param = param.split(': ', 1) if param[0] == 'SSID': essid = param[1] elif param[0] == 'signal': power = int(param[1][0:5]) if essid is None or power is None: logger.warning( 'Failure parsing the output of iw for essid %s. Complete line: %r', essid, network) continue powers[essid] = power logger.debug('Scan found networks %r', powers.keys()) nets = [] local = local_db() for net in local.find(Network, Network.essid.In(powers.keys())): nets.append({ 'essid': net.essid, 'key': net.key, 'power': powers[net.essid], }) return sorted(nets, lambda v: v['power'])
def scan(self): powers = {} raw_scan = iw('dev', self.dev, 'scan') for i in range(4,-1,-1): raw_scan = raw_scan.replace('\t'*i + '\n', '<tab{}>'.format(i)) for network in raw_scan.split('<tab0>'): essid = None power = None for param in network.split('<tab1>'): if not ': ' in param: continue param = param.split(': ', 1) if param[0] == 'SSID': essid = param[1] elif param[0] == 'signal': power = int(param[1][0:5]) if essid is None or power is None: logger.warning('Failure parsing the output of iw for essid %s. Complete line: %r', essid, network) continue powers[essid] = power logger.debug('Scan found networks %r', powers.keys()) nets = [] local = local_db() for net in local.find(Network, Network.essid.In(powers.keys())): nets.append({ 'essid': net.essid, 'key': net.key, 'power': powers[net.essid], }) return sorted(nets, lambda v: v['power'])
import logging from storm.locals import Bool, Int, Unicode from helpers import local_db, remote_db, settings local = local_db() local.execute(""" CREATE TABLE IF NOT EXISTS `networks` ( `bssid` varchar(255), `essid` varchar(255) NOT NULL, `key` varchar(255) NOT NULL, `location` varchar(255), `on_remote` tinyint(1) NOT NULL, PRIMARY KEY (`bssid`, `essid`) ); """) local.commit() del local class Network(object): __storm_table__ = 'networks' bssid = Unicode(primary=True) essid = Unicode() key = Unicode() on_remote = Bool() def __init__(self, bssid, essid, key=None, on_remote=False): self.bssid = bssid self.essid = essid
from networks import Network logger = logging.getLogger('airodump') def select_target_network(interface, exclude=set()): networks = None while networks is None: try: airodump_process = airodump_ng('--encrypt', 'wep', '-w', 'data/scan', interface.dev, _err=lambda l: 0) time.sleep(settings.AIRODUMP_SCAN_WAIT) airodump_process.terminate() networks = Reader('data/scan-01.csv').get_sorted_networks() except UnicodeDecodeError, e: logger.warning('Decoding the output of airodump failed, trying again from scratch (Error: %s)', e) local = local_db() excluded = None for net in networks: if net['bssid'] in exclude and excluded is None: excluded = net known_net = local.find(Network, And(Or(Network.bssid.like(u'_%'), Network.bssid == net['bssid']), Network.essid == net['essid'])) if known_net.is_empty(): return net return excluded def airodump(ch, interface): return airodump_ng('-c', ch, '-w', 'data/inj', interface.dev, _err=lambda l: 0) class Reader(object): def __init__(self, csv_path):