📗
Application Security Manual
  • Intro
    • Start Here
  • Vulnerabilities & Code
    • Injection Vulnerabilities
      • SQL Injection
      • Server-Side Template Injection (SSTI)
      • XPath Injection
      • XML External Entity (XXE) Injection
      • HTTP Request Smuggling
      • Insecure Deserialization
    • Input-based Vulnerabilities
      • Cross-Site Scripting (XSS)
      • Content Security Policy (CSP) Misconfiguration
      • HTTP Parameter Pollution
    • Origin-related Vulnerabilities
      • Cross-Site Request Forgery (CSRF)
      • Server-Side Request Forgery (SSRF)
      • Cross-Origin Resource Sharing (CORS) Misconfiguration
    • Access-related Vulnerabilities
      • Insecure Direct Object References (IDOR)
      • Authorization Code Interception
      • Session Hijacking
      • Insufficient Transport Layer Security (TLS)
      • JSON Web Token (JWT) Vulnerabilities
      • Insecure Client-Side Storage of JWTs
    • Logic & Timing Vulnerabilities
      • Business Logic Flaws
      • Type Juggling (Loose Type Comparison)
      • Timing Attack
  • Design Review
    • Product Security Design Review Framework
    • Security Design Review Checklist
  • Concepts & Culture
    • Implementing SAST Through Security-Driven Developer Culture
    • Security as Code: Baking Security into DevOps
Powered by GitBook
On this page
  • Concept
  • Vulnerable Scenario
  • Explanation
  • Prevention
  • Conclusion
  1. Vulnerabilities & Code
  2. Access-related Vulnerabilities

Session Hijacking

Concept

Session hijacking, also known as cookie hijacking, is a vulnerability that allows an attacker to gain unauthorized access to a user's session by stealing or predicting the session identifier (usually stored in a cookie). Once the attacker obtains a valid session identifier, they can impersonate the victim and perform actions on their behalf without proper authentication.

Vulnerable Scenario

Consider a web application that uses session identifiers to authenticate and track user sessions. The session identifiers are generated using a predictable algorithm or are not properly secured during transmission.

Example Code (Vulnerable)

from flask import Flask, request, session

app = Flask(__name__)
app.secret_key = 'insecure-secret-key'

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    
    # Authenticate the user
    if authenticate_user(username, password):
        session['user_id'] = get_user_id(username)
        return 'Login successful'
    else:
        return 'Invalid credentials'

@app.route('/sensitive-action')
def sensitive_action():
    if 'user_id' in session:
        # Perform sensitive action
        return 'Sensitive action performed'
    else:
        return 'Unauthorized'

Explanation

In this example, the web application uses session identifiers to authenticate users and track their sessions. The session identifier is stored in a cookie and is used to validate the user's identity for subsequent requests.

An attacker can exploit this vulnerability in several ways:

  1. Predictable session identifiers: If the session identifiers are generated using a predictable algorithm, an attacker can guess or calculate valid session identifiers and hijack user sessions.

  2. Insecure transmission: If the session identifier is transmitted over an unencrypted channel (HTTP instead of HTTPS), an attacker can intercept the traffic and steal the session identifier.

  3. Cross-site scripting (XSS): If the application is vulnerable to XSS attacks, an attacker can inject malicious scripts that steal the user's session identifier and send it to the attacker's server.

Once the attacker obtains a valid session identifier, they can use it to impersonate the victim and perform unauthorized actions on their behalf.

Prevention

To prevent session hijacking vulnerabilities, consider the following measures:

  1. Use strong and unpredictable session identifiers: Generate session identifiers using a secure random number generator and ensure they are sufficiently long and complex to resist guessing or brute-force attacks.

  2. Implement secure session management: Use secure session management techniques, such as regenerating session identifiers after authentication and invalidating sessions upon logout or inactivity.

  3. Use HTTPS: Encrypt all sensitive data, including session identifiers, during transmission using HTTPS (SSL/TLS) to prevent interception and tampering.

  4. Implement secure cookie settings: Set the Secure and HttpOnly flags on session cookies to ensure they are only transmitted over HTTPS and are not accessible through client-side scripts.

  5. Validate and sanitize user input: Implement proper input validation and sanitization techniques to prevent cross-site scripting (XSS) attacks that can steal session identifiers.

  6. Implement additional security measures: Use techniques like multi-factor authentication, IP address validation, or user behavior analysis to detect and prevent suspicious session activity.

Example Code (Secure)

from flask import Flask, request, session
import secrets

app = Flask(__name__)
app.secret_key = secrets.token_hex(16)

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    
    # Authenticate the user
    if authenticate_user(username, password):
        session.clear()
        session['user_id'] = get_user_id(username)
        session.permanent = True
        return 'Login successful'
    else:
        return 'Invalid credentials'

@app.route('/sensitive-action')
def sensitive_action():
    if 'user_id' in session:
        # Perform sensitive action
        return 'Sensitive action performed'
    else:
        return 'Unauthorized'

In the secure example, several improvements have been made:

  • The app.secret_key is generated using the secrets module, which provides secure random values.

  • After successful authentication, the previous session is cleared using session.clear() to ensure a new session identifier is generated.

  • The session.permanent flag is set to True to enable persistent sessions and ensure the session identifier is securely stored.

Additionally, it is important to use HTTPS, set secure cookie flags, implement proper input validation and sanitization, and consider additional security measures like multi-factor authentication.

Conclusion

Session hijacking is a serious vulnerability that allows attackers to gain unauthorized access to user sessions by stealing or predicting session identifiers. To mitigate this risk, it is crucial to use strong and unpredictable session identifiers, implement secure session management practices, use HTTPS for encrypted transmission, set secure cookie flags, validate user input, and consider additional security measures. Developers should follow best practices for session management and regularly update their knowledge of session security to protect against session hijacking attacks.

Semgrep Rule

This Semgrep rule can be used as a starting point to potentially identify this type of vulnerability.

rules:
  - id: session-hijacking
    patterns:
      - pattern: |
          app.secret_key = "$INSECURE_SECRET_KEY"
      - metavariable-regex:
          metavariable: $INSECURE_SECRET_KEY
          regex: (?i)(secret|key|password)
    message: "Potential session hijacking vulnerability. Use a secure and randomly generated secret key for session management."
    languages:
      - python
    severity: WARNING
PreviousAuthorization Code InterceptionNextInsufficient Transport Layer Security (TLS)

Last updated 1 year ago