3

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, e.g.:

enter image description here

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();
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
jeff
  • 75
  • 2
  • 8

2 Answers2

2

You need to run your app under elevated privileges, see Requested registry access is not allowed.

Community
  • 1
  • 1
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • I think you're right, but I have a doubt: lacking privileges app should be prevented to write those values to registry as it happens if OP clicks Continue. Am I wrong? – Marco Nov 29 '11 at 13:33
0

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.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
  • I'm running the app with admin privileges .... i edited the question with the part of the code that writes to the registry .. – jeff Nov 29 '11 at 16:07
  • Have you checked that the Administrators group has adequate permissions to write to all the target subkeys? It's possible that only a subset of the subkeys have more restricted permissions, which would also explain why you're seeing changes when you click Continue. – Nicole Calinoiu Nov 29 '11 at 16:19