Skip to content

Commit

Permalink
feat: Improve UAC prompt handling by focusing the UAC window, useful …
Browse files Browse the repository at this point in the history
…when UAC set as Do Not Dim.

Fixes #85
  • Loading branch information
gerardog committed May 28, 2024
1 parent 87226a3 commit 46a1587
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/gsudo/Helpers/ServiceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Security;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;

namespace gsudo.Helpers
Expand Down Expand Up @@ -175,13 +176,21 @@ internal static SafeProcessHandle StartService(int? allowedPid, TimeSpan? cacheD
else
{
if (SecurityHelper.IsMemberOfLocalAdmins() && InputArguments.GetIntegrityLevel() >= IntegrityLevel.High)
{
// UAC Popup doesnt always have focus, so we try to bring it to the front.
new Thread(UACWindowFocusHelper.FocusUacWindow).Start();

ret = ProcessFactory.StartElevatedDetached(ownExe, commandLine, !InputArguments.Debug).GetSafeProcessHandle();
}
else
ret = ProcessFactory.StartDetached(ownExe, commandLine, null, !InputArguments.Debug).GetSafeProcessHandle();
}
}
else
{
// UAC Popup doesnt always have focus, so we try to bring it to the front.
new Thread(UACWindowFocusHelper.FocusUacWindow).Start();

ret = ProcessFactory.StartElevatedDetached(ownExe, commandLine, !InputArguments.Debug).GetSafeProcessHandle();
}

Expand Down
38 changes: 38 additions & 0 deletions src/gsudo/Helpers/UACWindowFocusHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using gsudo.Native;
using System;
using System.Runtime.InteropServices;

namespace gsudo.Helpers
{
internal class UACWindowFocusHelper
{
[DllImport("user32.dll", SetLastError = true)]

Check warning on line 9 in src/gsudo/Helpers/UACWindowFocusHelper.cs

View workflow job for this annotation

GitHub Actions / Test

Specify marshaling for P/Invoke string arguments (https://docs.microsoft.com/visualstudio/code-quality/ca2101-specify-marshaling-for-p-invoke-string-arguments)

Check warning on line 9 in src/gsudo/Helpers/UACWindowFocusHelper.cs

View workflow job for this annotation

GitHub Actions / Test

Specify marshaling for P/Invoke string arguments (https://docs.microsoft.com/visualstudio/code-quality/ca2101-specify-marshaling-for-p-invoke-string-arguments)
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

internal static void FocusUacWindow()
{
try
{
for (int i = 0; i < 10; i++)
{
// Wait a moment to allow the UAC prompt to appear
System.Threading.Thread.Sleep(100);

// Find the UAC window
string classname = "Credential Dialog Xaml Host"; // Found using Visual Studio spyxx_amd64.exe, this is the value for Windows 10 & 11.
IntPtr uacWindow = FindWindow(classname, null);
if (uacWindow != IntPtr.Zero)
{
// Set focus to the UAC window
WindowApi.SetForegroundWindow(uacWindow);

Check warning on line 27 in src/gsudo/Helpers/UACWindowFocusHelper.cs

View workflow job for this annotation

GitHub Actions / Test

FocusUacWindow calls SetForegroundWindow but does not use the HRESULT or error code that the method returns. This could lead to unexpected behavior in error conditions or low-resource situations. Use the result in a conditional statement, assign the result to a variable, or pass it as an argument to another method. (https://docs.microsoft.com/visualstudio/code-quality/ca1806-do-not-ignore-method-results)

Check warning on line 27 in src/gsudo/Helpers/UACWindowFocusHelper.cs

View workflow job for this annotation

GitHub Actions / Test

FocusUacWindow calls SetForegroundWindow but does not use the HRESULT or error code that the method returns. This could lead to unexpected behavior in error conditions or low-resource situations. Use the result in a conditional statement, assign the result to a variable, or pass it as an argument to another method. (https://docs.microsoft.com/visualstudio/code-quality/ca1806-do-not-ignore-method-results)
return;
}
}
}
catch (Exception ex)
{
Logger.Instance.Log("Error searching for UAC Window: " + ex.ToString(), LogLevel.Debug);
}
}
}
}

0 comments on commit 46a1587

Please sign in to comment.