AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 37379
Accepted
Jeff Sacksteder
Jeff Sacksteder
Asked: 2013-03-23 12:21:53 +0800 CST2013-03-23 12:21:53 +0800 CST 2013-03-23 12:21:53 +0800 CST

限制 SQL Server 上的连接权限

  • 772

我有一个要在生产中部署的应用程序,它使用“荣誉系统”安全性。也就是说,所有用户都使用 SQL 用户/密码凭据连接到数据库,并且应用程序自己管理权限。后一部分并不像连接对象包含嵌入式凭据并且可以自由复制这一事实那样困扰我。我试图找到一些方法来限制与一组更有限的客户端的连接。当然,我可以创建防火墙规则来限制 IP。有没有办法通过机器帐户或域成员资格“预认证”SQL 登录?

sql-server sql-server-2012
  • 2 2 个回答
  • 9072 Views

2 个回答

  • Voted
  1. Kin Shah
    2013-03-23T12:55:28+08:002013-03-23T12:55:28+08:00

    正如 Thomas 提到的,它可以使用 LOGON Trigger 来完成。以下是可以帮助您的脚本

    /*
    
    http://www.sqlservercentral.com/scripts/Security/69558/
    Credit: Gregory A. Ferdinandsen
    [email protected]
    --Revision 1.0, 8 Feb 10
    --Requires SQL 2005 SP2 or higher
    */
    if not exists (select 1 from master..sysdatabases where name = 'SQL_Audit')
     begin
     create database SQL_Audit
      end
    USE [SQL_Audit]
    GO
    
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    SET ANSI_PADDING ON
    GO
    
    CREATE TABLE [dbo].[BlackList](
    [SRV_Rule] [int] IDENTITY(1,1) NOT NULL,
    [HostName] [varchar](64) NULL,
    [IP_Address] [varchar](15) NULL,
    [LoginName] [varchar](128) NULL,
    [AppName] [varchar](256) NULL,
    [RestrictionEnabled] [bit] NULL,
    [Description] [varchar](2048) NULL,
    CONSTRAINT [PK_BlackList] PRIMARY KEY CLUSTERED 
    (
    [SRV_Rule] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    
    SET ANSI_PADDING OFF
    GO
    
    ALTER TABLE [dbo].[BlackList] ADD CONSTRAINT [DF_BlackList_RestrictionEnabled] DEFAULT ((0)) FOR [RestrictionEnabled]
    GO
    
    ---------------------------------------------------------------------
    ---------------------------------------------------------------------
    USE [SQL_Audit]
    GO
    
    
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    SET ANSI_PADDING ON
    GO
    
    CREATE TABLE [dbo].[Violations](
    [ViolationNum] [int] IDENTITY(1,1) NOT NULL,
    [PostDate] [datetime] NOT NULL,
    [LoginName] [varchar](128) NULL,
    [IPAddress] [varchar](15) NULL,
    [HostName] [nvarchar](64) NULL,
    [ServerName] [varchar](96) NULL,
    [AppName] [nvarchar](256) NULL,
    [ViolationType] [varchar](512) NULL,
    CONSTRAINT [PK_Violations] PRIMARY KEY CLUSTERED 
    (
    [ViolationNum] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    
    SET ANSI_PADDING OFF
    GO
    
    ALTER TABLE [dbo].[Violations] ADD CONSTRAINT [DF_Violations_PostDate] DEFAULT (getdate()) FOR [PostDate]
    GO
    ---------------------------------------------------------------------
    ---------------------------------------------------------------------
    --(c) Gregory A. Ferdinandsen
    [email protected]
    --Revision 1.0, 8 Feb 10
    --Requires SQL 2005 SP2 or higher
    
    --
    --Change with <<Execute as 'Domain\SQL'>> for a valid service account that has sa rights
    --
    --Information on Logon Triggers: http://msdn.microsoft.com/en-us/library/bb326598.aspx
    --
    USE Master
    go
    
    CREATE Trigger [trg_LoginBlackList]
     on all Server 
    
     as
    begin
    
     declare @data XML
    declare @User as varchar(128)
    declare @HostName as varchar(64)
    declare @IPAddress as varchar(15)
    declare @AppName as nvarchar(256)
    declare @SPID as int
    declare @SrvName as nvarchar(96)
    declare @PostTime as datetime
    declare @LogMsg as varchar(1024)
    
    set @data = EVENTDATA()
    set @User = @data.value('(/EVENT_INSTANCE/LoginName)[1]', 'nvarchar(128)')
    set @IPAddress = @data.value('(/EVENT_INSTANCE/ClientHost)[1]', 'nvarchar(15)')
    set @SPID = @data.value('(/EVENT_INSTANCE/SPID)[1]', 'int')
    set @SrvName = @data.value('(/EVENT_INSTANCE/ServerName)[1]', 'nvarchar(96)')
    set @PostTime = @data.value('(/EVENT_INSTANCE/PostTime)[1]', 'datetime')
    set @HostName = Cast(Host_Name() as nvarchar(64))
    set @AppName = Cast(App_Name() as nvarchar(256))
    
    --Check to see if the blacklist table exists, if the table does not exist, exit the Trigger, as otherwise all user would be locked out.
    
    if Not Exists (select * from SQL_Audit.INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'BlackList')
    begin
    return;
    end
    
    
    --#1
    --If a user connects from a given work station and with a given UserName, they will be dissconected
    --This user need to be set up in SQL_Audit..Blacklist with a user name and a host name, no IP Address is necesary
    --This is the prefered method of blacklisting, as DHCP could reak havoc on any IP restrictions
    If(Exists(Select * from SQL_Audit.dbo.BlackList where LoginName = @User and HostName = @HostName and RestrictionEnabled = 1))
    begin
    --Any data modifications made up to the point of ROLLBACK TRANSACTION are rolled back
    --The current trigger continues to execute any remaining statements that appear after the ROLLBACK statement. 
    --If any of these statements modify data, the modifications are not rolled back.
    --http://technet.microsoft.com/en-us/library/bb153915.aspx
    rollback
    
    insert into SQL_Audit..Violations
    (PostDate, LoginName, IPAddress, HostName, ServerName, AppName, ViolationType)
    values (@PostTime, @User, @IPAddress, @HostName, @SrvName, @AppName, 'LoginName, HostName')
    
    --Exit trigger without evaluating any further conditions
    return;
    end
    
    --#2
    --If a user connects from a given IP Address and with a given UserName, they will be dissconected
    --This user need to be set up in SQL_Audit..Blacklist with a user name and a IP Address, no HostName is necesary
    If(Exists(Select * from SQL_Audit.dbo.BlackList where LoginName = @User and IP_Address = @IPAddress and RestrictionEnabled = 1))
    begin
    --Any data modifications made up to the point of ROLLBACK TRANSACTION are rolled back
    --The current trigger continues to execute any remaining statements that appear after the ROLLBACK statement. 
    --If any of these statements modify data, the modifications are not rolled back.
    --http://technet.microsoft.com/en-us/library/bb153915.aspx
    rollback
    
    insert into SQL_Audit..Violations
    (PostDate, LoginName, IPAddress, HostName, ServerName, AppName, ViolationType)
    values (@PostTime, @User, @IPAddress, @HostName, @SrvName, @AppName, 'LoginName, IP Address')
    
    --Exit trigger without evaluating any further conditions
    return;
    end
    
    --#3
    --If a user connects from a given Blacklisted IP Address, regardless of the host name or SQL Server User
    --This IPAddress need to be set up in SQL_Audit..Blacklist with only an IP Address, no other information is needed
    --This will block all connections from the designated IP Address
    If(Exists(Select * from SQL_Audit.dbo.BlackList where IP_Address = @IPAddress and LoginName is NULL and HostName is NULL and RestrictionEnabled = 1))
    begin
    --Any data modifications made up to the point of ROLLBACK TRANSACTION are rolled back
    --The current trigger continues to execute any remaining statements that appear after the ROLLBACK statement. 
    --If any of these statements modify data, the modifications are not rolled back.
    --http://technet.microsoft.com/en-us/library/bb153915.aspx
    rollback
    
    insert into SQL_Audit..Violations
    (PostDate, LoginName, IPAddress, HostName, ServerName, AppName, ViolationType)
    values (@PostTime, @User, @IPAddress, @HostName, @SrvName, @AppName, 'IP Address')
    
    --Exit trigger without evaluating any further conditions
    return;
    end
    
    --#4
    --If a user connects from a given Blacklisted Workstation, regardless of the IP Address or SQL Server User
    --This Client need to be set up in SQL_Audit..Blacklist with only a value for HostName, no other information is needed
    --This will block all connections from the designated Host
    If(Exists(Select * from SQL_Audit.dbo.BlackList where HostName = @HostName and LoginName is NULL and IP_Address is NULL and RestrictionEnabled = 1))
    begin
    --Any data modifications made up to the point of ROLLBACK TRANSACTION are rolled back
    --The current trigger continues to execute any remaining statements that appear after the ROLLBACK statement. 
    --If any of these statements modify data, the modifications are not rolled back.
    --http://technet.microsoft.com/en-us/library/bb153915.aspx
    rollback
    
    insert into SQL_Audit..Violations
    (PostDate, LoginName, IPAddress, HostName, ServerName, AppName, ViolationType)
    values (@PostTime, @User, @IPAddress, @HostName, @SrvName, @AppName, 'HostName')
    
    --Exit trigger without evaluating any further conditions
    return;
    end
    
    --#5
    --If a particular application connects to SQL Server, regardless of IP Address, UserName, or HostName, the session is terminated
    If(Exists(Select * from SQL_Audit.dbo.BlackList where AppName = @AppName and HostName is NULL and LoginName is NULL and IP_Address is NULL and RestrictionEnabled = 1))
    begin
    --Any data modifications made up to the point of ROLLBACK TRANSACTION are rolled back
    --The current trigger continues to execute any remaining statements that appear after the ROLLBACK statement. 
    --If any of these statements modify data, the modifications are not rolled back.
    --http://technet.microsoft.com/en-us/library/bb153915.aspx
    rollback
    
    insert into SQL_Audit..Violations
    (PostDate, LoginName, IPAddress, HostName, ServerName, AppName, ViolationType)
    values (@PostTime, @User, @IPAddress, @HostName, @SrvName, @AppName, 'ApplicationName')
    
    --Exit trigger without evaluating any further conditions
    return;
    end
    
    --#6
    --If a particular application connects to SQL Server, with a given UserName (i.e. service account cannot connect with SSMS)
    If(Exists(Select * from SQL_Audit.dbo.BlackList where AppName = @AppName and LoginName = @User and RestrictionEnabled = 1))
    begin
    --Any data modifications made up to the point of ROLLBACK TRANSACTION are rolled back
    --The current trigger continues to execute any remaining statements that appear after the ROLLBACK statement. 
    --If any of these statements modify data, the modifications are not rolled back.
    --http://technet.microsoft.com/en-us/library/bb153915.aspx
    rollback
    
    insert into SQL_Audit..Violations
    (PostDate, LoginName, IPAddress, HostName, ServerName, AppName, ViolationType)
    values (@PostTime, @User, @IPAddress, @HostName, @SrvName, @AppName, 'ApplicationName, UserName')
    
    --Exit trigger without evaluating any further conditions
    return;
    end
    end;
    
    GO
    
    SET ANSI_NULLS OFF
    GO
    
    SET QUOTED_IDENTIFIER OFF
    GO
    
    ENABLE TRIGGER [trg_LoginBlackList] ON ALL SERVER
    GO
    
    • 12
  2. Best Answer
    Thomas Stringer
    2013-03-23T12:30:10+08:002013-03-23T12:30:10+08:00

    您可以通过登录触发器来实现这一点。在您的登录触发器中,您可以使用逻辑来执行您正在寻找的必要检查(例如机器名称)。不幸的是,如果您使用 SQL Server 身份验证,我认为没有办法获取用户的域成员身份。

    您可以查看使用EVENTDATA函数来查看是否可以提取其他信息以确定是否应允许连接。如果您不希望该特定登录成功,您可以简单地有条件地测试并发出ROLLBACK.

    • 8

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    授予用户对所有表的访问权限

    • 5 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve