For the complete documentation index, see llms.txt. This page is also available as Markdown.

Pentesting MsSql

Scanning

nmap --script ms-sql-info,ms-sql-empty-password,ms-sql-xp-cmdshell,ms-sql-config,ms-sql-ntlm-info,ms-sql-tables,ms-sql-hasdbaccess,ms-sql-dac,ms-sql-dump-hashes --script-args mssql.instance-port=1433,mssql.username=sa,mssql.password=,mssql.instance-name=MSSQLSERVER -sV -p 1433 intranet.poo

mssqlclient.py

  • Use impacket mssqlclient.py to connect

python mssqlclient.py ARCHETYPE/sql_svc@10.129.62.77 -windows-auth
mssqlclient.py -db POO_PUBLIC external_user:'password123'@10.13.38.11

# Newer impacket naming
impacket-mssqlclient <domain>/<user>:'<pass>'@<ip> -windows-auth

With a forged MSSQL silver ticket, use Kerberos auth and make the hostname match the SPN used in the ticket:

export KRB5CCNAME=Administrator.ccache
impacket-mssqlclient DOMAIN/Administrator@SQL_HOST_FQDN -target-ip 127.0.0.1 -windows-auth -k -no-pass
impacket-mssqlclient nagoya-industries.com/Administrator@nagoya.nagoya-industries.com -target-ip 127.0.0.1 -windows-auth -k -no-pass

See Silver Ticket for the exact-SPN ticketer flow.

Quick Module Shortcuts (impacket-mssqlclient)

# Enable xp_cmdshell in one command
enable_xp_cmdshell
xp_cmdshell whoami
  • https://book.hacktricks.xyz/pentesting/pentesting-mssql-microsoft-sql-server

  • Check what is the role we have in the server

  • If the output is 1 , it translates to True .

  • Check to see if xp_cmdshell is enabled

  • Set up the command execution through the xp_cmdshell:

  • First as it’s disabled by default:

  • Now we are able to execute system commands:

  • Better Command Execution

  • Get a shell on target with nc or msfvenom

  • Find the admin password from the shell

mssqliclient modules

  • enumerate the db

  • enumerate the server links

  • enumerate any ability to impersonate a user

  • enumerate owners of the database

  • Attempt to download a remote file off the host

MSSQLPWNER

  • This is a great automated tool to help enumeration / access for mssql

  • https://github.com/ScorpionesLabs/MSSqlPwner.git

  • To perform automated general enumeration

  • To go interactive

  • attempt command execution

  • see linked servers

  • attempt ntlm relay oppertunities

msdat

  • check the output carefully and KO and OK can look very similiar

Linked Server Exploitation

  • Sometimes your user might not have sysadmin on the linked server. Additionally the user you are utilizing to run code on the linked server might not have sysadmin permissions on that linked server. However that user on the linked server might have SA permissions on your server. Im going to call this backward linked server

  • create new sa user on the server you have access to using this backward link attack

MSSQL Raw Commands

System Databases

Database
Description

master

Tracks all system info for SQL Server instance

model

Template for every new database

msdb

SQL Server Agent scheduling jobs and alerts

tempdb

Storage for temporary objects

resource

Read-only database with system objects included with SQL Server

Default Configuration

  • Default service account: NT SERVICE\MSSQLSERVER

  • Clients may not use encryption

  • Self-signed certificates can be spoofed

  • Named pipes may be enabled

  • Weak or default sa credentials are common

Metasploit MSSQL Ping

SQL Queries


Additional MSSQL Clients

sqsh (Linux)

With formatting:

Windows Authentication (local account — note the .\\ prefix):

sqlcmd (Windows)

With output formatting:

dbeaver (GUI Client)


MySQL vs MSSQL Syntax Comparison

Operation
MySQL
MSSQL

Show databases

SHOW DATABASES;

SELECT name FROM master.dbo.sysdatabases GO

Select database

USE htbusers;

USE htbusers GO

Show tables

SHOW TABLES;

SELECT table_name FROM htbusers.INFORMATION_SCHEMA.TABLES GO

Select all rows

SELECT * FROM users;

SELECT * FROM users go


Write Local Files (Ole Automation Procedures)

Enable Ole Automation:

Write a webshell:


Read Local Files (OPENROWSET)


Capture MSSQL Service Hash

Force the MSSQL service to authenticate to an attacker-controlled SMB share to capture its NTLMv2 hash.

Start Responder or impacket-smbserver first to catch the hash:

Then trigger the authentication from the MSSQL session:

Using xp_dirtree:

Using xp_subdirs:


MSSQL Impersonation

Identify users that can be impersonated:

Verify current user and role:

Impersonate sa:

Revert with REVERT.

Last updated