![]() |
|
|
By: K. Brian Kelley | Read Comments (11) | Print Brian is a SQL Server author and columnist focusing primarily on SQL Server security. Related Tips: More |
|
Problem
I want to audit whenever a member of the sysadmin role logs on to my SQL Server. Auditing all successful logins provides that information, however, it logs every connection. The sysadmin logins are being lost amidst all the noise. How can I just track the sysadmin logins to my SQL Server instance?
Solution
In SQL Server 2005 Service Pack 2, Microsoft introduced logon triggers into the core functionality. Like DDL and DML triggers, these triggers fire on particular events, in this case, whenever a logon to the SQL Server instance occurs. We can use a logon trigger to audit when members of a particular role, such as the syadmin fixed server role, logs on.
In order to audit for members of the sysadmin fixed server role, we will need to use two system views: sys.server_role_members and sys.server_principals. We will join these views when a logon event occurs to determine if the logon is a member of the sysadmin role or not. We can get the members by joining these two views together as shown below:
| SELECT sp.principal_id FROM sys.server_role_members srm INNER JOIN sys.server_principals sp ON srm.member_principal_id = sp.principal_id WHERE srm.role_principal_id = ( SELECT principal_id FROM sys.server_principals WHERE [Name] = 'sysadmin') |
This query will be the basis for our logon trigger. By adding an AND clause in our final set of code, we will be able to test whether or not the incoming logon is a member of the sysadmin fixed server role.
We will also need to some place to record the event when it occurs. One of the easiest ways to do this is to use a table created for this purpose in a work database. For the purposes of this example, I am going to assume the table can be stored in a DBA database by the name of DBAWork. Here is the associated code:
| CREATE TABLE dbo.AuditSysAdminLogin (AuditEventId INT IDENTITY(1,1) NOT NULL, EventTime DATETIME NOT NULL, ServerLogin NVARCHAR(100) NOT NULL, CONSTRAINT PK_AuditSysAdminLogin PRIMARY KEY CLUSTERED (AuditEventID)); GO |
Once we have the audit table, we can create our logon trigger. The basic syntax for a logon trigger is similar to a DDL trigger as shown below:
| CREATE TRIGGER <trigger name> ON ALL SERVER FOR LOGON AS <SQL Statements> |
Following this format and using the query identified above to help identify who is a member of the SysAdmin role, the only aspect we are missing is a way to identify the logon. There is a system function, ORIGINAL_LOGIN(), which provides that information. Putting it all together, here is our logon trigger:
USE master; |
Next Steps
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
| Friday, November 21, 2008 - 1:34:50 AM - Bas | Read The Tip |
|
Hello nice trigger but how can i Disable this trigger I can't find it on my server |
|
| Friday, November 21, 2008 - 1:51:23 AM - Bas | Read The Tip |
|
i found the solution drop trigger trigLogon_CheckForSysAdmin ON ALL SERVER |
|
| Friday, November 21, 2008 - 8:01:12 AM - K. Brian Kelley | Read The Tip |
|
That deletes it. If you want to leave it in place to be re-enabled later, you'll want to use DISABLE TRIGGER:
DISABLE TRIGGER trigLogon_CheckForSysAdmin ON ALL SERVER;
|
|
| Friday, January 23, 2009 - 2:31:53 PM - DBSQL | Read The Tip |
|
How can I audit /Monitor if some one Change the SQL server system configuration ? |
|
| Saturday, January 24, 2009 - 3:46:19 PM - K. Brian Kelley | Read The Tip |
|
In 2005 and below, you have to pay careful attention to the SQL Server log. In 2008 you can use Policy Management to check and enforce settings. You can also have it just report on change.
|
|
| Wednesday, March 04, 2009 - 10:49:17 PM - audi08 | Read The Tip |
|
I don't want to monitor all the request that come to the sql cluster/server. I have a sq 2005 cluster and i want to monitor if some one physically login to the server vis management studio or does some major things like stop or Make change to the configuration in SQL 2005 cluster.
worst case at least audit who actually logged on to the SQL server cluster or sql 2005 server.
thanks Audi08 |
|
| Wednesday, May 13, 2009 - 2:31:33 PM - anwark | Read The Tip |
|
This trigger does not log a sysadmin logon from windows group. It will be nice that anybody fix this detail |
|
| Monday, May 18, 2009 - 8:05:22 AM - anwark | Read The Tip |
|
Hello! I think that i found the way to log users from windows group with sysadmin privileges.
CREATE TRIGGER [ -- SysAdmin from Windows Group IF @user_windows = 1 |
|
| Friday, October 09, 2009 - 8:15:25 AM - tdinh | Read The Tip |
|
Hello, Can we take this another step where we want to track all DDL and DML activities performed by sysadmin? Is this possible? Thanks! |
|
| Saturday, October 17, 2009 - 6:52:48 PM - K. Brian Kelley | Read The Tip |
|
[quote user="tdinh"]Can we take this another step where we want to track all DDL and DML activities performed by sysadmin? Is this possible?[/quote] To a certain extent. You can track all DDL changes. But in this case you'd be building DDL triggers in each database. You can track INSERT, UPDATE, and DELETE operations using standard DML triggers (INSTEAD OF and AFTER triggers). But you won't be able to track SELECTs using triggers. You'll either have to use the Audit object (if you're on SQL Server 2008 Enterprise Edition) or you'll have to rely on traces.
|
|
| Wednesday, July 28, 2010 - 5:59:27 AM - ALZDBA | Read The Tip |
|
Why not just use the system function (in the server level trigger)
if IS_SRVROLEMEMBER ('sysadmin') = 1
begin insert ... end |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |