1433 - MSSQL
101
SQL Servers offer strong lateral movement opportunities, as domain users can be mapped to database roles, allowing privilege escalation or command execution across systems.
Authentication
Windows authentication mode
This is the default, often referred to as integrated security because the SQL Server security model is tightly integrated with Windows/Active Directory. Specific Windows user and group accounts are trusted to log in to SQL Server. Windows users who have already been authenticated do not have to present additional credentials.
Mixed mode
Mixed mode supports authentication by Windows/Active Directory accounts and SQL Server. Username and password pairs are maintained within SQL Server.
Schemas
In SQL Server, each table belongs to a specific schema (e.g., dbo, sales, hr, etc.). If you do not specify a schema, SQL Server assumes dbo by default.
SELECT * from flags;
// ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name '#flags'. (208) (SQLExecDirectW)")
# Specify the database context explicitly
SELECT * from app.dbo.flags;Below are the default MSSQL system schemas:
master
Keeps the information for an instance of SQL Server
msdb
Used by SQL Server Agent
model
Template database copied for each new database
resource
Read-only, keeps sys objects visible in every server database in sys schema
tempdb
Keeps temporary objects for SQL queries
Users
sa is the default administrator-level account in MSSQL.
Syntax
When querying from a CLI tool, a SQL statement must be submitted ending with ; followed by a GO on a separate line.
CLI Tools
Alternative to sqlcmd for Linux (-h disables headers & footers for a cleaner output):
If we define the domain or hostname, it will use Windows authentication. If we don't, it will assume SQL Authentication and authenticate against the users created in the SQL server.
Impacket script:
Native Windows tool:
We can use the params -y (SQLCMDMAXVARTYPEWIDTH) and -Y (SQLCMDMAXFIXEDTYPEWIDTH) for better looking output (may affect performance):
If we use sqlcmd, we will need to use GO after our query to execute the SQL syntax:
Attacks
Context Change
When executing OS-level commands via xp_cmdshell, the commands run in the security context of the SQL Server service account:
SQL Server supports executing external R or Python scripts through the sp_execute_external_script stored procedure. These scripts run under a separate runtime and can have different OS-level execution contexts, potentially with higher privileges:
SQLi
RCE
RCE via xp_cmdshell can also be achieved via nxc mssql.
In MSSQL, the xp_cmdshell function allows execution of system commands from SQL by passing a string to the command shell and returning the output as text rows. It operates synchronously—control returns only after the command completes—and runs with the same permissions as the SQL Server service account. Due to its power and potential for abuse, xp_cmdshell is disabled by default and can be enabled or disabled using Policy-Based Management or the sp_configure command. Functions like this, which can lead to remote code execution (RCE), are typically restricted to privileged users, and exploiting them via a SQL injection (SQLi) vulnerability usually requires the use of stacked queries.
The Linux version of MSSQL does not support xp_cmdshell.
Hash Capture
In MSSQL, it's possible to capture the SQL Server service account's NTLMv2 hash using the undocumented stored procedures xp_dirtree or xp_subdirs, which leverage the SMB protocol to list directories. By pointing one of these procedures to an attacker-controlled SMB server, the server running SQL Server is tricked into authenticating and sends its NTLMv2 hash. To perform this, tools like responder or impacket-smbserver must be started first, followed by executing a crafted query that triggers the SMB request.
If the service account has access to our server, we will obtain its hash:
We can then attempt to crack it or relay it to another host.
User Impersonation
SQL Server includes a special permission called IMPERSONATE, which allows a user to assume the permissions of another user or login until the session ends or the context is reset. While sysadmins can impersonate any user by default, non-admin users require explicit permission to do so. To identify which users can be impersonated, a specific query can be run to check assigned impersonation rights:
To get an idea of privilege escalation possibilities, check if the current user has the sysadmin role:
As the return value 0 indicates, we do not have the sysadmin role, but we can impersonate the sa user. To impersonate a user, we can use the Transact-SQL statement EXECUTE AS LOGIN and set it to the user we want to impersonate:
We can now execute any command as a sysadmin. To revert the operation and return to our previous user, we can use the Transact-SQL statement REVERT.
If we find a user who is not
sysadmin, we can still check if the user has access to other databases or linked servers.
Linked Servers
MSSQL supports a configuration option called linked servers, which allows the database engine to run Transact-SQL queries across different SQL Server instances or even other database systems like Oracle.
If an attacker gains access to a SQL Server with linked servers configured, this can potentially be used for lateral movement. Admins may configure linked servers using credentials from the remote server, and if those credentials have sysadmin privileges, it may be possible to execute commands on the remote SQL instance.
Database links work even across forest trusts; they are direct server connections, so they have no boundaries!

Identify linked servers from the current server (1 → remote server, 0 → linked server):
Enumerate the user used for the connection and their privileges. The EXECUTE statement can be used to send pass-through commands to linked servers. The target command is added between parenthesis and the linked server is specified between square brackets:
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 multiple commands at once we can divide them up with a ;.
The executed queries on the linked server will run as sysadmin. Next, database data can be read from any database or system commands can be executed with xp_cmdshell. Sometimes linked servers are cyclical, e.g. DB_CONFIG is a linked server for DB_PUBLIC and vice versa:

In this case, the account on the linked server might have elevated privileges on our current server:
Based on the above output, internal_user has sa rights on DB_PUBLIC. This can be leveraged for RCE directy using nested queries:
...or by first adding a new admin user on DB_PUBLIC and then using that account:
For RCE, either xp_cmdshell should be already enabled or if rpcout is enabled (disabled by default), xp_cmdshell can be enabled manually:
PowerUpSQL is a PowerShell toolkit for discovering, auditing, and attacking SQL Servers in Active Directory environments during internal pentests and red team operations.
Enumerate MSSQL servers, access, and server information:
Enumerate linked servers:
For RCE, either xp_cmdshell should be already enabled or if rpcout is enabled (disabled by default), xp_cmdshell can be enabled manually:
The Query and QueryTarget parameters can be used. Without the latter, it will try to use xp_cmdshell on every link of the chain:
Write Files
To write file in MSSQL, we need to enable Ole Automation Procedures, which requires admin rights, and then execute some stored procedures to create the file:
Read Files
By default, MSSQL allows file read on any file in the OS to which the account has read access:
To Be Added
https://github.com/quentinhardy/msdat
Resources
Last updated
Was this helpful?