Course contentsShow
← Computer Science
Lesson 2475 of 2,87255. Web SecurityFree lesson

Classic SQL Injection Example

Walking through a login bypass attack using ' OR '1'='1 to understand the basic exploitation mechanism.

Classic SQL Injection Example

You'll learn: How attackers exploit SQL injection with the infamous ' OR '1'='1 payload to bypass login authentication.

The Login Bypass Attack

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

SELECT * FROM users WHERE username = 'input_username' AND password = 'input_password'

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

The Attack Payload

Now, what if an attacker enters this username:

admin' OR '1'='1

And any password (or even the same payload). The resulting query becomes:

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

Because '1'='1' is always true, the OR operator makes the entire condition evaluate to true—regardless of whether the password matches. The query returns user records, and the attacker gains access without knowing any credentials.

Why This Works

The application naively inserts user input directly into the SQL string without validation or parameterization. The single quote (') breaks out of the string context, and the attacker injects their own SQL logic (OR '1'='1') that hijacks the query's intended behavior.

Think of it like a conversation where you ask someone to "Bring me the red box," but they respond, "I'll bring the red box OR any box"—suddenly they've changed the rule.

Key Takeaway: The classic ' OR '1'='1 SQL injection demonstrates how unsanitized user input can break query logic and bypass authentication by injecting always-true conditions that override intended access controls.