Denial Of Service in Aspnet with Mutual Tls
Denial Of Service in Aspnet with Mutual Tls
Mutual TLS (mTLS) adds a strong authentication layer by requiring the client to present a certificate, but in ASP.NET it can also change how resource consumption behaves during handshake and request processing. A Denial of Service (DoS) risk arises when the server performs expensive operations—such as certificate validation, decryption, and authentication—before the framework reaches authorization or application logic. If an attacker sends many incomplete or malformed TLS handshakes, or connects with valid client certificates but immediately sends malformed requests, the server may spend disproportionate CPU on cryptographic work and connection state management, exhausting thread pool or connection limits.
In ASP.NET, DoS with mTLS can manifest in several concrete ways. A flood of TLS handshakes without completing the HTTP request can keep I/O threads and Kestrel connection pools occupied. Additionally, if client certificate validation is implemented with custom logic that performs synchronous or remote checks (e.g., checking revocation via network calls), each connection incurs latency and thread usage, making the service more susceptible to resource exhaustion under load. Large client certificates also increase the handshake size and memory pressure per connection. Without proper limits, an attacker could leverage valid-but-costly certificates to degrade availability.
middleBrick scans unauthenticated attack surfaces and can surface DoSS indicators related to authentication and rate limiting among its 12 security checks. A low risk score in the Rate Limiting category, combined with findings in Authentication, may suggest that the ASP.NET endpoint is exposed to resource stress when mTLS is enforced. The scanner does not attempt to exploit these conditions but highlights where configuration or code changes are recommended.
To assess this behavior safely, developers can inspect the pipeline and configuration. Consider the following realistic ASP.NET snippet that enforces mTLS and performs custom validation:
// Program.cs in ASP.NET minimal API
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.ListenAnyIP(5001, listenOptions =>
{
listenOptions.UseHttps(httpsOptions =>
{
httpsOptions.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
httpsOptions.AllowAnyClientCertificate();
});
});
});
builder.Services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
.AddCertificate(options =>
{
options.Events = new CertificateAuthenticationEvents
{
OnCertificateValidated = context =>
{
// Example of a slow or remote validation that can amplify DoS
var cert = context.ClientCertificate;
// Perform revocation check or custom policy validation here
// Avoid synchronous remote calls in production
context.AcceptIfRemoteErrorLoggingAllowed();
return Task.CompletedTask;
}
};
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/", () => "Hello");
app.Run();
In this example, if certificate validation includes network-bound operations or is not rate-limited, an attacker can open many connections with valid client certificates and trigger high CPU or thread starvation. Mitigations should focus on reducing handshake cost, enforcing request timeouts, and applying concurrency limits.
Mutual Tls-Specific Remediation in Aspnet
Remediation for DoS risks in ASP.NET with mTLS centers on reducing synchronous work, limiting resource consumption, and failing fast for abusive connections. Below are concrete, actionable changes to the pipeline and configuration.
First, avoid synchronous or remote validation inside certificate events. Use asynchronous lightweight checks and cache results where possible. Prefer built-in revocation checks with appropriate caching instead of custom remote calls. Here is a safer configuration example:
// Program.cs with improved handling
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromSeconds(10);
serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);
serverOptions.ListenAnyIP(5001, listenOptions =>
{
listenOptions.UseHttps(httpsOptions =>
{
httpsOptions.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
httpsOptions.AllowAnyClientCertificate();
});
});
});
builder.Services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
.AddCertificate(options =>
{
options.Events = new CertificateAuthenticationEvents
{
OnCertificateValidated = async context =>
{
var cert = context.ClientCertificate;
if (cert == null) context.Fail("No certificate");
// Lightweight policy checks; avoid synchronous network I/O
if (!await IsCertificateInAllowedStoreAsync(cert, context.HttpContext.RequestAborted))
{
context.Fail("Untrusted certificate");
}
}
};
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/", () => "Hello");
app.Run();
// Helper method example
async Task<bool> IsCertificateInAllowedStoreAsync(X509Certificate2 cert, CancellationToken ct)
{
// Replace with efficient lookup, caching, or local policy check
// This is a simplified example
await Task.Delay(1, ct); // placeholder for actual async work
return true;
}
Second, enforce concurrency and connection limits at the Kestrel level to prevent resource exhaustion:
// Kestrel limits configuration
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.Limits.MaxConcurrentConnections = 100;
serverOptions.Limits.MaxConcurrentUpgradedConnections = 100;
serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromSeconds(10);
serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);
});
Third, apply middleware-level timeouts and early rejection for slow or abusive clients. Use short request headers timeouts and disable lingering where appropriate:
app.Use(async (context, next) =>
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted);
cts.CancelAfter(TimeSpan.FromSeconds(15);
using var registration = context.Request.RegisterForCancellation(cts.Token);
await next();
});
Finally, monitor connection pools and thread usage in production. middleBrick’s dashboard can help track risk trends over time, while its CLI allows you to run repeated scans to validate that configuration changes reduce exposure in the Authentication and Rate Limiting categories.
Related CWEs: resourceConsumption
| CWE ID | Name | Severity |
|---|---|---|
| CWE-400 | Uncontrolled Resource Consumption | HIGH |
| CWE-770 | Allocation of Resources Without Limits | MEDIUM |
| CWE-799 | Improper Control of Interaction Frequency | MEDIUM |
| CWE-835 | Infinite Loop | HIGH |
| CWE-1050 | Excessive Platform Resource Consumption | MEDIUM |