以编程方式欺骗 MAC ID
Spoofing MAC ID programmatically
我正在尝试以编程方式更改系统 MAC ID 并使用以下代码,但它不起作用,因为 RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid)
为空。
我该如何解决这个问题。我是 运行 Win7 64 位 OS 如果重要的话。
还有public const string baseReg = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\";
和 nicid = 第一个网络适配器的当前 mac ID
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;
}
}
}
经过数小时的挖掘和浏览,我解决了这个问题。
附带说明一下,这个有一个问题,它只使用 ActiveAdapterMAC 列表中的第一个适配器。
PSS - 此代码不是最好的代码,我几乎没有什么可以修复的。但我相信这可以让你朝着正确的方向前进。
这是获取当前活动网络适配器 NICID 然后使用它来设置 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(@"SYSTEM\CurrentControlSet\Control\Class\{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!");
}
}
它是如何工作的?
我用了这个逻辑
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_MACHINE\SYSTEM\CurrentControlSet\Control\Class{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.
来自 => TMAC 网站。
我正在尝试以编程方式更改系统 MAC ID 并使用以下代码,但它不起作用,因为 RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid)
为空。
我该如何解决这个问题。我是 运行 Win7 64 位 OS 如果重要的话。
还有public const string baseReg = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\";
和 nicid = 第一个网络适配器的当前 mac ID
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;
}
}
}
经过数小时的挖掘和浏览,我解决了这个问题。 附带说明一下,这个有一个问题,它只使用 ActiveAdapterMAC 列表中的第一个适配器。
PSS - 此代码不是最好的代码,我几乎没有什么可以修复的。但我相信这可以让你朝着正确的方向前进。 这是获取当前活动网络适配器 NICID 然后使用它来设置 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(@"SYSTEM\CurrentControlSet\Control\Class\{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!");
}
}
它是如何工作的? 我用了这个逻辑
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_MACHINE\SYSTEM\CurrentControlSet\Control\Class{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.
来自 => TMAC 网站。