Esempio n. 1
0
        public Task <int> Execute()
        {
            Native.ConsoleApi.FreeConsole();
            if (!Native.ConsoleApi.AttachConsole(-1))
            {
                Native.ConsoleApi.AllocConsole();
                Logger.Instance.Log($"Failed to attach console.\r\n{new Win32Exception().ToString()}", LogLevel.Error);
            }

            var app  = CommandToRun.First();
            var args = string.Join(" ", CommandToRun.Skip(1).ToArray());

            if (InputArguments.IntegrityLevel.HasValue &&
                (int)InputArguments.IntegrityLevel != ProcessHelper.GetCurrentIntegrityLevel() &&
                Environment.GetEnvironmentVariable("gsudoAttachRun") != "1")
            {
                Environment.SetEnvironmentVariable("gsudoAttachRun", "1"); // prevents infinite loop on machines with UAC disabled.

                var process = ProcessFactory.StartAttachedWithIntegrity(
                    InputArguments.GetIntegrityLevel(), app, args, Directory.GetCurrentDirectory(), false, true);

                process.GetProcessWaitHandle().WaitOne();

                if (ProcessApi.GetExitCodeProcess(process, out var exitCode))
                {
                    return(Task.FromResult(exitCode));
                }
            }
            else
            {
                Helpers.ProcessFactory.StartAttached(app, args).WaitForExit();
            }

            return(Task.FromResult(0));
        }
Esempio n. 2
0
        private static int RunWithoutService(string exeName, string args, ElevationRequest elevationRequest)
        {
            Logger.Instance.Log("Already running as the specified user/permission-level. Running in-process...", LogLevel.Debug);
            var sameIntegrity = (int)InputArguments.GetIntegrityLevel() == ProcessHelper.GetCurrentIntegrityLevel();

            // No need to escalate. Run in-process

            if (!string.IsNullOrEmpty(elevationRequest.Prompt))
            {
                Environment.SetEnvironmentVariable("PROMPT", Environment.ExpandEnvironmentVariables(elevationRequest.Prompt));
            }

            if (sameIntegrity)
            {
                if (elevationRequest.NewWindow)
                {
                    using (var process = ProcessFactory.StartDetached(exeName, args, Environment.CurrentDirectory, false))
                    {
                        if (elevationRequest.Wait)
                        {
                            process.WaitForExit();
                            var exitCode = process.ExitCode;
                            Logger.Instance.Log($"Process exited with code {exitCode}", exitCode == 0 ? LogLevel.Debug : LogLevel.Info);
                            return(exitCode);
                        }
                        return(0);
                    }
                }
                else
                {
                    using (Process process = ProcessFactory.StartAttached(exeName, args))
                    {
                        process.WaitForExit();
                        var exitCode = process.ExitCode;
                        Logger.Instance.Log($"Process exited with code {exitCode}", exitCode == 0 ? LogLevel.Debug : LogLevel.Info);
                        return(exitCode);
                    }
                }
            }
            else // lower integrity
            {
                var p = ProcessFactory.StartAttachedWithIntegrity(InputArguments.GetIntegrityLevel(), exeName, args, elevationRequest.StartFolder, InputArguments.NewWindow, !InputArguments.NewWindow);
                if (p == null || p.IsInvalid)
                {
                    return(Constants.GSUDO_ERROR_EXITCODE);
                }

                if (elevationRequest.Wait)
                {
                    ProcessHelper.GetProcessWaitHandle(p.DangerousGetHandle()).WaitOne();
                    ProcessApi.GetExitCodeProcess(p, out var exitCode);
                    Logger.Instance.Log($"Process exited with code {exitCode}", exitCode == 0 ? LogLevel.Debug : LogLevel.Info);
                    return(exitCode);
                }

                return(0);
            }
        }
Esempio n. 3
0
        public Task <int> GetResult()
        {
            try
            {
                _ = ProcessApi.ResumeThread(_processInformation.hThread);

                if (_elevationRequest.Wait)
                {
                    _process.GetProcessWaitHandle().WaitOne();
                    if (ProcessApi.GetExitCodeProcess(_process, out int exitCode))
                    {
                        return(Task.FromResult(exitCode));
                    }
                }

                return(Task.FromResult(0));
            }
            finally
            {
                ConsoleApi.SetConsoleCtrlHandler(ConsoleHelper.IgnoreConsoleCancelKeyPress, false);
            }
        }