MS SQL Impersonation
Last updated
Last updated
MS SQL impersonation, or context switching, is a means which allows the executing user to assume the permissions of another user without needing to know their password. One handy use case for the feature is to allow administrators to impersonate a user for testing purposes, e.g. a user is having a problem and they want to eliminate permissions as an issue.
Impersonations must be explicitly granted through securable configurations.
\
\
In this example, DEV\Domain Users
have been granted the ability to impersonate the DEV\mssql_svc
account. This is clearly a security issue because it gives all Domain Users sysadmin privileges on this instance.
We can discover accounts to impersonate manually using the following queries:
\
\
This shows that the grantee_principal_id
, 268, is allowed to impersonate the grantor_principal_id
, 267. The IDs don't mean much, so we can look them up with:
\
\
Here, we see that 267 is DEV\mssql_svc and 268 is DEV\Domain Users.
You can also write your own SQL query that will join these two, or use SQLRecon's impersonate module.
\
We can take advantage of this as bfarmer, who we know is not a sysadmin.
\
Use EXECUTE AS
to execute a query in the context of the target.
\
SQLRecon modules can also be run in "impersonation mode" by prefixing the module name with an i
and specifying the principal to impersonate.