约定规则:
触发器名称:forbiddensa
执行DB:
master
1.删除触发器
drop trigger forbiddensa on ALL server ;
2.构建触发器
CREATE TRIGGER forbiddensa ON ALL SERVER WITH EXECUTE AS 'sa' FOR LOGON AS BEGIN --选择sa用户进行限制: IF ORIGINAL_LOGIN() = 'sa' AND --允许sa在本机登录 ( SELECT EVENTDATA().value('(/EVENT_INSTANCE/ClientHost)[1]', 'NVARCHAR(15)') ) NOT IN ( '<local machine>', '127.0.0.1', '192.168.1.181', '192.168.1.176', '192.168.101.85' ) --NOT IN('<local machine>','127.0.0.1','192.168.1.181','192.168.101.85') BEGIN ROLLBACK; END; END; GO
脚本说明:需要放行的IP就放到IP清单后面
3.安全测试
在未指定IP的机子里面连接当前SQL服务时,基于合理的提示,则说明安全设置成功,具体提示如下:
4.查看连接
SELECT a.[session_id], a.[login_time], a.[host_name], a.[original_login_name], b.[client_net_address] FROM master.sys.dm_exec_sessions a INNER JOIN master.sys.dm_exec_connections b ON a.session_id = b.session_id;
5.测试反馈
测试Ok