1433 msSQL
Sources and resources
- nc64.exe.
- Impacket: mssqlclient.py.
- Pentesmonkey Cheat sheet.
- book.hacktricks.xyz.
- winPEAS.
By default, MSSQL uses ports TCP/1433
and UDP/1434
, and MySQL uses TCP/3306
. However, when MSSQL operates in a "hidden" mode, it uses the TCP/2433
port.
Enumeration
Basic enumeration:
If you don't know anything about the service:
We can also use Metasploit to run an auxiliary scanner called mssql_ping
that will scan the MSSQL service and provide helpful information in our footprinting process.
MSSQL Ping in Metasploit
Connect to database:
From Linux
sqsh
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.
From Windows
sqlcmd
From GUI Application
Server Management Studio or SSMS
Download from: SQL Server Management Studio or SSMS
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.
To install dbeaver using a Debian package we can download the release .deb package from https://github.com/dbeaver/dbeaver/releases and execute the following command:
Basic mssql 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
Attacks
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.
Capture MSSQL Service Hash
We can steal the MSSQL service account hash using xp_subdirs
or xp_dirtree
undocumented stored procedures, which use the SMB protocol to retrieve a list of child directories under a specified parent directory from the file system.
When we use one of these stored procedures and point it to our SMB server, the directory listening functionality will force the server to authenticate and send the NTLMv2 hash of the service account that is running the SQL Server.
1. First, start Responder or smbserver from impacket.
2. Run:
If the service account has access to our server, we will obtain its hash. We can then attempt to crack the hash or relay it to another host.
3. XP_SUBDIRS Hash Stealing with Responder
4. XP_SUBDIRS Hash Stealing with impacket
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:
Impersonating sysadmin:
Impersonating sa user:
It's recommended to run EXECUTE AS LOGIN within the master DB, because all users, by default, have access to that database.
To revert the operation and return to our previous user
Communicate with Other Databases with MSSQL
MSSQL
has a configuration option called linked servers. Linked servers are typically configured to enable the database engine to execute a Transact-SQL statement that includes tables in another instance of SQL Server, or another database product such as Oracle.
If we manage to gain access to a SQL Server with a linked server configured, we may be able to move laterally to that database server. Administrators can configure a linked server using credentials from the remote server. If those credentials have sysadmin privileges, we may be able to execute commands in the remote SQL instance.
As sysadmin
, we control the SQL Server instance. We can read data from any database or execute system commands with xp_cmdshell
.
If we need to use quotes in our query to the linked server, we need to use single double quotes to escape the single quote. To run multiples commands at once we can divide them up with a semi colon (;).
CVE-2012-2122 for MySQL 5.6.x
In the past, there was a vulnerability CVE-2012-2122 in MySQL 5.6.x
servers, among others, that allowed us to bypass authentication by repeatedly using the same incorrect password for the given account because the timing attack
vulnerability existed in the way MySQL handled authentication attempts.
In this timing attack, MySQL repeatedly attempts to authenticate to a server and measures the time it takes for the server to respond to each attempt. By measuring the time it takes the server to respond, we can determine when the correct password has been found, even if the server does not indicate success or failure. In the case of MySQL 5.6.x
, the server takes longer to respond to an incorrect password than to a correct one. Thus, if we repeatedly try to authenticate with the same incorrect password, we will eventually receive a response indicating that the correct password was found, even though it was not.