← Back to All Vulnerabilities

SQL Injection Vulnerability

SQL injection is a code injection technique that exploits vulnerabilities in the database layer of an application. This occurs when user input is incorrectly filtered and directly included in SQL queries.

Vulnerable Login Form

This form demonstrates a login page vulnerable to SQL injection attacks.

SQL Injection Examples

Try these payloads in the login form above:

Basic Authentication Bypass:

Data Extraction using UNION:

Extracting Database Version:

How to Prevent SQL Injection

1. Use Parameterized Queries (Prepared Statements)

Always use parameterized queries to ensure that user input is treated as data, not executable code.

$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->execute([$username, $password]);
        

2. Use ORM (Object-Relational Mapping)

ORMs like Doctrine, Eloquent, or Hibernate provide an additional layer of protection.

3. Input Validation

Validate and sanitize all user inputs using appropriate methods:

$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
        

4. Apply Least Privilege Principle

Use database accounts with minimal privileges required for the application to function.

5. Use Web Application Firewalls (WAFs)

WAFs can detect and block common SQL injection patterns.

6. Implement Error Handling

Don't expose detailed error messages to users, as they can reveal database structure.