# Enumerate Logins

## Configuring

### Adding SQL Server logings

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%2FtdocwrYMWM2br0HjPU44%2Fimage.png?alt=media&#x26;token=3e9b4194-009e-4de4-a691-f8e5aa86e895" 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%2F3q7j2gU4P4cWNM9EP7qE%2Fimage.png?alt=media&#x26;token=72e983c1-5a5f-4649-a8c3-2799ad94439a" alt=""></div>

4\. Create two new users copying the following settings. Unfold "Security" and "Logins" folders and right clicking on "Logins" and selecting "New Login".

Username: `Bob`, Password: `Bob`

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FpFTGbADMxF1OPbX7Pxb6%2Fimage.png?alt=media&#x26;token=5748a92f-c07c-4a7d-b789-e06844bd693b" alt=""></div>

Click "OK" and create the second user: Username: `SQLAdmin`, Password `Winter2022!`.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FTY501Rt6hGQIJKMrMvcf%2Fimage.png?alt=media&#x26;token=8b13896f-1534-4f51-b49b-24377f80a9ef" alt=""></div>

Then open the tab "Server Roles" and select "sysadmin" so the user `SQLAdmin` is sysadmin.

### Creating Domain Group and User to access the SQL Server.

1. The user AMSTERDAM\Richard already has access to the SQL Server. But lets create a group and a new user and add this user to the group and give it access.
2. Open the "Active Directory Users and Computers" administration tool on `DC02`.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FD7QY8F6nnnhsjK8USwTl%2Fimage.png?alt=media&#x26;token=fd4b34d1-2162-46ff-8287-597dc71a7ffc" alt=""></div>

3\. Open the "Employees" OU and right click on it and select "new" and then "Group". Name the group `DatabaseUsers`.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FqBuf1FhVBxH0XOXSph7R%2Fimage.png?alt=media&#x26;token=24c332bd-4bb6-4c21-8269-77ef96f38483" alt=""></div>

4\. Right click on the "Employees" OU and select "new" and then "User". Name the user also `Bob` and set the password `Fall2022!`. Make sure you deselect "User must change password at next Logon".

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FgFHLEmnu1Z9Es7pl5cgJ%2Fimage.png?alt=media&#x26;token=298f9944-b496-46ff-aabe-c3808241dc51" alt=""></div>

5\. Right click the user "Bob" and select "Properties". Then open the tab "Member Of" and click "Add". Fill in the group `DatabaseUsers` and click on "OK"

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FHn3nK2VUnSqG6QvNXO28%2Fimage.png?alt=media&#x26;token=1af5c4d5-d9da-4ba2-93d4-90d003a0d3d2" alt=""></div>

6\. Go back to `WEB01` and unfold "Security" and "Logins" folders and right clicking on "Logins" and selecting "New Login". Fill in Amsterdam\DatabaseUsers

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2F3QdiZ973d2MgSH23BU7x%2Fimage.png?alt=media&#x26;token=50d31253-78e9-4ddc-9346-248d3fee50e6" alt=""></div>

## Attacking

### How it works

SQL Servers can have two type of accounts that could connect to it. Domain user accounts or groups or SQL Server accounts. These accounts can be enumerated by any user with the "Public" role. By enumerating the SQL / Domain users and groups that have access to the SQL server, it might be possible to obtain access with a account that has higher privileges by for example password spraying all these users. Which might result in code execution on the SQL Server.

### Tools

* [PowerUpSQL](https://github.com/NetSPI/PowerUpSQL)

### Executing the attack

1. Login to `WS01` as `Richard` with the password `Sample123`.
2. Download PowerUpSQL on the kali machine and host it on a webserver:

```
wget https://raw.githubusercontent.com/NetSPI/PowerUpSQL/master/PowerUpSQL.ps1
python3 -m http.server 8090
```

3\. Start PowerShell and download and execute an amsi and PowerUpSQL in memory:

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2Fp5DMPvwFEGmhksyssoje%2Fimage.png?alt=media&#x26;token=2cb75427-3f79-4c6d-9bed-b98700b99c2f" alt=""></div>

4\. With PowerUPSQL loaded and the knowledge that Richard already has access to the SQL Server. We can enumerate the logins manually but since we aren't sysadmin it wont return all the users, only the one we can see from the database we have access to;

```
Get-SQLInstanceDomain | Get-SQLQuery -Query "SELECT name FROM sys.syslogins
Get-SQLInstanceDomain | Get-SQLQuery -Query "SELECT name FROM sys.server_principals"
```

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2F6fltJqXpFO1BjK83CaPn%2Fimage.png?alt=media\&token=332651e3-3045-48db-a017-fdd092ff46cd)

5\. If we run the queries on the SQL server itself as the Domain Admin we will receive all the users, since it is sysadmin on the SQL Server;

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FMsmt5oiCYH9BqU9BwuFS%2Fimage.png?alt=media&#x26;token=693f71da-6f35-415e-8482-06eb5942ad86" alt=""></div>

6\. So we aren't able to enumerate all the users using these queries. But we are able to query the users with the following query; `Select SUSER_NAME(ID)`. Using the SUSER\_NAME function. Where the ID starts with 1 and we keep incrementing it till we have all the users. The id `282` is from the `testadmin` SQL user.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FTeCl1al8y86Wu2JdwMjW%2Fimage.png?alt=media&#x26;token=55d79485-a87d-402f-a0b5-b7e90d52f917" alt=""></div>

7\. Doing this manually takes a while so luckely the authors of PowerUpSQL automated this by using the `Get-SQLFuzzServerLogin` cmdlet;

```
Get-SQLFuzzServerLogin -Instance web01
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FlxUewwql3Gh0A1ZCAtcG%2Fimage.png?alt=media&#x26;token=ce8731c1-2899-4ce9-acec-d9cb40eda2e0" alt=""></div>

8\. We discovered that the following user/groups can access the SQL Server. The users/groups with `AMSTERDAM\` or `BANK\` are domain users/groups.

```
AMSTERDAM\richard
Developer
Developer_test
BANK\administrator
testadmin
bob
SQLAdmin
AMSTERDAM\DatabaseUsers
```

## Defending

### Recommendations

* Restrict access to SQL Servers as much as possible. There is no need to give all "Domain Users" public access to the SQL Database.

### Detection

## References

{% embed url="<https://www.netspi.com/blog/technical/network-penetration-testing/hacking-sql-server-procedures-part-4-enumerating-domain-accounts>" %}

{% embed url="<https://github.com/NetSPI/PowerUpSQL>" %}
