Skip to content

Commit 69a9eab

Browse files
authored
Add logged warnings for elevated launch on windows (#199)
1 parent 93892bf commit 69a9eab

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/Tools/dotnet-monitor/Auth/AuthOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.Diagnostics.Tools.Monitor
88
{
99
internal sealed class AuthOptions : IAuthOptions
1010
{
11-
public bool EnableNegotiate => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
11+
public bool EnableNegotiate => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && KeyAuthenticationMode != KeyAuthenticationMode.NoAuth;
1212

1313
public KeyAuthenticationMode KeyAuthenticationMode { get; }
1414

src/Tools/dotnet-monitor/LoggingExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ internal static class LoggingExtensions
132132
logLevel: LogLevel.Critical,
133133
formatString: "{failure}");
134134

135+
private static readonly Action<ILogger, Exception> _runningElevated =
136+
LoggerMessage.Define(
137+
eventId: new EventId(21, "RunningElevated"),
138+
logLevel: LogLevel.Warning,
139+
formatString: "The process was launched elevated and will have access to all processes on the system. Do not run elevated unless you need to monitor processes launched by another user (e.g., IIS worker processes)");
140+
141+
private static readonly Action<ILogger, Exception> _disabledNegotiateWhileElevated =
142+
LoggerMessage.Define(
143+
eventId: new EventId(22, "DisabledNegotiateWhileElevated"),
144+
logLevel: LogLevel.Warning,
145+
formatString: "Negotiate, Kerberos, and NTLM authentication are not enabled when running with elevated permissions.");
146+
135147
public static void EgressProviderAdded(this ILogger logger, string providerName)
136148
{
137149
_egressProviderAdded(logger, providerName, null);
@@ -248,6 +260,16 @@ public static void OptionsValidationFailure(this ILogger logger, OptionsValidati
248260
_optionsValidationFalure(logger, failure, null);
249261
}
250262

263+
public static void RunningElevated(this ILogger logger)
264+
{
265+
_runningElevated(logger, null);
266+
}
267+
268+
public static void DisabledNegotiateWhileElevated(this ILogger logger)
269+
{
270+
_disabledNegotiateWhileElevated(logger, null);
271+
}
272+
251273
private static string Redact(string value)
252274
{
253275
return string.IsNullOrEmpty(value) ? value : "<REDACTED>";

src/Tools/dotnet-monitor/Startup.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Collections.Generic;
2222
using System.IO.Compression;
2323
using System.Linq;
24+
using System.Runtime.InteropServices;
25+
using System.Security.Principal;
2426
using System.Text.Json.Serialization;
2527

2628
namespace Microsoft.Diagnostics.Tools.Monitor
@@ -130,6 +132,8 @@ public void Configure(
130132

131133
lifetime.ApplicationStarted.Register(() => LogBoundAddresses(app.ServerFeatures, listenResults, logger));
132134

135+
LogElevatedPermissions(options, logger);
136+
133137
if (options.KeyAuthenticationMode == KeyAuthenticationMode.NoAuth)
134138
{
135139
logger.NoAuthentication();
@@ -209,5 +213,25 @@ private static void LogBoundAddresses(IFeatureCollection features, AddressListen
209213
logger.BoundMetricsAddress(metricAddress);
210214
}
211215
}
216+
217+
private static void LogElevatedPermissions(IAuthOptions options, ILogger logger)
218+
{
219+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
220+
{
221+
WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
222+
WindowsPrincipal principal = new WindowsPrincipal(currentUser);
223+
if (principal.IsInRole(WindowsBuiltInRole.Administrator))
224+
{
225+
logger.RunningElevated();
226+
// In the future this will need to be modified when ephemeral keys are setup
227+
if (options.EnableNegotiate)
228+
{
229+
logger.DisabledNegotiateWhileElevated();
230+
}
231+
}
232+
}
233+
234+
// in the future we should check that we aren't running root on linux (out of scope for now)
235+
}
212236
}
213237
}

0 commit comments

Comments
 (0)