the internet's safety

changemac-win.c

changemac-win.c
Posted Dec 31, 2005
Authored by Robbe De Keyzer

MAC changing utility that can be used on Windows from the command line.

systems | windows
MD5 | 4eff620a8f4c19d1135ff3278e7da1c3

changemac-win.c

Change Mirror Download

#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#include "devguid.h" //
http://doc.ddart.net/msdn/header/include/devguid.h.html

/*
Simple c file for changing the MAC address on a windows system from the
command line.

Consists solely of changing the below reg value, and restarting the network
adapter

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\....


Yes, NIC can be restarted using devcon.exe
Yes, code is bloathed


TODO:
- code cleanup
- make the listing of adapters better (only show network cards)
- reset MAC address option


Quickly whipped this up yesterday afternoon after seeing several programs
that do the
same thing (for 20 bucks)

Robbe De Keyzer, December 2005
*/


// This is where the MAC address is kept in all NT variants
const char *subkey =
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\";


void list_adapters(void);
char *get_adapter_description(int);
void set_mac(int, char *);
void restart_adapter(int);


int main(int argc, char *argv[])
{
int adapter_id;

if (argc < 2)
{
printf("Usage: %s [-l] [-s adapter_id MAC]\n", argv[0]);
printf("Where\n");
printf("\t -l gives a list of all adapters and their id\n");
printf("\t -s will set the supplied MAC address for the chosen
adapter\n");
printf("\nWarning! This will restart your internet connection.\n");
return(0);
}

if (!strncmp(argv[1], "-l", 2))
{
list_adapters();
}

if (!strncmp(argv[1], "-s", 2))
{
adapter_id = atoi(argv[2]);

set_mac(adapter_id, argv[3]);

restart_adapter(adapter_id);

printf("All done. MAC address should be changed.\n");
printf("Wait a few seconds and try ipconfig /all\n");
}


return(0);
}


/* Print a list of all network adapters */
void list_adapters()
{
int i;
char *descr;

printf("\nList of available network adapters\n");


/* Get a list of all network adapters from the registry */
for (i=0; i<=12; i++)
{
descr = get_adapter_description(i);

if (descr != NULL)
{
printf("%d: %s\n", i, descr);
}
}
}


/* Get the description of an adapter by its number in the registry*/
char *get_adapter_description(int regNum)
{
DWORD size;
char buf[256], value[256];
HKEY hKey;

strcpy(buf, "");
snprintf(buf, sizeof(buf), "%s%04d", subkey, regNum);

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey) ==
ERROR_SUCCESS)
{
/* First get the size of the data that's waiting using a NULL lpData
parameter */
RegQueryValueEx(hKey, "DriverDesc", NULL, NULL, NULL, &size);

/* Then we print the name of the network adapter */
if(RegQueryValueEx(hKey, "DriverDesc", NULL, NULL, value, &size) ==
ERROR_SUCCESS)
{
RegCloseKey(hKey);
return(value);
}

RegCloseKey(hKey);
}

return(NULL);
}

/*
Write our supplied MAC to the NetworkAddress key in the registry
*/
void set_mac(int regNum, char *mac)
{
DWORD size, nRet;
char buf[256], value[256];
HKEY hKey;

strcpy(buf, "");
snprintf(buf, sizeof(buf), "%s%04d", subkey, regNum);

/* If the NetworkAddress key does not exist, create it */
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_ALL_ACCESS, &hKey) ==
ERROR_SUCCESS)
{
RegSetValueEx(hKey,"NetworkAddress",0,REG_SZ,mac,strlen(mac));

RegCloseKey(hKey);
}
}

/*
Copied from
http://groups.google.com/group/microsoft.public.vb.winapi.networks/browse_thread/thread/e8d7e5f627e57c11/58bdcdc53ce70e0f
*/
BOOL StateChange(DWORD NewState, DWORD SelectedItem,HDEVINFO hDevInfo)
{
SP_PROPCHANGE_PARAMS PropChangeParams ={sizeof(SP_CLASSINSTALL_HEADER)};
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};

//
// Get a handle to the Selected Item.
//
if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))
return FALSE;

//
// Set the PropChangeParams structure.
//
PropChangeParams.ClassInstallHeader.InstallFunction =
DIF_PROPERTYCHANGE;
PropChangeParams.Scope = DICS_FLAG_GLOBAL;
PropChangeParams.StateChange = NewState;

if (!SetupDiSetClassInstallParams(hDevInfo,
&DeviceInfoData,
(SP_CLASSINSTALL_HEADER *)&PropChangeParams,
sizeof(PropChangeParams)))
return FALSE;

//
// Call the ClassInstaller and perform the change.
//
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,
hDevInfo,
&DeviceInfoData))
return FALSE;

return TRUE;

}


void restart_adapter(int adapter_id)
{
int i;
HDEVINFO hdi;
SP_DEVINFO_DATA DeviceInfoData;

// get a list of all devices of class 'GUID_DEVCLASS_NET'
hdi = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT);
if (hdi == INVALID_HANDLE_VALUE)
{
printf("Invalid handle\n");
return;
}

/*
We can just use the first entry in the hdi list as our adapter but we loop
through
them all to make sure we are working with the right one

Copied from
http://msdn.microsoft.com/library/en-us/devio/base/displaying_the_installed_devices.asp
*/

// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

for (i=0; SetupDiEnumDeviceInfo(hdi,i,&DeviceInfoData); i++)
{
LPTSTR buffer = NULL;
DWORD buffersize = 0;

while (!SetupDiGetDeviceRegistryProperty(hdi,&DeviceInfoData,
SPDRP_DEVICEDESC,NULL,(PBYTE)buffer,buffersize,&buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
buffer = LocalAlloc(LPTR,buffersize);
}
else
{
break;
}
}

/* Do we have the right adapter? */
if (!strncmp(buffer, get_adapter_description(adapter_id), strlen(buffer)))
{
/* First disable it, then enable it again */
StateChange(DICS_DISABLE, 0, hdi);
StateChange(DICS_ENABLE, 0, hdi);

SetupDiDestroyDeviceInfoList(hdi);

if (buffer) LocalFree(buffer);

return;
}

if (buffer) LocalFree(buffer);
}

}

_________________________________________________________________
Gratis bloggen op MSN Spaces http://spaces.msn.com/?mkt=nl-be

Comments

RSS Feed Subscribe to this comment feed

No comments yet, be the first!

Login or Register to post a comment

File Archive:

May 2012

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    May 1st
    37 Files
  • 2
    May 2nd
    53 Files
  • 3
    May 3rd
    33 Files
  • 4
    May 4th
    4 Files
  • 5
    May 5th
    10 Files
  • 6
    May 6th
    17 Files
  • 7
    May 7th
    19 Files
  • 8
    May 8th
    36 Files
  • 9
    May 9th
    34 Files
  • 10
    May 10th
    35 Files
  • 11
    May 11th
    20 Files
  • 12
    May 12th
    18 Files
  • 13
    May 13th
    11 Files
  • 14
    May 14th
    27 Files
  • 15
    May 15th
    58 Files
  • 16
    May 16th
    54 Files
  • 17
    May 17th
    25 Files
  • 18
    May 18th
    53 Files
  • 19
    May 19th
    9 Files
  • 20
    May 20th
    15 Files
  • 21
    May 21st
    25 Files
  • 22
    May 22nd
    32 Files
  • 23
    May 23rd
    35 Files
  • 24
    May 24th
    26 Files
  • 25
    May 25th
    25 Files
  • 26
    May 26th
    0 Files
  • 27
    May 27th
    0 Files
  • 28
    May 28th
    0 Files
  • 29
    May 29th
    0 Files
  • 30
    May 30th
    0 Files
  • 31
    May 31st
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2012 Packet Storm. All rights reserved.

close