In this article, we will learn in detail how to pentest MSSQL servers using the Metasploit framework.
Table of Content:
- Identify the Server
- Retrieve login credentials
- Obtaining sysadmin privileges
- Exploiting xp_cmdshell
- MSSQL Enumeration
- Login User Enumeration
- Executing MSSQL Queries
- Mssql exec
- Executing SQL queries via .sql file
- Creating Database
- Find Sample Data
- Capturing MSSQL login
- Exploiting CLRIntegration
Metasploit is an excellent framework developed by H. D. Moore. It is a free and lightweight tool for penetration testing. It is open-source and cross-platform and has a range of features. Its popularity rests primarily on the fact that it is a powerful tool for auditing security. While this is true, it also has many features that can help people protect themselves. Personally speaking, this is my go-to tool for testing as it encapsulates the exploit a pentester can ever need. Through this article, we will learn how to use Metasploit to pentest/exploit MSSQL. Therefore, we will go through every exploit Metasploit has to offer step by step, from finding the MSSQL server in the network to retrieving the sensitive information from the database and gaining control. Without any further ado, let us begin.
Identify the Server
When testing MSSQL servers, whether remotely or locally, our first is to find the server in the network. And for this, we will use the following exploit in Metasploit:
And as you can see in the image above, the exploit will tell us the details about the active server in the network. There are many ways to locate the MSSQL server in the network. To learn these ways visit our article: MSSQL for Pentester: Discovery
Retrieve login credentials
We have found the server, so our next step is to retrieve the credentials of the server. We will enforce a dictionary attack for this, with the help of the following exploit:
Obtaining Sysadmin Privileges
Now that we have the credentials let’s check what privileges users have, such as whether they are a part of sysadmin. To exploit further, we need a user with sysadmin privileges. And for this, we will use the following exploit:
As you can see in the image above, our user doesn’t have the privilege of sysadmin. So now we will try and find if it can impersonate any other user that has sysadmin privileges. And for this, we will use the following exploit:
Now, as you can see in the image below, the lowpriv user can impersonate sa user. Sa user is a member of sysadmin, and with the help of the above exploit, lowpriv is now a sysadmin too as it impersonated sa user.
If we run the previous exploit, mssql_escalate_dbowner, we will see that the privilege escalation will not be required because of impersonating another user. The same is shown in the image below:
We found the MSSQL server in the network, retrieved the credentials, impersonated the user to have higher privileges. So now, let us try and get a meterpreter session of the server by exploit xp_cmdshell by using the following exploit:
As you can see in the above image, the exploit is trying to enable the xp_cmdshell to have our session. We have written a detailed article on xp_cmdshell, which you can read here. Once the xp_cmdshel is successfully enabled, we will have our meterpreter session as shown in the image below:
Let’s now enumerate the server and see what all information we can get, along with configuration settings. And for this, we will use the following exploit:
As the result of the above exploit, you can see what permissions are given to the database, which logins are available with other helpful information. The same can be seen in the image above.
Login User Enumeration
MSSQL provides various users to login into the server. The server can be accessed through all the windows users’ accounts and the database accounts. Thanks to Metasploit we have an exploit to enumerate all of these users and the exploit is as follows:
And as a result, you can see in the above image that the list of all users will be provided to you.
Executing MSSQL Queries
There is an exploit in Metasploit that will help you with each and every MSSQL query remotely. By default, it runs ‘select@@version’ but you can change to any desired query by using ‘set SQL *query*’ command. To deploy the said exploit use the following set of commands:
With the help of the above exploit, we have detailed information about the server as desired.
The following exploit will help you take advantage of the stored procedure called xp_cmdshell. Earlier in the article, we have successfully retrieved the credentials of the server. There, with help of the credentials, you can call upon the xp_cmdshell stored procedure and execute any command that you want. To deploy the said exploit, type:
And as you can see in the image above, the exploit is executed successfully, and we have our desired result, i.e., the list of all the net users.
Executing SQL query via .sql File
Another method to execute the desired command is to first write the Sql Syntax in .sql file with the following command:
Now we can use this .sql to run on the server, remotely, with the help of the following exploit:
And as a result, the above exploit will create a user with the name of test1. You can manually go to the server and confirm the creation of the user as shown in the image below:
Usually, any MSSQL server that you are Pentesting will have a database. But as the server on which we are performing this penetration testing is new as we also wanted to show the lab setup; therefore, for our next exploit to work, we will be creating a database in our server. To make the database, use the following command:
As the above query executes itself successfully, our next step is to type in the following query:
And so, as you can see in the image above, our table is created. Now, let’s add data to our table with the help of the following query:
This way, you can create your database.
Find Sample Data
Now that we have our database, let us learn how we can dump the content of the database with the help of Metasploit. Luckily, Metasploit has a particular exploit which uses particular keywords to match in the database and then give out all the information related to those keywords. These keywords are specified by the user. And to use the said exploit type:
Thus, using the above exploit will give the desired content of the database. For instance, the data we dumped had the information of the stored credit cards of the users.
The next exploit that we are going to use will dumb the schema of the SQL server instance. And to use this exploit, use the following set of commands:
And so, with the help of the above exploit, we have the data from the server.
Capturing MSSQL login
The next exploit that we are going to use capture/mssql. This exploit creates a fake server and tries to capture the authenticated credentials from the original server. To use this exploit, type;
Now, if the user tries to log in to the server, for instance, we will have the credentials with the following command:
And when you check your Metasploit, voila! You will have the correct login credentials of the server, which you can see in the image below as well:
The next exploit will help to take advantage of the CLR integration. This exploit will enable CLR integration, and along with that, it will also activate the trustworthy database property. After the exploit gives you the session, it restores all the settings to their original form. To use this exploit, type:
And as you can see, the exploit followed all the steps to exploit CLR integration to our potential. And it gives out the meterpreter session as shown in the image above.
Last but not least, our next exploit is used to dump the hashes of the users from the server. To use this exploit, type:
And that’s how the above exploit will give you the hashes of the passwords of the users.
Note: the hashdump exploit only works on MSSQL Server 2012.
All in all, Metasploit is one of the best tools to pentest MSSQL servers as it offers so many exploits and multiple ways to do so.