Session Hijacking in Aspnet
How Session Hijacking Manifests in Aspnet
In Aspnet applications, session hijacking typically occurs when an attacker steals or predicts a valid session identifier (session ID) to impersonate a user. Aspnet uses cookies (by default named ASP.NET_SessionId) or URL parameters to track sessions. If these identifiers are exposed via client-side scripts, logged insecurely, or generated with insufficient entropy, attackers can capture them through cross-site scripting (XSS), man-in-the-middle attacks on unencrypted traffic, or session fixation.
Aspnet-specific vulnerabilities include:
- Storing session IDs in URLs (
cookieless="UseUri"inweb.config), which can be leaked via Referer headers or browser history. - Using predictable session IDs due to misconfiguration of the
sessionStateelement, such as settingregenerateExpiredSessionId="false"or relying on default generators without sufficient randomness. - Failing to set the
HttpOnlyandSecureflags on session cookies, making them accessible to JavaScript (HttpOnly) or transmittable over non-HTTPS connections (Secure). - Session fixation: If Aspnet accepts a session ID from the request (e.g., via cookie or query string) without regenerating it after login, an attacker can fixate a known session ID and trick a user into using it.
For example, an Aspnet MVC application with [SessionState(SessionStateBehavior.ReadOnly)] on a controller might inadvertently allow session fixation if combined with cookieless=true and no post-login regeneration. Attackers can then hijack active sessions to access user data, perform unauthorized actions, or escalate privileges.
Aspnet-Specific Detection
Detecting session hijacking risks in Aspnet requires examining both configuration and runtime behavior. middleBrick identifies these issues through unauthenticated black-box scanning of the API surface, focusing on session-related responses and behaviors without needing source code or credentials.
Specifically, middleBrick checks for:
- Session cookies missing the
HttpOnlyflag (accessible to JavaScript, increasing XSS theft risk). - Session cookies missing the
Secureflag (transmitted over HTTP, vulnerable to interception). - Session IDs exposed in URLs (indicating cookieless sessions or URL-based session tracking).
- Low entropy or predictable patterns in session IDs (statistical analysis of issued tokens).
- Failure to regenerate session ID after login (tested via login sequence simulation).
- Session tokens that do not invalidate on logout (checked via post-logout cookie validity).
For instance, if an Aspnet API returns a Set-Cookie: ASP.NET_SessionId=abc123; path=/ without HttpOnly or Secure, middleBrick flags this as a medium-risk finding under the "Data Exposure" category, referencing OWASP API2:2023 (Broken Authentication). The scanner also tests whether the session ID changes after authenticating — if not, it reports a session fixation risk.
These checks are performed across all discovered endpoints, including those referenced in OpenAPI/Swagger specs (if provided), ensuring comprehensive coverage of the unauthenticated attack surface.
Aspnet-Specific Remediation
Mitigating session hijacking in Aspnet involves configuring session security correctly and leveraging built-in framework protections. All fixes should be applied in web.config or via code in Startup.cs (Aspnet Core) or Global.asax (Aspnet Framework).
Key remediation steps:
- Always set
HttpOnlyandSecureflags on session cookies:
<configuration>
<system.web>
<sessionState
cookieless="UseCookies"
regenerateExpiredSessionId="true"
timeout="20" /
</sessionState>
</system.web>
</configuration>
In Aspnet Core, configure cookie options in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(options =>
{
options.Cookie.HttpOnly = true; // Prevents client-side script access
options.Cookie.SecurePolicy = CookieSecurePolicy.Always; // Ensures HTTPS-only transmission
options.Cookie.IsEssential = true; // Required for GDPR compliance
options.IdleTimeout = TimeSpan.FromMinutes(20);
})
.AddMvc();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSession();
// ... other middleware
}
- Disable cookieless sessions (
cookieless="UseCookies") to prevent session IDs from appearing in URLs. - Ensure session ID regeneration after login by calling
Session.Abandon()followed bySession.Clear()and re-initialization, or rely onregenerateExpiredSessionId="true"in Aspnet Framework. - Implement logout that properly invalidates the session server-side (not just client-side cookie removal).
- Use
[ValidateAntiForgeryToken]on POST endpoints to mitigate CSRF, which often accompanies session hijacking.
These configurations ensure session identifiers are transmitted securely, inaccessible to scripts, and rotated appropriately — directly addressing the attack vectors middleBrick detects. Remediation guidance from middleBrick includes severity ratings and references to OWASP API2:2023 and CWE-384 (Session Fixation).