def get_wireless_profile_xml(profile_name, wireless_interface): handle = native_api.WlanOpenHandle() xml_data = native_api.WlanGetProfile(handle, wireless_interface.guid, native_api.LPCWSTR(profile_name)) xml = xml_data.value native_api.WlanFreeMemory(xml_data) native_api.WlanCloseHandle(handle) return xml
def get_wireless_networks_bss_list(wireless_interface): """Returns a list of WirelessNetworkBss objects based on the wireless networks availables.""" networks = [] handle = native_api.WlanOpenHandle() bss_list = native_api.WlanGetNetworkBssList(handle, wireless_interface.guid) # Handle the WLAN_BSS_LIST pointer to get a list of WLAN_BSS_ENTRY # structures. data_type = bss_list.contents.wlanBssEntries._type_ num = bss_list.contents.NumberOfItems bsss_pointer = native_api.addressof(bss_list.contents.wlanBssEntries) bss_entries_list = (data_type * num).from_address(bsss_pointer) for bss_entry in bss_entries_list: networks.append(WirelessNetworkBss(bss_entry)) native_api.WlanFreeMemory(bss_list) native_api.WlanCloseHandle(handle) return networks
def get_wireless_interfaces(): """Returns a list of WirelessInterface objects based on the wireless interfaces available.""" interfaces_list = [] handle = native_api.WlanOpenHandle() wlan_ifaces = native_api.WlanEnumInterfaces(handle) # Handle the WlanInterfaceInfoList pointer to get a list of WlanInterfaceInfo structures. data_type = wlan_ifaces.contents.InterfaceInfo._type_ num = wlan_ifaces.contents.NumberOfItems ifaces_pointer = native_api.addressof(wlan_ifaces.contents.InterfaceInfo) wlan_interface_info_list = (data_type * num).from_address(ifaces_pointer) for wlan_interface_info in wlan_interface_info_list: wlan_iface = WirelessInterface(wlan_interface_info) interfaces_list.append(wlan_iface) native_api.WlanFreeMemory(wlan_ifaces) native_api.WlanCloseHandle(handle) return interfaces_list
def query_interface(opcode_item, wireless_interface): handle = native_api.WlanOpenHandle() opcode_item_ext = "".join(["wlan_intf_opcode_", opcode_item]) opcode = None for key, val in native_api.WLAN_INTF_OPCODE_DICT.items(): if val == opcode_item_ext: opcode = native_api.WLAN_INTF_OPCODE(key) break result = native_api.WlanQueryInterface(handle, wireless_interface.guid, opcode) native_api.WlanCloseHandle(handle) r = result.contents if opcode_item == "interface_state": # WLAN_INTERFACE_STATE ext_out = native_api.WLAN_INTERFACE_STATE_DICT[r.value] elif opcode_item == "current_connection": # WLAN_CONNECTION_ATTRIBUTES isState = native_api.WLAN_INTERFACE_STATE_DICT[r.isState] wlanConnectionMode = native_api.WLAN_CONNECTION_MODE_KV[r.wlanConnectionMode] strProfileName = r.strProfileName aa = r.wlanAssociationAttributes wlanAssociationAttributes = { "dot11Ssid": aa.dot11Ssid.SSID, "dot11BssType": native_api.DOT11_BSS_TYPE_DICT_KV[aa.dot11BssType], "dot11Bssid": dot11bssidToString(aa.dot11Bssid), "dot11PhyType": native_api.DOT11_PHY_TYPE_DICT[aa.dot11PhyType], "uDot11PhyIndex": native_api.c_long(aa.uDot11PhyIndex).value, "wlanSignalQuality": native_api.c_long(aa.wlanSignalQuality).value, "ulRxRate": native_api.c_long(aa.ulRxRate).value, "ulTxRate": native_api.c_long(aa.ulTxRate).value, } sa = r.wlanSecurityAttributes wlanSecurityAttributes = { "bSecurityEnabled": sa.bSecurityEnabled, "bOneXEnabled": sa.bOneXEnabled, "dot11AuthAlgorithm": native_api.DOT11_AUTH_ALGORITHM_DICT[sa.dot11AuthAlgorithm], "dot11CipherAlgorithm": native_api.DOT11_CIPHER_ALGORITHM_DICT[sa.dot11CipherAlgorithm], } ext_out = { "isState": isState, "wlanConnectionMode": wlanConnectionMode, "strProfileName": strProfileName, "wlanAssociationAttributes": wlanAssociationAttributes, "wlanSecurityAttributes": wlanSecurityAttributes, } else: ext_out = None return result.contents, ext_out
def get_wireless_available_network_list(wireless_interface): """Returns a list of WirelessNetwork objects based on the wireless networks availables.""" networks = [] handle = native_api.WlanOpenHandle() network_list = native_api.WlanGetAvailableNetworkList(handle, wireless_interface.guid) # Handle the WLAN_AVAILABLE_NETWORK_LIST pointer to get a list of # WLAN_AVAILABLE_NETWORK structures. data_type = network_list.contents.Network._type_ num = network_list.contents.NumberOfItems network_pointer = native_api.addressof(network_list.contents.Network) networks_list = (data_type * num).from_address(network_pointer) for network in networks_list: networks.append(WirelessNetwork(network)) native_api.WlanFreeMemory(network_list) native_api.WlanCloseHandle(handle) return networks
def get_wireless_profiles(wireless_interface): """Returns a list of WirelessProfile objects based on the wireless profiles.""" profiles = [] handle = native_api.WlanOpenHandle() profile_list = native_api.WlanGetProfileList(handle, wireless_interface.guid) # Handle the WLAN_PROFILE_INFO_LIST pointer to get a list of # WLAN_PROFILE_INFO structures. data_type = profile_list.contents.ProfileInfo._type_ num = profile_list.contents.NumberOfItems profile_info_pointer = native_api.addressof(profile_list.contents.ProfileInfo) profiles_list = (data_type * num).from_address(profile_info_pointer) xml_data = None # safety: there may be no profiles for profile in profiles_list: xml_data = native_api.WlanGetProfile(handle, wireless_interface.guid, profile.ProfileName) profiles.append(WirelessProfile(profile, xml_data.value)) native_api.WlanFreeMemory(xml_data) native_api.WlanFreeMemory(profile_list) native_api.WlanCloseHandle(handle) return profiles
def register_notification(callback, ntfsource): handle = native_api.WlanOpenHandle() c_back = native_api.WlanRegisterNotification(handle, functools.partial(on_wlan_notification, callback), ntfsource) global_callbacks.append(c_back) global_handles.append(handle) return NotificationObject(handle, c_back)
def connect(wireless_interface, connection_params): """ The WlanConnect function attempts to connect to a specific network. DWORD WINAPI WlanConnect( _In_ HANDLE hClientHandle, _In_ const GUID *pInterfaceGuid, _In_ const PWLAN_CONNECTION_PARAMETERS pConnectionParameters, _Reserved_ PVOID pReserved ); connection_params should be a dict with this structure: { "connectionMode": "valid connection mode string", "profile": ("profile name string" | "profile xml" | None)*, "ssid": "ssid string", "bssidList": [ "desired bssid string", ... ], "bssType": valid bss type int, "flags": valid flag dword in 0x00000000 format } * Currently, only the name string is supported here. """ handle = native_api.WlanOpenHandle() cnxp = native_api.WLAN_CONNECTION_PARAMETERS() connection_mode = connection_params["connectionMode"] connection_mode_int = native_api.WLAN_CONNECTION_MODE_VK[connection_mode] cnxp.wlanConnectionMode = native_api.WLAN_CONNECTION_MODE(connection_mode_int) # determine strProfile if connection_mode == ('wlan_connection_mode_profile' or # name 'wlan_connection_mode_temporary_profile'): # xml cnxp.strProfile = native_api.LPCWSTR(connection_params["profile"]) else: cnxp.strProfile = NULL # ssid if connection_params["ssid"] is not None: dot11Ssid = native_api.Dot11Ssid() dot11Ssid.SSID = connection_params["ssid"] dot11Ssid.SSIDLength = len(connection_params["ssid"]) cnxp.pDot11Ssid = native_api.pointer(dot11Ssid) else: cnxp.pDot11Ssid = NULL # bssidList # NOTE: Before this can actually support multiple entries, # the DOT11_BSSID_LIST structure must be rewritten to # dynamically resize itself based on input. if connection_params["bssidList"] is not None: bssids = [] for bssidish in connection_params["bssidList"]: bssidish = tuple(int(n, 16) for n in bssidish.split(b":")) bssids.append(native_api.DOT11_MAC_ADDRESS(*bssidish)) bssidListEntries = native_api.c_ulong(len(bssids)) bssids = (native_api.DOT11_MAC_ADDRESS * len(bssids))(*bssids) bssidListHeader = native_api.NDIS_OBJECT_HEADER() bssidListHeader.Type = native_api.NDIS_OBJECT_TYPE_DEFAULT bssidListHeader.Revision = native_api.DOT11_BSSID_LIST_REVISION_1 # chr() bssidListHeader.Size = native_api.c_ushort(native_api.sizeof(native_api.DOT11_BSSID_LIST)) bssidList = native_api.DOT11_BSSID_LIST() bssidList.Header = bssidListHeader bssidList.uNumOfEntries = bssidListEntries bssidList.uTotalNumOfEntries = bssidListEntries bssidList.BSSIDs = bssids cnxp.pDesiredBssidList = native_api.pointer(bssidList) else: cnxp.pDesiredBssidList = NULL # required for XP # look up bssType # bssType must match type from profile if a profile is provided bssType = native_api.DOT11_BSS_TYPE_DICT_VK[connection_params["bssType"]] cnxp.dot11BssType = native_api.DOT11_BSS_TYPE(bssType) # flags cnxp.dwFlags = native_api.DWORD(connection_params["flags"]) # print(cnxp) result = native_api.WlanConnect(handle, wireless_interface.guid, cnxp) native_api.WlanCloseHandle(handle) return result
def delete_profile(profile_name, wireless_interface): handle = native_api.WlanOpenHandle() result = native_api.WlanDeleteProfile(handle, wireless_interface.guid, profile_name) native_api.WlanCloseHandle(handle) return result