Spoofing MAC ID programmatically
I am trying to change System MAC ID programatically and am using the following code but it does not work as RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid)
is null.
how can i fix this issue. I am running Win7 64 bit OS if that matters.
also public const string baseReg = @"SYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1-08002bE10318}";
and nicid = the current mac ID of the first network adapter
public static bool SetMAC(string nicid, string newmac)
{
bool ret = false;
using (RegistryKey bkey = GetBaseKey())
using (RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid))
{
if (key != null)
{
key.SetValue("NetworkAddress", newmac, RegistryValueKind.String);
ManagementObjectSearcher mos = new ManagementObjectSearcher(
new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
o.InvokeMethod("Disable", null);
o.InvokeMethod("Enable", null);
ret = true;
}
}
}
c# mac-address
add a comment |
I am trying to change System MAC ID programatically and am using the following code but it does not work as RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid)
is null.
how can i fix this issue. I am running Win7 64 bit OS if that matters.
also public const string baseReg = @"SYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1-08002bE10318}";
and nicid = the current mac ID of the first network adapter
public static bool SetMAC(string nicid, string newmac)
{
bool ret = false;
using (RegistryKey bkey = GetBaseKey())
using (RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid))
{
if (key != null)
{
key.SetValue("NetworkAddress", newmac, RegistryValueKind.String);
ManagementObjectSearcher mos = new ManagementObjectSearcher(
new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
o.InvokeMethod("Disable", null);
o.InvokeMethod("Enable", null);
ret = true;
}
}
}
c# mac-address
possible duplicate: stackoverflow.com/questions/22310464/…
– JohnB
Nov 12 '18 at 7:09
I already checked that and the Mentioned solution {with code} does not work.
– nikkku
Nov 12 '18 at 7:10
Well, you are using the code of this answer but you are missing in your question theGetBaseKey()
function. Have youregedit
and checked if the registry key does exists and the user executing the code has permission?
– bradbury9
Nov 12 '18 at 8:39
I have all the functions from that code and little extra to fetch the MAC ID of the first adapter. yes i manually checked and the key Exist and i tried running in Administrator mode as well. I am now trying out this answer => stackoverflow.com/a/22311067/10280431 but stuck after the 2nd step as i dont understand were or how the new MAC value is assigned and the Person who Responded with that answer doesn't explain either.
– nikkku
Nov 12 '18 at 8:45
add a comment |
I am trying to change System MAC ID programatically and am using the following code but it does not work as RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid)
is null.
how can i fix this issue. I am running Win7 64 bit OS if that matters.
also public const string baseReg = @"SYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1-08002bE10318}";
and nicid = the current mac ID of the first network adapter
public static bool SetMAC(string nicid, string newmac)
{
bool ret = false;
using (RegistryKey bkey = GetBaseKey())
using (RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid))
{
if (key != null)
{
key.SetValue("NetworkAddress", newmac, RegistryValueKind.String);
ManagementObjectSearcher mos = new ManagementObjectSearcher(
new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
o.InvokeMethod("Disable", null);
o.InvokeMethod("Enable", null);
ret = true;
}
}
}
c# mac-address
I am trying to change System MAC ID programatically and am using the following code but it does not work as RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid)
is null.
how can i fix this issue. I am running Win7 64 bit OS if that matters.
also public const string baseReg = @"SYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1-08002bE10318}";
and nicid = the current mac ID of the first network adapter
public static bool SetMAC(string nicid, string newmac)
{
bool ret = false;
using (RegistryKey bkey = GetBaseKey())
using (RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid))
{
if (key != null)
{
key.SetValue("NetworkAddress", newmac, RegistryValueKind.String);
ManagementObjectSearcher mos = new ManagementObjectSearcher(
new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
o.InvokeMethod("Disable", null);
o.InvokeMethod("Enable", null);
ret = true;
}
}
}
c# mac-address
c# mac-address
edited Nov 12 '18 at 7:11
JohnB
1,7641117
1,7641117
asked Nov 12 '18 at 7:05
nikkku
369
369
possible duplicate: stackoverflow.com/questions/22310464/…
– JohnB
Nov 12 '18 at 7:09
I already checked that and the Mentioned solution {with code} does not work.
– nikkku
Nov 12 '18 at 7:10
Well, you are using the code of this answer but you are missing in your question theGetBaseKey()
function. Have youregedit
and checked if the registry key does exists and the user executing the code has permission?
– bradbury9
Nov 12 '18 at 8:39
I have all the functions from that code and little extra to fetch the MAC ID of the first adapter. yes i manually checked and the key Exist and i tried running in Administrator mode as well. I am now trying out this answer => stackoverflow.com/a/22311067/10280431 but stuck after the 2nd step as i dont understand were or how the new MAC value is assigned and the Person who Responded with that answer doesn't explain either.
– nikkku
Nov 12 '18 at 8:45
add a comment |
possible duplicate: stackoverflow.com/questions/22310464/…
– JohnB
Nov 12 '18 at 7:09
I already checked that and the Mentioned solution {with code} does not work.
– nikkku
Nov 12 '18 at 7:10
Well, you are using the code of this answer but you are missing in your question theGetBaseKey()
function. Have youregedit
and checked if the registry key does exists and the user executing the code has permission?
– bradbury9
Nov 12 '18 at 8:39
I have all the functions from that code and little extra to fetch the MAC ID of the first adapter. yes i manually checked and the key Exist and i tried running in Administrator mode as well. I am now trying out this answer => stackoverflow.com/a/22311067/10280431 but stuck after the 2nd step as i dont understand were or how the new MAC value is assigned and the Person who Responded with that answer doesn't explain either.
– nikkku
Nov 12 '18 at 8:45
possible duplicate: stackoverflow.com/questions/22310464/…
– JohnB
Nov 12 '18 at 7:09
possible duplicate: stackoverflow.com/questions/22310464/…
– JohnB
Nov 12 '18 at 7:09
I already checked that and the Mentioned solution {with code} does not work.
– nikkku
Nov 12 '18 at 7:10
I already checked that and the Mentioned solution {with code} does not work.
– nikkku
Nov 12 '18 at 7:10
Well, you are using the code of this answer but you are missing in your question the
GetBaseKey()
function. Have you regedit
and checked if the registry key does exists and the user executing the code has permission?– bradbury9
Nov 12 '18 at 8:39
Well, you are using the code of this answer but you are missing in your question the
GetBaseKey()
function. Have you regedit
and checked if the registry key does exists and the user executing the code has permission?– bradbury9
Nov 12 '18 at 8:39
I have all the functions from that code and little extra to fetch the MAC ID of the first adapter. yes i manually checked and the key Exist and i tried running in Administrator mode as well. I am now trying out this answer => stackoverflow.com/a/22311067/10280431 but stuck after the 2nd step as i dont understand were or how the new MAC value is assigned and the Person who Responded with that answer doesn't explain either.
– nikkku
Nov 12 '18 at 8:45
I have all the functions from that code and little extra to fetch the MAC ID of the first adapter. yes i manually checked and the key Exist and i tried running in Administrator mode as well. I am now trying out this answer => stackoverflow.com/a/22311067/10280431 but stuck after the 2nd step as i dont understand were or how the new MAC value is assigned and the Person who Responded with that answer doesn't explain either.
– nikkku
Nov 12 '18 at 8:45
add a comment |
1 Answer
1
active
oldest
votes
So i Solved this Issue after hours of digging and browsing.
on the side note, there is a problem with this one, it only uses the first adapter from the ActiveAdapterMAC list.
PSS - This Code isn't the best one and i have few fixes to do. but i am sure this can get you to the right direction.
here is the Code to get the current active network adapter NICID then use that to set the MAC ID
public static void GetAllNetworkAdapters()
{
NetworkInterface adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters.Where(a => a.OperationalStatus == OperationalStatus.Up))
{
ActiveAdapters.Add(adapter.Id);
string MAC = adapter.GetPhysicalAddress().ToString();
ActiveAdapterMAC.Add(MAC);
Logger.LogGenericText($"Adapter Properties => {adapter.Id} / {MAC}");
}
}
public static string GetSubKeyValue()
{
RegistryKey Base = GetBaseKey();
RegistryKey Key = Base.OpenSubKey(@"SYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1-08002BE10318}", true);
string SelectedKey = null;
string Values = Key.GetSubKeyNames();
try
{
foreach (var key in Values)
{
RegistryKey ValueChecker = Base.OpenSubKey(Constants.baseReg + key, true);
string NetCFID = ValueChecker.GetValue("NetCfgInstanceId").ToString();
if (NetCFID == ActiveAdapters[0])
{
SelectedKey = key;
}
ValueChecker.Close();
}
}
catch(Exception ex)
{
if(ex is SecurityException)
{
Key.Close();
return SelectedKey;
}
}
Key.Close();
return SelectedKey;
}
public static void ConfigMAC(bool Reset = false, bool RegisterDefault = false)
{
RegistryKey Base = GetBaseKey();
string NICID = GetSubKeyValue();
string NewMAC = GenerateMACAddress();
CachedNICID = NICID;
if (Config.WifiConnection)
{
NewMAC = GetRandomWifiMacAddress();
}
if (RegisterDefault)
{
string CurrentMAC = GetMacIDWithCache();
Constants.CurrentMACID = CurrentMAC;
try
{
RegistryKey RegisterKey = Base.OpenSubKey(Constants.baseReg + NICID, true);
if (RegisterKey.GetValue(Constants.DefaultMAC) != null)
{
RegisterKey.Close();
return;
}
else
{
RegisterKey.SetValue(Constants.DefaultMAC, CurrentMAC);
RegisterKey.Close();
return;
}
}
catch (Exception ex)
{
Logger.LogGenericText(ex.ToString());
return;
}
}
if (Reset)
{
DeleteKey(NICID, true);
return;
}
Logger.LogGenericText("Opening Sub Key => " + Constants.baseReg + NICID);
RegistryKey Key = Base.OpenSubKey(Constants.baseReg + NICID, true);
string KeyValues = Key.GetValueNames();
if (!KeyValues.Contains(Constants.MacValue))
{
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
else
{
DeleteKey(NICID, false);
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
Key.Close();
}
public static void DeleteKey(string Nicid, bool Restart)
{
Logger.LogGenericText("Deleting Previous Changed MAC...");
RegistryKey Base = GetBaseKey();
RegistryKey ResetKey = Base.OpenSubKey(Constants.baseReg + Nicid, true);
ResetKey.DeleteValue(Constants.MacValue);
ResetKey.Close();
if (Restart)
{
Task.Run(() => ResetAdapter(Nicid));
return;
}
else
{
return;
}
}
public static void ResetAdapter(string Nicid)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher
(new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + Nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
Logger.LogGenericText("Disabling Adapter and Waiting for 6 Seconds.");
o.InvokeMethod("Disable", null);
Task.Delay(6000).Wait();
o.InvokeMethod("Enable", null);
Logger.LogGenericText("New MAC ID Set Sucessfully!");
}
}
How does it work?
I used this logic
This software just writes a value into the windows registry. When the Network Adapter Device is enabled, windows searches for the registry value 'NetworkAddress' in the key HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1- 08002bE10318}[ID of NIC e.g. 0001]. If a value is present, windows will use it as MAC address, if not, windows will use the hard coded manufacturer provided MAC address. Some Network Adapter drivers have this facility built-in. It can be found in the Advance settings tab in the Network Adapter's Device properties in Windows Device Manager.
From => TMAC website.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257309%2fspoofing-mac-id-programmatically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So i Solved this Issue after hours of digging and browsing.
on the side note, there is a problem with this one, it only uses the first adapter from the ActiveAdapterMAC list.
PSS - This Code isn't the best one and i have few fixes to do. but i am sure this can get you to the right direction.
here is the Code to get the current active network adapter NICID then use that to set the MAC ID
public static void GetAllNetworkAdapters()
{
NetworkInterface adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters.Where(a => a.OperationalStatus == OperationalStatus.Up))
{
ActiveAdapters.Add(adapter.Id);
string MAC = adapter.GetPhysicalAddress().ToString();
ActiveAdapterMAC.Add(MAC);
Logger.LogGenericText($"Adapter Properties => {adapter.Id} / {MAC}");
}
}
public static string GetSubKeyValue()
{
RegistryKey Base = GetBaseKey();
RegistryKey Key = Base.OpenSubKey(@"SYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1-08002BE10318}", true);
string SelectedKey = null;
string Values = Key.GetSubKeyNames();
try
{
foreach (var key in Values)
{
RegistryKey ValueChecker = Base.OpenSubKey(Constants.baseReg + key, true);
string NetCFID = ValueChecker.GetValue("NetCfgInstanceId").ToString();
if (NetCFID == ActiveAdapters[0])
{
SelectedKey = key;
}
ValueChecker.Close();
}
}
catch(Exception ex)
{
if(ex is SecurityException)
{
Key.Close();
return SelectedKey;
}
}
Key.Close();
return SelectedKey;
}
public static void ConfigMAC(bool Reset = false, bool RegisterDefault = false)
{
RegistryKey Base = GetBaseKey();
string NICID = GetSubKeyValue();
string NewMAC = GenerateMACAddress();
CachedNICID = NICID;
if (Config.WifiConnection)
{
NewMAC = GetRandomWifiMacAddress();
}
if (RegisterDefault)
{
string CurrentMAC = GetMacIDWithCache();
Constants.CurrentMACID = CurrentMAC;
try
{
RegistryKey RegisterKey = Base.OpenSubKey(Constants.baseReg + NICID, true);
if (RegisterKey.GetValue(Constants.DefaultMAC) != null)
{
RegisterKey.Close();
return;
}
else
{
RegisterKey.SetValue(Constants.DefaultMAC, CurrentMAC);
RegisterKey.Close();
return;
}
}
catch (Exception ex)
{
Logger.LogGenericText(ex.ToString());
return;
}
}
if (Reset)
{
DeleteKey(NICID, true);
return;
}
Logger.LogGenericText("Opening Sub Key => " + Constants.baseReg + NICID);
RegistryKey Key = Base.OpenSubKey(Constants.baseReg + NICID, true);
string KeyValues = Key.GetValueNames();
if (!KeyValues.Contains(Constants.MacValue))
{
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
else
{
DeleteKey(NICID, false);
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
Key.Close();
}
public static void DeleteKey(string Nicid, bool Restart)
{
Logger.LogGenericText("Deleting Previous Changed MAC...");
RegistryKey Base = GetBaseKey();
RegistryKey ResetKey = Base.OpenSubKey(Constants.baseReg + Nicid, true);
ResetKey.DeleteValue(Constants.MacValue);
ResetKey.Close();
if (Restart)
{
Task.Run(() => ResetAdapter(Nicid));
return;
}
else
{
return;
}
}
public static void ResetAdapter(string Nicid)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher
(new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + Nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
Logger.LogGenericText("Disabling Adapter and Waiting for 6 Seconds.");
o.InvokeMethod("Disable", null);
Task.Delay(6000).Wait();
o.InvokeMethod("Enable", null);
Logger.LogGenericText("New MAC ID Set Sucessfully!");
}
}
How does it work?
I used this logic
This software just writes a value into the windows registry. When the Network Adapter Device is enabled, windows searches for the registry value 'NetworkAddress' in the key HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1- 08002bE10318}[ID of NIC e.g. 0001]. If a value is present, windows will use it as MAC address, if not, windows will use the hard coded manufacturer provided MAC address. Some Network Adapter drivers have this facility built-in. It can be found in the Advance settings tab in the Network Adapter's Device properties in Windows Device Manager.
From => TMAC website.
add a comment |
So i Solved this Issue after hours of digging and browsing.
on the side note, there is a problem with this one, it only uses the first adapter from the ActiveAdapterMAC list.
PSS - This Code isn't the best one and i have few fixes to do. but i am sure this can get you to the right direction.
here is the Code to get the current active network adapter NICID then use that to set the MAC ID
public static void GetAllNetworkAdapters()
{
NetworkInterface adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters.Where(a => a.OperationalStatus == OperationalStatus.Up))
{
ActiveAdapters.Add(adapter.Id);
string MAC = adapter.GetPhysicalAddress().ToString();
ActiveAdapterMAC.Add(MAC);
Logger.LogGenericText($"Adapter Properties => {adapter.Id} / {MAC}");
}
}
public static string GetSubKeyValue()
{
RegistryKey Base = GetBaseKey();
RegistryKey Key = Base.OpenSubKey(@"SYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1-08002BE10318}", true);
string SelectedKey = null;
string Values = Key.GetSubKeyNames();
try
{
foreach (var key in Values)
{
RegistryKey ValueChecker = Base.OpenSubKey(Constants.baseReg + key, true);
string NetCFID = ValueChecker.GetValue("NetCfgInstanceId").ToString();
if (NetCFID == ActiveAdapters[0])
{
SelectedKey = key;
}
ValueChecker.Close();
}
}
catch(Exception ex)
{
if(ex is SecurityException)
{
Key.Close();
return SelectedKey;
}
}
Key.Close();
return SelectedKey;
}
public static void ConfigMAC(bool Reset = false, bool RegisterDefault = false)
{
RegistryKey Base = GetBaseKey();
string NICID = GetSubKeyValue();
string NewMAC = GenerateMACAddress();
CachedNICID = NICID;
if (Config.WifiConnection)
{
NewMAC = GetRandomWifiMacAddress();
}
if (RegisterDefault)
{
string CurrentMAC = GetMacIDWithCache();
Constants.CurrentMACID = CurrentMAC;
try
{
RegistryKey RegisterKey = Base.OpenSubKey(Constants.baseReg + NICID, true);
if (RegisterKey.GetValue(Constants.DefaultMAC) != null)
{
RegisterKey.Close();
return;
}
else
{
RegisterKey.SetValue(Constants.DefaultMAC, CurrentMAC);
RegisterKey.Close();
return;
}
}
catch (Exception ex)
{
Logger.LogGenericText(ex.ToString());
return;
}
}
if (Reset)
{
DeleteKey(NICID, true);
return;
}
Logger.LogGenericText("Opening Sub Key => " + Constants.baseReg + NICID);
RegistryKey Key = Base.OpenSubKey(Constants.baseReg + NICID, true);
string KeyValues = Key.GetValueNames();
if (!KeyValues.Contains(Constants.MacValue))
{
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
else
{
DeleteKey(NICID, false);
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
Key.Close();
}
public static void DeleteKey(string Nicid, bool Restart)
{
Logger.LogGenericText("Deleting Previous Changed MAC...");
RegistryKey Base = GetBaseKey();
RegistryKey ResetKey = Base.OpenSubKey(Constants.baseReg + Nicid, true);
ResetKey.DeleteValue(Constants.MacValue);
ResetKey.Close();
if (Restart)
{
Task.Run(() => ResetAdapter(Nicid));
return;
}
else
{
return;
}
}
public static void ResetAdapter(string Nicid)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher
(new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + Nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
Logger.LogGenericText("Disabling Adapter and Waiting for 6 Seconds.");
o.InvokeMethod("Disable", null);
Task.Delay(6000).Wait();
o.InvokeMethod("Enable", null);
Logger.LogGenericText("New MAC ID Set Sucessfully!");
}
}
How does it work?
I used this logic
This software just writes a value into the windows registry. When the Network Adapter Device is enabled, windows searches for the registry value 'NetworkAddress' in the key HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1- 08002bE10318}[ID of NIC e.g. 0001]. If a value is present, windows will use it as MAC address, if not, windows will use the hard coded manufacturer provided MAC address. Some Network Adapter drivers have this facility built-in. It can be found in the Advance settings tab in the Network Adapter's Device properties in Windows Device Manager.
From => TMAC website.
add a comment |
So i Solved this Issue after hours of digging and browsing.
on the side note, there is a problem with this one, it only uses the first adapter from the ActiveAdapterMAC list.
PSS - This Code isn't the best one and i have few fixes to do. but i am sure this can get you to the right direction.
here is the Code to get the current active network adapter NICID then use that to set the MAC ID
public static void GetAllNetworkAdapters()
{
NetworkInterface adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters.Where(a => a.OperationalStatus == OperationalStatus.Up))
{
ActiveAdapters.Add(adapter.Id);
string MAC = adapter.GetPhysicalAddress().ToString();
ActiveAdapterMAC.Add(MAC);
Logger.LogGenericText($"Adapter Properties => {adapter.Id} / {MAC}");
}
}
public static string GetSubKeyValue()
{
RegistryKey Base = GetBaseKey();
RegistryKey Key = Base.OpenSubKey(@"SYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1-08002BE10318}", true);
string SelectedKey = null;
string Values = Key.GetSubKeyNames();
try
{
foreach (var key in Values)
{
RegistryKey ValueChecker = Base.OpenSubKey(Constants.baseReg + key, true);
string NetCFID = ValueChecker.GetValue("NetCfgInstanceId").ToString();
if (NetCFID == ActiveAdapters[0])
{
SelectedKey = key;
}
ValueChecker.Close();
}
}
catch(Exception ex)
{
if(ex is SecurityException)
{
Key.Close();
return SelectedKey;
}
}
Key.Close();
return SelectedKey;
}
public static void ConfigMAC(bool Reset = false, bool RegisterDefault = false)
{
RegistryKey Base = GetBaseKey();
string NICID = GetSubKeyValue();
string NewMAC = GenerateMACAddress();
CachedNICID = NICID;
if (Config.WifiConnection)
{
NewMAC = GetRandomWifiMacAddress();
}
if (RegisterDefault)
{
string CurrentMAC = GetMacIDWithCache();
Constants.CurrentMACID = CurrentMAC;
try
{
RegistryKey RegisterKey = Base.OpenSubKey(Constants.baseReg + NICID, true);
if (RegisterKey.GetValue(Constants.DefaultMAC) != null)
{
RegisterKey.Close();
return;
}
else
{
RegisterKey.SetValue(Constants.DefaultMAC, CurrentMAC);
RegisterKey.Close();
return;
}
}
catch (Exception ex)
{
Logger.LogGenericText(ex.ToString());
return;
}
}
if (Reset)
{
DeleteKey(NICID, true);
return;
}
Logger.LogGenericText("Opening Sub Key => " + Constants.baseReg + NICID);
RegistryKey Key = Base.OpenSubKey(Constants.baseReg + NICID, true);
string KeyValues = Key.GetValueNames();
if (!KeyValues.Contains(Constants.MacValue))
{
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
else
{
DeleteKey(NICID, false);
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
Key.Close();
}
public static void DeleteKey(string Nicid, bool Restart)
{
Logger.LogGenericText("Deleting Previous Changed MAC...");
RegistryKey Base = GetBaseKey();
RegistryKey ResetKey = Base.OpenSubKey(Constants.baseReg + Nicid, true);
ResetKey.DeleteValue(Constants.MacValue);
ResetKey.Close();
if (Restart)
{
Task.Run(() => ResetAdapter(Nicid));
return;
}
else
{
return;
}
}
public static void ResetAdapter(string Nicid)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher
(new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + Nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
Logger.LogGenericText("Disabling Adapter and Waiting for 6 Seconds.");
o.InvokeMethod("Disable", null);
Task.Delay(6000).Wait();
o.InvokeMethod("Enable", null);
Logger.LogGenericText("New MAC ID Set Sucessfully!");
}
}
How does it work?
I used this logic
This software just writes a value into the windows registry. When the Network Adapter Device is enabled, windows searches for the registry value 'NetworkAddress' in the key HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1- 08002bE10318}[ID of NIC e.g. 0001]. If a value is present, windows will use it as MAC address, if not, windows will use the hard coded manufacturer provided MAC address. Some Network Adapter drivers have this facility built-in. It can be found in the Advance settings tab in the Network Adapter's Device properties in Windows Device Manager.
From => TMAC website.
So i Solved this Issue after hours of digging and browsing.
on the side note, there is a problem with this one, it only uses the first adapter from the ActiveAdapterMAC list.
PSS - This Code isn't the best one and i have few fixes to do. but i am sure this can get you to the right direction.
here is the Code to get the current active network adapter NICID then use that to set the MAC ID
public static void GetAllNetworkAdapters()
{
NetworkInterface adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters.Where(a => a.OperationalStatus == OperationalStatus.Up))
{
ActiveAdapters.Add(adapter.Id);
string MAC = adapter.GetPhysicalAddress().ToString();
ActiveAdapterMAC.Add(MAC);
Logger.LogGenericText($"Adapter Properties => {adapter.Id} / {MAC}");
}
}
public static string GetSubKeyValue()
{
RegistryKey Base = GetBaseKey();
RegistryKey Key = Base.OpenSubKey(@"SYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1-08002BE10318}", true);
string SelectedKey = null;
string Values = Key.GetSubKeyNames();
try
{
foreach (var key in Values)
{
RegistryKey ValueChecker = Base.OpenSubKey(Constants.baseReg + key, true);
string NetCFID = ValueChecker.GetValue("NetCfgInstanceId").ToString();
if (NetCFID == ActiveAdapters[0])
{
SelectedKey = key;
}
ValueChecker.Close();
}
}
catch(Exception ex)
{
if(ex is SecurityException)
{
Key.Close();
return SelectedKey;
}
}
Key.Close();
return SelectedKey;
}
public static void ConfigMAC(bool Reset = false, bool RegisterDefault = false)
{
RegistryKey Base = GetBaseKey();
string NICID = GetSubKeyValue();
string NewMAC = GenerateMACAddress();
CachedNICID = NICID;
if (Config.WifiConnection)
{
NewMAC = GetRandomWifiMacAddress();
}
if (RegisterDefault)
{
string CurrentMAC = GetMacIDWithCache();
Constants.CurrentMACID = CurrentMAC;
try
{
RegistryKey RegisterKey = Base.OpenSubKey(Constants.baseReg + NICID, true);
if (RegisterKey.GetValue(Constants.DefaultMAC) != null)
{
RegisterKey.Close();
return;
}
else
{
RegisterKey.SetValue(Constants.DefaultMAC, CurrentMAC);
RegisterKey.Close();
return;
}
}
catch (Exception ex)
{
Logger.LogGenericText(ex.ToString());
return;
}
}
if (Reset)
{
DeleteKey(NICID, true);
return;
}
Logger.LogGenericText("Opening Sub Key => " + Constants.baseReg + NICID);
RegistryKey Key = Base.OpenSubKey(Constants.baseReg + NICID, true);
string KeyValues = Key.GetValueNames();
if (!KeyValues.Contains(Constants.MacValue))
{
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
else
{
DeleteKey(NICID, false);
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
Key.Close();
}
public static void DeleteKey(string Nicid, bool Restart)
{
Logger.LogGenericText("Deleting Previous Changed MAC...");
RegistryKey Base = GetBaseKey();
RegistryKey ResetKey = Base.OpenSubKey(Constants.baseReg + Nicid, true);
ResetKey.DeleteValue(Constants.MacValue);
ResetKey.Close();
if (Restart)
{
Task.Run(() => ResetAdapter(Nicid));
return;
}
else
{
return;
}
}
public static void ResetAdapter(string Nicid)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher
(new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + Nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
Logger.LogGenericText("Disabling Adapter and Waiting for 6 Seconds.");
o.InvokeMethod("Disable", null);
Task.Delay(6000).Wait();
o.InvokeMethod("Enable", null);
Logger.LogGenericText("New MAC ID Set Sucessfully!");
}
}
How does it work?
I used this logic
This software just writes a value into the windows registry. When the Network Adapter Device is enabled, windows searches for the registry value 'NetworkAddress' in the key HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlClass{4D36E972-E325-11CE-BFC1- 08002bE10318}[ID of NIC e.g. 0001]. If a value is present, windows will use it as MAC address, if not, windows will use the hard coded manufacturer provided MAC address. Some Network Adapter drivers have this facility built-in. It can be found in the Advance settings tab in the Network Adapter's Device properties in Windows Device Manager.
From => TMAC website.
edited Nov 14 '18 at 14:37
answered Nov 14 '18 at 14:32
nikkku
369
369
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257309%2fspoofing-mac-id-programmatically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
possible duplicate: stackoverflow.com/questions/22310464/…
– JohnB
Nov 12 '18 at 7:09
I already checked that and the Mentioned solution {with code} does not work.
– nikkku
Nov 12 '18 at 7:10
Well, you are using the code of this answer but you are missing in your question the
GetBaseKey()
function. Have youregedit
and checked if the registry key does exists and the user executing the code has permission?– bradbury9
Nov 12 '18 at 8:39
I have all the functions from that code and little extra to fetch the MAC ID of the first adapter. yes i manually checked and the key Exist and i tried running in Administrator mode as well. I am now trying out this answer => stackoverflow.com/a/22311067/10280431 but stuck after the 2nd step as i dont understand were or how the new MAC value is assigned and the Person who Responded with that answer doesn't explain either.
– nikkku
Nov 12 '18 at 8:45