Example #1
0
        public static string GetLoggedOnUserSID()
        {
            IntPtr tokenOwnerPtr;
            int    tokenSize;
            IntPtr hToken;

            // Get a token from the logged on session
            // !!! this line will only work within the SYSTEM session !!!
            WTSQueryUserToken(WTSGetActiveConsoleSessionId(), out hToken);

            // Get the size required to host a SID
            GetTokenInformation(hToken, TokenInformationClass.TokenOwner, IntPtr.Zero, 0, out tokenSize);
            tokenOwnerPtr = Marshal.AllocHGlobal(tokenSize);

            // Get the SID structure within the TokenOwner class
            GetTokenInformation(hToken, TokenInformationClass.TokenOwner, tokenOwnerPtr, tokenSize, out tokenSize);
            TokenOwner tokenOwner = (TokenOwner)Marshal.PtrToStructure(tokenOwnerPtr, typeof(TokenOwner));

            // Convert the SID into a string
            string strSID = "";

            ConvertSidToStringSid(tokenOwner.Owner, ref strSID);
            Marshal.FreeHGlobal(tokenOwnerPtr);
            return(strSID);
        }
Example #2
0
        public static TokenHandle Create(
            TokenAccess access,
            string name,
            ObjectFlags objectFlags,
            DirectoryHandle rootDirectory,
            TokenType tokenType,
            Luid authenticationId,
            long expirationTime,
            Sid user,
            Sid[] groups,
            PrivilegeSet privileges,
            Sid owner,
            Sid primaryGroup,
            Acl defaultDacl,
            TokenSource source
            )
        {
            NtStatus          status;
            TokenUser         tokenUser         = new TokenUser(user);
            TokenGroups       tokenGroups       = new TokenGroups(groups);
            TokenPrivileges   tokenPrivileges   = new TokenPrivileges(privileges);
            TokenOwner        tokenOwner        = new TokenOwner(owner);
            TokenPrimaryGroup tokenPrimaryGroup = new TokenPrimaryGroup(primaryGroup);
            TokenDefaultDacl  tokenDefaultDacl  = new TokenDefaultDacl(defaultDacl);
            ObjectAttributes  oa = new ObjectAttributes(name, objectFlags, rootDirectory);
            IntPtr            handle;

            try
            {
                if ((status = Win32.NtCreateToken(
                         out handle,
                         access,
                         ref oa,
                         tokenType,
                         ref authenticationId,
                         ref expirationTime,
                         ref tokenUser,
                         ref tokenGroups,
                         ref tokenPrivileges,
                         ref tokenOwner,
                         ref tokenPrimaryGroup,
                         ref tokenDefaultDacl,
                         ref source
                         )) >= NtStatus.Error)
                {
                    Win32.Throw(status);
                }
            }
            finally
            {
                oa.Dispose();
            }

            return(new TokenHandle(handle, true));
        }