C# 如何通过代码欺骗MAC地址
我试图欺骗执行我的程序的计算机的MAC地址。现在,我通过cmd使用“getmac”命令获取机器的当前MAC地址,然后我想通过“RegistryKey”classwindows.system32更改它 问题是我不知道传递给OpenSubKey方法的字符串 例如,这是使用注册表项读取来读取当前MAC的方法:C# 如何通过代码欺骗MAC地址,c#,mac-address,C#,Mac Address,我试图欺骗执行我的程序的计算机的MAC地址。现在,我通过cmd使用“getmac”命令获取机器的当前MAC地址,然后我想通过“RegistryKey”classwindows.system32更改它 问题是我不知道传递给OpenSubKey方法的字符串 例如,这是使用注册表项读取来读取当前MAC的方法: private string readMAC() { RegistryKey rkey; string MAC; rkey = Regi
private string readMAC()
{
RegistryKey rkey;
string MAC;
rkey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\0012", true); //--->this is the string to change
MAC = (string)rkey.GetValue("NetworkAddress");
rkey.Close();
return MAC;
}
这将为您指明正确的方向,但您必须找出代码: 在HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\中查看,您将在网络连接控制面板中看到与接口对应的几个子键。可能只有一个具有有效的IP,而其他的将具有0.0.0.0。您需要进行一些模式匹配,以确定哪一个是正确的。 获取接口的密钥名称它是一个GUID,或者至少看起来像一个,然后返回HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318},检查每个接口的NetCfgInstanceId值或搜索接口的GUID。
我很好奇,所以我找到了消息来源。事实证明,将核心逻辑移植到C是非常简单的,因此,如果有人感兴趣,请看这里
private const string baseReg =
@"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\";
public static bool SetMAC(string nicid, string newmac)
{
bool ret = false;
using (RegistryKey bkey = GetBaseKey())
using (RegistryKey key = bkey.OpenSubKey(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;
}
}
}
return ret;
}
public static IEnumerable<string> GetNicIds()
{
using (RegistryKey bkey = GetBaseKey())
using (RegistryKey key = bkey.OpenSubKey(baseReg))
{
if (key != null)
{
foreach (string name in key.GetSubKeyNames().Where(n => n != "Properties"))
{
using (RegistryKey sub = key.OpenSubKey(name))
{
if (sub != null)
{
object busType = sub.GetValue("BusType");
string busStr = busType != null ? busType.ToString() : string.Empty;
if (busStr != string.Empty)
{
yield return name;
}
}
}
}
}
}
}
public static RegistryKey GetBaseKey()
{
return RegistryKey.OpenBaseKey(
RegistryHive.LocalMachine,
InternalCheckIsWow64() ? RegistryView.Registry64 : RegistryView.Registry32);
}
为了简洁起见,我省略了InternalCheckIsWow64的实现,但是可以找到它。如果没有这一点,由于32位和64位操作系统之间的结构差异,我会遇到找不到所需注册表的问题
强制性免责声明-使用注册表将自担风险。Windows 10
HKEY\U LOCAL\U MACHINE\SYSTEM\ControlSet001\Control\Class{4d36e972-e325-11ce-bfc1-08002be10318}\0004\NetworkAddress您能更改MAC地址吗?我认为这是由你正在运行的硬件决定的。@RobertHarvey大多数驱动程序都允许更改MAC地址。除其他原因外,因为它们不能保证唯一性-我反复使用多个NIC的bches,地址相同,从技术上讲,它只能在一个广播域内唯一。实际上,现在所有的驱动程序都必须允许它。你认为Hyper-V网络可以附加多个MAC地址;现代高端芯片组在这方面必须非常灵活。您在示例代码中传递给OpenSubKey的密钥有什么问题?没有问题,但如果代码在其他计算机上运行,我必须更改它…非常感谢您的提示,这可能是最好的方法,我会遵循它。当然,祝您好运。顺便说一句,如果你喜欢这个答案,别忘了接受它,点击复选标记。