【问题标题】:how to spoof MAC address via code如何通过代码欺骗 MAC 地址
【发布时间】:2017-03-03 05:23:56
【问题描述】:

我试图欺骗执行我的程序的计算机的 MAC 地址。现在我正在通过 cmd 使用“getmac”命令获取机器的当前 MAC 地址,然后我想通过“RegistryKey”类(windows.system32)更改它。

问题是我不知道要传递给OpenSubKey 方法的字符串。

例如,这是通过读取注册表项来读取当前 MAC 的方法:

 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;
    }

【问题讨论】:

  • 可以更改MAC地址吗?我认为这是由您运行的硬件决定的。
  • @RobertHarvey 大多数驱动程序允许更改 MAC 地址。除其他原因外,因为它们不能保证是唯一的 - 我反复得到具有相同地址的多个 NIC 的 bches(因为从技术上讲,它只能在一个广播域中是唯一的)。
  • 实际上现在所有的司机都必须允许。您如何认为 Hyper-V 网络可以附加多个 MAC 地址 ;) 现代高端芯片组在这方面必须非常灵活。
  • 您在示例代码中传递给OpenSubKey 的密钥有什么问题?
  • 没有错,但如果代码在另一台计算机上运行,​​我必须更改它...

标签: c# mac-address


【解决方案1】:

我很好奇,所以我提取了MadMACs 的来源。事实证明,将核心逻辑移植到 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() 的实现,但可以在here 中找到它。如果没有这个,由于 32 位和 64 位操作系统之间的结构差异,我会遇到找不到我想要的注册表的问题。

强制性免责声明 -- 玩弄注册表,后果自负。

【讨论】:

    【解决方案2】:

    这应该为您指明正确的方向,但您必须弄清楚代码:

    1. 查看HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\,您会看到与“网络连接”控制面板中的接口对应的几个子键。可能只有一个具有有效 IP,而其他的将具有 0.0.0.0 您需要进行一些模式匹配以确定哪个是正确的。
    2. 获取接口的键名(它是一个 GUID,或者至少看起来像一个),然后返回 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318} 并检查每个人的 NetCfgInstanceId 值(或搜索)以获取接口的 GUID。

    【讨论】:

    • 非常感谢您的提示,可能是最好的方法,我会遵循它。
    • 当然,祝你好运。顺便说一句,如果您喜欢这个答案,请不要忘记接受它(单击复选标记)。
    【解决方案3】:

    Windows 10

    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class{4d36e972-e325-11ce-bfc1-08002be10318}\0004\NetworkAddress

    【讨论】:

      猜你喜欢
      • 2012-05-02
      • 2012-11-07
      • 2017-11-21
      • 2019-02-24
      • 2012-03-21
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多