1. Home
  2. Web App Vulnerabilities
  3. How to remediate – SQL Injection

How to remediate – SQL Injection

1. Introduction

SQL Injection is a vulnerability where an attacker can interfere with the queries that an application makes to its database. This allows them to view, modify or delete data. It affects any web application or service using a database backend without proper security measures. Successful exploitation could lead to loss of confidential data and complete control over server components.

2. Technical Explanation

  • Root cause: Lack of input validation and use of dynamic SQL queries without parameterisation.
  • Exploit mechanism: An attacker submits malicious SQL code through an application’s input fields (e.g., login forms, search boxes). If the application doesn’t properly sanitise this input, it is passed to the database as part of a query. For example, submitting ‘OR 1=1’ into a username field could bypass authentication.
  • Scope: Web applications using MSSQL, MySQL, Oracle and other SQL databases are affected if they do not use parameterized queries.

3. Detection and Assessment

You can confirm vulnerability by testing for database error responses or attempting to extract data using known injection payloads.

  • Quick checks: Check application source code for direct concatenation of user input into SQL queries.
  • Scanning: Use web application scanners like OWASP ZAP or Burp Suite with active scanning enabled, looking for SQL Injection signatures. These are examples only and may require configuration.
  • Logs and evidence: Examine database server logs for error messages related to invalid SQL syntax or unexpected query behaviour. Look for patterns of failed login attempts followed by successful queries using unusual parameters.
mysql -u root -p -e "SELECT 1 FROM users WHERE username = 'admin' OR 1=1;"

4. Solution / Remediation Steps

The only reliable way to prevent SQL Injection is to use parameterized queries (also known as prepared statements). This ensures that user input is treated as data, not executable code.

4.1 Preparation

  • Ensure you have access to the application source code or configuration files. A roll back plan involves restoring the database backup and reverting any code changes.
  • Change windows may be needed for production systems, requiring approval from security teams.

4.2 Implementation

  1. Step 1: Identify all locations in the application code where SQL queries are constructed dynamically using user input.
  2. Step 2: Replace direct concatenation of user input into SQL queries with parameterized queries. Use your database driver’s built-in support for prepared statements.
  3. Step 3: Test each change thoroughly to ensure that the application still functions correctly and that no new vulnerabilities have been introduced.

4.3 Config or Code Example

Before

query = "SELECT * FROM users WHERE username = '" + request.form['username'] + "'"

After

query = "SELECT * FROM users WHERE username = %s"
cursor.execute(query, (request.form['username'],))

4.4 Security Practices Relevant to This Vulnerability

Several security practices can help prevent SQL Injection attacks.

  • Least privilege: Limit database user permissions to only the necessary functions and data.

4.5 Automation (Optional)

Static code analysis tools can identify potential SQL Injection vulnerabilities during development.

# Example using SonarQube:
# Run a scan of the application source code with SonarQube, focusing on SQL Injection rules.
# Review and address any identified issues.

5. Verification / Validation

  • Post-fix check: Attempt a simple SQL Injection payload (e.g., ‘OR 1=1’) through an input field and verify that it does not result in successful authentication or data extraction.
  • Re-test: Re-run the scanner used earlier to confirm that the vulnerability is no longer detected.
  • Monitoring: Monitor database server logs for any unusual query activity or error messages.
mysql -u root -p -e "SELECT 1 FROM users WHERE username = 'admin' OR 1=1;" # Should return no results or an error

6. Preventive Measures and Monitoring

Regular security assessments, code reviews, and patch management are essential for preventing SQL Injection attacks.

  • Baselines: Implement a secure coding standard that prohibits direct concatenation of user input into SQL queries.
  • Pipelines: Integrate static application security testing (SAST) tools into the CI/CD pipeline to automatically identify potential vulnerabilities during development.
  • Asset and patch process: Maintain an up-to-date inventory of all database servers and applications, and apply security patches promptly.

7. Risks, Side Effects, and Roll Back

Applying parameterized queries may require code changes that could introduce new bugs or compatibility issues.

  • Risk or side effect 1: Code changes might break existing functionality if not tested thoroughly.
  • Risk or side effect 2: Parameterized query syntax can vary between database drivers, requiring careful attention to detail.
  • Roll back: Restore the database backup and revert any code changes made during the remediation process.

8. References and Resources

Updated on December 27, 2025

Was this article helpful?

Related Articles