Serksa
All Concepts
Security

SQL Injection

1

What is it?

<strong>SQL injection</strong> is a security vulnerability where attackers insert malicious SQL code into your queries through user input, potentially exposing, modifying, or deleting your entire database.

2

Think of it like...

The Corrupted Order Analogy

Imagine a restaurant where you write your order. SQL injection is like writing 'Pizza AND give me all the money in the register'. If the kitchen blindly follows orders, disaster happens.

📝

Order Form (Input)

What user enters

😈

Malicious Order (SQL Injection)

Hidden commands

🏭

Kitchen (Database)

Executes commands

3

Visual Flow

😈Attacker

Malicious Input

💉SQL Injection

Injected Query

🗄️Database

Compromised

4

Where you see it

1

Vulnerable code

query = 'SELECT * FROM users WHERE id = ' + userInput

2

Attacker inputs

1 OR 1=1; DROP TABLE users;--

3

Query becomes

SELECT * FROM users WHERE id = 1 OR 1=1; DROP TABLE users;--

4

Database executes

Returns all users, then deletes the table!

5

Prevention: Use parameterized queries

query('SELECT * FROM users WHERE id = ?', [userInput])

5

Common Mistake

Wrong

"SQL injection only affects old websites"

Correct

<strong>SQL injection is still a top vulnerability</strong>. Even modern apps are vulnerable if developers don't use parameterized queries or ORMs properly.

💡 Real-World Example

Famous SQL injection:

1

Login form: username = admin' OR '1'='1

2

Query: SELECT * FROM users WHERE username='admin' OR '1'='1' AND password='...'

3

OR '1'='1' is always true → bypasses password check

4

Attacker logs in as admin without knowing password!