MSSQL

MSSQL Services

Microsoft SQL Server (MSSQL) is a relational database management system developed by Microsoft. It's widely deployed in enterprise environments, making it a valuable target during penetration testing engagements. This guide covers essential techniques for enumerating, accessing, and exploiting MSSQL servers.

Protocol Overview

MSSQL primarily operates on:

  • TCP/1433: Default port for the SQL Server service

  • UDP/1434: SQL Server Browser service (helps clients find instance information)

  • TCP/2433: Used when MSSQL operates in "hidden" mode

Authentication Mechanisms

MSSQL supports two authentication modes:

Authentication Type
Description

Windows authentication mode (default)

Integrates with Windows/Active Directory security. Windows user and group accounts are trusted to log in to SQL Server.

Mixed mode

Supports both Windows authentication and SQL Server authentication (username/password pairs stored in SQL Server).

Enumeration Techniques

Port Scanning

# Basic scan
nmap -p 1433,1434 -sV 10.129.201.57

# Script scan
nmap -p 1433 --script ms-sql* 10.129.201.57

Example output:

Using crackmapexec

CrackMapExecarrow-up-right is excellent for MSSQL enumeration:

MSSQL Client Connections

Using sqlcmd

sqlcmd is Microsoft's command-line utility for SQL Server:

Command options:

  • -S: Server name

  • -U: Username

  • -P: Password

  • -y: SQLCMDMAXVARTYPEWIDTH (for better output formatting)

  • -Y: SQLCMDMAXFIXEDTYPEWIDTH (for better output formatting)

Using sqsh (Linux alternative)

Using Impacket's mssqlclient.py

When prompted, enter the password.

Default System Databases

MSSQL includes several system databases:

Database
Purpose

master

Stores system-level information for an SQL Server instance

msdb

Used by SQL Server Agent for scheduling alerts and jobs

model

Template database that's copied for each new database

resource

Read-only database containing system objects

tempdb

Holds temporary objects or intermediate result sets

Post-Authentication Enumeration

Once authenticated, explore the MSSQL environment:

Listing Databases

Example output:

Selecting a Database

Listing Tables

Querying Data

Command Execution through MSSQL

MSSQL provides several methods for executing system commands:

Using xp_cmdshell

Example output:

Other Command Execution Methods

  • Extended stored procedures: Adding custom procedures

  • CLR Assemblies: Using .NET code within SQL Server

  • SQL Server Agent Jobs: Scheduled tasks that can execute commands

  • External scripts: Running scripts in external languages (R, Python)

File System Access

Reading Files

Writing Files

Capturing MSSQL Service Account Hash

MSSQL servers can be tricked into authenticating to an attacker-controlled SMB server, revealing the service account's NTLMv2 hash:

To capture the hash:

User Impersonation

MSSQL allows users with the IMPERSONATE permission to take on the permissions of other users:

Linked Servers

Linked servers allow a SQL Server to connect to other database servers, potentially extending your attack surface:

MSSQL Penetration Testing Methodology

  1. Discovery: Identify MSSQL instances through port scanning

  2. Version enumeration: Determine SQL Server version

  3. Authentication testing: Test common credentials and authentication methods

  4. Privilege assessment: Determine the privileges of authenticated users

  5. Configuration review: Check for misconfigurations

  6. Data enumeration: Explore accessible databases and data

  7. Command execution testing: Test for xp_cmdshell and other methods

  8. Linked server testing: Identify and test linked servers

  9. Lateral movement: Use MSSQL as a pivot point to access other systems

Common Vulnerabilities

  1. Weak credentials: Default or weak passwords

  2. Excessive privileges: Users with unnecessary sysadmin role

  3. xp_cmdshell enabled: Allows command execution

  4. Unpatched instances: Missing security updates

  5. Insecure configuration: Improper service account privileges

  6. Linked server misconfigurations: Overly permissive links between servers

RDP Access Through MSSQL

If you've gained administrative access to a SQL Server, you might be able to enable RDP access:

Practical Attack Scenarios

Scenario 1: Initial Access via Weak Credentials

  1. Discover MSSQL server with port scanning

  2. Brute force sa account using common passwords

  3. Authenticate to the server

  4. Enable and execute xp_cmdshell

  5. Create a reverse shell

Scenario 2: Lateral Movement via Linked Servers

  1. Authenticate to a MSSQL server

  2. Discover linked servers

  3. Test for command execution on linked servers

  4. Extract credentials or create backdoors on linked servers

  5. Pivot to additional network segments

Scenario 3: Privilege Escalation via User Impersonation

  1. Authenticate with limited privileges

  2. Identify users that can be impersonated

  3. Impersonate a user with sysadmin role

  4. Execute privileged commands

  5. Establish persistence

Defensive Measures

When reporting MSSQL vulnerabilities, consider recommending:

  1. Use Windows Authentication: Avoid SQL authentication when possible

  2. Apply principle of least privilege: Limit sysadmin role

  3. Disable xp_cmdshell: Unless explicitly needed

  4. Implement network segmentation: Restrict access to SQL Servers

  5. Regular patching: Keep SQL Server updated

  6. Audit user activities: Enable SQL Server auditing

  7. Secure linked servers: Carefully control linked server configurations

  8. Use strong service account passwords: Prevent credential theft attacks

Practical MSSQL Testing Commands

Finding MSSQL Servers

Testing Multiple Credentials

Automating Command Execution

By understanding MSSQL services and their attack vectors, penetration testers can effectively identify vulnerabilities and provide valuable recommendations for securing these critical database systems.

Last updated