MSSQL Enumeration

Overview

Microsoft SQL (MSSQL) is Microsoft's SQL-based relational database management system. Unlike MySQL, which is open-source, MSSQL is closed source and was initially written to run on Windows operating systems. It is popular among database administrators and developers when building applications that run on Microsoft's .NET framework due to its strong native support for .NET.

Key Characteristics:

  • Port 1433: Default MSSQL port

  • Authentication: Windows Authentication or SQL Server Authentication

  • Default Instance: MSSQLSERVER

  • Protocol: Tabular Data Stream (TDS)

  • Platform: Primarily Windows (Linux/MacOS versions available)

MSSQL Clients

SQL Server Management Studio (SSMS)

SQL Server Management Studio (SSMS) comes as a feature that can be installed with the MSSQL install package or downloaded separately. Key points:

  • Commonly installed on the server for initial configuration

  • Can be installed on any system for remote database management

  • May contain saved credentials on vulnerable systems

  • Provides full database management capabilities

Alternative MSSQL Clients

Client
Description

mssql-cli

Command-line interface for MSSQL

SQL Server PowerShell

PowerShell module for MSSQL

HeidiSQL

Lightweight GUI client

SQLPro

Professional database client

Impacket's mssqlclient.py

Python-based client (preferred for pentesting)

Locating Impacket MSSQL Client

Default System Databases

MSSQL has default system databases that help understand the structure of all databases hosted on a target server:

Database
Description

master

Tracks all system information for an SQL server instance

model

Template database that acts as a structure for every new database created

msdb

Used by SQL Server Agent to schedule jobs & alerts

tempdb

Stores temporary objects

resource

Read-only database containing system objects included with SQL server

Default Configuration

Initial Setup

When an admin initially installs and configures MSSQL to be network accessible:

  • Service Account: SQL service runs as NT SERVICE\MSSQLSERVER

  • Authentication: Windows Authentication by default

  • Encryption: Not enforced by default

  • Access Control: Uses Windows OS for authentication processing

Authentication Methods

  1. Windows Authentication:

    • Uses local SAM database or domain controller

    • Integrates with Active Directory

    • Can lead to privilege escalation if compromised

  2. SQL Server Authentication:

    • Uses database-specific user accounts

    • Independent of Windows authentication

Dangerous Settings

Common misconfigurations that can lead to security issues:

Setting
Risk Level
Description

No encryption

High

MSSQL clients not using encryption to connect

Self-signed certificates

Medium

Can be spoofed during attacks

Named pipes enabled

Medium

Additional attack surface

Default SA credentials

Critical

Weak or unchanged SA account passwords

SA account enabled

High

Admins may forget to disable default SA account

Footprinting the Service

Comprehensive Nmap Scan

Example Nmap Output Analysis

Key Information Extracted:

  • Hostname: SQL-01

  • Instance: MSSQLSERVER

  • Version: Microsoft SQL Server 2019 RTM (15.00.2000.00)

  • Named Pipes: Enabled (\target\pipe\sql\query)

  • Clustering: Not clustered

Metasploit MSSQL Ping Scanner

Connecting with mssqlclient.py

Windows Authentication

Basic Database Enumeration

SQL Server Authentication

Advanced Enumeration

Database Information Gathering

System Information

HTB Academy Lab Questions

Question 1: Hostname Detection

Task: Enumerate the target and list the hostname of MSSQL server

Solution:

Question 2: Non-Default Database Discovery

Task: Connect using account (backdoor:Password1) and list non-default database

Solution:

Enumeration Techniques

1. Service Detection

2. Authentication Testing

3. Database Analysis

Security Assessment

Common Vulnerabilities

  1. Default Credentials: SA account with weak passwords

  2. Windows Authentication: Compromised domain accounts

  3. Missing Encryption: Plaintext communication

  4. Excessive Permissions: Over-privileged database users

  5. Outdated Software: Unpatched MSSQL instances

Enumeration Checklist

Attack Vectors

1. Credential-based Access

2. Command Execution

3. Data Extraction

Tools and Techniques

Essential Tools

Defensive Measures

Security Best Practices

  1. Disable SA account: Use Windows Authentication only

  2. Enable encryption: Force SSL/TLS connections

  3. Least privilege: Restrict database permissions

  4. Regular updates: Apply security patches

  5. Monitor access: Enable audit logging

  6. Network security: Firewall restrictions

Last updated