Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

32-bit Windows service writing to the 64-bit registry. (AutoAdminLogon Keys)

Tags:

c#

registry

// Edit: oh wow. It's odd that I've been working on this for a day now and just realized I needed to do:

key = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);

And then everything worked. I did not know that you had to do that. Thanks for everyone that responded. I was just messing around and searched for my key and noticed it was being placed in the wrong spot.

// Original question:

I haven't seen a working solution for this and I'm not sure if it's a bug.

I have a C# 32-bit Windows service running on Windows 7 64-bit. My goal is to write to the 64-bit registry and not the Wow6432Node subkey since for AutoAdminLogon a 64-bit system doesn't seem to check the 32-bit view of the keys.

So my code is as follows:

static public void LoginAsGuest(EventLog eventLogger)
{
    RegistrySecurity userSecurity = new RegistrySecurity();
    RegistryAccessRule userRule = new RegistryAccessRule("Everyone", RegistryRights.FullControl, AccessControlType.Allow);
    userSecurity.AddAccessRule(userRule);

    var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", RegistryKeyPermissionCheck.ReadWriteSubTree);

    if (key == null)
    {
        eventLogger.WriteEntry("Error accessing the registry key");
    }
    else
    {
        try
        {
            key.SetValue("AutoAdminLogon", "1", RegistryValueKind.String);
            key.SetValue("DefaultUserName", "guest", RegistryValueKind.String);
            key.SetValue("DefaultPassword", "password", RegistryValueKind.String);
        }
        catch (Exception exception)
        {
            eventLogger.WriteEntry("Problem setting up keys: " + exception);
        }
    }
    key.Close();

    Reboot();
}

No exception or error is thrown. Nothing is written to the registry in the 32-bit or 64-bit view. I've tried using:

key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);

But it has the same outcome. Now if I just write without any view then my program successfully writes to the subkey:

SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Winlogon

Basically all I want is to write to the subkey:

SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon

Anyone know why the above code isn't writing to the requested key? (I will point out that AutoAdminLogon and the other two keys are used by the windows default credential provider such that when windows starts it checks those keys and if AutoAdminLogon is set to 1 then it logs in automatically with the given username and password. I'm doing this to allow a computer to be logged in as guest by setting the keys then rebooting the computer).

like image 919
Sirisian Avatar asked Mar 21 '12 18:03

Sirisian


1 Answers

It's odd that I've been working on this for a day now and just realized I needed to do:

key = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);

And then everything worked. I did not know that you had to do that. Thanks for everyone that responded. I was just messing around and searched for my key and noticed it was being placed in the wrong spot.

like image 130
Sirisian Avatar answered Oct 25 '22 14:10

Sirisian