private static WLAN_PROFILE_INFO_LIST GetWirelessProfiles(IntPtr WlanHandle, Guid interfaceGuid) { Guid pInterfaceGuid = interfaceGuid; IntPtr ppProfileList = new IntPtr(); WlanGetProfileList(WlanHandle, ref pInterfaceGuid, new IntPtr(), ref ppProfileList); WLAN_PROFILE_INFO_LIST wlanProfileInfoList = new WLAN_PROFILE_INFO_LIST(ppProfileList); NetworkInterop.WlanFreeMemory(ppProfileList); return wlanProfileInfoList; }
/// <summary> /// Returns all the profiles associated with this Wireless Interface /// </summary> /// <param name="wirelessInterface">The wirelessInterface to get the profiles from</param> /// <returns></returns> public List<Profile> GetProfilesForWirelessInterface(WirelessInterface wirelessInterface) { IntPtr handle; // The interfaceGuid is passed in as a pointer which is why we need to extract it into a variable. // the function takes a const Guid* which cannot be exposed in C# Guid interfaceGuid = wirelessInterface.InterfaceGuid; uint result = NativeWireless.WlanGetProfileList(this.GetHandle(), ref interfaceGuid, IntPtr.Zero, out handle); result.ThrowIfNotSuccess(); var wlanProfileInfoList = new WLAN_PROFILE_INFO_LIST(handle); NativeWireless.WlanFreeMemory(handle); List<Profile> profilesForWirelessInterface = wlanProfileInfoList.ProfileInfo.Select((wlanProfileInfo, index) => new Profile((uint) index, wlanProfileInfo.strProfileName)).ToList(); return profilesForWirelessInterface; }
static void Main(string[] args) { const int dwClientVersion = 2; IntPtr clientHandle = IntPtr.Zero; IntPtr pdwNegotiatedVersion = IntPtr.Zero; IntPtr pInterfaceList = IntPtr.Zero; WLAN_INTERFACE_INFO_LIST interfaceList; WLAN_PROFILE_INFO_LIST wifiProfileList; Guid InterfaceGuid; IntPtr pAvailableNetworkList = IntPtr.Zero; string wifiXmlProfile = null; IntPtr wlanAccess = IntPtr.Zero; IntPtr profileList = IntPtr.Zero; string profileName = ""; try { // Open Wifi Handle WlanOpenHandle(dwClientVersion, IntPtr.Zero, out pdwNegotiatedVersion, ref clientHandle); // Find Wi-Fi interface GUID WlanEnumInterfaces(clientHandle, IntPtr.Zero, ref pInterfaceList); interfaceList = new WLAN_INTERFACE_INFO_LIST(pInterfaceList); InterfaceGuid = ((WLAN_INTERFACE_INFO)interfaceList.InterfaceInfo[0]).InterfaceGuid; // Get Wifi Profile WlanGetProfileList(clientHandle, InterfaceGuid, IntPtr.Zero, ref profileList); wifiProfileList = new WLAN_PROFILE_INFO_LIST(profileList); Console.WriteLine(""); Banner(); Console.WriteLine("Found {0} SSIDs: ", wifiProfileList.dwNumberOfItems); Console.WriteLine("============================"); Console.WriteLine(""); for (int i = 0; i < wifiProfileList.dwNumberOfItems; i++) { try { profileName = (wifiProfileList.ProfileInfo[i]).strProfileName; int decryptKey = 63; //https://docs.microsoft.com/en-us/windows/win32/nativewifi/wlan-profileschema-keymaterial-sharedkey-element // Retrieve Wifi SSID Name and Passsword WlanGetProfile(clientHandle, InterfaceGuid, profileName, IntPtr.Zero, out wifiXmlProfile, ref decryptKey, out wlanAccess); XmlDocument xmlProfileXml = new XmlDocument(); xmlProfileXml.LoadXml(wifiXmlProfile); XmlNodeList pathToSSID = xmlProfileXml.SelectNodes("//*[name()='WLANProfile']/*[name()='SSIDConfig']/*[name()='SSID']/*[name()='name']"); XmlNodeList pathToPassword = xmlProfileXml.SelectNodes("//*[name()='WLANProfile']/*[name()='MSM']/*[name()='security']/*[name()='sharedKey']/*[name()='keyMaterial']"); foreach (XmlNode ssid in pathToSSID) { Console.WriteLine("SSID: " + ssid.InnerText); foreach (XmlNode password in pathToPassword) { Console.WriteLine("Password: "******"----------------------------"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } // Close Wifi Handle WlanCloseHandle(clientHandle, IntPtr.Zero); } catch (Exception e) { Console.WriteLine(e.Message); } }