Privilege Escalation
Overview
Privilege escalation in Microsoft SQL Server is the process of gaining higher levels of access or control over the SQL Server instance or database than originally granted.
Examples
Enumeration IMPERSONATE permissions
IMPERSONATE permissions in Microsoft SQL Server allow a user to impersonate another user or login and execute statements on their behalf. This can be useful for troubleshooting and testing permissions, but it also introduces potential security risks if not used properly.
Enabling IMPERSONATE permissions
To grant IMPERSONATE permission at the server level, use the following command:
To grant IMPERSONATE permission at the database level, use the following command:
To grant IMPERSONATE permission at the schema level, use the following command:
Finding IMPERSONATE permissions
To find out which users or logins have been granted IMPERSONATE permission in Microsoft SQL Server, you can use the following query:
EXECUTE AS LOGIN
The EXECUTE AS LOGIN statement in Microsoft SQL Server allows a user to execute a batch of SQL statements in the context of a specified login. This can be useful for testing permissions and troubleshooting issues related to permissions and access controls.
When using EXECUTE AS LOGIN, the user must have the IMPERSONATE permission on the target login. This permission is typically granted to members of the sysadmin fixed server role, but can also be granted to other users and roles as needed.
EXECUTE AS USER
The EXECUTE AS USER statement in Microsoft SQL Server allows a user to execute a batch of SQL statements in the context of a specified user. This can be useful for testing permissions and troubleshooting issues related to permissions and access controls.
When using EXECUTE AS USER, the user must have the IMPERSONATE permission on the target user. This permission is typically granted to members of the sysadmin fixed server role, but can also be granted to other users and roles as needed.
From db_owner to sysadmin
In Microsoft SQL Server, the db_owner role has a high level of privileges within a specific database, but it does not have permissions outside that database. However, if a database has been marked as trustworthy, a user with the db_owner role can execute code within that database with the permissions of the database owner. This can allow an attacker to elevate their privileges and potentially gain access to the sysadmin role.
Identify a trustworthy database
Use the following query to identity trustworthy domains:
Use the following query to verify your user role:
If your user is a db_owner of a trustedworthy database, you can elevate your privileges:
Last updated