Mssql Injection

MSSQL Injection is a type of security vulnerability that can occur when an attacker can insert or "inject" malicious SQL code into a query executed by a Microsoft SQL Server (MSSQL) database. This typically happens when user inputs are directly included in SQL queries without proper sanitization or parameterization. SQL Injection can lead to serious consequences such as unauthorized data access, data manipulation, and even gaining control over the database server.

Summary

MSSQL Default Databases

Name
Description

pubs

Not available on MSSQL 2005

model

Available in all versions

msdb

Available in all versions

tempdb

Available in all versions

northwind

Available in all versions

information_schema

Available from MSSQL 2000 and higher

MSSQL Comments

Type
Description

/* MSSQL Comment */

C-style comment

--

SQL comment

;%00

Null byte

MSSQL Enumeration

Description
SQL Query

DBMS version

SELECT @@version

Database name

SELECT DB_NAME()

Database schema

SELECT SCHEMA_NAME()

Hostname

SELECT HOST_NAME()

Hostname

SELECT @@hostname

Hostname

SELECT @@SERVERNAME

Hostname

SELECT SERVERPROPERTY('productversion')

Hostname

SELECT SERVERPROPERTY('productlevel')

Hostname

SELECT SERVERPROPERTY('edition')

User

SELECT CURRENT_USER

User

SELECT user_name();

User

SELECT system_user;

User

SELECT user;

MSSQL List Databases

MSSQL List Tables

MSSQL List Columns

MSSQL Union Based

  • Extract databases names

  • Extract tables from Injection database

  • Extract columns for the table Users

  • Finally extract the data

MSSQL Error Based

Name
Payload

CONVERT

AND 1337=CONVERT(INT,(SELECT '~'+(SELECT @@version)+'~')) -- -

IN

AND 1337 IN (SELECT ('~'+(SELECT @@version)+'~')) -- -

EQUAL

AND 1337=CONCAT('~',(SELECT @@version),'~') -- -

CAST

CAST((SELECT @@version) AS INT)

  • For integer inputs

  • For string inputs

MSSQL Blind Based

MSSQL Blind With Substring Equivalent

Function
Example

SUBSTRING

SUBSTRING('foobar', <START>, <LENGTH>)

Examples:

MSSQL Time Based

In a time-based blind SQL injection attack, an attacker injects a payload that uses WAITFOR DELAY to make the database pause for a certain period. The attacker then observes the response time to infer whether the injected payload executed successfully or not.

MSSQL Stacked Query

  • Stacked query without any statement terminator

  • Use a semi-colon ";" to add another query

MSSQL File Manipulation

MSSQL Read File

Permissions: The BULK option requires the ADMINISTER BULK OPERATIONS or the ADMINISTER DATABASE BULK OPERATIONS permission.

Example:

MSSQL Write File

MSSQL Command Execution

XP_CMDSHELL

xp_cmdshell is a system stored procedure in Microsoft SQL Server that allows you to run operating system commands directly from within T-SQL (Transact-SQL).

If you need to reactivate xp_cmdshell, it is disabled by default in SQL Server 2005.

Python Script

Executed by a different user than the one using xp_cmdshell to execute commands

MSSQL Out of Band

MSSQL DNS exfiltration

Technique from @ptswarmarrow-up-right

  • Permission: Requires VIEW SERVER STATE permission on the server.

  • Permission: Requires the CONTROL SERVER permission.

MSSQL UNC Path

MSSQL supports stacked queries so we can create a variable pointing to our IP address then use the xp_dirtree function to list the files in our SMB share and grab the NTLMv2 hash.

The links between databases work even across forest trusts.

Manual exploitation

MSSQL Privileges

MSSQL List Permissions

  • Listing effective permissions of current user on the server.

  • Listing effective permissions of current user on the database.

  • Listing effective permissions of current user on a view.

  • Check if current user is a member of the specified server role.

MSSQL Make User DBA

MSSQL Database Credentials

  • MSSQL 2000: Hashcat mode 131: 0x01002702560500000000000000000000000000000000000000008db43dd9b1972a636ad0c7d4b8c515cb8ce46578

  • MSSQL 2005: Hashcat mode 132: 0x010018102152f8f28c8499d8ef263c53f8be369d799f931b2fbe

MSSQL OPSEC

Use SP_PASSWORD in a query to hide from the logs like : ' AND 1=1--sp_password

References

Last updated