MS-SQL (Microsoft SQL Service)
What is MS SQL?
MS SQL, also known as Microsoft SQL Server, is a relational database management system (RDBMS) developed by Microsoft.
Default MS-SQL System Tables
master Database: Records all the system-level information for an instance of SQL Server.
msdb Database: used by SQL Server Agent for scheduling alerts and jobs.
model Database: used as the template for all databases created on the instance of SQL Server. Modifications made to the model database, such as database size, collation, recovery model, and other database options, are applied to any databases created afterwards.
Resource Database: a read-only database that contains system objects that are included with SQL Server. System objects are physically persisted in the Resource database, but they logically appear in the sys schema of every database.
tempdb Database : a work-space for holding temporary objects or intermediate result sets.
Runs on TCP port 1433 and UDP port 1434
Enumeration
Using Impacket
Using impacket-mssqlclient we can connect to the MS-SQL service using the username/user-id and password from a possible publicly available production .dtsConfig
files.
Check user access control
If it returns 1, it is True and the user can run cmd
commands.
Exploitation
Command execution via xp_cmdshell()
Once we have access to the SQL database we can check if we can use the xp_cmdshell() command using:
Successful execution would produce an output similar to this:
However, if we get the following ERROR, we don't have access to the command and we need to reconfigure the service:
Alternatively, you can check it using the following query/command.
It would give an output like the one shown below. Here you can find the configuration values of other options/commands as well.
Reconfiguration steps
One line command (impacket-mssqlclient)
The simple one-liner in impacket-mssqlclient can do the honours for us.... :)
Alternative
If the sp_configure command is set to show advanced options we can just directly set the xp_cmdshell option (aka directly skip to step 5). The best way to know this is to run the command in step 5.
If we get the error shown below:
Then we have to set the show advanced options to TRUE by using the command:
Reconfigure using the RECONFIGURE command.
Check if sp_configure is working with advanced options
Allow
xp_cmdshell()
using the command:
Run reconfigure again to allow the xp_cmdshell method. Test if it's working or not using the above diagnosis command.
We can use xp_cmdshell after enabling it like follows:
The sample output is as follows:
Last updated