Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Web attacks

Workbook · Medium · 3 hoursFree account

SQL Injection Fundamentals

How SQL injection works, how a tester finds it, and how a defender stops it — from database basics to parameterised queries.

  • sql-injection
  • sql
  • blind-sqli
  • out-of-band
  • database-fundamentals
On this page 6 sections
  1. ·About This Workbook
  2. ·Chapter 1 — How Databases and SQL Work
  3. Locked: Chapter 2 — Finding SQL Injection Points
  4. Locked: Chapter 3 — Exploiting SQL Injection
  5. Locked: Chapter 4 — Preventing SQL Injection
  6. Locked: Key terms

SQL injection is a web flaw that lets a user's input change the shape of a query, not just its value. The flaw sits where the application joins text and data — a single place that touches every table the database account can reach. This workbook builds the idea from the ground up. Chapter one shows how a relational database stores rows and answers a SELECT. Chapter two walks through the quote, boolean and timing probes a tester uses to find an injection point. Chapter three reads a second table through UNION SELECT, and the final chapter shuts the door with parameterised queries, input validation and least privilege.

What You'll Learn

  • Explain how a relational database stores and answers a query
  • Find a place where user input reaches a SQL query
  • Explain the difference between in-band, blind and out-of-band injection
  • Read an injected query and predict what the database returns
  • Fix a vulnerable query with a parameterised query
  • Explain why input validation and least privilege matter even after the fix

About This Workbook

SQL injection happens when user input changes the structure of a SQL query instead of only its data. This workbook builds the idea from the ground up: how a database stores data, how an application turns user input into a query, how a tester finds and reads an injection point, and how a developer closes it.

Every example in this workbook runs on your own machine against a small SQLite database that this workbook creates for you. No example reaches a real website, and none of the ideas here need a network. The worked examples use Python's built-in sqlite3 module, so you need only python3.

Authorised targets only. The commands in the matching labs — SQL Injection Deep Dive and Blind SQL Injection — run against a lab target that you build and own. Never run an injection probe against a system that you do not have written permission to test.

This workbook is theory and short worked examples. For step-by-step practice against a running lab target, use SQL Injection Deep Dive after Chapter 3, and Blind SQL Injection after Chapter 3 or 4.

Chapter 1 — How Databases and SQL Work

You will be able to:

  • Name the parts of a relational table
  • Read a SELECT query with a WHERE clause
  • Explain why string concatenation inside a query is dangerous

Explanation

A relational database stores data in tables. A table has rows (one record each) and columns (one named field each). Most tables have a primary key: a column whose value is unique for every row, so the database and the application can both name one exact row.

SQL (Structured Query Language) is how an application asks the database for data or changes it. The four verbs you meet most often:

KeywordJob
SELECTChoose which columns to return
WHEREChoose which rows to return
INSERTAdd a new row
UPDATEChange an existing row

A query reads as a sentence. SELECT name FROM products WHERE id = 2 means: "From the products table, give me the name column, but only for the row where id equals 2."

An application almost never writes a fixed query. It takes a value from the user, such as a product ID from a URL or a username from a form. It places that value inside the query text, and it must do so safely. If the application builds the query by joining strings together, with the user's value as one of the strings, user input can change the shape of the query, not just its value. That is the root cause of SQL injection. Chapter 4 shows the safe way to do this.

Worked example

This example creates a small products table and reads one row from it. Later chapters reuse this setup, so study it once here.

import sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price_usd INTEGER)")
cur.executemany(
    "INSERT INTO products (id, name, price_usd) VALUES (?, ?, ?)",
    [(1, "USB Cable", 8), (2, "Lab Badge", 15), (3, "Notebook", 5)],
)
con.commit()

cur.execute("SELECT id, name, price_usd FROM products WHERE id = 2")
for row in cur.fetchall():
    print(row)

Save this as ch1.py and run it.

python3 ch1.py

Example output:

(2, 'Lab Badge', 15)

The query asked for the row where id equals 2, and the database returned exactly one row: the tuple (2, 'Lab Badge', 15). Notice that the INSERT statement uses ? placeholders, not string concatenation. Chapter 4 explains why that detail matters for security, not only for style.

Practice questions

1. A table users has the columns id, email and created_at. Which column is most likely the primary key, and why?

Answer

id. A primary key must be unique for every row. An email could also be unique, but id is the usual choice because it never needs to change if the user updates their email.

2. Write, in plain words, what SELECT email FROM users WHERE id = 7 asks the database to do.

Answer

From the users table, return the email column, but only for the row where id equals 7.

3. An application builds this query by joining strings: "SELECT * FROM products WHERE id = " + user_input. What is risky about this, even before you think about attacks?

Answer

The query text and the user's value are the same string. The database cannot tell where the fixed query ends and the user's value begins. Anything the user types becomes part of the SQL, not only a value inside it.

Sign in to continue

4 more sections are for signed-in members. It is free: sign in with Google.

Related