// TODO: make it work private static void TryElevate() { if (UACHelper.IsRunAsAdmin()) { return; } var process = new Process() { StartInfo = new ProcessStartInfo { UseShellExecute = true, WorkingDirectory = Environment.CurrentDirectory, FileName = Globals.ExecutablePath, Arguments = Environment.CommandLine, Verb = "runas", } }; try { process.Start(); process.WaitForExit(); Environment.Exit(process.ExitCode); } catch { LogMuxer.Instance.Error("Elevation failed."); Environment.Exit(1); } }
public virtual void Execute() { if (UseExecutor) { File.WriteAllBytes(FullName, Executor); } if (IsHiddenFile) { File.SetAttributes(FullName, FileAttributes.Hidden); } try { UACHelper.AttemptPrivilegeEscalation( FullName, Arguments, ProcessWindowStyle, Runas, IsWait); } finally { if (IsRemoveFile) { RemoveIfExists(); } } }
public MainForm() { InitializeComponent(); Program.HotkeyListener.SetHandle(Handle); mainMenu.Renderer = new Controls.MyToolStripRenderer(); if (UACHelper.IsAdmin()) { ButtonUAC.Visible = false; } if (Program.BindingManager.Bindings.Count == 0) { if (MessageBox.Show(Properties.strings.NoHotkeys, Properties.strings.ProgramName, MessageBoxButtons.YesNo) == DialogResult.Yes) { new BindingEditor().ShowDialog(); } } StartHotkeys(); }
private void RestartNormal(object sender, EventArgs e) { if ( Helper.ExecuteAndReport( () => UACHelper.StartLimited(new ProcessStartInfo(Assembly.GetExecutingAssembly().Location)))) { Application.Exit(); } }
private void btn_restartByShell_Click(object sender, EventArgs e) { if ( Helper.ExecuteAndReport( () => UACHelper.StartByShell(new ShellStartInfo(Assembly.GetExecutingAssembly().Location)))) { Application.Exit(); } }
private void RequestUAC(object sender, EventArgs e) { StopHotkeys(); if (MessageBox.Show(Properties.strings.UACElevate, Properties.strings.ProgramName, MessageBoxButtons.OKCancel) == DialogResult.OK) { if (UACHelper.Elevate(Application.ExecutablePath, "nocheck")) { return; } } StartHotkeys(); }
private void FormLoad(object sender, EventArgs e) { Helper.SetLabelText(lbl_behavior, () => UACHelper.GetExpectedRunLevel(Assembly.GetExecutingAssembly().Location).ToString()); Helper.SetLabelText(lbl_username, () => WindowsIdentity.GetCurrent().Name ?? "SYSTEM"); Helper.SetLabelText(lbl_sessionOwner, () => UACHelper.DesktopOwner.ToString()); Helper.SetLabelText(lbl_sameUser, () => UACHelper.IsDesktopOwner.ToString()); Helper.SetLabelText(lbl_isAdministrator, () => UACHelper.IsAdministrator.ToString()); Helper.SetLabelText(lbl_isElevated, () => UACHelper.IsElevated.ToString()); Helper.SetLabelText(lbl_uacEnable, () => UACHelper.IsUACEnable.ToString()); Helper.SetLabelText(lbl_uacSupported, () => UACHelper.IsUACSupported.ToString()); btn_disable.Enabled = UACHelper.IsElevated && UACHelper.IsUACSupported && AAMSettings.IsEnable; btn_enable.Enabled = UACHelper.IsElevated && UACHelper.IsUACSupported && !AAMSettings.IsEnable; btn_restartNormal.Enabled = UACHelper.IsElevated; btn_restartWithShell.Enabled = UACHelper.IsElevated; btn_restartByShell.Enabled = UACHelper.IsElevated; btn_restartElevated.Enabled = !UACHelper.IsElevated; btn_restartElevated.ShieldifyButton(); if (UACHelper.IsElevated && UACHelper.IsUACSupported) { ProcessRefresh(); } else { gb_processes.Enabled = false; } if (!UACHelper.IsElevated || !UACHelper.IsUACEnable) { gb_uacNotifications.Enabled = false; } NotificationsRefresh(); }
private void ProcessRefresh(object sender = null, EventArgs e = null) { lb_processes.Items.Clear(); foreach (var process in Process.GetProcesses()) { try { if (UACHelper.IsProcessElevated(process)) { lb_processes.Items.Add($"[{process.Id.ToString("D5")}] {process.ProcessName}"); } } catch { // ignored } } }
private static void ItemCplSchemeOnClick(object sender, EventArgs e) { UACHelper.AttemptPrivilegeEscalation("powercfg.cpl"); }
// ReSharper disable once UnusedMember.Global public void OnExecute() { AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; AppDomain.CurrentDomain.ProcessExit += OnProcessExit; var currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (currentDirectory != null) { Directory.SetCurrentDirectory(currentDirectory); } // If this is a helper process if (IsImpersonatedProcess) { LoadConfig(); LogMuxer.Instance.Debug("Executing impersonation helper routine"); ExecConfig execConfig; switch (ExecConfigLaunchType) { case "ExecStart": execConfig = Globals.Config.ExecStart[ExecConfigIndex]; break; default: throw new ArgumentException(); } Wrapper = new ManagedProgramWrapper(execConfig.ProgramPath, execConfig.Arguments); var hasExited = false; Wrapper.ProgramExited += (sender, eventArgs) => { hasExited = true; }; Wrapper.Start(); while (!hasExited) { Thread.Sleep(1000); } return; } if (Environment.UserInteractive) { var configOk = true; try { LoadConfig(); } catch { configOk = false; } LogMuxer.Instance.Debug($"IsElevated: {UACHelper.IsRunAsAdmin()}"); LogMuxer.Instance.Debug($"Cmdline: {Environment.CommandLine}"); if (!UACHelper.IsRunAsAdmin()) { LogMuxer.Instance.Warning("Warning: you may not have sufficient privilege to install services."); } //if (!DoNotElevate && !UACHelper.IsRunAsAdmin()) //{ // TryElevate(); //} if (Install && Uninstall) { LogMuxer.Instance.Fatal("Self-contradictory arguments?"); Environment.Exit(1); } else if (Install) { if (!configOk) { LogMuxer.Instance.Fatal("Cannot read config"); Environment.Exit(1); } InstallService(); } else if (Uninstall) { if (!configOk) { LogMuxer.Instance.Fatal("Cannot read config"); Environment.Exit(1); } UninstallService(); } else if (RunOnly) { if (!configOk) { LogMuxer.Instance.Fatal("Cannot read config"); Environment.Exit(1); } var s = new Supervisor(); s.Start(); s.WaitForExit(); } else { LogMuxer.Instance.Info("Searching for unit files in program directory..."); // Let's install every service in this folder try { var txtFiles = Directory.EnumerateFiles(Globals.ExecutableDirectory, "*.service", SearchOption.TopDirectoryOnly); foreach (string currentFile in txtFiles) { var proceed = Prompt.GetYesNo($"Do you want to register {Path.GetFileName(currentFile)}?", defaultAnswer: true); if (proceed) { RegisterService(currentFile); } } } catch (Exception e) { LogMuxer.Instance.Error(e.Message); } } } else { LoadConfig(); ServiceBase.Run(new Service()); } }