MSSQL Privilege Escalation via Impersonation
SQL Server's EXECUTE AS LOGIN statement allows one login to assume the identity of another within the current session. When a low-privilege login has been explicitly granted IMPERSONATE permission on a sysadmin account — intentionally or by misconfiguration — it can escalate to sysadmin rights without any credential for the target account. This is a common privilege escalation path on OSCP-style targets where a low-privilege SQL login is the starting point.
Identify Impersonable Logins
Connect with the low-privilege login and check which logins the current user has IMPERSONATE permission on:
name ---- sa Admin
Any name returned here is a login you can impersonate. sa is the highest value target. Confirm your current privilege level before and after impersonation to measure the escalation:
current_login is_sysadmin appuser 0
Impersonate sa and Confirm Escalation
Execute EXECUTE AS LOGIN to assume the sa identity for the current session:
current_login is_sysadmin sa 1
is_sysadmin returning 1 after EXECUTE AS LOGIN confirms the impersonation succeeded and you now have sysadmin rights in this session. Enable xp_cmdshell and proceed to OS command execution:
output -------------------- nt authority\system
Database-Level Impersonation
Impersonation also works at the database level using EXECUTE AS USER. This is useful when a database user has elevated permissions within a specific database even if the mapped login does not have server-level rights. Check which users can be impersonated in the current database:
dbo 1
Landing as dbo with db_owner membership in a database that has TRUSTWORTHY set allows escalating to sysadmin through a stored procedure. Check TRUSTWORTHY status on databases where you have db_owner:
name is_trustworthy_on msdb 1 appdb 1
TRUSTWORTHY Database Escalation to Sysadmin
A TRUSTWORTHY database where you have db_owner allows creating a stored procedure that runs with sysadmin context. This escalates from database owner to server sysadmin:
1
A return value of 1 confirms the appuser login is now sysadmin at the server level. The session retains this privilege permanently rather than just for the current EXECUTE AS scope.
References
-
Microsoft — EXECUTE ASlearn.microsoft.com/en-us/sql/t-sql/statements/execute-as-transact-sql (opens in new tab)
EXECUTE AS LOGIN and EXECUTE AS USER syntax and scope behavior
-
Microsoft — TRUSTWORTHY Database Propertylearn.microsoft.com/en-us/sql/relational-databases/security/trustworthy-database-property (opens in new tab)
TRUSTWORTHY flag behavior and sysadmin escalation risk
Was this helpful?
Your feedback helps improve this page.