Course contentsShow
← Security
Lesson 563 of 3,05213. Web Security - Injection AttacksFree lesson

Classic SQL Injection Example

Walkthrough of a login bypass using ' OR '1'='1 to understand basic SQL injection mechanics.

Classic SQL Injection Example

What you'll learn: How attackers use the infamous ' OR '1'='1 string to bypass login authentication and manipulate SQL queries.

The Vulnerable Login

Imagine a website login form where you enter a username and password. Behind the scenes, the application might build a SQL query like this:

SELECT * FROM users WHERE username = 'alice' AND password = 'secret123'

If the query returns a user record, you're logged in. If not, access denied.

The Attack in Action

Now, what if an attacker enters this as the username:

' OR '1'='1

And anything (or nothing) as the password? The resulting SQL query becomes:

SELECT * FROM users WHERE username = '' OR '1'='1' AND password = 'anything'

The magic happens because '1'='1' is always true. The OR operator means if either condition is true, the whole WHERE clause succeeds. The attacker just injected SQL code (the OR '1'='1 part) into the query, breaking out of the intended string comparison.

Why This Works

The single quote (') closes the username string prematurely. Then the attacker adds their own SQL logic (OR '1'='1). The database executes this modified query, which always returns all user records instead of checking credentials properly—often logging the attacker in as the first user in the database (frequently an admin).

Key Takeaway: Classic SQL injection exploits occur when user input is directly concatenated into SQL queries without validation or parameterization, allowing attackers to inject malicious SQL fragments that alter the query's logic and bypass authentication or extract sensitive data.