Web Security / SQL Injection

Understanding time-based blind SQL injection

A tutorial about Understanding time-based blind SQL injection

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

A code injection technique that attackers can use to exploit vulnerabilities in a web application's database layer.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help