-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathMyMACAddr.cpp
372 lines (326 loc) · 10.5 KB
/
MyMACAddr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Copyright (C) 2016 SaEeD
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*/
#include "MyMACAddr.h"
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
MyMACAddr::MyMACAddr()
{
//Seeding for random numbers
srand((unsigned)time(0));
}
MyMACAddr::~MyMACAddr()
{
}
//-----------------------------------------------
// Generate Random MAC Addresses
//-----------------------------------------------
string MyMACAddr::GenRandMAC()
{
stringstream temp;
int number = 0;
string result;
for (int i = 0; i < 6; i++)
{
number = rand() % 254;
temp << setfill('0') << setw(2) << hex << number;
if (i != 5)
{
temp << "-";
}
}
result = temp.str();
for (auto &c : result)
{
c = toupper(c);
}
return result;
}
//-----------------------------------------------
// Original Code from : https://msdn.microsoft.com/en-us/library/windows/desktop/aa366062(v=vs.85).aspx
// Print the details of Network Interfaces
//-----------------------------------------------
void MyMACAddr::showAdapterList()
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
UINT i;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
cerr << "Error allocating memory needed to call GetAdaptersinfo." << endl;
}
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
cerr << "Error allocating memory needed to call GetAdaptersinfo" << endl;
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
cout<<"\n\tComboIndex: \t" << pAdapter->ComboIndex << endl;
cout << "\tAdapter Name: \t" << pAdapter->AdapterName << endl;
cout << "\tAdapter Desc: \t" << pAdapter->Description <<endl;
cout << "\tAdapter Addr: \t";
for (i = 0; i < pAdapter->AddressLength; i++) {
if (i == (pAdapter->AddressLength - 1))
printf("%.2X\n", (int)pAdapter->Address[i]);
else
printf("%.2X-", (int)pAdapter->Address[i]);
}
cout << "\tIP Address: \t" << pAdapter->IpAddressList.IpAddress.String << endl;
cout << "\tIP Mask: \t" << pAdapter->IpAddressList.IpMask.String << endl;
cout << "\tGateway: \t" <<pAdapter->GatewayList.IpAddress.String << endl;
pAdapter = pAdapter->Next;
}
}
else {
cerr << "GetAdaptersInfo failed with error: " << dwRetVal<< endl;
}
if (pAdapterInfo)
FREE(pAdapterInfo);
}
//-----------------------------------------------
// Get Network Adapter's Name and MAC addresses
//-----------------------------------------------
unordered_map<string, string> MyMACAddr::getAdapters()
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
unordered_map<string, string> result;
stringstream temp;
string str_mac;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
cerr << "Error allocating memory needed to call GetAdaptersinfo" << endl;
}
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
cerr << "Error allocating memory needed to call GetAdaptersinfo\n" << endl;
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
for (UINT i = 0; i < pAdapter->AddressLength; i++) {
temp << setfill('0') << setw(2) << hex << (int)pAdapter->Address[i];
if (i != pAdapter->AddressLength - 1)
{
temp << "-";
}
}
str_mac = temp.str();
temp.str("");
temp.rdbuf();
for (auto&c : str_mac)
{
c = toupper(c);
}
result.insert({ pAdapter->Description, str_mac });
pAdapter = pAdapter->Next;
}
}
else {
cerr << "GetAdaptersInfo failed with error: " << dwRetVal << endl;
}
if (pAdapterInfo)
FREE(pAdapterInfo);
return result;
}
//-----------------------------------------------
// Assing Random MAC address to Network Interface
//-----------------------------------------------
void MyMACAddr::AssingRndMAC()
{
//-------- Copy Network interfaces to Vector
vector <string> list;
unordered_map<string, string> AdapterDetails = getAdapters();
for (auto &itm : AdapterDetails)
{
list.push_back(itm.first);
}
cout << "\n[+]List of Available Adapters: " << endl;
int range = 0;
for (auto itm = list.begin(); itm != list.end(); itm++)
{
cout << '\t' << range + 1 << ")" << *itm << endl;
range++;
}
cout << "[*]Selection: ";
//-------- Input validation
int selection = 0;
cin >> selection;
if ( cin.fail() || (selection < 1) || (selection > range) )
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "[!]Invalid Selection Input!" << endl;
return;
}
cout << "----------------------------------------------" << endl;
cout << "[-]Selected Adapter is: " << list.at(selection - 1) << endl;
cout << "[+]Old MAC: " << AdapterDetails.at(list.at(selection - 1)) << endl;
//-------- Converting to Wide characters
wstring wstr(list.at(selection - 1).begin(), list.at(selection - 1).end());
const wchar_t *wAdapterName = wstr.c_str();
//-------- Registry Key for Network Interfaces
bool bRet = false;
HKEY hKey = NULL;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"),
0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
DWORD dwIndex = 0;
TCHAR Name[1024];
DWORD cName = 1024;
while (RegEnumKeyEx(hKey, dwIndex, Name, &cName,
NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
HKEY hSubKey = NULL;
if (RegOpenKeyEx(hKey, Name, 0, KEY_ALL_ACCESS, &hSubKey) == ERROR_SUCCESS)
{
BYTE Data[1204];
DWORD cbData = 1024;
if (RegQueryValueEx(hSubKey, _T("DriverDesc"), NULL, NULL, Data, &cbData) == ERROR_SUCCESS)
{
if (_tcscmp((TCHAR*)Data, wAdapterName) == 0)
{
string temp = GenRandMAC();
string newMAC = temp;
temp.erase(std::remove(temp.begin(), temp.end(), '-'), temp.end());
wstring wstr_newMAC(temp.begin(), temp.end());
const wchar_t *newMACAddr = wstr_newMAC.c_str();
//--------Add new MAC to Registry Subkey and disable and re-enable the interface
// to effect changes
if (RegSetValueEx(hSubKey, _T("NetworkAddress"), 0, REG_SZ,
(const BYTE*)newMACAddr, sizeof(TCHAR) * ((DWORD)_tcslen(newMACAddr) + 1)) == ERROR_SUCCESS)
{
cout << "[+]New Random MAC: " << newMAC << endl;
DisableEnableConnections(false, wAdapterName);
DisableEnableConnections(true, wAdapterName);
}
}
}
RegCloseKey(hSubKey);
}
cName = 1024;
dwIndex++;
}
RegCloseKey(hKey);
}
else
{
cerr << "[!]Cannot Access Registry - Maybe you don't have Administer permission." << endl;
return;
}
cout << "----------------------------------------------" << endl;
}
//-----------------------------------------------
// Original Code: https://social.msdn.microsoft.com/Forums/en-US/ad3ae21d-515d-4f67-8519-216f1058e390/enabledisable-network-card?forum=netfxnetcom
// Modified to only Disable and Re-enable the given Adapter
//-----------------------------------------------
HRESULT MyMACAddr::DisableEnableConnections(BOOL bEnable , const wchar_t *AdapterName)
{
HRESULT hr = E_FAIL;
CoInitialize(NULL);
INetConnectionManager *pNetConnectionManager = NULL;
hr = CoCreateInstance(CLSID_ConnectionManager,
NULL,
CLSCTX_LOCAL_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
IID_INetConnectionManager,
reinterpret_cast<LPVOID *>(&pNetConnectionManager)
);
if (SUCCEEDED(hr))
{
/*
Get an enumurator for the set of connections on the system
*/
IEnumNetConnection* pEnumNetConnection;
pNetConnectionManager->EnumConnections(NCME_DEFAULT, &pEnumNetConnection);
ULONG ulCount = 0;
BOOL fFound = FALSE;
hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
HRESULT hrT = S_OK;
/*
Enumurate through the list of adapters on the system and look for the one we want
NOTE: To include per-user RAS connections in the list, you need to set the COM
Proxy Blanket on all the interfaces. This is not needed for All-user RAS
connections or LAN connections.
*/
do
{
NETCON_PROPERTIES* pProps = NULL;
INetConnection * pConn;
/*
Find the next (or first connection)
*/
hrT = pEnumNetConnection->Next(1, &pConn, &ulCount);
if (SUCCEEDED(hrT) && 1 == ulCount)
{
/*
Get the connection properties
*/
hrT = pConn->GetProperties(&pProps);
if (S_OK == hrT)
{
//printf("* %S\n", pProps->pszwName);
if (bEnable && (_tcscmp((TCHAR*)pProps->pszwDeviceName, AdapterName) == 0) )
{
//printf(" Enabling adapter: %S...\n", pProps->pszwName);
printf("[+]Enabling adapter: %S...\n", pProps->pszwDeviceName);
hr = pConn->Connect();
}
else if (_tcscmp((TCHAR*)pProps->pszwDeviceName, AdapterName) == 0)
{
printf("[+]Disabling adapter: %S...\n", pProps->pszwDeviceName);
hr = pConn->Disconnect();
}
CoTaskMemFree(pProps->pszwName);
CoTaskMemFree(pProps->pszwDeviceName);
CoTaskMemFree(pProps);
}
pConn->Release();
pConn = NULL;
}
} while (SUCCEEDED(hrT) && 1 == ulCount && !fFound);
if (FAILED(hrT))
{
hr = hrT;
}
pEnumNetConnection->Release();
}
if (FAILED(hr) && hr != HRESULT_FROM_WIN32(ERROR_RETRY))
{
printf("Could not enable or disable connection (0x%08x)\r\n", hr);
}
pNetConnectionManager->Release();
CoUninitialize();
return hr;
}