Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345»»»

Blocking Users by IP Expand / Collapse
Author
Message
Posted Friday, March 20, 2009 7:41 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 1:51 AM
Points: 1,262, Visits: 3,422
Vampire (3/20/2009)
Thanks for the post Brian.

However, I am stuck with few issues. Hope you could throw some light on them and clarify .

1. I executed the Step to insert <local machine< into ValidIP table and still i am not able to connect from my local machine, bcoz of trigger execution.

2. I changed the trigger to capture the @IP value in troubleshooting the above said issue. Below is my code:

CREATE TRIGGER tr_logon_CheckIP
ON ALL SERVER
FOR LOGON
AS
DECLARE @IP NVARCHAR(15),@SqlCmd NVARCHAR(100);
SET @IP = (SELECT EVENTDATA().value('(/EVENT_INSTANCE/ClientHost)[1]', 'NVARCHAR(15)'));
INSERT INTO dbo.ValidIP (IP) VALUES (@IP);
BEGIN
IF IS_SRVROLEMEMBER('sysadmin') = 1
BEGIN

IF NOT EXISTS(SELECT IP FROM DBAWork.dbo.ValidIP WHERE IP = @IP)
ROLLBACK;
END;
END;
GO

But this is not inserting any @IP value in the ValidIP table. Does triggers have constraint of not inserting any value into the tables in execution.

3. If @IP value can't be inserted into the table ,as desired, is there any other way to see the output of EventData() or IP later.

Thanks in advance!!
Vampire



Your code here is like a virus for SQL Server ( never never can connect to SQL )

You should have the DAC ( Dedicated Admin Connection) available or active before working with this trigger. Then connect to SQL server with DAC like Brian said in the article and be sure that the SQL Browser is running ...so you will connect to the Server on master DB and you should disable or delete the trigger!





============================================================
SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #680378
Posted Friday, March 20, 2009 1:54 PM


Keeper of the Duck

Keeper of the Duck

Group: Moderators
Last Login: Monday, August 18, 2014 8:24 AM
Points: 6,634, Visits: 1,871
Vampire (3/20/2009)
this is not inserting any @IP value in the ValidIP table. Does triggers have constraint of not inserting any value into the tables in execution.

3. If @IP value can't be inserted into the table ,as desired, is there any other way to see the output of EventData() or IP later.

Thanks in advance!!
Vampire


The trigger is still, for accessing other objects, executing in the context of the login that's coming in. That means all logins need INSERT rights agaisnt the ValidIP table. If that's in a separate database, they need to have access to said database.


K. Brian Kelley, CISA, MCSE, Security+, MVP - SQL Server
Regular Columnist (Security), SQLServerCentral.com
Author of Introduction to SQL Server: Basic Skills for Any SQL Server User
| Professional Development blog | Technical Blog | LinkedIn | Twitter
Post #680683
Posted Tuesday, March 24, 2009 1:19 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 10:31 PM
Points: 167, Visits: 409
Brian,

I guess I dig grave for my own SQL Server
I have executed this trigger on a SQL Instance .\Inst1 and now it is not allowing me to connect from my local machine. However, let me state few facts:

1. I have inserted <local machine>, My computer Name and my IP address as values into ValidIP table, and now when i connect to the Instance from my computer(the instance on which trigger is created, also resides on my computer), the trigger executes. The login am using is a SysAdmin Acct.

2. Based on the above fact, my question is : This trigger allows only connections from the computers listed in ValidIP table..right? If yes, though I inserted my computer name in the ValidIP table..why is trigger executing?

3. What does statement return : (SELECT EVENTDATA().value('(/EVENT_INSTANCE/ClientHost)[1]', 'NVARCHAR(15)'));
Returns value as "<local machine>"; if connected from my computer OR
IP address of Local machine or computer from which connection origins??

4. I have used your tip of DAC to break this trigger barrier..it seems my DAC is also failing. I am pasting the Error output of my DAC connection. My question here is..does SQL instances support DAC Or is DAC available only for default instances.


C:\Documents and Settings\vampire>sqlcmd -S ADMIN:.\inst1 -A -dMaster
HResult 0xFFFFFFFF, Level 16, State 1
SQL Network Interfaces: An error occurred while obtaining the dedicated administrator
connection (DAC) port. Make sure that SQL Browser is running, or check the
error log for the port number [xFFFFFFFF].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.


If DAC is available for SQL Instances also..so please based on the above output..can you suggest me some work around.

Thanks
Vampire


--In 'thoughts'...
Lonely Rogue
Post #682106
Posted Tuesday, March 24, 2009 2:13 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 1:51 AM
Points: 1,262, Visits: 3,422
Vampire (3/24/2009)
Brian,

I guess I dig grave for my own SQL Server
I have executed this trigger on a SQL Instance .\Inst1 and now it is not allowing me to connect from my local machine. However, let me state few facts:

1. I have inserted <local machine>, My computer Name and my IP address as values into ValidIP table, and now when i connect to the Instance from my computer(the instance on which trigger is created, also resides on my computer), the trigger executes. The login am using is a SysAdmin Acct.

2. Based on the above fact, my question is : This trigger allows only connections from the computers listed in ValidIP table..right? If yes, though I inserted my computer name in the ValidIP table..why is trigger executing?

3. What does statement return : (SELECT EVENTDATA().value('(/EVENT_INSTANCE/ClientHost)[1]', 'NVARCHAR(15)'));
Returns value as "<local machine>"; if connected from my computer OR
IP address of Local machine or computer from which connection origins??

4. I have used your tip of DAC to break this trigger barrier..it seems my DAC is also failing. I am pasting the Error output of my DAC connection. My question here is..does SQL instances support DAC Or is DAC available only for default instances.


C:\Documents and Settings\vampire>sqlcmd -S ADMIN:.\inst1 -A -dMaster
HResult 0xFFFFFFFF, Level 16, State 1
SQL Network Interfaces: An error occurred while obtaining the dedicated administrator
connection (DAC) port. Make sure that SQL Browser is running, or check the
error log for the port number [xFFFFFFFF].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.


If DAC is available for SQL Instances also..so please based on the above output..can you suggest me some work around.

Thanks
Vampire


I told you in my post above that you should be sure that the SQL Browser is running ... also the DAC is only available from a client on the server!!!

Or you can do something in sqlcmd like this:

sqlcmd –A –d master

A - it will use the DAC connection
d - connection to the master DB

then you can kill or drop the trigger!!



============================================================
SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #682122
Posted Tuesday, March 24, 2009 2:58 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 10:31 PM
Points: 167, Visits: 409
@Dugi

Thanks for your tip..it worked. Am now able to login. However,I have my SQL Browser running earlier too...

I earlier used this command to connect to DAC as Admin..but couldn't
sqlcmd -S ADMIN:.\inst1 -A -dMaster


Dugi/Brian:

Could either of you please let me know the difference between these two along with answers to other questions ( posted in my previous reply, as this reply of yours answers my 4th question only).

Thanks a lot!!
Vampire


--In 'thoughts'...
Lonely Rogue
Post #682146
Posted Tuesday, March 24, 2009 12:38 PM


Keeper of the Duck

Keeper of the Duck

Group: Moderators
Last Login: Monday, August 18, 2014 8:24 AM
Points: 6,634, Visits: 1,871
Vampire (3/24/2009)
@Dugi

Thanks for your tip..it worked. Am now able to login. However,I have my SQL Browser running earlier too...

I earlier used this command to connect to DAC as Admin..but couldn't
sqlcmd -S ADMIN:.\inst1 -A -dMaster


Dugi/Brian:

Could either of you please let me know the difference between these two along with answers to other questions ( posted in my previous reply, as this reply of yours answers my 4th question only).

Thanks a lot!!
Vampire


That means you connected to the default instance, not a named instance if you followed Dugi's example and left off -S.


K. Brian Kelley, CISA, MCSE, Security+, MVP - SQL Server
Regular Columnist (Security), SQLServerCentral.com
Author of Introduction to SQL Server: Basic Skills for Any SQL Server User
| Professional Development blog | Technical Blog | LinkedIn | Twitter
Post #682703
Posted Thursday, April 23, 2009 8:17 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 12, 2012 6:15 AM
Points: 13, Visits: 8
The idea is very nice, but I can´t make this work.
The problem is that value() does not work inside logon triggers. In fact, it seems xqueries don´t work inside logon triggers.
I don´t have any trouble using them inside DDL triggers.
What I can do in a logon trigger is inserting the result of EVENTDATA() into a xml column. That is not what I need.
If anyone has an explanation or a solution for this, I would appreciate it very much.
Post #703237
Posted Friday, April 24, 2009 2:44 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 12, 2012 6:15 AM
Points: 13, Visits: 8
I will answer my own question. ARITHABORT must be on in order that xqueries such as VALUE() work.

So I began my logon trigger code with:

set arithabort on

and it worked nicely.

Thanxs :)
Post #704307
Posted Friday, April 15, 2011 2:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 6, 2014 2:40 AM
Points: 6, Visits: 45
Hopefully I'm not being dim here, but this seems like a lot of effort and increased overhead on your SQL database, for something that can be handled much more efficiently at the transport layer. In general, if you want to ensure that connections to a server are only made by a specific IP address, then you set the network card up to only accept connections from a specific IP, address, or you use a firewall, since this is more robust at detecting IP spoofing ect... by all means, limit the access of the security principle, but creating a trigger for each time a connection is made seems like a really bad idea.
Post #1093954
Posted Friday, April 15, 2011 2:40 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, August 22, 2014 1:49 AM
Points: 563, Visits: 1,006
In the past I have used IP restriction on the infrastructure side of things to get this done. Restricting PROD access to application, software management and SQL machines. But this is a nice way of doing it all in SQL.
Post #1093973
« Prev Topic | Next Topic »

Add to briefcase «««12345»»»

Permissions Expand / Collapse