MODULO SQL & Database Design Master SeriesLAB

Locking Down Your Data: Mastering Encryption and SQL Injection Defense

Introduction

In today's digital landscape, data security is more crucial than ever. Two critical aspects of database security are encryption and SQL injection defense. In this masterclass, we'll delve into the world of encryption and SQL injection, exploring the concepts, techniques, and best practices to protect your data from unauthorized access.

Encryption

Encryption is the process of converting plaintext data into unreadable ciphertext to prevent unauthorized access. In the context of databases, encryption can be applied at various levels:

Column-Level Encryption

Column-level encryption involves encrypting specific columns or fields within a table. This approach is useful when you need to protect sensitive data, such as passwords, credit card numbers, or personal identifiable information (PII).

Example: Column-Level Encryption using SQL Server

CREATE TABLE customers (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(100),
    password VARBINARY(128) -- encrypted column
);

-- insert encrypted data
INSERT INTO customers (name, email, password)
VALUES ('John Doe', 'john.doe@example.com', ENCRYPTBYPASSPHRASE('my_secret_key', 'my_password'));

-- decrypt data
SELECT CONVERT(VARCHAR(50), DECRYPTBYPASSPHRASE('my_secret_key', password)) AS decrypted_password
FROM customers;

Table-Level Encryption

Table-level encryption involves encrypting entire tables or datasets. This approach is useful when you need to protect sensitive data that is scattered across multiple columns.

Example: Table-Level Encryption using MySQL

CREATE TABLE customers (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(100),
    password VARCHAR(128)
) ENGINE=InnoDB ENCRYPTION='Y';

-- insert encrypted data
INSERT INTO customers (name, email, password)
VALUES ('John Doe', 'john.doe@example.com', 'my_password');

Database-Level Encryption

Database-level encryption involves encrypting the entire database, including all tables, indexes, and logs. This approach is useful when you need to protect sensitive data at rest.

Example: Database-Level Encryption using PostgreSQL

CREATE DATABASE mydatabase WITH ENCRYPTION 'AES-256-CBC';

-- create table and insert encrypted data
CREATE TABLE customers (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(100),
    password VARCHAR(128)
);

INSERT INTO customers (name, email, password)
VALUES ('John Doe', 'john.doe@example.com', 'my_password');

SQL Injection

SQL injection is a type of attack where an attacker injects malicious SQL code into a web application's database in order to extract or modify sensitive data. There are several types of SQL injection attacks:

Classic SQL Injection

Classic SQL injection involves injecting malicious SQL code as user input.

Example: Classic SQL Injection

-- vulnerable code
SELECT * FROM customers WHERE name = '$username';

-- attacker input
$username = "Robert'); DROP TABLE customers; --";

-- resulting query
SELECT * FROM customers WHERE name = 'Robert'); DROP TABLE customers; --';

Blind SQL Injection

Blind SQL injection involves injecting malicious SQL code without seeing the database error messages.

Example: Blind SQL Injection

-- vulnerable code
SELECT * FROM customers WHERE name = '$username';

-- attacker input
$username = "Robert' AND (SELECT COUNT(*) FROM customers) > 0; --";

-- resulting query
SELECT * FROM customers WHERE name = 'Robert' AND (SELECT COUNT(*) FROM customers) > 0; --';

Time-Based SQL Injection

Time-based SQL injection involves injecting malicious SQL code that affects the database response time.

Example: Time-Based SQL Injection

-- vulnerable code
SELECT * FROM customers WHERE name = '$username';

-- attacker input
$username = "Robert'); SELECT SLEEP(5); --";

-- resulting query
SELECT * FROM customers WHERE name = 'Robert'); SELECT SLEEP(5); --';

Defense Against SQL Injection

To defend against SQL injection attacks, follow these best practices:

  • Use Prepared Statements: Prepared statements separate the SQL code from the user input, making it impossible for an attacker to inject malicious SQL code.
  • Use Parameterized Queries: Parameterized queries use placeholders for user input, making it impossible for an attacker to inject malicious SQL code.
  • Escape User Input: Escaping user input involves replacing special characters with their escaped equivalents, making it difficult for an attacker to inject malicious SQL code.
  • Limit Database Privileges: Limiting database privileges involves restricting the database user's privileges to the minimum required, making it difficult for an attacker to exploit vulnerabilities.

Example: Prepared Statement using MySQL

PREPARE stmt FROM 'SELECT * FROM customers WHERE name = ?';
SET @username = 'Robert';
EXECUTE stmt USING @username;

Example: Parameterized Query using PostgreSQL

PREPARE stmt(text) AS SELECT * FROM customers WHERE name = $1;
EXECUTE stmt('Robert');

Example: Escaping User Input using PHP

$username = mysqli_real_escape_string($conn, $_POST['username']);
$query = "SELECT * FROM customers WHERE name = '$username'";

Conclusion

In conclusion, encryption and SQL injection defense are critical aspects of database security. By mastering these concepts and techniques, you can protect your data from unauthorized access and ensure the integrity of your database.

⚔️ YOUR MISSION

Mission:

You are a database administrator tasked with securing a database containing sensitive customer information. Your mission is to:

  1. Encrypt the password column in the customers table using column-level encryption.
  2. Implement prepared statements to prevent SQL injection attacks on the customers table.
  3. Limit the database privileges of the db_user account to the minimum required.

Deliverables:

  • A SQL script that encrypts the password column in the customers table.
  • A SQL script that implements prepared statements for the customers table.
  • A SQL script that limits the database privileges of the db_user account.

Constraints:

  • Use a database management system of your choice (e.g., MySQL, PostgreSQL, SQL Server).
  • Use a programming language of your choice (e.g., Python, Java, C#).
  • Ensure that your solution is secure and follows best practices.
SQL practice editor
Read the exercise prompt

Initial Query:

CREATE TABLE customers (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(100),
    password VARCHAR(128)
);

INSERT INTO customers (name, email, password)
VALUES ('John Doe', 'john.doe@example.com', 'my_password');
WAITING FOR SIGNAL...
BACKNEXT STAGE