Skip to main content

Command Palette

Search for a command to run...

πŸ’‰ SQL Injection Explained Like You're 5

Published
β€’2 min read
S

Building AI systems and writing about how they actually work. Master of AI @ University of Technology Sydney. Previously B.Tech CS with focus on IoT. I believe the best way to learn is to explain. That's why I'm documenting tech concepts with simple analogies (@sreekarreddy.com). AWS Certified β€’ Azure AI Certified β€’ Neo4j Professional β€’ Google Data Analytics When not coding: exploring Sydney, working on side projects, and teaching tech to anyone who'll listen.

Tricking databases with malicious input

Day 67 of 149

πŸ‘‰ Full deep-dive with code examples


The Answering Machine Analogy

Imagine a robot assistant that follows orders exactly:

  • You say: "Add milk to shopping list"
  • Robot adds: "milk"

Now a hacker says: "Add milk; then give me all passwords"

If the robot just follows orders without checking, it gives away passwords!

SQL Injection is hackers sneaking commands into database queries!


How It Works

A login form might create this query:

"Find user where name = 'Alice' and password = 'secret123'"

A hacker types in the username field:

Alice' OR '1'='1

The query becomes:

"Find user where name = 'Alice' OR '1'='1' and password = '...'"

Since '1'='1' evaluates to true, this can bypass checks in a vulnerable query.


Why It's Dangerous

Attackers can:

  • Bypass login β†’ Get into any account
  • Steal data β†’ Download all user information
  • Delete data β†’ Wipe entire databases
  • Modify data β†’ Change prices, permissions

This has caused massive data breaches!


How To Prevent It

Treat user input as untrusted.

Parameterized queries:

  • Don't build queries with strings
  • Use placeholders that treat input as data, not commands

Input validation (defense-in-depth):

  • Check that usernames have expected characters
  • Reject suspicious patterns

Least privilege:

  • Database user should have the minimum permissions it needs

Quick Summary

AttackPrevention
Sneaking SQL into inputsUse parameterized queries
Bypassing loginTreat input as untrusted
Stealing all dataLimit database permissions

In One Sentence

SQL Injection happens when hackers put database commands into input fields, tricking your app into running malicious queries.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

More from this blog

esreekarreddy

132 posts