Introduction
SQL injection is a way bad people trick databases to run commands they should not. It helps us understand how to keep data safe.
SELECT * FROM users WHERE username = 'user_input';
SELECT * FROM users WHERE username = 'admin';
SELECT * FROM users WHERE username = '' OR '1'='1'; --
SELECT * FROM users WHERE username = 'admin'; DROP TABLE users; --
CREATE TABLE users (id INT, username VARCHAR(20)); INSERT INTO users VALUES (1, 'admin'), (2, 'guest'); -- Unsafe query with user input '' OR '1'='1 SELECT * FROM users WHERE username = '' OR '1'='1';
SELECT * FROM users WHERE username = '" + userInput + "';SELECT * FROM users WHERE username = '" + userInput + "';admin' OR '1'='1, what will the query become and what is the likely result?query = "SELECT * FROM users WHERE username = '" + userInput + "';"SELECT * FROM users WHERE username = '" + userInput + "' AND password = '" + passInput + "';admin' -- as username and anything as password, what happens and why?Pass the test and this pattern moves unknown