what you don't know can hurt you
Home Files News &[SERVICES_TAB]About Contact Add New

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
SHA-256 | 90c5fbc6757814acbd1f1a07456780bb3a9a61b9ef64a246eb092af41bd2f1e8

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
Login or Register to add favorites

File Archive:

September 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Sep 1st
    261 Files
  • 2
    Sep 2nd
    17 Files
  • 3
    Sep 3rd
    38 Files
  • 4
    Sep 4th
    52 Files
  • 5
    Sep 5th
    23 Files
  • 6
    Sep 6th
    27 Files
  • 7
    Sep 7th
    0 Files
  • 8
    Sep 8th
    1 Files
  • 9
    Sep 9th
    16 Files
  • 10
    Sep 10th
    38 Files
  • 11
    Sep 11th
    21 Files
  • 12
    Sep 12th
    40 Files
  • 13
    Sep 13th
    18 Files
  • 14
    Sep 14th
    0 Files
  • 15
    Sep 15th
    0 Files
  • 16
    Sep 16th
    21 Files
  • 17
    Sep 17th
    51 Files
  • 18
    Sep 18th
    23 Files
  • 19
    Sep 19th
    48 Files
  • 20
    Sep 20th
    36 Files
  • 21
    Sep 21st
    0 Files
  • 22
    Sep 22nd
    0 Files
  • 23
    Sep 23rd
    0 Files
  • 24
    Sep 24th
    0 Files
  • 25
    Sep 25th
    0 Files
  • 26
    Sep 26th
    0 Files
  • 27
    Sep 27th
    0 Files
  • 28
    Sep 28th
    0 Files
  • 29
    Sep 29th
    0 Files
  • 30
    Sep 30th
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2024 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close