MSSQL - Microsoft SQL Server
Related resources
Languages and dictionaries
Server | Dictionary |
---|---|
MySQL | MySQL payloads. |
MSSQL | MSSQL payloads. |
PostgreSQL | PostgreSQL payloads. |
Oracle | Oracle SQL payloads. |
SQLite | SQLite payloads. |
Cassandra | Cassandra payloads. |
Attack-based dictionaries
Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which may run either on the same computer or on another computer across a network. Wikipedia.
By default, MSSQL uses ports TCP/1433
and UDP/1434
. However, when MSSQL operates in a "hidden" mode, it uses the TCP/2433
port.
MSSQL Databases
MSSQL has default system databases that can help us understand the structure of all the databases that may be hosted on a target server.
Default System Database | Description |
---|---|
master |
Tracks all system information for an SQL server instance |
model |
Template database that acts as a structure for every new database created. Any setting changed in the model database will be reflected in any new database created after changes to the model database |
msdb |
The SQL Server Agent uses this database to schedule jobs & alerts |
tempdb |
Stores temporary objects |
resource |
Read-only database containing system objects included with SQL server |
Table source: System Databases Microsoft Doc and HTB Academy
Authentication Mechanisms
MSSQL supports two authentication modes, which means that users can be created in Windows or the SQL Server:
- 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.
MSSQL Clients
- SQL Server Management Studio (
SSMS
) comes as a feature that can be installed with the MSSQL install package or can be downloaded & installed separately - mssql-cli
- SQL Server PowerShell|
- HediSQL
- SQLPro
- Impacket's mssqlclient.py To locate it:
Of the MSSQL clients listed above, pentesters may find Impacket's mssqlclient.py to be the most useful due to SecureAuthCorp's Impacket project being present on many pentesting distributions at install.
Database configuration
When an admin initially installs and configures MSSQL to be network accessible, the SQL service will likely run as NT SERVICE\MSSQLSERVER
. Connecting from the client-side is possible through Windows Authentication, and by default, encryption is not enforced when attempting to connect.
Authentication being set to Windows Authentication
means that the underlying Windows OS will process the login request and use either the local SAM database or the domain controller (hosting Active Directory) before allowing connectivity to the database management system.
Misconfigurations to look at:
- MSSQL clients not using encryption to connect to the MSSQL server.
- The use of self-signed certificates when encryption is being used. It is possible to spoof self-signed certificates
- The use of named pipes
- Weak & default
sa
credentials. Admins may forget to disable this account.##
The SA password for SQL Server is the SQL Administrator account built into the program. The SA password is established during the installation of SQL Server.
Interact with MSSQL
From Linux
sqsh
If we are targetting MSSQL
from Linux, we can use sqsh
as an alternative to sqlcmd
: sqsh
mssqlclient.py from impacket
If we can guess or gain access to credentials, this allows us to remotely connect to the MSSQL server and start interacting with databases using T-SQL (Transact-SQL
). Authenticating with MSSQL will enable us to interact directly with databases through the SQL Database Engine. From Pwnbox or a personal attack host, we can use Impacket's mssqlclient.py to connect as seen in the output below. Once connected to the server, it may be good to get a lay of the land and list the databases present on the system.
From Windows
The sqlcmd
utility lets you enter Transact-SQL statements, system procedures, and script files through a variety of available modes:
- At the command prompt.
- In Query Editor in SQLCMD mode.
- In a Windows script file.
- In an operating system (Cmd.exe) job step of a SQL Server Agent job.
Careful. In some environments the command GO needs to be in lowercase.
GUI Application
mssql-cli, mssqlclient.py, dbeaver
SQL Server Management Studio or SSMS
Only in windows. Download, install, and connect to database.
dbeaver
dbeaver is a multi-platform database tool for Linux, macOS, and Windows that supports connecting to multiple database engines such as MSSQL, MySQL, PostgreSQL, among others, making it easy for us, as an attacker, to interact with common database servers.
mssqlclient.py from impacket
Alternatively, we can use the tool from Impacket with the name mssqlclient.py
.
If we can guess or gain access to credentials, this allows us to remotely connect to the MSSQL server and start interacting with databases using T-SQL (Transact-SQL
). Authenticating with MSSQL will enable us to interact directly with databases through the SQL Database Engine. From Pwnbox or a personal attack host, we can use Impacket's mssqlclient.py to connect as seen in the output below. Once connected to the server, it may be good to get a lay of the land and list the databases present on the system.
Basic commands
Also, you might be interested in executing a cmd shell using xp_cmdshell by reconfiguring sp_configure (see the section Executing cmd shell in a SQL command line
).
Write files using MSSQL
To write files using MSSQL, we need to enable Ole Automation Procedures, which requires admin privileges, and then execute some stored procedures to create the file:
Create files using MSSQL
Read files using MSSQL
Executing cmd shell in a SQL command line
Our goal can be to spawn a Windows command shell and pass in a string for execution. For that Microsoft SQL syntaxis has the command xp_cmdshell, that will allow us to use the SQL command line as a CLI.
Because malicious users sometimes attempt to elevate their privileges by using xp_cmdshell, xp_cmdshell is disabled by default. xp_cmdshell
can be enabled and disabled by using the Policy-Based Management or by executing sp_configure
sp_configure displays or changes global configuration settings for the current settings. This is how you may take advantage of it:
Note: The Windows process spawned by
xp_cmdshell
has the same security rights as the SQL Server service account
Now we can use the MSSQL terminal to execute commands:
There are other methods to get command execution, such as adding extended stored procedures, CLR Assemblies, SQL Server Agent Jobs, and external scripts.
Last update: 2025-01-18 Created: January 6, 2023 21:39:12