DEV Community

Cover image for One Quote Can Log You In as Admin: SQL Injection in PHP (and the Fix)
Muiduzzaman Lipu for Refactorly

Posted on

One Quote Can Log You In as Admin: SQL Injection in PHP (and the Fix)

One quote character in a login form can log an attacker in as your admin, without the password.

In this post I build a vulnerable PHP login, attack my own local test database with two classic payloads, and then fix it properly. Everything below was run on PHP 8.1 with PDO and an in-memory SQLite database. Only test this on code and data you own.

Video version:

Spot the bug

$email    = $_POST['email'];
$password = $_POST['password'];

$sql  = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";
$user = $pdo->query($sql)->fetch();

if ($user) { login($user); }
Enter fullscreen mode Exit fullscreen mode

The user's input is pasted directly into the SQL string. That means the user isn't just sending data, they're writing part of your query.

Attack 1: admin@site.com' --

Type this into the email field, and anything as the password:

email:    admin@site.com' --
password: wrong
Enter fullscreen mode Exit fullscreen mode

The query becomes:

SELECT * FROM users WHERE email = 'admin@site.com' --' AND password = 'wrong'
Enter fullscreen mode Exit fullscreen mode

The ' closes the email string, and -- starts a SQL comment, so the password check is commented out. Result from my test database:

rows: 1 first: admin@site.com
Enter fullscreen mode Exit fullscreen mode

Logged in as admin, with the wrong password.

Attack 2: ' OR '1'='1

SELECT * FROM users WHERE email = '' OR '1'='1'
Enter fullscreen mode Exit fullscreen mode

'1'='1' is always true, so the WHERE clause matches every row:

rows: 3        # all users in the table
Enter fullscreen mode Exit fullscreen mode

On a page that lists results, that's your whole users table on screen.

addslashes() is not the fix

It's tempting to "escape" quotes. On SQLite, addslashes() doesn't even match the database's escaping rules:

SQLSTATE[HY000]: General error: 1 unrecognized token: "\"
Enter fullscreen mode Exit fullscreen mode

Escaping depends on the database, the connection charset and the context. Don't hand-roll it.

The fix: prepared statements

$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$email]);
$user = $stmt->fetch();
Enter fullscreen mode Exit fullscreen mode

The SQL and the data are sent separately. The ? is a placeholder, and whatever the user types is treated as a value, never as SQL. A quote is just a character in an email address that doesn't exist.

Named placeholders work too: WHERE email = :email with execute(['email' => $email]).

Bonus: never compare passwords in SQL

The vulnerable query also had a second problem: it stored and compared plain-text passwords. Hash them with password_hash() and check them in PHP with password_verify():

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

if ($user && password_verify($password, $user['password_hash'])) {
    login($user);
}
Enter fullscreen mode Exit fullscreen mode

With the fixed code, both attacks fail, and a real login still works:

safe attack1: login failed
safe attack2: login failed
safe real:    admin@site.com
Enter fullscreen mode Exit fullscreen mode

password_hash() produces a salted bcrypt hash ($2y$10$…), so even if your database leaks, the passwords aren't sitting there in plain text.

Laravel is safe… until you write raw SQL

Eloquent and the query builder use bindings for you:

User::where('email', $email)->first();   // ✅ safe
Enter fullscreen mode Exit fullscreen mode

But the "raw" methods take your string as-is:

// ❌ vulnerable
User::whereRaw("email = '$email'")->first();
DB::select("SELECT * FROM users WHERE email = '$email'");

// ✅ safe: pass bindings
User::whereRaw('email = ?', [$email])->first();
DB::select('SELECT * FROM users WHERE email = ?', [$email]);
Enter fullscreen mode Exit fullscreen mode

Search your codebase for whereRaw(, DB::select(, DB::statement( and orderByRaw( with a $ inside the string.

Cheat sheet

  • Never put user input inside a SQL string.
  • Use $pdo->prepare() + execute([$value]).
  • addslashes() is not protection.
  • Passwords: password_hash() to store, password_verify() to check.
  • Laravel where() is safe; whereRaw / DB::select need ? bindings.

Go grep your code for "... '$ inside SQL strings. 👇


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)