| |
| View previous topic :: View next topic |
ven Moderator

Joined: 10 Dec 2004 Posts: 2064 Location: USA
|
Posted: 23.01.05 07:05 Post subject: CHMAC: Change MAC address in Windows |
|
|
|
I think the "C/C++, Assembler" section has been empty too long. So, as a remedy, I decided to post the source code to a little C++ program I wrote, called CHMAC, that anyone with Windows XP or later (it might work with 2000, but I haven't tested that) can use to change the MAC address of the network card. The compiled binary is available on my site.
| Code: | #include <iostream.h>
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#include <stdlib.h>
#include "string.h"
unsigned long enableDisable = -1;
char* newAddr;
int main( int argc, char *argv[ ], char *envp[ ] )
{
if(argc > 2){
enableDisable = atoi(argv[1]);
newAddr = argv[2];
}
else{
cout << "CHMAC - Change MAC address. Also sounds cool when pronounced as one word.\r\nUSAGE: chmac adapter address\r\nwhere adapter is the desired adapter number from the list below,\r\nand address is a MAC address in the format dd11223344f8 or RESET to reset\r\nto the default value. NOTE: A MAC address can only contain hexadecimals\r\n(0123456789ABCDEF). There is NO automatic checking!\r\nDisclaimer: This program is provided \"AS-IS\". Anything that may or may not\r\nhappen because of it is entirely the user's fault.\r\n\r\n";
}
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
SP_PROPCHANGE_PARAMS Param;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
unsigned long netguid = 1295444338;
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
if (buffer) LocalFree(buffer);
buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);
}
else
{
break;
}
}
if(DeviceInfoData.ClassGuid.Data1 == netguid && enableDisable == -1) cout << i << ": " << buffer << endl;
if(i == enableDisable && DeviceInfoData.ClassGuid.Data1 == netguid){
HKEY k = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_ALL_ACCESS);
if(k == INVALID_HANDLE_VALUE){
cout << "ERROR: Cannot open driver information for editing!" << endl;
exit(1);
}
TCHAR regData[128];
unsigned long regDataLen = sizeof(regData);
if(RegQueryValueEx(k, "NetworkAddress", NULL, NULL, (unsigned char*)®Data, ®DataLen) == 0){
//something exists; KILL IT!!!
cout << "Previous value exists; deleting..." << endl;
RegDeleteValue(k, "NetworkAddress");
}
if(stricmp((const char*)newAddr, "reset") != 0){
if(RegSetValueEx(k, "NetworkAddress", 0, REG_SZ, (const unsigned char*)newAddr, strlen((const char*)newAddr)) != ERROR_SUCCESS){
cout << "ERROR: Could not set new MAC address!" << endl;
exit(1);
}
}
else{
cout << "MAC reset successful. Proceeding..." << endl;
}
cout << "Change successful. Rebooting adapter..." << endl;
RegCloseKey(k);
Param.ClassInstallHeader.cbSize = sizeof(Param.ClassInstallHeader);
Param.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
Param.HwProfile = 0;
Param.Scope = DICS_FLAG_GLOBAL;
//disable it:
Param.StateChange = DICS_DISABLE;
SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER *)&Param, sizeof(Param));
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData);
//reenable it:
Param.StateChange = DICS_ENABLE;
SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER *)&Param, sizeof(Param));
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData);
cout << "CHMAC complete." << endl;
}
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
return 1;
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
} |
I realize I probably have around three comments in the whole thing, but I never planned on doing anything with the code to require any comments. So if anyone dares to analyze that code and has a question, please ask. |
|
| Back to top
|
|
|
dellanio Funnilein
Joined: 05 Jun 2005 Posts: 1 Location: california
|
Posted: 05.06.05 16:20 Post subject: Cool but.... |
|
|
|
But where is the compiled binary??? Your site doesnt have this.
Thanks! |
|
| Back to top
|
|
|
dennisvanhouts Guest
|
Posted: 05.06.05 17:18 Post subject: |
|
|
|
Well, if I go to the link he provided, I can download the EXE file.  |
|
| Back to top
|
|
|
ven Moderator

Joined: 10 Dec 2004 Posts: 2064 Location: USA
|
Posted: 06.06.05 00:56 Post subject: |
|
|
|
Exactly. I don't know how much more plain (text) it can be. _________________

|
|
| Back to top
|
|
|
animestash Little-Fun

Joined: 19 Oct 2005 Posts: 22
|
Posted: 20.10.05 09:32 Post subject: |
|
|
|
| I didn't know you can change MAC IP, isn't that hardcoded into the NIC's e2prom? |
|
| Back to top
|
|
|
ven Moderator

Joined: 10 Dec 2004 Posts: 2064 Location: USA
|
Posted: 20.10.05 17:47 Post subject: |
|
|
|
It's not a "MAC IP," it's just a MAC address. An IP is something totally different. Yes, the MAC is hardcoded, but that can be bypassed using a special setting in Windows, which my program takes advantage of. _________________

|
|
| Back to top
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
| |
|