Nouvelle réglementation de cybersécurité 2026 en vigueur au Maroc. Obtenir un audit de conformité gratuit →
← Retour au blog
Owasp 2025-02-10 ⏱️ 12 min

SQL Injection complete guide with real payloads and bypass techniques

The Complete Guide to SQL Injection (SQLi) in 2025

SQL Injection (SQLi) remains one of the most devastating and heavily exploited vulnerabilities on the internet today. Despite being over two decades old, SQLi regularly ranks in the top tier of the OWASP Top 10. In this complete guide, we will explore the granular details of SQL Injection, from basic payloads to advanced bypass techniques, explicitly designed to educate security professionals and developers on how to prevent unauthorized database access.

What is SQL Injection?

SQL Injection occurs when user-supplied data is interpreted as code by the backend database. By manipulating input fields, an attacker can tamper with the intended SQL query, forcing the database to execute arbitrary commands. This can lead to unauthorized data disclosure, data modification, or even complete system compromise. When dealing with web applications, improper input sanitization is the primary cause of SQL injection.

Real-World Impact

A successful SQL injection attack can have catastrophic consequences for an organization. Attackers can bypass authentication forms, extract sensitive intellectual property, or dump entire customer databases containing PII (Personally Identifiable Information). In severe cases, SQLi can be leveraged to achieve Remote Code Execution (RCE) via xp_cmdshell in Microsoft SQL Server or INTO OUTFILE in MySQL.

Types of SQL Injection

1. In-Band SQLi (Classic)

In-band SQL Injection is the most straightforward variant. The attacker uses the same communication channel to both launch the attack and gather results.

  • Error-Based SQLi: The attacker forces the database to generate an error message that inadvertently reveals information about the database structure or version. For instance, inputting a single quote ' in a parameter might cause a syntax error, confirming the vulnerability.
  • Union-Based SQLi: This technique leverages the UNION operator to combine the results of the original query with the results of a maliciously crafted query. The injected query must have the same number of columns and compatible data types as the original query.

2. Inferential SQLi (Blind)

In a blind SQL injection attack, the web application does not return database errors or the results of the injected query directly to the screen. The attacker must infer information by asking the database true/false questions.

  • Boolean-Based Blind SQLi: The attacker sends an SQL query that asks a question (e.g., "Is the first letter of the administrator's password 'A'?"). The application's response (e.g., returning the normal page or a "not found" page) reveals whether the condition is true or false.
  • Time-Based Blind SQLi: When the application responds identically regardless of the query's truth value, the attacker commands the database to pause for a specified amount of time (e.g., using SLEEP(10) or WAITFOR DELAY '0:0:10') before returning a response. The delay confirms the injection.

3. Out-of-Band SQLi

Out-of-band SQLi is used when the attacker cannot use the same channel to launch the attack and gather results, or when blind techniques are too slow. This method relies on the database server's ability to make outbound network requests (e.g., DNS or HTTP) to deliver data to a server controlled by the attacker.

Real Payloads and Bypass Techniques

Security filters and Web Application Firewalls (WAFs) often attempt to block common SQL injection payloads. However, attackers continuously develop bypass techniques.

Authentication Bypass

A classic authentication bypass payload:

' OR '1'='1

When injected into a login form, this alters the query to always evaluate as true, logging the attacker in without a valid password.

Advanced Union Payloads

To extract the database version using Union-based SQLi:

' UNION SELECT null, null, @@version -- 

WAF Bypass Strategies

  1. Case Variation: If a filter looks for SELECT, using SeLeCt might bypass it.
  2. Whitespace Manipulation: Replacing spaces with comments (/**/) or other whitespace characters (%0a, %09).
SELECT/**/password/**/FROM/**/users
  1. Encoding: Using URL encoding, hex encoding, or HTML entities to obfuscate the payload.
  2. Boolean Equivalents: Replacing = with LIKE or <>.

Real Exploitation Examples

Let's consider a vulnerable PHP application:

$username = $_POST['user'];
$query = "SELECT * FROM users WHERE username = '$username'";
$result = mysqli_query($conn, $query);

An attacker submits the following input for $username: admin' --

The resulting query executed by the database becomes:

SELECT * FROM users WHERE username = 'admin' --'

The database treats everything after -- as a comment, effectively logging the attacker in as the admin user without evaluating the password condition (if one existed in a more complex query).

Prevention and Secure Coding Practices

The only reliable way to prevent SQL Injection is by separating the data from the code.

1. Parameterized Queries (Prepared Statements)

Parameterized queries ensure that user input is treated strictly as data, not as executable code. This is the primary defense against SQLi.

Example in PHP (PDO):

$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->execute(['username' => $username]);
$user = $stmt->fetch();

Example in Java (JDBC):

String query = "SELECT * FROM users WHERE username = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, username);
ResultSet results = pstmt.executeQuery();

2. Stored Procedures

Stored procedures can provide a similar level of protection to parameterized queries, provided the SQL inside the procedure does not dynamically construct queries using user input.

3. Input Validation

Implement strict allow-list input validation. Ensure that user input matches the expected format, type, and length before processing it.

4. Principle of Least Privilege

Configure database accounts with the minimum necessary privileges. A web application should never connect to the database as the sa or root user. If an application only needs to read data from a specific table, its database account should only have SELECT permissions on that table.

Further Reading and Internal Resources

To further understand how these vulnerabilities fit into broader security strategies, you should review our articles on: - XSS: Stored vs Reflected - Penetration Testing vs Security Audits

For a definitive list of known vulnerabilities, always refer to the NVD CVE Database.

Conclusion

SQL Injection is a critical vulnerability that demands serious attention from developers and security professionals alike. By understanding the mechanics of SQLi, studying real-world payloads, and consistently applying secure coding practices like parameterized queries, organizations can effectively eliminate this threat.

Need to secure your applications?

Contact Cayvora Security to schedule a comprehensive penetration test today.

📱 Chat with us on WhatsApp

Besoin d'un audit de sécurité ?

Contactez Cayvora pour une consultation gratuite et protégez votre entreprise contre les cybermenaces.

📱 Contacter via WhatsApp

Articles connexes