Loopus

Pro Content

This lesson requires Loopus Pro access. Upgrade to unlock all courses, labs, and challenges.

Web Application HackingSQL Injection

What is SQL Injection?

15 min
theory
+25 XP

Learning Objectives

  • Understand what SQL injection is and why it occurs
  • Learn how databases process queries
  • Recognize vulnerable code patterns

Understanding SQL Injection Fundamentals

SQL injection remains one of the most dangerous and prevalent web application vulnerabilities, consistently ranking at or near the top of security vulnerability lists. Understanding SQL injection begins with understanding how web applications interact with databases.

How Database Queries Work

When you log into a website or search for products in an online store, the web application needs to retrieve your information from a database. It does this by constructing SQL queries, which are commands written in Structured Query Language that tell the database what to find.

A simple login check might look like this in the application's code: the server takes your username and password from the login form and inserts them into a SQL query template. That query travels to the database, which searches for matching records and returns the results. If a match is found, you're logged in.

The problem arises when applications insert user input directly into SQL queries without proper safety measures. Users are supposed to enter a username like "john" and a password like "secret123." But what if they don't? What if instead of a username, they enter something that looks like SQL code?

The Mechanics of Injection

Consider a login query constructed like this: the application takes whatever username you enter and drops it directly into a string that becomes SELECT FROM users WHERE username='USER_INPUT'. If you enter "john" as your username, the query becomes SELECT FROM users WHERE username='john', which is exactly what the developer intended.

But if you enter ' OR '1'='1 as your username, something very different happens. The resulting query becomes SELECT * FROM users WHERE username=' OR '1'='1'. That OR condition is always true because 1 always equals 1, so the database returns all users regardless of the password. The attacker has broken out of the intended data context and started executing arbitrary SQL logic.

This is the essence of SQL injection: the attacker's input is interpreted as code rather than data. The single quote terminates the string that was supposed to contain the username, allowing the attacker to add their own SQL commands to the query.

Why This Vulnerability Persists

You might wonder why SQL injection still exists when its causes and solutions are well understood. The answer lies in the complexity of modern applications. Large applications make thousands of database queries, and each one is a potential injection point. Developers working under pressure make mistakes, and legacy code often contains vulnerabilities that would never pass modern code review.

Additionally, SQL injection takes many forms. While classic injection attacks are well known, newer techniques like time-based blind injection and second-order injection can bypass defenses that stop simpler attacks. Applications using stored procedures or multiple database backends create even more complexity.

Understanding SQL injection isn't just about knowing the basic attack string. it's about understanding query construction deeply enough to identify and exploit injection vulnerabilities in any context, even when the application doesn't return obvious error messages or directly display query results.

Answer the Questions0 / 4 completed

📚 KnowledgeQuestion 1

What string operation causes SQLi?

Format: *************(13 chars)
Exact match required
⌨️ Hands-OnQuestion 2

What character often tests for SQLi?

Format: *****(5 chars)
Exact match required
📚 KnowledgeQuestion 3

What SQLi type relies on true/false?

Format: *****(5 chars)
Exact match required
⌨️ Hands-OnQuestion 4

What clause is bypassed with OR 1=1?

Format: *****(5 chars)
Exact match required
Answer all questions correctly to unlock the next lesson

Interactive Sandbox

Loading sandbox...
Previous
Answer all questions to continue