Solved

Programmatically switching the WEP key

Posted on 2006-07-05
5
891 Views
Last Modified: 2013-12-09
I need to change the WEP key on my wireless connection to Win XP/CE on demand through a C++ program.  I looked for an ntddndis.h file - is it available somewhere on my windows system folder?  I can't find it.   I looked around here and MSDN - do you need to use the OID_802_11_ADD_WEP function?  Is it built in somewhere in a library?
0
Comment
Question by:crayola3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
opanza earned 500 total points
ID: 17114019
Hi,

In order to change your WEP key on demand you have to use OID_802_11_ADD_WEP Oid. This is a sample function that does the job:

HRESULT SetWEPKey(PNDIS_802_11_WEP pWepKey)
{
//UCHAR               SetBuffer[sizeof(NDIS_OID) + sizeof(NDIS_802_11_WEP)];
UCHAR               SetBuffer[ 1024 ] = { 0 };
PNDISUIO_SET_OID    pSetOid;
HRESULT                  hRes = S_OK;
DWORD                    dwBytesReturned = 0;

     pSetOid = (PNDISUIO_SET_OID)SetBuffer;
     pSetOid->Oid = OID_802_11_ADD_WEP;
     pSetOid->ptcDeviceName = m_pwDeviceName;

     memcpy( pSetOid->Data, (LPVOID)pWepKey, pWepKey->Length );

     if( DeviceIoControl( m_hFileHandle,
                          IOCTL_NDISUIO_SET_OID_VALUE,
                          (LPVOID)SetBuffer,
                          sizeof( SetBuffer ),
                          (LPVOID)SetBuffer,
                          0,
                          &dwBytesReturned,
                          NULL ) )
     {
          hRes = S_OK;
     }
     else
     {
          hRes = GetLastError();
     }

     return hRes;
}

Then, just use ir:

NDIS_802_11_WEP WepKey;
UCHAR KeyMaterial[13];

// Key 1 (WEP key you want to set)
// 0123456789ABCDEF0123456789
KeyMaterial[0] = 0x01;
KeyMaterial[1] = 0x23;
KeyMaterial[2] = 0x45;
KeyMaterial[3] = 0x67;
KeyMaterial[4] = 0x89;
KeyMaterial[5] = 0xAB;
KeyMaterial[6] = 0xCD;
KeyMaterial[7] = 0xEF;
KeyMaterial[8] = 0x01;
KeyMaterial[9] = 0x23;
KeyMaterial[10] = 0x45;
KeyMaterial[11] = 0x67;
KeyMaterial[12] = 0x89;

// Fill in WEP Key structure

// key index - 0
WepKey.KeyIndex = 0x80000000;
WepKey.KeyLength = sizeof( KeyMaterial );

// Copy WEP Key
memset(WepKey.KeyMaterial,0,sizeof(WepKey.KeyMaterial));
memcpy(WepKey.KeyMaterial, KeyMaterial, WepKey.KeyLength);
WepKey.Length = sizeof(NDIS_802_11_WEP) + WepKey.KeyLength;

// Set the key
if( ( hr = SetWEPKey(&WepKey) ) == S_OK )
{
      //WEP Key 1 set
}
else
{
      // Error setting key
}
0
 

Author Comment

by:crayola3
ID: 17124298
Getting many compile errors on your code - I'm likely not including something correctly -- either a missing .h or some variables I need are not defined.

What are m_pwDeviceName, m_hFileHandle and GetLastError() ?  Are they included somewhere or do I need to define them?

I have your code in my gw.cpp source file:

#include "stdafx.h"
#include "ntddndis.h"

HRESULT SetWEPKey(PNDIS_802_11_WEP pWepKey) <-- I get a compile syntax error -- HRESULT not defined ?
{
UCHAR               SetBuffer[ 1024 ] = { 0 };
PNDISUIO_SET_OID    pSetOid;     <--- I get compile syntax errors here -- PNDISUIO_SET_OID not defined ?
HRESULT                  hRes = S_OK;
DWORD                    dwBytesReturned = 0;

     pSetOid = (PNDISUIO_SET_OID)SetBuffer;
     pSetOid->Oid = OID_802_11_ADD_WEP;
     pSetOid->ptcDeviceName = m_pwDeviceName;

     memcpy( pSetOid->Data, (LPVOID)pWepKey, pWepKey->Length );

     if( DeviceIoControl( m_hFileHandle,
                          IOCTL_NDISUIO_SET_OID_VALUE,
                          (LPVOID)SetBuffer,
                          sizeof( SetBuffer ),
                          (LPVOID)SetBuffer,
                          0,
                          &dwBytesReturned,
                          NULL ) )
     {
          hRes = S_OK;
     }
     else
     {
          hRes = GetLastError();
     }

     return hRes;
}
int main(int argc, char* argv[])
{
NDIS_802_11_WEP WepKey;
UCHAR KeyMaterial[13];
HRESULT hr;

// Key 1 (WEP key you want to set)
// 0123456789ABCDEF0123456789
KeyMaterial[0] = 0x01;
KeyMaterial[1] = 0x23;
KeyMaterial[2] = 0x45;
KeyMaterial[3] = 0x67;
KeyMaterial[4] = 0x89;
KeyMaterial[5] = 0xAB;
KeyMaterial[6] = 0xCD;
KeyMaterial[7] = 0xEF;
KeyMaterial[8] = 0x01;
KeyMaterial[9] = 0x23;
KeyMaterial[10] = 0x45;
KeyMaterial[11] = 0x67;
KeyMaterial[12] = 0x89;

// Fill in WEP Key structure

// key index - 0
WepKey.KeyIndex = 0x80000000;
WepKey.KeyLength = sizeof( KeyMaterial );

// Copy WEP Key
memset(WepKey.KeyMaterial,0,sizeof(WepKey.KeyMaterial));
memcpy(WepKey.KeyMaterial, KeyMaterial, WepKey.KeyLength);
WepKey.Length = sizeof(NDIS_802_11_WEP) + WepKey.KeyLength;

// Set the key
if( ( hr = SetWEPKey(&WepKey) ) == S_OK )
{
     //WEP Key 1 set
}
else
{
     // Error setting key
}
      printf("Hello World!\n");
      return 0;
}
0
 
LVL 4

Expert Comment

by:opanza
ID: 17162795
I solved this problem by installing the Platform SDK.
This post talks about it as well:

http://www.experts-exchange.com/Programming/Wireless_Programming/802_11x_Dev/Q_21250950.html

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question