Get a Pentest and security assessment of your IT network.

Cyber Security

Blind SQL Injection Fix

TL;DR

This guide explains how to identify and fix Blind SQL Injection vulnerabilities, potentially linked to older HTTP versions. We’ll cover testing, mitigation using prepared statements/parameterized queries, input validation, and web server configuration.

Understanding the Problem

Blind SQL Injection happens when a web application is vulnerable to SQL injection but doesn’t directly show error messages or query results in the response. Attackers infer information by observing how the application behaves based on their injected payloads (e.g., timing differences, different content displayed). An HTTP version dependency might mean older versions are more susceptible due to differing parsing behaviours.

Step 1: Identifying Blind SQL Injection

  1. Initial Testing: Start with basic SQL injection attempts in input fields (e.g., username, password, search box). Try single quotes (`’`) and common SQL keywords like `OR 1=1`.
  2. Time-Based Blind SQL Injection: If direct errors aren’t visible, try payloads that cause a delay if the condition is true.
    SELECT IF(condition, SLEEP(5), 0);

    Monitor response times. A significant delay indicates a successful injection point.

  3. Boolean-Based Blind SQL Injection: Use payloads that return different results based on the truthiness of a condition.
    SELECT IF(condition, 'true', 'false');

    Observe changes in the application’s response (e.g., content displayed, redirects).

  4. HTTP Version Check: Use tools like curl -I or browser developer tools to determine the HTTP version used by the server.
    curl -I https://example.com

    Older versions (e.g., HTTP/1.0) might have less robust input handling.

Step 2: Mitigating SQL Injection with Prepared Statements

Prepared statements are the most effective way to prevent SQL injection. They separate the SQL code from user-supplied data.

  1. Use Parameterized Queries: Instead of directly embedding user input into your SQL queries, use placeholders.
    // Example in PHP using PDO
    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
    $stmt->execute([$_POST['username'], $_POST['password']]);
  2. Avoid Dynamic SQL: Never construct SQL queries by concatenating strings with user input.

Step 3: Input Validation

While not a replacement for prepared statements, input validation adds an extra layer of security.

  1. Whitelist Valid Characters: Define allowed characters for each input field (e.g., alphanumeric only).
    // Example in JavaScript
    function validateUsername(username) {
      const regex = /^[a-zA-Z0-9]+$/;
      return regex.test(username);
    }
    
  2. Sanitize Input: Remove or encode potentially harmful characters.
    // Example in Python using a library like bleach
    import bleach
    cleaned_input = bleach.clean(user_input, tags=[], attributes={}, styles=[])
  3. Limit Input Length: Restrict the maximum length of input fields to prevent excessively long payloads.

Step 4: Web Server Configuration

Configure your web server to improve security and potentially mitigate some injection risks.

  1. Update HTTP Version: Upgrade to the latest stable version of HTTP (HTTP/2 or HTTP/3) for improved security features.
  2. Web Application Firewall (WAF): Implement a WAF to detect and block malicious requests, including SQL injection attempts.
  3. Regular Security Audits: Conduct regular penetration testing and vulnerability scans to identify and address potential weaknesses.

Step 5: Error Handling

Configure your application to log errors securely without revealing sensitive information to users.

  1. Generic Error Messages: Display generic error messages to users (e.g., “Invalid input.”) instead of detailed SQL error messages.
  2. Centralized Logging: Log all errors to a secure, centralized logging system for analysis by administrators.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation