⚫MSSQL

Default system schemas/databases:

  • master - keeps the information for an instance of SQL Server.

  • msdb - used by SQL Server Agent.

  • model - a template database copied for each new database.

  • resource - a read-only database that keeps system objects visible in every database on the server in sys schema.

  • tempdb - keeps temporary objects for SQL queries.

Enumeration

Basic Nmap scan to grab the banner of the SQL server

  • 1433 - MSSQL

nmap -Pn -sVC -p 1433 IP

Misconfigurations

This can allow access to the service without credentials if anonymous access is enabled.

Other common problems with how privileges are set may enable us to perform an action such as:

  • Read or change the contents of the DB

  • Read or change server configuration

  • Execute commands

  • Read local files

  • Capture the local system hash

Reading the Database

With Windows authentication we need to specify the domain name or the hostname or our target otherwise it will assume SQL authentication and authenticate against the users in the SQL server.

If we want to target a local account we use

  • SERVERNAME\\accountname or .\\accountname

  • -h Cleaner output by disabling headers and footers

If using Windows sqlcmd we must type GO to run our query

We can also use Impacket's suite of tools

Command Execution

If we have appropriate privileges we can use the SQL database to execute system commands

XP_CMDSHELL

Allows us to execute system commands using SQL, disabled by default but can be enabled using the Policy-Based Management or executing sp_configure

We can run any system command meaning we can list directories and read files

xp_cmdshell "type C:\Users"

xp_cmdshell "type C:\Users\Administrator\Desktop\secret.txt"

Enable xp_cmdshell

Writing Local Files

We have to enable Ole Automation Procedures

Enable Ole Automation Procedures

Create a File

Read Local Files

Capture MSSQL Service Hash

We can attempt to steal the MSSQL service hash using xp_subdirs or xp_diretree stored procedures. They both use SMB protocols to retrieve a list of child directories under a parent directory in the file system.

To make these work start Responder or Impacket first

XP_DIRETREE Hash Stealing

XP_SUBDIRS Hash Stealing

Remember to use your attack IP

XP_SUBDIRS Hash Stealing Continues

Responder

Impacket

We now to try to crack the hash or attempt to "Pass the Hash"

Impersonate Existing Users

IMPERSONATE allows us to take on the permissions of another user or log in.

First, we must identify users that we can impersonate. By default sysadmin can impersonate anyone

Next, we check if the current user has the sysadmin role

If 0 is returned then we do not have the role but we can impersonate the sa role.

Communicate with Other Databases

We can use a Linked server to execute an SQL statement that includes tables in another instance of SQL.

This may allow us lateral movement to that database in that server. Admins can configure a linked server using credentials from the remote server. If the credentials have sufficient (sa)privileges then we may be able to execute commands in the remote SQL instance.

Remember we can pass any command into the EXECUTE

Last updated

Was this helpful?