How to use “Native Wifi API” Windows API functions with ruby

1.4k views Asked by At

My Setup: Windows 7, Ruby 1.9.3

I want to manage the wireless NIC to access different Wireless Routers, here's what I have tried:

Method 1: netsh wlan

Managing the WLAN adapter through the netsh command tools.

So I setup my Wireless Network in Windows and exported it using

netsh wlan export profile name="wuhongliang" folder = "d:\" interface="Wireless" key=clear

Which worked, I can then add the profile and connect by doing:

> netsh wlan add profile ^
filename="d:\wireless-wuhongliang.xml" ^
interface="wireless" Profile

wuhongliang is added on interface Wireless.

> netsh wlan connect name="wuhongliang" ^
ssid="wuhongliang"
Connection request was completed successfully.

Which works and connects me to the WLAN using the exported profile.

So looking at the XML Profile:

<?xml version="1.0" ?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
  <name>wuhongliang</name>
  <SSIDConfig>
    <SSID>
      <hex>7775686F6E676C69616E67</hex>
      <name>wuhongliang</name>
    </SSID>
  </SSIDConfig>
  <connectionType>ESS</connectionType>
  <connectionMode>auto</connectionMode>
  <MSM>
    <security>
      <authEncryption>
        <authentication>WPA2PSK</authentication>
        <encryption>AES</encryption>
        <useOneX>false</useOneX>
      </authEncryption>
      <sharedKey>
        <keyType>passPhrase</keyType>
        <protected>true</protected>
        <keyMaterial>[Long Encrypted Key]</keyMaterial>
      </sharedKey>
    </security>
  </MSM>
</WLANProfile>

The keyMaterial is obviously the encrypted network password. So if I change the router password then the profile breaks because I don't have the new encrypted keyMaterial.

Since I don't know how to generate the keyMaterial this won't work for me.

Method 2: using wlanapi.dll

I found this article on codeproject.com and it looked promising. But I have no experience in C#,C,C++.

I tried using the dll with Ruby DL and Win32API but I don't how to set the parameters or use those libraries in general.

This is the Ruby code I used call the WlanEnumInterfaces API (but I'm pretty sure it's incorrect):

c_handle = 1
c_reserved = 0
c_interfacelist=" "*10000 
dllname = "wlanapi.dll" 
pro=Win32API.new(dllname, "WlanEnumInterfaces", ['p', 'l', 'p'], "v") 
p pro.call(handle, c_reserved, c_interfacelist) 
p c_interfacelist.unpack("l*")
1

There are 1 answers

2
Azolo On

In the sharedKey tag change the protected value to false and the keyMaterial value can be imported in plaintext.

<sharedKey>
    <keyType>passPhrase</keyType>
    <protected>false</protected>
    <keyMaterial>[The Password]</keyMaterial>
  </sharedKey>

The key=clear option was supposed to do that for you, but it obviously wasn't.