Blind SQL Injection

Overview

SQL is a standardized language for interacting with relational databases.

Top 5 Databases (Dec 2022)

Rank
Database

1

Oracle

2

MySQL

3

Microsoft SQL Server

4

PostgreSQL

5

IBM Db2

Note: This module focuses on Blind SQL Injection using MSSQL. Techniques can be adapted to other databases since SQL is standardized.


Interacting with MSSQL

SQLCMD (Windows, Command Line)

Microsoft's command-line tool for MSSQL.

Connection Syntax

sqlcmd -S 'SQL01' -U 'thomas' -P 'TopSecretPassword23!' -d bsqlintro -W
Flag
Description

-S

Server name

-U

Username

-P

Password

-d

Database

-W

Remove trailing spaces

Running Queries

Note: Type GO to execute the query batch.

Example Output

JOIN Query Example


Impacket-MSSQLClient (Linux, Command Line)

Part of Impacket toolset, preinstalled on many security distros.

Connection Syntax

Running Queries

Pentesting Features

Enable xp_cmdshell:


SQL Server Management Studio (Windows, GUI)

Microsoft's GUI tool for MSSQL administration.

Workflow

  1. Connect - Enter server name, authentication, credentials

  2. Browse - Open Databases β†’ [database] β†’ Tables

  3. Query - Right-click database β†’ New Query

  4. Execute - Click Execute button


Common Enumeration Queries

List Tables

List Columns

Example Column Output

Users table:

Posts table:


Complex Query Example

Requirements

Find password hash where:

  1. First name begins with 'S'

  2. Email > 20 characters

  3. Wrote post with title starting with 'N'

  4. Sorted by first name ascending

Query


Quick Reference

Connection Commands

Tool
Platform
Command

SQLCMD

Windows

sqlcmd -S <server> -U <user> -P <pass> -d <db>

Impacket

Linux

impacket-mssqlclient <user>:'<pass>'@<server> -db <db>

SSMS

Windows

GUI connection dialog

Useful SQL Syntax

Operation
Syntax

Top N rows

SELECT TOP N ...

String starts with

LIKE 'X%'

String length

LEN(column)

Join tables

JOIN table ON condition

Order results

ORDER BY column ASC/DESC


References

Last updated