def parse_pcap(filename): logging.info("grid: parsing %s ..." % filename) net_id = os.path.basename(filename).replace('.pcap', '') if '_' in net_id: # /root/handshakes/ESSID_BSSID.pcap essid, bssid = net_id.split('_') else: # /root/handshakes/BSSID.pcap essid, bssid = '', net_id it = iter(bssid) bssid = ':'.join([a + b for a, b in zip(it, it)]) info = { WifiInfo.ESSID: essid, WifiInfo.BSSID: bssid, } try: info = extract_from_pcap(filename, [WifiInfo.BSSID, WifiInfo.ESSID]) except Exception as e: logging.error("grid: %s" % e) return info[WifiInfo.ESSID], info[WifiInfo.BSSID]
def parse_pcap(filename): logging.info(f"[grid] parsing {filename}...") net_id = os.path.basename(filename).replace('.pcap', '') if '_' in net_id: # /root/handshakes/ESSID_BSSID.pcap essid, bssid = net_id.split('_') else: # /root/handshakes/BSSID.pcap essid, bssid = '', net_id mac_re = re.compile('[0-9a-fA-F]{12}') if not mac_re.match(bssid): return '', '' it = iter(bssid) bssid = ':'.join([a + b for a, b in zip(it, it)]) info = { WifiInfo.ESSID: essid, WifiInfo.BSSID: bssid, } try: info = extract_from_pcap(filename, [WifiInfo.BSSID, WifiInfo.ESSID]) except Exception as e: logging.error(f"[grid] {e}") return info[WifiInfo.ESSID], info[WifiInfo.BSSID]
def on_internet_available(self, agent): """ Called in manual mode when there's internet connectivity """ if not self.ready or self.lock.locked(): return from scapy.all import Scapy_Exception config = agent.config() display = agent.view() reported = self.report.data_field_or('reported', default=list()) handshake_dir = config['bettercap']['handshakes'] all_files = os.listdir(handshake_dir) all_gps_files = [ os.path.join(handshake_dir, filename) for filename in all_files if filename.endswith('.gps.json') ] all_gps_files = remove_whitelisted(all_gps_files, self.options['whitelist']) new_gps_files = set(all_gps_files) - set(reported) - set(self.skip) if new_gps_files: logging.info( "[wigle] Internet connectivity detected, Uploading new handshakes to wigle.net..." ) csv_entries = list() no_err_entries = list() for gps_file in new_gps_files: pcap_filename = gps_file.replace('.gps.json', '.pcap') if not os.path.exists(pcap_filename): logging.debug(f"[wigle] Can't find pcap for {gps_file}.") self.skip.append(gps_file) continue try: gps_data = _extract_gps_data(gps_file) except OSError as os_err: logging.debug(f"[wigle] {os_err}") self.skip.append(gps_file) continue except json.JSONDecodeError as json_err: logging.debug(f"[wigle]: {json_err}") self.skip.append(gps_file) continue if gps_data['Latitude'] == 0 and gps_data['Longitude'] == 0: logging.debug( f"[wigle] Not enough gps-information for {gps_file}. Trying again next time." ) self.skip.append(gps_file) continue try: pcap_data = extract_from_pcap(pcap_filename, [ WifiInfo.BSSID, WifiInfo.ESSID, WifiInfo.ENCRYPTION, WifiInfo.CHANNEL, WifiInfo.RSSI ]) except FieldNotFoundError: logging.debug( f"[wigle] Could not extract all information. Skip {gps_file}." ) self.skip.append(gps_file) continue except Scapy_Exception as sc_e: logging.debug(f"[wigle]: {sc_e}") self.skip.append(gps_file) continue new_entry = _transform_wigle_entry(gps_data, pcap_data) csv_entries.append(new_entry) no_err_entries.append(gps_file) if csv_entries: display.set('status', "Uploading gps-data to wigle.net...") display.update(force=True) try: _send_to_wigle(csv_entries, self.options['api_key']) reported += no_err_entries self.report.update(data={'reported': reported}) logging.info( f"[wigle] Successfully uploaded {len(no_err_entries)} files." ) except requests.exceptions.RequestException as re_e: self.skip += no_err_entries logging.debug( f"[wigle] Got an exception while uploading {re_e}") except OSError as os_e: self.skip += no_err_entries logging.debug(f"[wigle] Got the following error: {os_e}")
def on_internet_available(agent): from scapy.all import RadioTap, Dot11Elt, Dot11Beacon, rdpcap, Scapy_Exception, Dot11, Dot11ProbeResp, Dot11AssoReq, \ Dot11ReassoReq, Dot11EltRSN, Dot11EltVendorSpecific, Dot11EltMicrosoftWPA """ Called in manual mode when there's internet connectivity """ global ALREADY_UPLOADED global SKIP if READY: config = agent.config() display = agent.view() handshake_dir = config['bettercap']['handshakes'] all_files = os.listdir(handshake_dir) all_gps_files = [ os.path.join(handshake_dir, filename) for filename in all_files if filename.endswith('.gps.json') ] new_gps_files = set(all_gps_files) - set(ALREADY_UPLOADED) - set(SKIP) if new_gps_files: logging.info( "WIGLE: Internet connectivity detected. Uploading new handshakes to wigle.net" ) csv_entries = list() no_err_entries = list() for gps_file in new_gps_files: pcap_filename = gps_file.replace('.gps.json', '.pcap') if not os.path.exists(pcap_filename): logging.error("WIGLE: Can't find pcap for %s", gps_file) SKIP.append(gps_file) continue try: gps_data = _extract_gps_data(gps_file) except OSError as os_err: logging.error("WIGLE: %s", os_err) SKIP.append(gps_file) continue except json.JSONDecodeError as json_err: logging.error("WIGLE: %s", json_err) SKIP.append(gps_file) continue try: pcap_data = extract_from_pcap(pcap_filename, [ WifiInfo.BSSID, WifiInfo.ESSID, WifiInfo.ENCRYPTION, WifiInfo.CHANNEL, WifiInfo.RSSI ]) except FieldNotFoundError: logging.error( "WIGLE: Could not extract all informations. Skip %s", gps_file) SKIP.append(gps_file) continue except Scapy_Exception as sc_e: logging.error("WIGLE: %s", sc_e) SKIP.append(gps_file) continue new_entry = _transform_wigle_entry(gps_data, pcap_data) csv_entries.append(new_entry) no_err_entries.append(gps_file) if csv_entries: display.set('status', "Uploading gps-data to wigle.net ...") display.update(force=True) try: _send_to_wigle(csv_entries, OPTIONS['api_key']) ALREADY_UPLOADED += no_err_entries with open('/root/.wigle_uploads', 'a') as up_file: for gps in no_err_entries: up_file.write(gps + "\n") logging.info("WIGLE: Successfuly uploaded %d files", len(no_err_entries)) except requests.exceptions.RequestException as re_e: SKIP += no_err_entries logging.error("WIGLE: Got an exception while uploading %s", re_e) except OSError as os_e: SKIP += no_err_entries logging.error("WIGLE: Got the following error: %s", os_e)
def on_internet_available(agent): from scapy.all import Scapy_Exception """ Called in manual mode when there's internet connectivity """ global REPORT global SKIP if READY: config = agent.config() display = agent.view() reported = REPORT.data_field_or('reported', default=list()) handshake_dir = config['bettercap']['handshakes'] all_files = os.listdir(handshake_dir) all_gps_files = [ os.path.join(handshake_dir, filename) for filename in all_files if filename.endswith('.gps.json') ] new_gps_files = set(all_gps_files) - set(reported) - set(SKIP) if new_gps_files: logging.info( "WIGLE: Internet connectivity detected. Uploading new handshakes to wigle.net" ) csv_entries = list() no_err_entries = list() for gps_file in new_gps_files: pcap_filename = gps_file.replace('.gps.json', '.pcap') if not os.path.exists(pcap_filename): logging.error("WIGLE: Can't find pcap for %s", gps_file) SKIP.append(gps_file) continue try: gps_data = _extract_gps_data(gps_file) except OSError as os_err: logging.error("WIGLE: %s", os_err) SKIP.append(gps_file) continue except json.JSONDecodeError as json_err: logging.error("WIGLE: %s", json_err) SKIP.append(gps_file) continue if gps_data['Latitude'] == 0 and gps_data['Longitude'] == 0: logging.warning( "WIGLE: Not enough gps-informations for %s. Trying again next time.", gps_file) SKIP.append(gps_file) continue try: pcap_data = extract_from_pcap(pcap_filename, [ WifiInfo.BSSID, WifiInfo.ESSID, WifiInfo.ENCRYPTION, WifiInfo.CHANNEL, WifiInfo.RSSI ]) except FieldNotFoundError: logging.error( "WIGLE: Could not extract all informations. Skip %s", gps_file) SKIP.append(gps_file) continue except Scapy_Exception as sc_e: logging.error("WIGLE: %s", sc_e) SKIP.append(gps_file) continue new_entry = _transform_wigle_entry(gps_data, pcap_data) csv_entries.append(new_entry) no_err_entries.append(gps_file) if csv_entries: display.set('status', "Uploading gps-data to wigle.net ...") display.update(force=True) try: _send_to_wigle(csv_entries, OPTIONS['api_key']) reported += no_err_entries REPORT.update(data={'reported': reported}) logging.info("WIGLE: Successfuly uploaded %d files", len(no_err_entries)) except requests.exceptions.RequestException as re_e: SKIP += no_err_entries logging.error("WIGLE: Got an exception while uploading %s", re_e) except OSError as os_e: SKIP += no_err_entries logging.error("WIGLE: Got the following error: %s", os_e)
def on_internet_available(agent): from scapy.all import Scapy_Exception global OPTIONS global READY global REPORT global SKIP # Anti-spaghet if not READY: return config = agent.config() display = agent.view() reported = REPORT.data_field_or('reported', default=list()) all_gps_files = [ os.path.join(config['bettercap']['handshakes'], filename) for filename in os.listdir(config['bettercap']['handshakes']) if filename.endswith('.gps.json') ] new_gps_files = set(all_gps_files) - set(reported) - set(SKIP) # Anti-spaghet if not new_gps_files: return logging.info( "WIGLE: Internet connectivity detected. Uploading new handshakes to wigle.net" ) no_err_entries = list() data_tuples = [] for gps_file in new_gps_files: pcap_filename = gps_file.replace('.gps.json', '.pcap') if not os.path.exists(pcap_filename): logging.error("WIGLE: Can't find pcap for %s", gps_file) SKIP.append(gps_file) continue try: gps_data = _extract_gps_data(gps_file) except OSError as os_err: logging.error("WIGLE: %s", os_err) SKIP.append(gps_file) continue except json.JSONDecodeError as json_err: logging.error("WIGLE: %s", json_err) SKIP.append(gps_file) continue if gps_data['Latitude'] == 0 and gps_data['Longitude'] == 0: logging.warning( "WIGLE: Not enough gps-information for %s. Trying again next time.", gps_file) SKIP.append(gps_file) continue try: pcap_data = extract_from_pcap(pcap_filename, [ WifiInfo.BSSID, WifiInfo.ESSID, WifiInfo.ENCRYPTION, WifiInfo.CHANNEL, WifiInfo.RSSI ]) except FieldNotFoundError: logging.error("WIGLE: Could not extract all information. Skip %s", gps_file) SKIP.append(gps_file) continue except Scapy_Exception as sc_e: logging.error("WIGLE: %s", sc_e) SKIP.append(gps_file) continue data_tuples.append((gps_data, pcap_data)) no_err_entries.append(gps_file) # Anti-spaghet if len(data_tuples) == 0: return display.set('status', "Uploading gps-data to wigle.net ...") display.update(force=True) try: _send_to_wigle(_create_kismet_wigle_csv(data_tuples), OPTIONS['api_key']) reported += no_err_entries REPORT.update(data={'reported': reported}) logging.info( "WIGLE: Successfully uploaded one file with %s access points.", len(no_err_entries)) # The previous exceptions here were as pointless as the one that follows: except Exception as e: # Ignoring valid Wigle data because we hit an exception doesn't seem like a good idea. # We would be throwing away legit, hard-earned data for things like "requests failed because internet sucks" logging.error( "WIGLE: Encountered an exception while uploading Kismet Wigle CSV file: %s", str(e))