Abusing SQL Linked Servers
Cross-forest MSSQL linked servers facilitate communication and data exchange between SQL Server instances located in different Active Directory forests. This configuration allows SQL Server instances in one forest to access data and resources hosted by SQL Server instances in another forest.
If we manage to get access to a SQL database with linked servers we can try to "hop" the trust by executing command on those and get a foothold in a new domain.
Privileged Access to Server Links
This scenario occurs when a domain user has elevated remote access to a server in another domain with sa
(sysadmin) privileges.
To enumerate these privileges we can use PowerUpSQL
We can see that the SQL02 database is linked to the SQL01 one, looking at the LocalLogin
and RemoteLoginName
attributes we discover that the otter
user has sa
privileges over the linked server. To get further confirmation we can use the sp_helplinkedsrvlogin
to get more information about the remote logins
Alternatively we could use mssqlclient
from the Impacket suite which has some handy commands to perform actions on linked servers directly.
As sa
we can, for example, enable xp_cmdshell
to execute commands from the server; this can be done from mssqlclient
or Windows clients like SSMS or heidiSQL
Trustworthy Databases
If a user within our domain hasn't been granted remote login permissions as a sysadmin, but instead has been provided public privileges as a local SQL User in SQL02 (which is part of the other domain), we can pursue a strategy to enumerate trusted databases
on the targeted linked server. Our objective would be to determine if the user holds the db_owner
role for any trusted database, if that's the case we can create a stored procedure to enable xp_cmdshell
, ensuring it executes under the context of the OWNER
, which typically would be the sa
user.
Just like before we'll enumerate the linked databases
Here the LocalLogin
and RemoteLoginName
fields are empty, this means that the domain user we're executing the command with doesn't have sa
permissions; we can still retrieve the login ID of the domain user on the linked server using
Next, we must ascertain whether any of the databases enumerated possess trustworthiness enabled to identify if any of the enumerated databases have is_trustworthy_on
enabled.
Once we identify one or more databases we have to check if our user has db_owner
role.
The following query checks whether the owner of the database is also sysadmin
To abuse this configuration we'll create a stored procedure to escalate our privileges. First we need to check if the IS_RPC_OUT_ENABLED
option is enabled, this enables the SQL server to make outbound calls to other servers using RPC.
If the feature is enabled we can execute the following query
and then execute the procedure
We can do the same from linux
From this point on we're sysadmin and resume with the standard exploitation by enabling xp_cmdshell
and getting command execution on the server.
Last updated