Использование RegSetKeySecurity, чтобы избежать перенаправления реестра - PullRequest
1 голос
/ 28 июля 2011

Чтобы избежать перенаправления реестра на ключи Wow64, как перевести следующий код, который использует Microsoft.Win32 API

public void SetKeyAccessControl(
            RegistryKey rootKey, string subKeyName, string identity, 
            RegistryRights rights, InheritanceFlags inheritanceFlags,
            PropagationFlags propagationFlags, AccessControlType accessType)
{
   using (RegistryKey regKey = rootKey.OpenSubKey(subKeyName, true))
   {
       RegistrySecurity acl = new RegistrySecurity();
       RegistryAccessRule rule = new RegistryAccessRule(identity, rights, inheritanceFlags, propagationFlags, accessType);
       acl.AddAccessRule(rule);

       regKey.SetAccessControl(acl);
   }            
}

, в использование advapi32 RegSetKeySecurity API

[DllImport(@"advapi32.dll", EntryPoint = "RegSetKeySecurity", SetLastError = true)]
internal static extern int RegSetKeySecurity(IntPtr handle, uint securityInformation, IntPtr pSecurityDescriptor);

Ответы [ 2 ]

3 голосов
/ 28 июля 2011

Чтобы избежать перенаправления реестра, вы можете сделать что-то вроде этого ...

SafeRegistryHandle handle = rootKey.Handle;

RegistryKey rootKey32 = RegistryKey.FromHandle(handle, RegistryView.Registry32);

RegistryKey rootKey64 = RegistryKey.FromHandle(handle, RegistryView.Registry64);

Затем вы можете использовать rootKey32 или rootKey64, чтобы открыть подраздел, и вы получите подраздел запрашиваемого представления.

По крайней мере, это работает в нескольких тестовых случаях, которые я пробовал. И, согласно документации для FromHandle ...

Параметр view для этого метода используется в последующих операциях, такие как открытие подразделов

0 голосов
/ 08 сентября 2011

Необходимо задействовать другой собственный метод и получить SDDL, следующий код устанавливает ACL для правильного раздела реестра:


[DllImport("Advapi32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool ConvertStringSecurityDescriptorToSecurityDescriptor(string stringSecurityDescriptor, int stringSDRevision, out IntPtr ppSecurityDescriptor, ref int securityDescriptorSize);

string sddl = "...";
IntPtr secDescriptor = IntPtr.Zero;
int size = 0;
ConvertStringSecurityDescriptorToSecurityDescriptor
   (
      sddl,
      1,                              // revision 1
      out secDescriptor,
      ref size
   );

// get handle with RegOpenKeyEx

RegSetKeySecurity
(
     handle,
     0x00000004,                      // DACL_SECURITY_INFORMATION
     secDescriptor
);
...