Connecting to WEP network via wpa_supplicant/dbus/python

Dan Williams dcbw at redhat.com
Tue Feb 17 11:19:14 EST 2009


On Tue, 2009-02-17 at 20:17 +1100, James Rayner wrote:
> I'm attempting to connect to a WEP network with wpa_supplicant via the
> dbus interface with python.
> 
> wpa_supplication reports association status as complete, however it
> does not seem to be connected correctly as dhcp cannot get an IP nor
> does it work with static IP. I am however able to connect using a
> regular wpa_supplicant.conf or iwconfig manually.
> 
> I'm fairly sure I'm passing the wep key incorrectly in the code as
> incorrectly passing the key in quotes in the wpa_supplicant config
> file gives _exactly_ the same result (same incorrect key in iwconfig
> output and same behaviour). How should a hex WEP key be passed via
> dbus?
> 
> Driver is ipw2100, rmmod and modprobed before each test, router is
> wrt54gl. 2.6.28, wpa supplicant versions 0.5.11 and 0.6.7.
> 
> Outputs below, code at the bottom:
> 
> iwconfig output after wepcase.py connection
>  - dhcp & manual IP fail
>  - note the different key
> ipw0      IEEE 802.11b  ESSID:"somenetwork"  Nickname:"ipw2100"
>           Mode:Managed  Frequency:2.437 GHz  Access Point: 00:16:B6:46:3A:CA
>           Bit Rate=11 Mb/s   Tx-Power:16 dBm
>           Retry short limit:7   RTS thr:off   Fragment thr:off
>           Encryption key:3031-3743-4633-4330-4545   Security mode:open
>           Power Management:off
>           Link Quality=88/100  Signal level=-70 dBm
>           Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
>           Tx excessive retries:0  Invalid misc:0   Missed beacon:0
> 
> iwconfig output after wpa_supplicant config file or manual iwconfig connection
>  - dhcp & manual IP work
>  - correct key shown
> ipw0      IEEE 802.11b  ESSID:"somenetwork"  Nickname:"ipw2100"
>           Mode:Managed  Frequency:2.437 GHz  Access Point: 00:16:B6:46:3A:CA
>           Bit Rate=11 Mb/s   Tx-Power:16 dBm
>           Retry short limit:7   RTS thr:off   Fragment thr:off
>           Encryption key:017C-F3C0-EE   Security mode:open
>           Power Management:off
>           Link Quality=89/100  Signal level=-69 dBm
>           Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
>           Tx excessive retries:0  Invalid misc:0   Missed beacon:0
> 
> dhcp:
>     dhcpcd ipw0
> 
> manual ip:
>     ip addr add dev eth0 192.168.1.23/24 brd +
>     ip route add default via 192.168.1.1
> 
> iwlist scan:
>           Cell 02 - Address: 00:16:B6:46:3A:CA
>                     ESSID:"somenetwork"
>                     Protocol:IEEE 802.11bg
>                     Mode:Master
>                     Frequency:2.437 GHz (Channel 6)
>                     Encryption key:on
>                     Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
>                               11 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
>                               48 Mb/s; 54 Mb/s
>                     Quality:55  Signal level:0  Noise level:0
>                     Extra: Last beacon: 180ms ago
> 
> config file:
> ctrl_interface=/var/run/wpa_supplicant
> ctrl_interface_group=wheel
> network={
> 	ssid="rayner"
> 	key_mgmt=NONE
> 	wep_key0=017CF3C0EE
> 	wep_tx_keyidx=1
> 	group=WEP40
> }
> 
> 
> Code:
> from time import sleep
> import dbus
> from os import system
> 
> # dbus constants.
> WPAS_DBUS_SERVICE = "fi.epitest.hostap.WPASupplicant"
> WPAS_DBUS_INTERFACE = "fi.epitest.hostap.WPASupplicant"
> WPAS_DBUS_OPATH = "/fi/epitest/hostap/WPASupplicant"
> 
> WPAS_DBUS_INTERFACES_INTERFACE = "fi.epitest.hostap.WPASupplicant.Interface"
> WPAS_DBUS_INTERFACES_OPATH = "/fi/epitest/hostap/WPASupplicant/Interfaces"
> WPAS_DBUS_BSSID_INTERFACE = "fi.epitest.hostap.WPASupplicant.BSSID"
> WPAS_DBUS_NETWORKS_INTERFACE = "fi.epitest.hostap.WPASupplicant.Network"
> 
> # Configuration
> profile={"INTERFACE":"ipw0", "KEY":"017CF3C0EE", "ESSID":"somenetwork" }
> 
> # Start wpa_supplicant
> system("wpa_supplicant -ud -P/var/run/wpa_supplicant.pid &")
> 
> # Connect to wpa_supplicant
> bus = dbus.SystemBus()
> wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
> wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
> 
> # Add/Get interface path
> try:
>     path = wpas.getInterface(profile["INTERFACE"])
> except dbus.exceptions.DBusException:
>     path = wpas.addInterface(profile["INTERFACE"],
> {"driver":dbus.String("wext",variant_level=1)})
> 
> # Get interface object
> if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
> iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE);
> 
> # Add network
> path = iface.addNetwork()
> net_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
> rnet = dbus.Interface(net_obj, WPAS_DBUS_NETWORKS_INTERFACE)
> iface.selectNetwork(rnet)
> 
> # This works for a WPA network
> # opts = dbus.Dictionary({"ssid": dbus.ByteArray(profile['ESSID']),
> "psk": dbus.String(profile['KEY'])}, signature="sv")
> 
> # This doesnt work for WEP networks
> # For all except ssid, it makes no difference whether the type is
> explicit or left to dbus-python
> opts = dbus.Dictionary({"ssid": dbus.ByteArray(profile['ESSID']),
>                         "wep_key0": dbus.ByteArray(profile['KEY']), #

How is the key stored in 'profile'?  It's likely you're storing the key
as a hex string, right?  That's *not* actually the binary key.  To get
the actual binary key, you need to convert the key from hex string
representation to bytes.  For every two characters of the hex key,
convert that hex value to a byte value from 0 - 255 (ie, '1a' == 0x1a).
Thus, of course, the binary key will be 1/2 the size of the hexadecimal
key.

Note that WPA-PSK keys can be sent as the passphrase, and the D-Bus
interface will quote them correctly.  But since WEP has no standard
passphrase -> key hashing mechanism, that's not really do-able for WEP.
Furthermore, for WEP, hex keys are actually acceptable passphrases.  So
the D-Bus interface takes the position that for WEP, since the key type
cannot be detected based on the input material, the caller must do the
hashing themselves, and pass the actual 5- or 13-byte WEP key to the
supplicant.

Yeah, the D-Bus interface is somewhat ugly; I admit that.  I've got
vague, hand-wavy plans to fix it.

Dan

> Both String and ByteArray have the same result
>                         "wep_tx_keyidx": dbus.Int32(0), # 1 or 0 make
> no difference
>                         "key_mgmt":dbus.String("NONE"),
>                         # "group":"WEP40",             # makes no difference
>                         }, signature="sv")
> 
> rnet.set(opts)
> 
> # Check every second for 15, then give up.
> for n in range(15):
>     sleep(1)
>     if iface.state() == "COMPLETED":
>         print "completed!"
>         break
>     elif n==14:
>         print "fail" + iface.state()
>         system("pkill wpa_supplicant")
> _______________________________________________
> HostAP mailing list
> HostAP at lists.shmoo.com
> http://lists.shmoo.com/mailman/listinfo/hostap



More information about the HostAP mailing list