How to trick a MAC address through code

I am trying to trick the MAC address of a computer that is running my program. Right now, I get the current MAC address of the machine using the getmac command via cmd, then I want to change it using the RegistryKey class (windows.system32).

The problem is that I don’t know which line to pass to the method OpenSubKey.

For example, this is a method for reading the current MAC address with a registry key:

 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;
    }
+4
source share
3 answers

This should point in the right direction, but you need to figure out the code:

  • HKEY_LOCAL_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 .
+1

, MadMACs. , #, , - .

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- .

- .

+3

Windows 10

HKEY_LOCAL_MACHINE \ SYSTEM \ ControlSet001 \ Control \ {Class 4d36e972-E325-11CE-BFC1-08002BE10318} \ 0004 \ NetworkAddress

0
source

Source: https://habr.com/ru/post/1531043/


All Articles