Beispiel #1
0
        private static List <IntPtr> GetTokensForProcess(string processName)
        {
            var result = new List <IntPtr>();

            var process = Process.GetProcessesByName(processName);

            if (process == null || process.Count() == 0)
            {
                Console.WriteLine($"No instance of {processName} found");
                return(result);
            }

            var grouped = process.GroupBy((p) => p.SessionId);

            foreach (var group in grouped)
            {
                var sessionId = group.Key;
                var handle    = group.First().Handle;
                var token     = IntPtr.Zero;

                if (NativeMethod.OpenProcessToken(handle, TokenAdjuster.TOKEN_READ | TokenAdjuster.TOKEN_QUERY | TokenAdjuster.TOKEN_DUPLICATE | TokenAdjuster.TOKEN_ASSIGN_PRIMARY, ref token) == 0)
                {
                    Console.WriteLine($"Failed to open token from {sessionId} ({Marshal.GetLastWin32Error()}). Skipping ...");
                    continue;
                }

                result.Add(token);
            }

            return(result);
        }
Beispiel #2
0
        internal static bool EnablePrivilege(string privilegeName, bool bEnablePrivilege)
        {
            Console.WriteLine($"Trying to enable {privilegeName}");

            int returnLength    = 0;
            var token           = IntPtr.Zero;
            var tokenPrivileges = new TOKEN_PRIVILEGES
            {
                Privileges = new int[3]
            };
            var oldTokenPrivileges = new TOKEN_PRIVILEGES
            {
                Privileges = new int[3]
            };
            var tLUID = new LUID();

            tokenPrivileges.PrivilegeCount = 1;
            if (bEnablePrivilege)
            {
                tokenPrivileges.Privileges[2] = SE_PRIVILEGE_ENABLED;
            }
            else
            {
                tokenPrivileges.Privileges[2] = 0;
            }

            var unmanagedTokenPrivileges = IntPtr.Zero;

            try
            {
                if (!NativeMethod.LookupPrivilegeValue(null, privilegeName, ref tLUID))
                {
                    Console.WriteLine($"Failed to Lookup {privilegeName}");
                    return(false);
                }

                var process = Process.GetCurrentProcess();
                if (process.Handle == IntPtr.Zero)
                {
                    Console.WriteLine($"Failed to get process handle");
                    return(false);
                }


                if (NativeMethod.OpenProcessToken(process.Handle, TOKEN_ALL_ACCESS, ref token) == 0)
                {
                    Console.WriteLine($"Failed to open process token ({Marshal.GetLastWin32Error()})");
                    return(false);
                }

                tokenPrivileges.PrivilegeCount = 1;
                tokenPrivileges.Privileges[2]  = SE_PRIVILEGE_ENABLED;
                tokenPrivileges.Privileges[1]  = tLUID.HighPart;
                tokenPrivileges.Privileges[0]  = tLUID.LowPart;
                const int bufLength = 256;
                unmanagedTokenPrivileges = Marshal.AllocHGlobal(bufLength);
                Marshal.StructureToPtr(tokenPrivileges, unmanagedTokenPrivileges, true);
                if (NativeMethod.AdjustTokenPrivileges(token, 0, unmanagedTokenPrivileges, bufLength, IntPtr.Zero, ref returnLength) == 0)
                {
                    Console.WriteLine($"Failed to adjust privileges ({Marshal.GetLastWin32Error()})");
                    Console.WriteLine(new Win32Exception(Marshal.GetLastWin32Error()));
                    return(false);
                }

                if (Marshal.GetLastWin32Error() != 0)
                {
                    Console.WriteLine($"Failed to adjust privileges ({Marshal.GetLastWin32Error()})");
                    return(false);
                }

                Console.WriteLine($"Successfully enabled privilege {privilegeName}");
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error while trying to enable {privilegeName}");
                Console.WriteLine(ex);
                return(false);
            }
            finally
            {
                if (token != IntPtr.Zero)
                {
                    NativeMethod.CloseHandle(token);
                }
                if (unmanagedTokenPrivileges != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(unmanagedTokenPrivileges);
                }
            }
        }