Course contentsShow
← Software Engineering
Lesson 1054 of 1,93332. Security EngineeringFree lesson

SQL Injection Fundamentals

How attackers manipulate SQL queries through user input to access or modify database data without authorization.

SQL Injection Fundamentals

You'll learn how attackers manipulate SQL queries through unsanitized user input to gain unauthorized database access.

The Core Vulnerability

SQL injection occurs when an application constructs database queries by directly concatenating user input into SQL strings without proper validation or sanitization. Instead of treating user input as pure data, the database interprets it as executable SQL code.

Think of it like filling out a form where you're asked for your name, but instead of writing "John," you write instructions that change what the form does entirely—and the system actually follows those instructions.

How It Works

Imagine a login query built like this:

"SELECT * FROM users WHERE username = '" + userInput + "' AND password = '" + passwordInput + "'"

If an attacker enters admin' -- as the username, the query becomes:

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

The -- comments out the password check. The attacker just logged in as admin without knowing the password.

More dangerous payloads can extract data, modify records, or even delete entire tables:

  • '; DROP TABLE users; --
  • ' OR '1'='1
  • ' UNION SELECT credit_card FROM payments --

Why It Happens

SQL injection exploits a fundamental trust boundary violation: treating untrusted user input as trusted SQL code. The application fails to distinguish between instructions and data.

This vulnerability directly violates the "Trust Boundaries and Input Validation" principle—all external input must be validated and sanitized before use.

Key Takeaway: SQL injection turns user input fields into command injection points by failing to separate SQL code from data, allowing attackers to execute arbitrary database operations through string manipulation.