Deep Dive: SQL Injection Explained

Test yourself on Technical Attacks with exam style questions, marked instantly.

Flashcards that come back just before you would forget them.

A revision plan that remembers what you know and shows what to do next.

See which topics you have nailed and which need another look.

Quiz yourself on Technical AttacksGet your flashcardsBuild my revision planTrack my progress

Deep Dive: SQL Injection Explained

How SQL Injection Works

SQL injection exploits poorly coded websites that don't validate user input. Here's a step-by-step example:

Normal login process:

  • User enters username: alice
  • Website creates SQL query: SELECT * FROM users WHERE username='alice'
  • Database checks if 'alice' exists and returns the user record

SQL injection attack:

  • Attacker enters username: ' OR '1'='1
  • Website creates SQL query: SELECT * FROM users WHERE username='' OR '1'='1'
  • Since '1'='1' is ALWAYS true, this returns ALL users - bypassing authentication!
  • Attacker gains unauthorized access without knowing any passwords

Prevention methods:

  • Input validation: Check that inputs match expected format (e.g., usernames contain only letters/numbers)
  • Input sanitization: Remove or escape special characters like quotes
  • Parameterised queries: Separate SQL code from user data so injected code isn't executed
  • Prepared statements: Pre-compile SQL queries so user input can't alter query structure

Make this stick

Signing in gives access to all PrepWise features.

PrepWise is a trading name of A3M Quality Solutions Limited, registered in England and Wales, company number 14959172. Registered office: 202 Stroud Road, Gloucester, GL1 5LA. ICO registration ZC267069.