Understanding time-based blind SQL injection

Tutorial 3 of 5

1. Introduction

1.1 Goal of the Tutorial

This tutorial aims to provide an understanding of Time-Based Blind SQL Injections, a subset of SQL Injection vulnerabilities that can affect web applications.

1.2 Learning Outcomes

By the end of this tutorial, you will be able to:

  • Understand what time-based blind SQL injection is
  • Identify potential vulnerabilities in your web application
  • Learn how to exploit and prevent time-based blind SQL injection

1.3 Prerequisites

Before proceeding with this tutorial, you should:

  • Have a basic understanding of SQL (Structured Query Language)
  • Have some familiarity with web development concepts.

2. Step-by-Step Guide

2.1 Understanding Time-Based Blind SQL Injection

A Time-Based Blind SQL Injection is a type of SQL Injection attack where an attacker can make queries to a database by forcing the application to wait for a specified amount of time, then return a result. The waiting time allows the attacker to infer if the payload used returned true or false, even though no data from the database is returned.

2.2 Concepts and Best Practices

  • Parameterized Queries: This is a best practice for preventing SQL Injection. It involves pre-compiling SQL statements so that user input is treated as a string literal instead of part of the SQL command.

  • Input Validation: This is another best practice where user inputs are checked against a set of rules before being processed.

3. Code Examples

3.1 Exploiting Time-Based Blind SQL Injection

If an application has a SQL Injection vulnerability, an attacker can inject a SQL command like the following:

SELECT * FROM users WHERE username='' OR SLEEP(5)=''; --'

In this scenario, if the application waits for 5 seconds before responding, this indicates that it is vulnerable to time-based blind SQL injection.

3.2 Preventing Time-Based Blind SQL Injection

To prevent SQL Injection, use parameterized queries. Here is an example using PHP and MySQLi:

$user = $_POST['user'];
$pass = $_POST['pass'];

$stmt = $conn->prepare('SELECT * FROM users WHERE username = ? AND password = ?');
$stmt->bind_param('ss', $user, $pass);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // process rows
}

In this code:

  • The prepare function is used to compile the SQL statement.
  • The bind_param function is used to bind the user inputs to the SQL statement.
  • The execute function is used to execute the prepared statement.

4. Summary

In this tutorial:

  • We learned about time-based blind SQL injection, a form of SQL injection where an attacker can infer if their payload was successful based on the response time of the application.
  • We explored how to exploit and prevent time-based blind SQL injection.
  • We learned about best practices such as parameterized queries and input validation.

To continue your learning, consider studying other types of SQL Injection attacks, as well as other web application vulnerabilities.

5. Practice Exercises

Exercise 1

Can you identify if the following PHP code is vulnerable to time-based blind SQL injection?

$user = $_POST['user'];
$pass = $_POST['pass'];

$sql = "SELECT * FROM users WHERE username = '$user' AND password = '$pass'";
$result = mysqli_query($conn, $sql);

Exercise 2

Rewrite the above PHP code snippet to use parameterized queries.

Solutions

  1. Yes, the code is vulnerable to time-based blind SQL injection. It directly includes user inputs in the SQL query.

  2. Here is the code rewritten to use parameterized queries:

$user = $_POST['user'];
$pass = $_POST['pass'];

$stmt = $conn->prepare('SELECT * FROM users WHERE username = ? AND password = ?');
$stmt->bind_param('ss', $user, $pass);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // process rows
}

Continue practicing by identifying vulnerable code snippets and rewriting them to use parameterized queries.