Example #1
0
 internal extern static bool DuplicateTokenEx(
     IntPtr hExistingToken,
     TokenAccessLevels dwDesiredAccess,
     SECURITY_ATTRIBUTES lpTokenAttributes,
     TokenImpersonationLevel ImpersonationLevel,
     TOKEN_TYPE TokenType,
     out IntPtr phNewToken);
 internal static extern bool DuplicateTokenEx(
     [In] SafeCloseHandle existingToken,
     [In] TokenAccessLevels desiredAccess,
     [In] IntPtr tokenAttributes,
     [In] SecurityImpersonationLevel impersonationLevel,
     [In] TokenType tokenType,
     out SafeCloseHandle newToken);
 DuplicateTokenEx(
     [In] SafeCloseHandle ExistingToken,
     [In] TokenAccessLevels DesiredAccess,
     [In] IntPtr TokenAttributes,
     [In] SecurityImpersonationLevel ImpersonationLevel,
     [In] TokenType TokenType,
     [Out] out SafeCloseHandle NewToken);
 private static extern bool DuplicateTokenEx(
     IntPtr hExistingToken,
     TokenAccessLevels dwDesiredAccess,
     IntPtr lpTokenAttributes,
     SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
     TOKEN_TYPE TokenType,
     out IntPtr phNewToken);
Example #5
0
 internal static extern bool DuplicateTokenEx(
     SafeTokenHandle existingTokenHandle,
     TokenAccessLevels desiredAccess,
     IntPtr tokenAttributes,
     SecurityImpersonationLevel impersonationLevel,
     TokenType tokenType,
     ref SafeTokenHandle duplicateTokenHandle);
Example #6
0
 bool DuplicateTokenEx(
     [In]    IntPtr ExistingToken,
     [In]    TokenAccessLevels DesiredAccess,
     [In]    IntPtr TokenAttributes,
     [In]    SecurityImpersonationLevel ImpersonationLevel,
     [In]    TokenType TokenType,
     [In, Out] ref IntPtr NewToken);
Example #7
0
 internal static extern bool DuplicateTokenEx(
     SafeCloseHandle hExistingToken,
     TokenAccessLevels dwDesiredAccess,
     IntPtr lpTokenAttributes,
     SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
     TOKEN_TYPE TokenType,
     ref SafeCloseHandle phNewToken);
Example #8
0
            internal static SafeCloseHandle OpenThreadToken(TokenAccessLevels desiredAccess, bool openAsSelf)
            {
                SafeCloseHandle threadToken;

                if (!Private.OpenThreadToken(NativeMethods.Private.GetCurrentThread(), desiredAccess, openAsSelf, out threadToken))
                {
                    int error = Marshal.GetLastWin32Error();
                    if (error != WinError.ERROR_NO_TOKEN)
                    {
                        throw GetIoExceptionForError(error, desiredAccess.ToString());
                    }

                    SafeCloseHandle processToken = OpenProcessToken(TokenAccessLevels.Duplicate);
                    if (!Private.DuplicateTokenEx(
                            processToken,
                            TokenAccessLevels.Impersonate | TokenAccessLevels.Query | TokenAccessLevels.AdjustPrivileges,
                            IntPtr.Zero,
                            SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
                            TOKEN_TYPE.TokenImpersonation,
                            ref threadToken))
                    {
                        error = Marshal.GetLastWin32Error();
                        throw GetIoExceptionForError(error, desiredAccess.ToString());
                    }
                }

                return(threadToken);
            }
Example #9
0
        internal static int OpenThreadToken(TokenAccessLevels dwDesiredAccess, WinSecurityContext dwOpenAs, out SafeTokenHandle phThreadToken)
        {
            int  hr         = 0;
            bool openAsSelf = true;

            if (dwOpenAs == WinSecurityContext.Thread)
            {
                openAsSelf = false;
            }

            if (!Interop.Advapi32.OpenThreadToken((IntPtr)(-2), dwDesiredAccess, openAsSelf, out phThreadToken))
            {
                if (dwOpenAs == WinSecurityContext.Both)
                {
                    openAsSelf = false;
                    hr         = 0;
                    if (!Interop.Advapi32.OpenThreadToken((IntPtr)(-2), dwDesiredAccess, openAsSelf, out phThreadToken))
                    {
                        hr = Marshal.GetHRForLastWin32Error();
                    }
                }
                else
                {
                    hr = Marshal.GetHRForLastWin32Error();
                }
            }
            if (hr != 0)
            {
                phThreadToken = null;
            }
            return(hr);
        }
Example #10
0
 internal static partial bool DuplicateTokenEx(
     SafeTokenHandle ExistingTokenHandle,
     TokenAccessLevels DesiredAccess,
     IntPtr TokenAttributes,
     SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
     System.Security.Principal.TokenType TokenType,
     ref SafeTokenHandle?DuplicateTokenHandle);
Example #11
0
 public static extern bool DuplicateTokenEx(
     SafeNativeHandle hExistingToken,
     TokenAccessLevels dwDesiredAccess,
     IntPtr lpTokenAttributes,
     SecurityImpersonationLevel ImpersonationLevel,
     TokenType TokenType,
     out SafeNativeHandle phNewToken);
        internal static SafeTokenHandle GetCurrentThreadToken(TokenAccessLevels desiredAccess, out int hr)
        {
            SafeTokenHandle handle;

            hr = System.Security.Principal.Win32.OpenThreadToken(desiredAccess, WinSecurityContext.Both, out handle);
            return(handle);
        }
 public static extern bool DuplicateTokenEx(
     SafeNativeHandle hExistingToken,
     TokenAccessLevels dwDesiredAccess,
     IntPtr lpTokenAttributes,
     NativeHelpers.SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
     NativeHelpers.TOKEN_TYPE TokenType,
     out SafeNativeHandle phNewToken);
Example #14
0
        internal static bool OpenCurrentThreadTokenCritical(TokenAccessLevels desiredAccess, bool openAsSelf, out SafeCloseHandleCritical tokenHandle, out int error)
        {
            bool flag = OpenThreadTokenCritical(GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle);

            error = Marshal.GetLastWin32Error();
            return(flag);
        }
Example #15
0
 bool DuplicateTokenEx(
     SafeTokenHandle ExistingToken,
     TokenAccessLevels DesiredAccess,
     IntPtr TokenAttributes,
     TokenImpersonationLevel ImpersonationLevel,
     TokenType TokenType,
     ref SafeTokenHandle NewToken);
        internal static bool OpenThreadToken(TokenAccessLevels desiredAccess, WinSecurityContext openAs, out SafeAccessTokenHandle tokenHandle)
        {
            bool openAsSelf = true;

            if (openAs == WinSecurityContext.Thread)
            {
                openAsSelf = false;
            }

            if (OpenThreadToken(Kernel32.GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle))
            {
                return(true);
            }

            if (openAs == WinSecurityContext.Both)
            {
                openAsSelf = false;
                if (OpenThreadToken(Kernel32.GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle))
                {
                    return(true);
                }
            }

            return(false);
        }
Example #17
0
 private static extern bool DuplicateTokenEx(
     SafeProcessHandle hExistingToken,
     TokenAccessLevels dwDesiredAccess,
     SECURITY_ATTRIBUTES lpTokenAttributes,
     TokenImpersonationLevel ImpersonationLevel,
     TokenType TokenType,
     out SafeProcessHandle phNewToken);
Example #18
0
 private static SafeAccessTokenHandle GetCurrentProcessToken(TokenAccessLevels desiredAccess, out int hr)
 {
     hr = 0;
     SafeAccessTokenHandle safeTokenHandle;
     if (!Interop.Advapi32.OpenProcessToken(Interop.Kernel32.GetCurrentProcess(), desiredAccess, out safeTokenHandle))
         hr = GetHRForWin32Error(Marshal.GetLastWin32Error());
     return safeTokenHandle;
 }
Example #19
0
 // ReSharper disable once TooManyArguments
 public static extern bool DuplicateTokenEx(
     [In] SafeNativeHandle hExistingToken,
     [In] TokenAccessLevels dwDesiredAccess,
     [In] IntPtr lpTokenAttributes,
     [In] TokenImpersonationLevel impersonationLevel,
     [In] TokenType tokenType,
     [Out] out SafeNativeHandle newToken
     );
 internal static extern bool DuplicateTokenEx(
     [In] SafeCloseHandle existingTokenHandle,
     [In] TokenAccessLevels desiredAccess,
     [In] IntPtr tokenAttributes,
     [In] SECURITY_IMPERSONATION_LEVEL impersonationLevel,
     [In] TokenType tokenType,
     [Out] out SafeCloseHandle duplicateTokenHandle
     );
Example #21
0
        public NativeToken OpenToken(TokenAccessLevels accessLevels, bool ignoreImpersonation = false)
        {
            bool success = OpenThreadToken(handle, (uint)accessLevels, ignoreImpersonation, out SafeTokenHandle tokenHandle);

            if (!success)
            {
                throw new Win32Exception();
            }
            return(new NativeToken(tokenHandle));
        }
Example #22
0
        public NativeToken OpenToken(TokenAccessLevels accessLevels)
        {
            bool success = OpenProcessToken(handle, (uint)accessLevels, out SafeTokenHandle tokenHandle);

            if (!success)
            {
                throw new Win32Exception();
            }
            return(new NativeToken(tokenHandle));
        }
Example #23
0
        /// <summary>
        /// The OpenProcessToken function opens the access token associated with a process.
        /// </summary>
        /// <param name="processHandle">A handle to the process whose access token is opened. The process must have the PROCESS_QUERY_INFORMATION access permission.</param>
        /// <param name="desiredAccess">
        /// Specifies an access mask that specifies the requested types of access to the access token.
        /// These requested access types are compared with the discretionary access control list (DACL) of the token to determine which accesses are granted or denied.
        /// </param>
        /// <returns>Returns a handle that identifies the newly opened access token.</returns>
        public static IntPtr OpenProcessToken(IntPtr processHandle, TokenAccessLevels desiredAccess)
        {
            if (!OpenProcessTokenNative(processHandle, desiredAccess, out IntPtr tokenHandle))
            {
                int hResult = Marshal.GetHRForLastWin32Error();
                Marshal.ThrowExceptionForHR(hResult);
            }

            return(tokenHandle);
        }
        private static SafeTokenHandle GetCurrentProcessToken(TokenAccessLevels desiredAccess, out int hr)
        {
            SafeTokenHandle handle;

            hr = 0;
            if (!Win32Native.OpenProcessToken(Win32Native.GetCurrentProcess(), desiredAccess, out handle))
            {
                hr = GetHRForWin32Error(Marshal.GetLastWin32Error());
            }
            return(handle);
        }
Example #25
0
        public static SafeNativeHandle DuplicateToken(SafeNativeHandle hToken, TokenAccessLevels access,
                                                      SecurityImpersonationLevel impersonationLevel, TokenType tokenType)
        {
            SafeNativeHandle dupToken;

            if (!NativeMethods.DuplicateTokenEx(hToken, access, IntPtr.Zero, impersonationLevel, tokenType, out dupToken))
            {
                throw new Win32Exception("Failed to duplicate token");
            }
            return(dupToken);
        }
Example #26
0
        private static SafeAccessTokenHandle GetCurrentProcessToken(TokenAccessLevels desiredAccess, out int hr)
        {
            hr = 0;
            SafeAccessTokenHandle safeTokenHandle;

            if (!Interop.mincore.OpenProcessToken(Interop.mincore.GetCurrentProcess(), desiredAccess, out safeTokenHandle))
            {
                hr = GetHRForWin32Error(Marshal.GetLastWin32Error());
            }
            return(safeTokenHandle);
        }
        private static SafeTokenHandle OpenProcessToken(IntPtr processHandle, TokenAccessLevels desiredAccess)
        {
            SafeTokenHandle tokenHandle;

            if (!OpenProcessToken(processHandle, desiredAccess, out tokenHandle))
            {
                throw new ApplicationException("Unable to open process token for handle " + processHandle);
            }

            return(tokenHandle);
        }
Example #28
0
        public static SafeNativeHandle OpenProcessToken(SafeNativeHandle hProcess, TokenAccessLevels access)
        {
            SafeNativeHandle hToken;

            if (!NativeMethods.OpenProcessToken(hProcess, access, out hToken))
            {
                throw new Win32Exception(String.Format("Failed to open proces token with access {0}",
                                                       access.ToString()));
            }

            return(hToken);
        }
Example #29
0
            internal static SafeCloseHandle OpenProcessToken(TokenAccessLevels desiredAccess)
            {
                SafeCloseHandle processToken;

                if (!Private.OpenProcessToken(Process.GetCurrentProcess().Handle, desiredAccess, out processToken))
                {
                    int error = Marshal.GetLastWin32Error();
                    throw GetIoExceptionForError(error, desiredAccess.ToString());
                }

                return(processToken);
            }
Example #30
0
        static extern Boolean AccessCheck(
            [MarshalAs(UnmanagedType.LPArray)]
            byte[] pSecurityDescriptor,
            IntPtr ClientToken,

            [MarshalAs(UnmanagedType.U4)]
            TokenAccessLevels accessmask,
            [In] ref GENERIC_MAPPING GenericMapping,
            IntPtr PrivilegeSet,
            ref Int32 PrivilegeSetLength,
            out uint GrantedAccess,
            [MarshalAs(UnmanagedType.Bool)]
            out Boolean AccessStatus);
        internal static bool OpenThreadToken(TokenAccessLevels desiredAccess, WinSecurityContext openAs, out SafeAccessTokenHandle tokenHandle)
        {
            bool openAsSelf = true;
            if (openAs == WinSecurityContext.Thread)
                openAsSelf = false;

            if (OpenThreadToken(GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle))
                return true;

            if (openAs == WinSecurityContext.Both)
            {
                openAsSelf = false;
                if (OpenThreadToken(GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle))
                    return true;
            }

            return false;
        }
Example #32
0
        internal static int OpenThreadToken(TokenAccessLevels dwDesiredAccess, WinSecurityContext dwOpenAs, out SafeTokenHandle phThreadToken)
        {
            int hr = 0;
            bool openAsSelf = true;
            if (dwOpenAs == WinSecurityContext.Thread)
                openAsSelf = false;

            if (!Interop.Advapi32.OpenThreadToken((IntPtr)(-2), dwDesiredAccess, openAsSelf, out phThreadToken))
            {
                if (dwOpenAs == WinSecurityContext.Both)
                {
                    openAsSelf = false;
                    hr = 0;
                    if (!Interop.Advapi32.OpenThreadToken((IntPtr)(-2), dwDesiredAccess, openAsSelf, out phThreadToken))
                        hr = Marshal.GetHRForLastWin32Error();
                }
                else
                {
                    hr = Marshal.GetHRForLastWin32Error();
                }
            }
            if (hr != 0) phThreadToken = null;
            return hr;
        }
 bool DuplicateTokenEx(SafeTokenHandle ExistingTokenHandle, 
     TokenAccessLevels DesiredAccess,
     IntPtr TokenAttributes,
     SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
     System.Security.Principal.TokenType TokenType,
     ref SafeTokenHandle DuplicateTokenHandle);       
 internal static extern bool OpenThreadToken(IntPtr ThreadHandle, TokenAccessLevels dwDesiredAccess, bool bOpenAsSelf, out SafeTokenHandle phThreadToken);
Example #35
0
 public static extern bool OpenProcessToken(
     IntPtr hProcess,
     TokenAccessLevels DesiredAccess,
     ref SafeTokenHandle TokenHandle);
 internal static extern bool OpenProcessToken(IntPtr processHandle,
     TokenAccessLevels desiredAccess,
     out IntPtr tokenHandle);
Example #37
0
 internal static extern Boolean OpenProcessToken(IntPtr ProcessToken,
    TokenAccessLevels DesiredAccess, [In, Out] ref SafeTokenHandle TokenHandle);
 [SecurityCritical]  // auto-generated
 private static SafeTokenHandle GetCurrentThreadToken(TokenAccessLevels desiredAccess, out int lastError) {
     SafeTokenHandle safeTokenHandle;
     Win32Native.OpenThreadToken(Win32Native.GetCurrentThread(), desiredAccess, true, out safeTokenHandle);
     lastError = Marshal.GetLastWin32Error();
     return safeTokenHandle;
 }
 [SecurityCritical]  // auto-generated
 private static SafeTokenHandle GetCurrentToken(TokenAccessLevels desiredAccess) {
     int lastError;
     SafeTokenHandle safeTokenHandle = GetCurrentThreadToken(desiredAccess, out lastError);
     if (safeTokenHandle.IsInvalid && lastError == Win32Native.ERROR_NO_TOKEN) {
         safeTokenHandle = GetCurrentProcessToken(desiredAccess, out lastError);
         if (safeTokenHandle.IsInvalid) {
             Marshal.ThrowExceptionForHR(lastError);
         }
     }
     return safeTokenHandle;
 }
 private static extern bool OpenProcessTokenPrivate(
     IntPtr ProcessHandle,
     TokenAccessLevels DesiredAccesss,
     out SafeCloseHandle TokenHandle);
 bool DuplicateTokenEx(
     SafeTokenHandle ExistingToken,
     TokenAccessLevels DesiredAccess,
     IntPtr TokenAttributes,
     TokenImpersonationLevel ImpersonationLevel,
     TokenType TokenType,
     ref SafeTokenHandle NewToken);
 public static extern bool OpenThreadToken(
     HANDLE ThreadToken,
     TokenAccessLevels DesiredAccess,
     [MarshalAs(UnmanagedType.Bool)]
     bool OpenAsSelf,
     ref SafeTokenHandle TokenHandle);
 public static extern bool OpenProcessToken(
     HANDLE ProcessToken,
     TokenAccessLevels DesiredAccess,
     ref SafeTokenHandle TokenHandle);
Example #44
0
 internal static extern Boolean DuplicateTokenEx(SafeTokenHandle ExistingToken, 
    TokenAccessLevels DesiredAccess, IntPtr TokenAttributes, 
    SecurityImpersonationLevel ImpersonationLevel, TokenType TokenType,
    [In, Out] ref SafeTokenHandle NewToken);
Example #45
0
 internal static extern Boolean OpenThreadToken(IntPtr ThreadToken, TokenAccessLevels DesiredAccess,
    Boolean OpenAsSelf, [In, Out] ref SafeTokenHandle TokenHandle);
Example #46
0
 public static extern bool OpenProcessToken(IntPtr ProcessHandle, TokenAccessLevels DesiredAccess, out IntPtr TokenHandle);
 internal static bool OpenCurrentThreadTokenCritical(TokenAccessLevels desiredAccess, bool openAsSelf, out SafeCloseHandleCritical tokenHandle, out int error)
 {
     bool flag = OpenThreadTokenCritical(GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle);
     error = Marshal.GetLastWin32Error();
     return flag;
 }
Example #48
0
 internal static extern bool DuplicateTokenEx(SafeTokenHandle ExistingTokenHandle,
                                              TokenAccessLevels DesiredAccess, IntPtr TokenAttributes,
                                              SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
                                              TokenType TokenType, ref SafeTokenHandle DuplicateTokenHandle);
 public static WindowsIdentity GetCurrent(TokenAccessLevels desiredAccess)
 {
 }
Example #50
0
 public static extern bool OpenThreadToken(
     IntPtr ThreadToken,
     TokenAccessLevels DesiredAccess,
     bool OpenAsSelf,
     ref SafeTokenHandle TokenHandle);
 internal static extern bool OpenThreadToken(
     IntPtr ThreadHandle,
     TokenAccessLevels DesiredAccess,
     [MarshalAs(UnmanagedType.Bool)] bool OpenAsSelf,
     out SafeCloseHandle TokenHandle);
            internal static SafeCloseHandle OpenProcessToken(TokenAccessLevels desiredAccess)
            {
                SafeCloseHandle processToken;
                if (!Private.OpenProcessToken(Process.GetCurrentProcess().Handle, desiredAccess, out processToken))
                {
                    int error = Marshal.GetLastWin32Error();
                    throw GetIoExceptionForError(error, desiredAccess.ToString());
                }

                return processToken;
            }
Example #53
0
 internal static extern int OpenThreadToken (TokenAccessLevels dwDesiredAccess, WinSecurityContext OpenAs, out SafeTokenHandle phThreadToken);
            internal static SafeCloseHandle OpenThreadToken(TokenAccessLevels desiredAccess, bool openAsSelf)
            {
                SafeCloseHandle threadToken;
                if (!Private.OpenThreadToken(NativeMethods.Private.GetCurrentThread(), desiredAccess, openAsSelf, out threadToken))
                {
                    int error = Marshal.GetLastWin32Error();
                    if (error != WinError.ERROR_NO_TOKEN)
                        throw GetIoExceptionForError(error, desiredAccess.ToString());

                    SafeCloseHandle processToken = OpenProcessToken(TokenAccessLevels.Duplicate);
                    if (!Private.DuplicateTokenEx(
                        processToken,
                        TokenAccessLevels.Impersonate | TokenAccessLevels.Query | TokenAccessLevels.AdjustPrivileges,
                        IntPtr.Zero,
                        SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
                        TOKEN_TYPE.TokenImpersonation,
                        ref threadToken))
                    {
                        error = Marshal.GetLastWin32Error();
                        throw GetIoExceptionForError(error, desiredAccess.ToString());
                    }
                }

                return threadToken;
            }
Example #55
0
		public static WindowsIdentity GetCurrent (TokenAccessLevels desiredAccess)
		{
			throw new NotImplementedException ();
		}
 internal static extern bool DuplicateTokenEx(
     SafeCloseHandle hExistingToken,
     TokenAccessLevels dwDesiredAccess,
     IntPtr lpTokenAttributes,
     SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
     TOKEN_TYPE TokenType,
     ref SafeCloseHandle phNewToken);
 internal static extern bool OpenProcessToken(IntPtr ProcessToken, TokenAccessLevels DesiredAccess, out SafeAccessTokenHandle TokenHandle);
 public static System.Security.Principal.WindowsIdentity GetCurrent(TokenAccessLevels desiredAccess)
 {
   return default(System.Security.Principal.WindowsIdentity);
 }