387 字
2 分鐘
How to Prohibit Specific Accounts from Using LinkServer? Block Directly with a Fake Account
Conclusion
Overriding LinkServer logins with “fake account mapping” to ensure specified users always fail to connect is the simplest and most controllable blocking method.
Where It’s Suitable
- To restrict “specific accounts” from using LinkServer
- To avoid affecting other users (maintaining public availability)
- When precise control of LinkServer with DENY is not possible
- When permissions are already chaotic and quick remediation is needed
Steps
1. Confirm Current LinkServer Login Settings
- Use
sp_helplinkedsrvloginto view mappings - You can see who maps to which remote account
- Determine if there is
NULL(meaning shared by everyone)
EXEC sp_helplinkedsrvlogin;2. Remove Existing Mappings for the Target User
- For all LinkServers
- Delete the login mapping for that User
- To prevent interference from old settings
EXEC sp_droplinkedsrvlogin @rmtsrvname = N'LinkServerName', @locallogin = N'UserA';3. Create a “Fake Account” to Force Login Failure
- Core method: Specify a non-existent account
- When the User calls LinkServer → login will definitely fail
- Equivalent to a “soft DENY”
EXEC sp_addlinkedsrvlogin @rmtsrvname = N'LinkServerName', @useself = N'False', @locallogin = N'UserA', @rmtuser = N'FakeDenyUser', @rmtpassword = N'WrongPassword';Additional Notes
- There is no
DENY USE LINKSERVERpermission; control can only be done via mapping @locallogin = NULLmeans shared by everyone; special attention is needed for the override order- This method does not affect other users (only targets specific accounts)
Command / Example Summary
Click to expand
-- 檢視權限EXEC sp_helplinkedsrvlogin;
-- 動態產生:刪除 + 建立 Fake MappingDECLARE @UserName sysname = N'UserA';DECLARE @FakeUser sysname = N'FakeDenyUser';DECLARE @FakePassword nvarchar(200) = N'WrongPassword';
DECLARE @SQL nvarchar(max) = N'';
-- 1. 刪除 mappingSELECT @SQL = @SQL + N'BEGIN TRY EXEC sp_droplinkedsrvlogin @rmtsrvname = N''' + REPLACE(name, '''', '''''') + ''', @locallogin = N''' + REPLACE(@UserName, '''', '''''') + '''; END TRY BEGIN CATCH END CATCH;' + CHAR(13) + CHAR(10)FROM sys.serversWHERE is_linked = 1;
-- 2. 建立 Fake mappingSELECT @SQL = @SQL + N'EXEC sp_addlinkedsrvlogin @rmtsrvname = N''' + REPLACE(name, '''', '''''') + ''', @useself = N''False'', @locallogin = N''' + REPLACE(@UserName, '''', '''''') + ''', @rmtuser = N''' + REPLACE(@FakeUser, '''', '''''') + ''', @rmtpassword = N''' + REPLACE(@FakePassword, '''', '''''') + ''';' + CHAR(13) + CHAR(10)FROM sys.serversWHERE is_linked = 1;
-- DebugPRINT @SQL;
-- 執行EXEC sp_executesql @SQL;Conclusion
No permission control for LinkServer? Then use the ‘make them unable to connect forever’ trick, it’s clean and decisive. Practical advice: Design permissions with separate accounts from the start, otherwise you’ll only be patching holes later.
How to Prohibit Specific Accounts from Using LinkServer? Block Directly with a Fake Account
https://joyceowo.github.io/posts/en/33ca78ea09fa8096b44ccfaf8486be0c/