1

完全にランダムな例外が発生しています。同じコードセットを1000回実行して(各「実行」はプログラムの完全なエンドエンドであるため、コマンドラインから独自のプロセスとして開始されてから存在します)、取得できます。 1回、または150回も失敗します。そして、私はそれを何度も何度も実行することができ、それは完全にランダムに失敗することを意味します。

System.Security.AccessControl.PrivilegeNotHeldException: The process does not possess the 'SeSecurityPrivilege' privilege which is required for this operation.
at System.Security.AccessControl.Win32.GetSecurityInfo(ResourceType resourceType, String name, SafeHandle handle, AccessControlSections accessControlSections, RawSecurityDescriptor& resultSd)
at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
at System.Security.AccessControl.RegistrySecurity..ctor(SafeRegistryHandle hKey, String name, AccessControlSections includeSections) 
at Microsoft.Win32.RegistryKey.GetAccessControl(AccessControlSections includeSections)

デバッグ時に失敗させることができないので、ランダムに失敗することを決定する理由を確認しようとして問題が発生しています。メソッド内で失敗した(RegistryKey).GetAccessControl(AccessControlSections.All)ため、次に何を試すべきかについて困惑しています。

また、私は複数のキーをループしていますが、1つのキーでこのアクセス許可の例外が発生して失敗すると判断した場合、それらはすべてそのプロセスで失敗します。

私はコマンドラインから(管理者として、UACで)実行しており、プロセスを開始すると、それが存在します。同じコマンドラインからプロセスを再開すると、ランダムに失敗します。

ユーザーハイブをロードし、レジストリ権限が昇格されていることを確認しています。これは、このランダムなバグを除いて機能します。

また、この問題は、システム(psexec)アカウントと管理者アカウントの両方で、複数のマシン(リモートではなく、常にローカルで実行されている)で発生します。

4

1 に答える 1

1

システムアカウントでSeSecurityPrivilegeが有効になっているとは思いませんし、管理者でもありません。

の代わりに(RegistryKey).GetAccessControl(AccessControlSections.All)、次を試してください。(RegistryKey).GetAccessControl(AccessControlSections.Access)

それでもエラーが発生しますか?ただし、SACLを取得することはできませんAccess

編集:アクセストークンの権限を調整するためにpinvokeからいくつかのコードを取得しました。これを行うには、管理者権限が必要です。(RegistryKey).GetAccessControl(AccessControlSections.All)SeSecurityPrivilege用に変更しました。「SetPriv();」を実行すると、エラーなしで使用できるようになります。と呼ばれます。Process Hacker 2を使用し、前後のトークンをチェックすることで、SeSecuirtyPrivilegeが有効になっていることを確認できました。

    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

    [DllImport("kernel32.dll", ExactSpelling = true)]
    internal static extern IntPtr GetCurrentProcess();

    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr
    phtok);

    [DllImport("advapi32.dll", SetLastError = true)]
    internal static extern bool LookupPrivilegeValue(string host, string name,
    ref long pluid);

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    internal struct TokPriv1Luid
    {
        public int Count;
        public long Luid;
        public int Attr;
    }

    internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
    internal const int TOKEN_QUERY = 0x00000008;
    internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    internal const string SeSecurity = "SeSecurityPrivilege";

    private bool SetPriv()
   {
       try
       {
           bool retVal;
           TokPriv1Luid tp;
           IntPtr hproc = GetCurrentProcess();
           IntPtr htok = IntPtr.Zero;
           retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
           tp.Count = 1;
           tp.Luid = 0;
           tp.Attr = SE_PRIVILEGE_ENABLED;
           retVal = LookupPrivilegeValue(null, SeSecurity, ref tp.Luid);
           retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
           return retVal;
       }
       catch (Exception ex)
       {
           throw;
           return false; 
       }

   }
于 2012-07-27T02:10:11.993 に答える