📗
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

Insufficient Transport Layer Security (TLS)

Concept

Transport Layer Security (TLS) is a cryptographic protocol that provides secure communication over a network, such as the internet. It ensures the confidentiality, integrity, and authenticity of the data transmitted between a client and a server. Insufficient or improper implementation of TLS can expose sensitive information to unauthorized parties, leaving the application and its users vulnerable to attacks such as eavesdropping, man-in-the-middle (MITM) attacks, and data tampering.

Common issues related to insufficient TLS include using outdated or weak encryption algorithms, improper certificate validation, lack of secure protocol versions, and misconfigurations in the TLS setup.

Vulnerable Scenario

Consider a web application that handles sensitive user information, such as login credentials or financial data. The application communicates with the server over an insecure channel, using HTTP instead of HTTPS, or uses weak encryption algorithms and outdated TLS versions.

Example Code (Vulnerable)

from flask import Flask, request

app = Flask(__name__)

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    
    # Perform authentication logic
    if authenticate_user(username, password):
        # Set session cookie and redirect to home page
        return redirect('/home')
    else:
        return "Invalid credentials", 401

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)

Explanation

In this example, the web application uses Flask to handle user authentication. However, the application has several issues related to insufficient transport layer security:

  1. The application runs on HTTP (port 80) instead of HTTPS, meaning that all data, including login credentials, is transmitted in plain text over the network. An attacker can easily intercept and read the sensitive information.

  2. The application does not enforce the use of secure TLS versions or strong encryption algorithms. It may be vulnerable to attacks that exploit weaknesses in older TLS versions or weak ciphers.

  3. The application does not properly validate server certificates, making it susceptible to man-in-the-middle attacks where an attacker can impersonate the server and intercept the communication.

Insufficient transport layer security exposes the application and its users to various risks, including:

  • Eavesdropping: Attackers can intercept and read the transmitted data, compromising the confidentiality of sensitive information.

  • Data tampering: Attackers can modify the data in transit, leading to unauthorized changes or malicious actions.

  • Impersonation: Attackers can impersonate the server or the client, tricking users into disclosing sensitive information or performing unintended actions.

Prevention

To address insufficient transport layer security and protect the application and its users, consider the following measures:

  1. Use HTTPS: Ensure that the application uses HTTPS (HTTP over TLS) for all communication, especially when transmitting sensitive data. Configure the server to enforce HTTPS and redirect HTTP requests to HTTPS.

  2. Use strong encryption algorithms: Employ strong and up-to-date encryption algorithms, such as AES with a minimum key size of 128 bits, for encrypting data in transit.

  3. Enforce secure TLS versions: Configure the server to support only secure TLS versions (TLS 1.2 and above) and disable support for older, insecure versions like SSL or early TLS versions.

  4. Proper certificate validation: Implement proper server certificate validation to ensure the authenticity of the server. Use trusted certificate authorities and verify the certificate chain, expiration dates, and host name matches.

  5. Enable HTTP Strict Transport Security (HSTS): Implement HSTS to instruct browsers to always communicate with the application over HTTPS, even if the user manually enters an HTTP URL.

  6. Regularly update and patch: Keep the server, TLS libraries, and dependencies up to date with the latest security patches to address known vulnerabilities.

Example Code (Secure)

from flask import Flask, request, redirect

app = Flask(__name__)

@app.route('/')
def index():
    if not request.is_secure:
        return redirect(request.url.replace('http://', 'https://'))
    return "Welcome to the secure application!"

@app.route('/login', methods=['POST'])
def login():
    if not request.is_secure:
        return "Login must be performed over HTTPS", 403
    
    username = request.form['username']
    password = request.form['password']
    
    # Perform authentication logic
    if authenticate_user(username, password):
        # Set session cookie and redirect to home page
        return redirect('/home')
    else:
        return "Invalid credentials", 401

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=443, ssl_context=('server.crt', 'server.key'))

In the secure example, several improvements have been made to address insufficient transport layer security:

  1. The application runs on HTTPS (port 443) and uses an SSL/TLS certificate and key (server.crt and server.key) to enable secure communication.

  2. The index route checks if the request is secure (HTTPS) and redirects HTTP requests to HTTPS, ensuring that all communication is encrypted.

  3. The login route verifies that the request is performed over HTTPS before processing the login credentials. If the request is not secure, it returns an error.

  4. Strong encryption algorithms and secure TLS versions should be configured on the server to ensure the use of up-to-date and robust security protocols.

By implementing these measures, the application ensures that all communication is encrypted, protecting sensitive data from unauthorized access and tampering during transmission.

Conclusion

Insufficient transport layer security is a critical vulnerability that exposes applications and their users to various security risks. It is essential to properly implement and configure TLS to protect sensitive data in transit and prevent attacks such as eavesdropping, data tampering, and impersonation. By using HTTPS, enforcing secure TLS versions and strong encryption algorithms, properly validating certificates, and keeping the server and dependencies up to date, developers can significantly enhance the security of their applications and safeguard user data.

Semgrep Rule

Semgrep can be used to identify instances where the application is running on an insecure port (HTTP) or not enforcing HTTPS.

rules:
  - id: insecure-transport
    patterns:
      - pattern: |
          app.run(..., port=$PORT, ...)
      - metavariable-comparison:
          metavariable: $PORT
          comparison: $PORT <= 80
    message: "Application is running on an insecure port (HTTP). Use HTTPS (port 443) for secure communication."
    languages:
      - python
    severity: ERROR

This Semgrep rule identifies code patterns where the application is running on a port less than or equal to 80, which typically indicates the use of HTTP instead of HTTPS. It suggests using HTTPS (port 443) for secure communication.

Note that this rule is a starting point and may need to be adapted based on your specific application and framework. It is important to thoroughly test the TLS configuration and ensure that it meets the security requirements of your application.

PreviousSession HijackingNextJSON Web Token (JWT) Vulnerabilities

Last updated 1 year ago