如何通过代码欺骗MAC地址

use*_*649 6 c# mac-address

我正在尝试欺骗执行程序的计算机的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;
    }
Run Code Online (Sandbox Code Playgroud)

Dan*_*Dan 5

我很好奇,所以我选择了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);
}
Run Code Online (Sandbox Code Playgroud)

为了简洁起见,我省略了的实现InternalCheckIsWow64(),但是可以在此处找到。没有这个,由于32位和64位操作系统之间的结构差异,我遇到了找不到所需注册表的问题。

强制性免责声明-自行承担注册表的风险。


1.6*_*618 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。