githubEdit

MSSQL

MSSQL Database Authentication and Dumping

Commands:

1) Kerberos auth

impacket-mssqlclient -k DOMAIN.LOCAL -no-pass

2) Windows Authentication (Domain Credentials)

impacket-mssqlclient -windows-auth DOMAIN\\USERNAME@TARGET_IP

3) Local Authentication

impacket-mssqlclient USERNAME@TARGET_IP 

DATABASE COMMANDS

1) Enumerate all databases within MSSQL instance

SELECT name FROM sys.databases; 

2) Enumerate tables of the target database

SELECT TABLE_NAME,TABLE_SCHEMA FROM targetdb.INFORMATION_SCHEMA.TABLES; 

3) Dump all the contents of the target table of the target database

SELECT * FROM targetdb.dbo.targettable 

GIVE ACCESS TO A DATABASE WITH SYSADMIN PRIVILEGES

1) Upon login, check if the user is a sysadmin

If 1, the user is sysadmin.

2) Give user full access to target DB

3) Dump tables from target DB

LINKED DATABASES ABUSE

1) Check if our user has admin privileges to run commands with xp_cmdshell

OR

If it returns 1, the user has admin privileges.

2) Check if there are any linked servers on the current database. Isremote determines if it is linked or remote. If it is 1, it means it is linked, else is remote.

3) Check in whose context we are able to query the linked server

4) Check for sysadmin sa privileges

5) Check the remote DB server if it has more links

6) Nested queries. This use case means that we have encountered a circular link between database servers!

7) If we are sa, then we add a user with sysadmin privileges for us

COMMAND EXECUTION WITH XP_CMDSHELL

1) Enable command execution

2) Enable execution of external scripts written in R or python

3) Run commands using Python

MSSQL Tool: PowerUpSQL

Link: https://github.com/NetSPI/PowerUpSQL Cheatsheet: https://github.com/NetSPI/PowerUpSQL/wiki/PowerUpSQL-Cheat-Sheet

Import Module

Enumerating from the network without domain session

1) Get local MSSQL instance (if any)

2) If you don't have an AD account, you can try to find MSSQL scanning via UDP

First, you will need a list of hosts to scan

If you have some valid credentials and you have discovered valid MSSQL hosts you can try to login into them

The discovered MSSQL servers must be on the file: C:\temp\instances.txt

Enumerating from inside the domain

1) Get local MSSQL instance (if any)

2) #Get info about valid MSQL instances running in domain

This looks for SPNs that starts with MSSQL (not always is a MSSQL running instance)

3) Test connections with each one

4) Try to connect and obtain info from each MSSQL server (also useful to check connectivity)

5) Get DBs, test connections, and get info in oneliner

MSSQL Abuse

1) Perform an SQL query

2) Dump an instance (a lotof CVSs generated in current dir)

3) Search keywords in columns trying to access the MSSQL DBs. This won't use trusted SQL links

MSSQL RCE

1) It might be also possible to execute commands inside the MSSQL host

Invoke-SQLOSCmd automatically checks if xp_cmdshell is enabled and enables it if necessary

If a MSSQL instance is trusted (database link) by a different MSSQL instance. If the user has privileges over the trusted database, he is going to be able to use the trust relationship to execute queries also in the other instance. This trusts can be chained and at some point the user might be able to find some misconfigured database where he can execute commands.

TIP: The links between databases work even across forest trusts.

1) Look for MSSQL links of an accessible instance

2) Crawl trusted links, starting form the given one (the user being used by the MSSQL instance is also specified)

3) If you are sysadmin in some trusted link you can enable xp_cmdshell with:

4) Execute a query in all linked instances (try to execute commands), output should be in CustomQuery field

5) Obtain a shell

6) Check for possible vulnerabilities on an instance where you have access

7) Try to escalate privileges on an instance

Last updated