Solved

Programmatically switching the WEP key

Posted on 2006-07-05
5
875 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
  • 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now