Пример #1
0
        public void Revert()
        {
            int num = 0;

            if (!this.currentThread.Equals(Thread.CurrentThread))
            {
                throw new InvalidOperationException(DiagnosticsResources.WrongThread);
            }
            if (!this.NeedToRevert)
            {
                return;
            }
            bool flag = true;

            try
            {
                if (this.stateWasChanged && (this.tlsContents.ReferenceCountValue > 1 || !this.tlsContents.IsImpersonating))
                {
                    NativeMethods.TOKEN_PRIVILEGE token_PRIVILEGE = default(NativeMethods.TOKEN_PRIVILEGE);
                    token_PRIVILEGE.PrivilegeCount       = 1U;
                    token_PRIVILEGE.Privilege.Luid       = this.luid;
                    token_PRIVILEGE.Privilege.Attributes = (this.initialState ? 2U : 0U);
                    NativeMethods.TOKEN_PRIVILEGE token_PRIVILEGE2 = default(NativeMethods.TOKEN_PRIVILEGE);
                    uint num2 = 0U;
                    if (!NativeMethods.AdjustTokenPrivileges(this.tlsContents.ThreadHandle, false, ref token_PRIVILEGE, (uint)Marshal.SizeOf(token_PRIVILEGE2), ref token_PRIVILEGE2, ref num2))
                    {
                        num  = Marshal.GetLastWin32Error();
                        flag = false;
                    }
                }
            }
            finally
            {
                if (flag)
                {
                    this.Reset();
                }
            }
            if (num == 8)
            {
                throw new OutOfMemoryException();
            }
            if (num == 5)
            {
                throw new UnauthorizedAccessException(DiagnosticsResources.UnauthorizedAccess);
            }
            if (num != 0)
            {
                throw new Win32Exception(num);
            }
        }
Пример #2
0
        private void ToggleState(bool enable)
        {
            int num = 0;

            if (!this.currentThread.Equals(Thread.CurrentThread))
            {
                throw new InvalidOperationException(DiagnosticsResources.WrongThread);
            }
            if (this.NeedToRevert)
            {
                throw new InvalidOperationException(DiagnosticsResources.RevertPrivilege);
            }
            try
            {
                this.tlsContents = (Thread.GetData(Privilege.tlsSlot) as Privilege.TlsContents);
                if (this.tlsContents == null)
                {
                    this.tlsContents = new Privilege.TlsContents();
                    Thread.SetData(Privilege.tlsSlot, this.tlsContents);
                }
                else
                {
                    this.tlsContents.IncrementReferenceCount();
                }
                NativeMethods.TOKEN_PRIVILEGE token_PRIVILEGE = default(NativeMethods.TOKEN_PRIVILEGE);
                token_PRIVILEGE.PrivilegeCount       = 1U;
                token_PRIVILEGE.Privilege.Luid       = this.luid;
                token_PRIVILEGE.Privilege.Attributes = (enable ? 2U : 0U);
                NativeMethods.TOKEN_PRIVILEGE token_PRIVILEGE2 = default(NativeMethods.TOKEN_PRIVILEGE);
                uint num2 = 0U;
                if (!NativeMethods.AdjustTokenPrivileges(this.tlsContents.ThreadHandle, false, ref token_PRIVILEGE, (uint)Marshal.SizeOf(token_PRIVILEGE2), ref token_PRIVILEGE2, ref num2))
                {
                    num = Marshal.GetLastWin32Error();
                }
                else if (1300 == Marshal.GetLastWin32Error())
                {
                    num = 1300;
                }
                else
                {
                    this.initialState    = ((token_PRIVILEGE2.Privilege.Attributes & 2U) != 0U);
                    this.stateWasChanged = (this.initialState != enable);
                    this.needToRevert    = (this.tlsContents.IsImpersonating || this.stateWasChanged);
                }
            }
            finally
            {
                if (!this.needToRevert)
                {
                    this.Reset();
                }
            }
            if (num == 1300)
            {
                throw new PrivilegeNotHeldException(Privilege.privileges[this.luid] as string);
            }
            if (num == 8)
            {
                throw new OutOfMemoryException();
            }
            if (num == 5 || num == 1347)
            {
                throw new UnauthorizedAccessException(DiagnosticsResources.UnauthorizedAccess);
            }
            if (num != 0)
            {
                throw new Win32Exception(num);
            }
        }
Пример #3
0
 internal static extern bool AdjustTokenPrivileges([In] SafeTokenHandle TokenHandle, [In] bool DisableAllPrivileges, [In] ref NativeMethods.TOKEN_PRIVILEGE NewState, [In] uint BufferLength, [In][Out] ref NativeMethods.TOKEN_PRIVILEGE PreviousState, [In][Out] ref uint ReturnLength);