简体   繁体   中英

Permission For writing to LOCAL_MACHINE

I made an app that allows windows users to spoof Mac Address . It works by adding "NetworkAdapter": "00ff00ff00ff" key/value pair to registry of the users selected nic. The problem is that every time the app tries to make changes to windows registry Windows pop's up a warning dialog, eg:

在此输入图像描述

but clicking continue will add the registry values successfully and the app functions normally. What can i do/or add changes in my code to make the dialog box disappear or can i do it in a better way? The app requires Admin Privileges here's the git repo of the app

here's the method:

public void SetMac(string macAddress)
{
    const string Name = @"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}";
    using (RegistryKey key0 = Registry.LocalMachine.OpenSubKey(Name, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl))
    {

        string[] x = key0.GetSubKeyNames();
        foreach (string name in x)
        {
                var var1 = Registry.LocalMachine.OpenSubKey(Name,RegistryKeyPermissionCheck.ReadWriteSubTree,RegistryRights.FullControl);
                var v = var1.OpenSubKey(name, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
                var z = v.GetValue("DriverDesc");
                if (comboBox1.Text == z.ToString() )
                {
                    v.SetValue("NetworkAddress",comboBox2.Text);
                    MessageBox.Show(z.ToString());
                }
                v.Close();
                var1.Close();
        }
        key0.Close();
    }
}

您需要在提升的权限下运行您的应用程序,请参阅不允许请求的注册表访问

The problem here is that the user does not have permission to open the target key for writing. As abatishchev has already suggested, you need to run the application elevated so that the user actually has Administrators group membership when the code is executed.

The reason that this looks like a CAS permission error is a design flaw in the RegistryKey.OpenSubKey method. It ought to throw an UnauthorizedAccessException when the target key cannot be opened for writing due to inadequate user permissions, but it actually throws a SecurityException instead. The problem ends up appearing to be due to insuffience CAS permissions when it is really the user, not the code, that lacks permissions to edit the key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM