def analyzeData(capture, gui=False): if gui: print('-------------------Processing data-------------------') printToConsole( '-------------------Processing data-------------------\n') else: print('-------------------Processing data-------------------') ssid_dict = {} signal_str_min_dict = {} signal_str_max_dict = {} signal_str = None bssid = None ssid = None for packet in capture: for layer in packet.layers: if layer.get('signal_dbm'): signal_str = int(layer.get('signal_dbm')) elif layer.get('bssid'): bssid = layer.get('bssid') elif layer.get('wlan.ssid'): ssid = layer.get('wlan.ssid') if not bssid in ssid_dict: ssid_dict[bssid] = ssid if bssid in signal_str_min_dict: if signal_str < signal_str_min_dict[bssid]: signal_str_min_dict[bssid] = signal_str else: signal_str_min_dict[bssid] = signal_str if bssid in signal_str_max_dict: if signal_str > signal_str_max_dict[bssid]: signal_str_max_dict[bssid] = signal_str else: signal_str_max_dict[bssid] = signal_str signal_str = None bssid = None ssid = None if gui: print('-------------------Done processing new data-------------------') printToConsole( '-------------------Done processing new data-------------------\n') else: print('-------------------Done processing new data-------------------') return ssid_dict, signal_str_min_dict, signal_str_max_dict
def startLocating(): db_conn = connectToDatabase(user.get(), db_pw.get(), host.get(), db_table.get()) if not db_conn: return if locating_interface_dropdown.get(): clearConsoleArea() locateWithGui(database_connection=db_conn, interface=locating_interface_dropdown.get(), with_adapter=locating_is_adapter.get(), sudo_pw=sudo_pw.get(), debug=debug_mode.get()) else: printToConsole('interface is not set\n') db_conn.close()
def connectToDatabase(user, password, host, database_table): global cnx db_config = { 'user': user, 'password': password, 'host': host, 'database': database_table, 'raise_on_warnings': True } try: cnx = mysql.connector.connect(**db_config) except: printToConsole( 'Could not connect to the database or could not find the given table\n' ) return False return cnx
def collectWithGui(database_connection, building, room, floor, roomSize, specific_location, method, fileName, interface, sudo_pw, gui, internal_interface, internal_adapter_only=False): global cnx, use_database if database_connection: cnx = database_connection use_database = True else: use_database = False packet_count = None if method == 'live': packet_count = iw_scanner.determinePacketCount(internal_interface, sudo_pw) abort_command = initMethod(building, room, floor, roomSize, specific_location, method, fileName, packet_count, interface, gui, sudo_pw, internal_adapter_only=internal_adapter_only, internal_interface=internal_interface) if abort_command: return printToConsole('[+] Scan complete room ' + room + ' now mapped!\n')
def customAreaMethod(building, location, floor, specific_location, packetCount=500, interface=None, gui=False, sudo_pw=None, internal_adapter_only=False, internal_interface=None): if gui: printToConsole( '[!] Please move to the specified place (' + specific_location + ') in the room and press Continue (or Abort to exit): \n') command = askConfirmation(gui) else: command = input( '[!] Please move to the specified place (' + specific_location + ') in the room and press Enter (or type anything and press Enter to exit): ' ) if command == '': captureDataLive(interface, building, location, specific_location, floor, location.replace(' ', '_'), packetCount, sudo_pw, gui, internal_adapter_only=internal_adapter_only, internal_interface=internal_interface) else: if gui: printToConsole('Aborting..\n') return 'Abort' print('Exiting...') exit()
def NESWMethod(building, location, floor, packetCount=500, interface=None, corner=None, gui=False, sudo_pw=None, internal_adapter_only=False, internal_interface=None): if gui: printToConsole( '[!] Please move to the ' + corner + ' corner of the room and press Continue (or Abort to exit): \n') command = askConfirmation(gui) else: command = input( '[!] Please move to the ' + corner + ' corner of the room and press Enter (or type anything and press Enter to exit): ' ) if command == '': captureDataLive(interface, building, location, corner + ' corner', floor, location, packetCount, sudo_pw, gui, internal_adapter_only=internal_adapter_only, internal_interface=internal_interface) else: if gui: printToConsole('Aborting..\n') return 'Abort' print('Exiting...') exit()
def collectData(): if not building.get(): printToConsole('Building field can not be empty\n') return if not floor.get(): printToConsole('Floor field can not be empty\n') return else: if not is_integer(floor.get()): printToConsole('Floor has to be an integer value\n') return if not room.get(): printToConsole('Room field can no be empty\n') return db_conn = connectToDatabase(user.get(), db_pw.get(), host.get(), db_table.get()) if not db_conn: printToConsole('Would you like to continue anyway?\n') answer = askConfirmation(window) if answer == 'Abort': return spec_location = None room_size_value = None only_use_internal = False internal_interface = None if method_dropdown.get() == 'Live': method = 'live' interface = data_interface_dropdown.get() if not interface: printToConsole('Interface is not set\n') return else: if data_is_adapter.get(): internal_interface = data_internal_interface_dropdown.get() if not internal_interface: printToConsole( 'Internal interface needs to be set if using external adapter\n' ) return internal_interface = data_internal_interface_dropdown.get() else: internal_interface = interface only_use_internal = True if selection_radio.get(): if selection_radio.get() == 'room_size': if room_size_radio_selection.get(): room_size_value = room_size_radio_selection.get() else: printToConsole('Room size has to be specified\n') return else: if specific_location.get(): spec_location = specific_location.get() else: printToConsole('Location has to be specified!\n') return else: printToConsole( 'You have to specify either room size or more specific location\n' ) return clearConsoleArea() collectWithGui(database_connection=db_conn, building=building.get(), room=room.get(), floor=floor.get(), roomSize=room_size_value, specific_location=spec_location, method=method, fileName=None, interface=interface, sudo_pw=sudo_pw.get(), gui=window, internal_interface=internal_interface, internal_adapter_only=only_use_internal) else: method = 'file' spec_location = specific_location.get() clearConsoleArea() collectWithGui(database_connection=db_conn, building=building.get(), room=room.get(), floor=floor.get(), roomSize=room_size_value, specific_location=spec_location, method=method, fileName=file_name.get(), interface=None, sudo_pw=sudo_pw.get(), gui=window, internal_interface=internal_interface, internal_adapter_only=only_use_internal) if db_conn: db_conn.close()
def addDataToDb(building, location, specified_location, floor, ssid_dict, signals_min_dict, signals_max_dict, without_adapter=False, gui=False): global cnx if gui: print('-------------------Adding data to DB-------------------') printToConsole( '-------------------Adding data to DB-------------------\n') else: print('-------------------Adding data to DB-------------------') table_place = 'place' table_place_detail = 'place_detail' if without_adapter: table_place += '_without_adapter' table_place_detail += '_without_adapter' add_access_point = ("INSERT INTO access_point " "(bssid, ssid) " "VALUES (%s, %s)") add_place = ( "INSERT INTO " + table_place + " " "(building, location, floor, access_point_id, signal_str_min, signal_str_max) " "VALUES (%s, %s, %s, %s, %s, %s)") add_place_detail = ( "INSERT INTO " + table_place_detail + " " "(place_id, detailed_location, access_point_id, signal_str_min, signal_str_max) " "VALUES (%s, %s, %s, %s, %s)") cursor = cnx.cursor(buffered=True) for item in ssid_dict.items(): cursor.execute('SELECT id ' 'FROM access_point ' 'WHERE bssid=' + '"' + item[0] + '"') ap_id = cursor.fetchone() if ap_id: cursor.execute('SELECT id, signal_str_min, signal_str_max ' 'FROM ' + table_place + ' ' 'WHERE location=' + '"' + location + '" ' 'AND floor=' + str(floor) + ' ' 'AND access_point_id=' + str(ap_id[0]) + ' ' 'AND building="' + building + '"') place_data = cursor.fetchone() if place_data: place_id = place_data[0] else: place_id = False if place_id: data_place_detail = (place_id, specified_location, ap_id[0], signals_min_dict[item[0]], signals_max_dict[item[0]]) if signals_min_dict[item[0]] < place_data[1]: cursor.execute('UPDATE ' + table_place + ' ' 'SET signal_str_min=' + str(signals_min_dict[item[0]]) + ' ' 'WHERE id=' + str(place_id)) if signals_max_dict[item[0]] > place_data[2]: cursor.execute('UPDATE ' + table_place + ' ' 'SET signal_str_max=' + str(signals_max_dict[item[0]]) + ' ' 'WHERE id=' + str(place_id)) else: data_place = (building, location, floor, ap_id[0], signals_min_dict[item[0]], signals_max_dict[item[0]]) cursor.execute(add_place, data_place) data_place_detail = (cursor.lastrowid, specified_location, ap_id[0], signals_min_dict[item[0]], signals_max_dict[item[0]]) else: if item[1] == None: item = list(item) #convert to list as tuples are immutable item[1] = 'Unknown' data_ap = (item[0], item[1]) cursor.execute(add_access_point, data_ap) ap_id = cursor.lastrowid data_place = (building, location, floor, ap_id, signals_min_dict[item[0]], signals_max_dict[item[0]]) cursor.execute(add_place, data_place) data_place_detail = (cursor.lastrowid, specified_location, ap_id, signals_min_dict[item[0]], signals_max_dict[item[0]]) cursor.execute(add_place_detail, data_place_detail) cnx.commit() cursor.close() if gui: print( '-------------------Done adding items to database-------------------' ) printToConsole( '-------------------Done adding items to database-------------------\n' ) else: print( '-------------------Done adding items to database-------------------' )
def initMethod(building, room, floor, roomSize=None, specific_location=None, method=None, fileName=None, packet_count=500, interface=None, gui=False, sudo_pw=None, internal_adapter_only=False, internal_interface=None): if method == 'live': if specific_location: if gui: printToConsole('[+] Mapping custom area in the room\n') else: print('[+] Mapping custom area in the room') abort_command = customAreaMethod( building, room, floor, specific_location, packet_count, interface, gui, sudo_pw, internal_adapter_only=internal_adapter_only, internal_interface=internal_interface) if abort_command: return abort_command elif roomSize: if roomSize == 'S': if gui: printToConsole( '[+] Using centering method to map the room\n') else: print('[+] Using centering method to map the room') abort_command = centerMethod( building, room, floor, packet_count, interface, gui, sudo_pw, internal_adapter_only=internal_adapter_only, internal_interface=internal_interface) if abort_command: return abort_command elif roomSize == 'M': locations = ['left', 'right'] if gui: printToConsole( '[+] Using left-right method to map the room\n') else: print('[+] Using left-right method to map the room') for location in locations: abort_command = leftRightMethod( building, room, floor, packet_count, interface, location, gui, sudo_pw, internal_adapter_only=internal_adapter_only, internal_interface=internal_interface) if abort_command: return abort_command else: locations = [ 'north-east', 'north-west', 'south-west', 'south-east' ] if gui: printToConsole('[+] Using NESW method to map the room\n') else: print('[+] Using NESW method to map the room') for location in locations: abort_command = NESWMethod( building, room, floor, packet_count, interface, location, gui, sudo_pw, internal_adapter_only=internal_adapter_only, internal_interface=internal_interface) if abort_command: return abort_command else: locations = [ 'north-east', 'north-west', 'south-west', 'south-east' ] if gui: printToConsole( '[+] Using NESW method by default to map the room\n') else: print('[+] Using NESW method by default to map the room') for location in locations: abort_command = NESWMethod( building, room, floor, packet_count, interface, location, gui, sudo_pw, internal_adapter_only=internal_adapter_only, internal_interface=internal_interface) if abort_command: return abort_command else: captureFromFile(building, room, specific_location, floor, fileName, gui) if not gui: while True: print( '[+] Enter another file name for processing (or press enter to finish)' ) fileName = getFileName() specific_location = input( '[+] Enter new specific location name for this file (or press enter to finish): ' ) if specific_location == '': return captureFromFile(building, room, specific_location, floor, fileName)
def captureDataLive(interface, building, location, specified_location, floor, filename, packetCount=500, sudo_pw=None, gui=False, internal_adapter_only=False, internal_interface=None): capture_file = 'pcap_dumps/' + filename + '_' + specified_location.replace( ' ', '_') + '.pcap' if not internal_adapter_only: #add check if interface is in monitor mode or set timeout capture = pyshark.LiveCapture( interface=interface, output_file=capture_file, debug=False, bpf_filter='wlan type mgt subtype beacon') if gui: print( '-------------------Starting data capture with adapter-------------------' ) printToConsole( '-------------------Starting data capture with adapter-------------------\n' ) else: print( '-------------------Starting data capture with adapter-------------------' ) capture.load_packets(packetCount) if gui: print('-------------------Data captured-------------------') printToConsole( '-------------------Data captured-------------------\n') else: print('-------------------Data captured-------------------') ssids, signals_min, signals_max = analyzeData(capture, gui) if use_database: addDataToDb(building, location, specified_location, floor, ssids, signals_min, signals_max, gui=gui) if gui: print( '-------------------Executing scans without adapter-------------------' ) printToConsole( '-------------------Executing scans without adapter-------------------\n' ) else: print( '-------------------Executing scans without adapter-------------------' ) ssids_without_adapter, signals_min_without_adapter, signals_max_without_adapter = iw_scanner.doMultipleScans( internal_interface, sudo_pw) if gui: print( '-------------------Done executing scans without adapter-------------------' ) printToConsole( '-------------------Done executing scans without adapter-------------------\n' ) else: print( '-------------------Done executing scans without adapter-------------------' ) if use_database: addDataToDb(building, location, specified_location, floor, ssids_without_adapter, signals_min_without_adapter, signals_max_without_adapter, without_adapter=True, gui=gui)
def findLocation(interface, with_adapter, sudo_pw=None, gui=False, debug_mode=False): cursor = cnx.cursor(buffered=True) table_place = 'place' table_place_detail = 'place_detail' if not with_adapter: table_place += '_without_adapter' table_place_detail += '_without_adapter' query = 'SELECT p.location ' \ 'FROM access_point ap ' \ 'INNER JOIN ' + table_place + ' p ON ap.id=p.access_point_id ' \ 'WHERE ap.bssid = %(bssid)s ' \ 'AND %(signal_str)s BETWEEN p.signal_str_min AND p.signal_str_max' possible_locations = {} percentages = {} scanResults = scanAndParse(interface, sudo_pw) if not scanResults: if gui: printToConsole('No results found with given interface') else: print('No results found with given interface') return for result in scanResults: cursor.execute(query, {'bssid': result['mac'], 'signal_str': result['signal_level_dBm']}) locations = cursor.fetchall() if debug_mode: if gui: printToConsole("locations for mac %s with ssid %s and signal strength %s\n" % (result['mac'], result['essid'], result['signal_level_dBm'])) printToConsole(str(locations) + '\n') else: print("locations for mac %s with ssid %s and signal strength %s" % (result['mac'], result['essid'], result['signal_level_dBm'])) print(locations) for item in locations: location = item[0] if location in possible_locations: possible_locations[location] += 1 else: possible_locations[location] = 1 if debug_mode: if gui: printToConsole('Locations counts - ' + str(possible_locations) + '\n') else: print(possible_locations) #calculate percentages for key, value in possible_locations.items(): percentages[key] = str(round(value / len(scanResults) * 100, 2)) + '%' if debug_mode: if gui: printToConsole('Likelihood percentages - ' + str(percentages) + '\n') else: print(percentages) if possible_locations: if gui: printToConsole('Your current location is most probably ' + str(getMaxValueKey(possible_locations)) + '\n') else: print('Your current location is most probably ' + str(getMaxValueKey(possible_locations))) else: if gui: printToConsole('No matching results found. Unable to locate you. Try to scan again or change to another database table\n') else: print('No matching results found. Unable to locate you. Try to scan again or change to another database table')