awkman / pywifi

A cross-platform module for manipulating WiFi devices.
MIT License
447 stars 154 forks source link

iface.scan() not detecting cipher types #62

Open hersh-nv opened 4 years ago

hersh-nv commented 4 years ago

when using iface.scan() to scan for available networks, the cipher field in the returned network profiles is always 0, corresponding with const.CIPHER_TYPE_NONE. this is incorrect, as in my case all available networks actually used cipher type CCMP.

>>> import pywifi
>>> iface = pywifi.PyWiFi().interfaces()[0]
>>> iface.scan()
>>> time.sleep(10)
>>> profiles = iface.scan_results()
>>> [profile.cipher for profile in profiles]
[0, 0, 0, 0, 0, 0, 0]

this means that when adding a key to a profile prior to connecting to it, i also needed to amend the cipher field. e.g. if i wanted to connect to profiles[0] above, i would have to:

>>> from pywifi import const
>>> profiles[0].cipher = const.CIPHER_TYPE_CCMP
>>> profiles[0].key = "networkpasswordhere"
>>> profile = iface.add_network_profile(profiles[0])
>>> iface.connect(profile)

without setting the cipher type, the profile would not add to the interface. (i believe this may be the cause of issue #45.)

the issue here is that the scan should be able to return the cipher type of each profile; the user shouldn't have to manually determine the cipher type through other means and enter it into the profile. for instance in windows 10, using the netsh command to scan for available networks returns the cipher type under the attribute 'Encryption'.

>netsh wlan show networks interface="Wi-Fi"

Interface name : Wi-Fi
There are 5 networks currently available

SSID 1 : ExampleSSID
    Network type             : Infrastructure
    Authentication           : WPA2-Personal
    Encryption               : CCMP

...

issue found in Windows 10 and Linux (Ubuntu 19.10), and occurs regardless of whether interface is connected to an AP or in disconnected status when using iface.scan().

abrararies commented 3 years ago

Facing same issue

Lgum commented 3 years ago

The issue seems to be a mixup in the returned values in '_wifiutil_win.py'. The returned 'akm' value is actually the Ciphertype and the 'auth_alg'-value includes the authentification algorithm and the key management. ( see: https://docs.microsoft.com/en-us/windows/win32/nativewifi/dot11-auth-algorithm and https://docs.microsoft.com/en-us/windows/win32/nativewifi/dot11-cipher-algorithm ).

In my case, the returned values are correct (auth-algorithm = 7 => DOT11_AUTH_ALGO_RSNA_PSK and akm = 4 =>DOT11_CIPHER_ALGO_CCMP) but the assignment is wrong.

A quick fix is to just take the akm-value returned from pywifi and use an enum to determine the cipher type.