SQL Injection (Boolean)

  • PCI 3.2-6.5.2
  • CWE 89
  • CAPEC 66
  • OWASP 2017-A1
  • WASC 19

SQL Injection is a high-severity web application vulnerability that occurs when untrusted user input is concatenated into SQL queries without proper validation or parameterization. This flaw allows an attacker to modify the structure of a backend SQL query, enabling unauthorized data access, data manipulation, administrative operations, and—in some cases—full control over the underlying database server.

Unlike Code Injection, which targets application-level interpreters, SQL Injection specifically targets the database query language. Attackers supply malicious SQL fragments that are executed as part of the application’s SQL statement. The attack’s scope depends on the privileges of the database user account and the database engine’s capabilities, such as file access, stored procedures, or command execution.

Common attack scenarios:

  • String-based injections where user input is appended directly into WHERE, ORDER BY, LIMIT, or UNION clauses.
  • Boolean-based blind SQL injection where attackers infer true/false responses from subtle application changes.
  • Time-based blind SQL injection where attackers use functions like SLEEP() or WAITFOR DELAY to extract data bit by bit.
  • Error-based SQL injection where database error messages reveal query structure or sensitive information.
  • Out-of-band channels using features like xp_cmdshell, DNS exfiltration, file writes, or HTTP request functions.

Typical impacts include:

  • Extraction of sensitive data such as credentials, personal information, and payment details.
  • Unauthorized changes to database contents (INSERT/UPDATE/DELETE).
  • Creation of new administrative accounts or escalation of privileges.
  • Execution of stored procedures or OS-level commands depending on DBMS capabilities.
  • Full database compromise and potential pivoting into the broader infrastructure.

Conditions enabling SQL Injection:

  • SQL queries constructed using string concatenation.
  • Lack of parameterized queries or prepared statements.
  • Insufficient input validation or improper handling of special characters.
  • Overly permissive database accounts with unnecessary privileges.
  • Verbose error messages that expose query internals.
Remediation

Mitigating SQL Injection requires eliminating unsafe query construction patterns, strictly validating input, and enforcing strong database security controls.

  1. Use Parameterized Queries / Prepared Statements
    Replace dynamic query concatenation with parameterized constructs (SqlParameter, PreparedStatement, PDO::prepare). Ensure all input is treated as data, not query logic.

  2. Use Stored Procedures Safely
    Prefer stored procedures that do not embed uncontrolled dynamic SQL. However, ensure they also use parameters rather than concatenating input.

  3. Input Validation (Whitelisting)
    Enforce strict validation rules for user-supplied fields, especially numeric IDs, filters, and category selectors. Reject unexpected characters such as quotes, semicolons, comments, or operators.

  4. Least Privilege Database Accounts
    Ensure the application connects to the database using an account with only the permissions required. Prevent access to administrative features such as file operations, command execution, or schema modifications.

  5. Error Handling and Message Hygiene
    Do not expose SQL errors, stack traces, or database diagnostics to users. Log errors securely for internal analysis but return generic messages to the client.

  6. Use ORM Frameworks Carefully
    If using ORMs (e.g., Entity Framework, Hibernate, Django ORM), prefer API methods that automatically parameterize inputs. Avoid dynamic query building functions unless parameters are used.

  7. Escaping as a Secondary Measure
    Use context-appropriate escaping functions when parameterization is not possible. However, escaping alone should never replace prepared statements.

  8. WAF and Query Anomaly Detection
    Deploy Web Application Firewalls or database anomaly detection tools as a secondary defense to block common injection payloads.

  9. Regular Security Testing
    Perform automated scans, static code analysis, and manual penetration tests targeting common SQL injection vectors. Include blind and time-based test cases.

  10. Secure Configuration
    Disable dangerous database extensions (e.g., xp_cmdshell). Restrict outbound network calls from the database to prevent data exfiltration.

References