exploit the possibilities
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:

May 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    May 1st
    44 Files
  • 2
    May 2nd
    5 Files
  • 3
    May 3rd
    11 Files
  • 4
    May 4th
    0 Files
  • 5
    May 5th
    0 Files
  • 6
    May 6th
    28 Files
  • 7
    May 7th
    3 Files
  • 8
    May 8th
    4 Files
  • 9
    May 9th
    54 Files
  • 10
    May 10th
    12 Files
  • 11
    May 11th
    0 Files
  • 12
    May 12th
    0 Files
  • 13
    May 13th
    17 Files
  • 14
    May 14th
    11 Files
  • 15
    May 15th
    17 Files
  • 16
    May 16th
    13 Files
  • 17
    May 17th
    22 Files
  • 18
    May 18th
    0 Files
  • 19
    May 19th
    0 Files
  • 20
    May 20th
    0 Files
  • 21
    May 21st
    0 Files
  • 22
    May 22nd
    0 Files
  • 23
    May 23rd
    0 Files
  • 24
    May 24th
    0 Files
  • 25
    May 25th
    0 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

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close