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:

GRANT IMPERSONATE ON LOGIN::[login_name] TO [user_name];

To grant IMPERSONATE permission at the database level, use the following command:

GRANT IMPERSONATE ON USER::[user_name] TO [user_name];

To grant IMPERSONATE permission at the schema level, use the following command:

GRANT IMPERSONATE ON SCHEMA::[schema_name] TO [user_name];

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:

SELECT distinct b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = 'IMPERSONATE'

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 LOGIN = 'sa';
-- SQL statements to be executed as 'mylogin'
REVERT;

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.

EXECUTE AS USER = 'sa';
-- SQL statements to be executed as 'myuser'
REVERT;

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:

SELECT name, is_trustworthy_on FROM sys.databases WHERE is_trustworthy_on = 1;

Use the following query to verify your user role:

use <trustworthy_db>
SELECT r.name AS RoleName FROM sys.database_role_members rm INNER JOIN sys.database_principals p ON rm.member_principal_id = p.principal_id INNER JOIN sys.database_principals r ON rm.role_principal_id = r.principal_id WHERE p.name = USER_NAME() AND r.name = 'db_owner';

If your user is a db_owner of a trustedworthy database, you can elevate your privileges:

"use msdb; EXECUTE AS USER = 'dbo';"

Last updated