![]() |
|
Low-impact SQL Server auditing of all user activity and data changes
|
|
By: Svetlana Golovko | Read Comments (4) | Related Tips: More > Auditing and Compliance |
One of the Support Analysts asked me if it is possible to send him notifications 5 days before a user's password expires for a SQL Server Standard login. He wants to be proactive and help users reset their passwords before they expired. The application uses SQL Server authentication and does not have password management functionality. Check out this tip to learn more.
In one of our last tips we have provided several scripts for the SQL Server audit, including logins audit. But there is more to it. DBAs can dig deeper and see how many bad login attempts a user had, when was the last time the password was set and get other useful audit related information using LOGINPROPERTY function. One of the past tips covers this function and it's usage in details.
In this tip we will provide a SQL Server Reporting Service (SSRS) report for DBAs that could be emailed and reviewed daily. Also we will provide scripts to setup an alert for the Support Analyst, to notify them about upcoming passwords expirations.
The report will display a list of logins that meet the following criteria:
If you are not familiar with SSRS a good place to start is this tutorial.
Create the new report using the query below as a dataset:
SELECT l.name,
CAST(CASE sp.[state] WHEN N'D' THEN 1 ELSE 0 END AS bit) AS DenyWindowsLogin,
CASE WHEN N'U' = l.type THEN 0
WHEN N'G' = l.type THEN 1
WHEN N'S' = l.type THEN 2
WHEN N'C' = l.type THEN 3
WHEN N'K' = l.type THEN 4 END AS LoginType,
CAST(CASE WHEN (sp.[state] IS NULL) THEN 0 ELSE 1 END AS bit) AS HasAccess,
CAST(sl.is_policy_checked AS bit) AS PasswordPolicyEnforced,
CAST(sl.is_expiration_checked AS bit) AS PasswordExpirationEnabled,
l.create_date AS CreateDate,
l.modify_date AS DateLastModified,
LOGINPROPERTY(l.name, N'BadPasswordCount') AS BadPasswordCount,
LOGINPROPERTY(l.name, N'BadPasswordTime') AS BadPasswordTime,
LOGINPROPERTY(l.name, N'DaysUntilExpiration') AS DaysUntilExpiration,
LOGINPROPERTY(l.name, N'IsExpired') AS IsExpired,
LOGINPROPERTY(l.name, N'IsLocked') AS IsLocked,
LOGINPROPERTY(l.name, N'IsMustChange') AS IsMustChange,
LOGINPROPERTY(l.name, N'LockoutTime') AS LockoutTime,
LOGINPROPERTY(l.name, N'PasswordLastSetTime') AS PasswordLastSetTime,
l.is_disabled AS IsDisabled
FROM sys.server_principals AS l
LEFT OUTER JOIN sys.server_permissions AS sp
ON sp.grantee_principal_id = l.principal_id
AND sp.[type] = N'COSQ' -- Connect permissions
LEFT OUTER JOIN sys.sql_logins AS sl
ON sl.principal_id = l.principal_id
LEFT OUTER JOIN sys.credentials AS c
ON c.credential_id = l.credential_id
WHERE
l.[type] IN ('U', 'G', 'S', 'C', 'K')
AND l.principal_id NOT BETWEEN 101 AND 255 -- ##MS% certificates
AND
( sp.[state] = N'D' -- DenyWindowsLogin
OR sp.[state] IS NULL -- HasAccess
OR CAST(sl.is_policy_checked AS bit) = 0
OR CAST(sl.is_expiration_checked AS bit) = 0
OR l.create_date > GETDATE()-1
OR l.modify_date > GETDATE()-1
OR l.is_disabled > 0
OR LOGINPROPERTY(l.name, N'DaysUntilExpiration')<= 5
OR LOGINPROPERTY(l.name, N'IsExpired') > 0
OR LOGINPROPERTY(l.name, N'IsLocked') > 0
OR LOGINPROPERTY(l.name, N'IsMustChange') > 0
OR LOGINPROPERTY(l.name, N'BadPasswordCount') > 2
)Create a simple table layout and use all of the fields from the query above:

Set conditional formatting to display text in red (using "Color" property) as following:
=iif(Value=0,"Black","Red")

=iif(Value=True,"Black","Red")
=iif(Value>5,"Black","Red")
Read this tip to find out more about SSRS conditional formatting.
Run the report and review the results.

We will use a query that utilizes the same (LOGINPROPERTY) function. Also, we will create custom error message, alert, operator and job that will run daily to monitor the logins. Read more about SQL Server Agent (including Notifications and Operators) here.
First, create a custom error message (you can use any number above 50000 that is not in use in your environment):
EXEC msdb.dbo.sp_addmessage
@msgnum = 60001,
@severity = 10,
@msgtext = 'Password is about to expire (or expired) for the user(s): %s.
Please, contact DBA and/or user(s) to reset the password(s).',
@with_log ='TRUE'
Create operator:
EXEC msdb.dbo.sp_add_operator @name=N'Support_App1', @enabled=1, @weekday_pager_start_time=90000, @weekday_pager_end_time=163000, @pager_days=62, @email_address=N'Support_App1@domain.com'
Create alert using the error message we have created above:
EXEC msdb.dbo.sp_add_alert @name=N'SQL Login Change Password - action required', @message_id=60001, @severity=0, @enabled=1, @delay_between_responses=0, @include_event_description_in=1
Create notification for the Support Analyst:
EXEC msdb.dbo.sp_add_notification @alert_name=N'SQL Login Change Password - action required', @operator_name=N'Support_App1', @notification_method = 1
Now create the SQL Server Agent Job that will run daily. The SQL Server Agent Job will have one Transact-SQL step. The command for the step is:
IF EXISTS (SELECT name, LOGINPROPERTY(name, N'DaysUntilExpiration')
FROM sys.server_principals
WHERE type = 'S'
AND LOGINPROPERTY(name, N'DaysUntilExpiration') < 6
AND default_database_name = 'APP1_Prod')
BEGIN
DECLARE @str VARCHAR(200)
SELECT @str = COALESCE(@str + ',', '') + name FROM sys.server_principals
WHERE type = 'S' AND LOGINPROPERTY(name, N'DaysUntilExpiration') < 6
AND default_database_name = 'APP1_Prod'
RAISERROR (60001, 10, 1, @str );
ENDYou can add exceptions to the notifications by filtering this statement, for example:
....
AND default_database_name = 'APP1_Prod'
AND name NOT IN ('sa', 'app_service_login')
....
.... AND default_database_name = 'APP1_Prod' AND is_disabled = 0 AND LOGINPROPERTY(name, N'IsLocked') = 0 ....
| Friday, March 22, 2013 - 12:30:17 AM - juno tiwari | Read The Tip |
|
Nice Post |
|
| Wednesday, March 27, 2013 - 6:13:24 PM - Svetlana Golovko | Read The Tip |
|
Thank you, Juno |
|
| Monday, April 22, 2013 - 10:35:20 AM - Ed - sqlscripter | Read The Tip |
|
Nice article, here is a very simple query to view sql login properties.
Set nocount ON |
|
| Tuesday, April 23, 2013 - 8:22:22 AM - Junior Galvão - MVP | Read The Tip |
|
Congratulations for this post.
Very nice. |
|
|
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 |