MSSQL Attacks
MSSQL Clients
SQL Server Management Studio (
SSMS)Many other clients can be used to access a database running on MSSQL. Including but not limited to:
MSSQL Databases
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
Footprinting the Service
NMAP MSSQL Script Scan
sudo nmap --script ms-sql-info,ms-sql-empty-password,ms-sql-xp-cmdshell,ms-sql-config,ms-sql-ntlm-info,ms-sql-tables,ms-sql-hasdbaccess,ms-sql-dac,ms-sql-dump-hashes --script-args mssql.instance-port=1433,mssql.username=sa,mssql.password=,mssql.instance-name=MSSQLSERVER -sV -p 1433 10.129.201.248MSSQL Ping in Metasploit
msf6 auxiliary(scanner/mssql/mssql_ping) > set rhosts 10.129.201.248Connecting with Mssqlclient.py
python3 mssqlclient.py [email protected] -windows-authLink 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
sqlcmdwe can use the parameters-y(SQLCMDMAXVARTYPEWIDTH) and-Y(SQLCMDMAXFIXEDTYPEWIDTH) for better looking output. Keep in mind it may affect performance.
Impersonate Existing Users with MSSQL
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
0indicates, we do not have the sysadmin role, but we can impersonate thesauser
Impersonating the SA User
MSSQL - Enable Ole Automation Procedures
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
MSSQL - Create a File
Read Local Files
By default,
MSSQLallows 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
Execute Commands
Command executionis 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.MSSQLhas a extended stored procedures called xp_cmdshell which allow us to execute system commands using SQL. Keep in mind the following aboutxp_cmdshell:xp_cmdshellis a powerful feature and disabled by default.xp_cmdshellcan be enabled and disabled by using the Policy-Based Management or by executing sp_configureThe Windows process spawned by
xp_cmdshellhas the same security rights as the SQL Server service accountxp_cmdshelloperates 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_cmdshellis not enabled, we can enable it, if we have the appropriate privileges, using the following command:
There are other methods to get command execution, such as adding extended stored procedures, CLR Assemblies, SQL Server Agent Jobs, and external scripts.
However, besides those methods there are also additional functionalities that can be used like the
xp_regwritecommand that is used to elevate privileges by creating new entries in the Windows registry
Last updated