MSSQL Attacks
Last updated
Last updated
(SSMS
)
Many other clients can be used to access a database running on MSSQL. Including but not limited to:
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
NMAP MSSQL Script Scan
MSSQL Ping in Metasploit
Connecting with Mssqlclient.py
Link to commands
https://learn.microsoft.com/en-us/sql/relational-databases/databases/view-a-list-of-databases-on-an-instance-of-sql-server?view=sql-server-ver16
Connecting with sqlcmd
Note: When we authenticate to MSSQL using sqlcmd
we can use the parameters -y
(SQLCMDMAXVARTYPEWIDTH) and -Y
(SQLCMDMAXFIXEDTYPEWIDTH) for better looking output. Keep in mind it may affect performance.
SQL Server has a special permission, named IMPERSONATE
, that allows the executing user to take on the permissions of another user or login until the context is reset or the session ends.
Identify Users that We Can Impersonate
Verifying our Current User and Role
As the returned value 0
indicates, we do not have the sysadmin role, but we can impersonate the sa
user
Impersonating the SA User
MSSQL - Create a File
By default, MSSQL
allows file read on any file in the operating system to which the account has read access. We can use the following SQL query:
Read Local Files in MSSQL
Command execution
is one of the most desired capabilities when attacking common services because it allows us to control the operating system. If we have the appropriate privileges, we can use the SQL database to execute system commands or create the necessary elements to do it.
The Windows process spawned by xp_cmdshell
has the same security rights as the SQL Server service account
xp_cmdshell
operates synchronously. Control is not returned to the caller until the command-shell command is completed
To execute commands using SQL syntax on MSSQL, use:
XP_CMDSHELL
If xp_cmdshell
is not enabled, we can enable it, if we have the appropriate privileges, using the following command:
However, besides those methods there are also additional functionalities that can be used like the xp_regwrite
command that is used to elevate privileges by creating new entries in the Windows registry
To write files using MSSQL
, we need to enable , which requires admin privileges, and then execute some stored procedures to create the file
MSSQL
has a called which allow us to execute system commands using SQL. Keep in mind the following about xp_cmdshell
:
xp_cmdshell
is a powerful feature and disabled by default. xp_cmdshell
can be enabled and disabled by using the or by executing
There are other methods to get command execution, such as adding , , , and .