When it comes to SQL Server security, it’s important to note that there are server and database security levels. All work done by a user is performed on a database, but in order to access the database and do the work, the user first needs to access the server, and afterwards the database - the server security level affects the database security level
To access the server, an appropriate login entity must be set for the user. A SQL Server login entity determines user credentials for accessing SQL Server. Security on SQL Server initially depends on properly set logins since they actually represent an entrance to SQL Server and its databases. Also, logins are an important realm of any compliance regulation – confidentiality, consistency, and accuracy of data can be easily jeopardized if a user is granted sufficient permissions within his login.
There are two SQL Server login properties related to SQL Server security:
These properties can be changed using either SQL Server Management Studio or T-SQL. In both cases the GRANT, REVOKE, or DENY operations are actually applied on the SQL Server login entity that is being changed.
To meet compliance regulations and maintain SQL Server secured it’s required to audit security changes, whether the changes were intentional or accidental.
Changes applied to SQL Server logins related to the Securables and Server Roles properties can be audited using DDL triggers on server level. In the following example, we’ll use the ADD_SERVER_ROLE_MEMBER, DDL_GDR_SERVER_EVENTS, and DROP_SERVER_ROLE_MEMBER SQL Server security events in the DDL trigger, which will fire and capture login entities altering operations.
CREATE TRIGGER DDL_AUDIT_Logins ON ALL SERVER FOR ADD_SERVER_ROLE_MEMBER ,DDL_GDR_SERVER_EVENTS ,DROP_SERVER_ROLE_MEMBER AS SET NOCOUNT ON; DECLARE @EventsTable TABLE ( EType NVARCHAR(max) ,EObject VARCHAR(100) ,EDate DATETIME ,EUser VARCHAR(100) ,ECommand NVARCHAR(max) ); DECLARE @EType NVARCHAR(max); DECLARE @ESchema NVARCHAR(max); DECLARE @DBName VARCHAR(100); DECLARE @Subject VARCHAR(200); DECLARE @EObject VARCHAR(100); DECLARE @EObjectType VARCHAR(100); DECLARE @EMessage NVARCHAR(max); DECLARE @ETSQL NVARCHAR(max); SELECT @EType = EVENTDATA().value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(max)') ,@ESchema = EVENTDATA().value('(/EVENT_INSTANCE/SchemaName)[1]', 'nvarchar(max)') ,@EObject = EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]', 'nvarchar(max)') ,@EObjectType = EVENTDATA().value('(/EVENT_INSTANCE/ObjectType)[1]', 'nvarchar(max)') ,@DBName = EVENTDATA().value(' (/EVENT_INSTANCE/DatabaseName)[1]', 'nvarchar(max)') ,@ETSQL = EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]', 'nvarchar(max)'); INSERT INTO @EventsTable SELECT @EType ,@EObject ,GETDATE() ,SUSER_SNAME() ,@ETSQL; SET @EMessage = 'Login_Event: ' + @EType + CHAR(10) + 'Event Occured at: ' + Convert(VARCHAR, GETDATE()) + CHAR(10) + 'Changed Login: ' + @EObject + CHAR(10) + 'Changed by: ' + SUSER_SNAME() + CHAR(10) + 'Executed T-SQL: ' + @ETSQL SELECT @Subject = 'SQL Server Login changed on ' + @@servername; EXEC msdb.dbo.sp_send_dbmail @recipients = 'DDL_Alert@companydomain.com' ,@body = @EMessage ,@subject = @Subject ,@body_format = 'HTML'; SET NOCOUNT OFF; GO
Trigger auditing provides viable login changes tracking. On the other hand, it requires certain level of T-SQL knowledge, and the trigger can be disabled easily by a user with sufficient permissions
The SQL Server Audit feature tracks both server and database events, utilizing the Extended Events technology. The feature can be setup either using T-SQL or SQL Server Management Studio. We’ll use SQL Server Management Studio in this article; the corresponding T-SQL scripts can be easily generated using the CREATE To option
To track and document login entity changes on a specific SQL Server instance:
Using the following steps we’ll create a server audit specification, which requires a previously created SQL Server audit object (AuditLoginChangesSpecification). The server audit specification will be used to fine-tune and specify exactly what will be tracked - changes on login entities
After the audit object and server audit specification are set and enabled, the SQL Server security related changes executed on login entities will be tracked and documented in the .sqlaudit file
To view the audited login changes on SQL Server:
The other way to export and provide captured data is via the fn_get_audit_file function. The function reads the *.sqlaudit repository files created by the SQL Server Audit feature
The following script queries the captured information related to SQL Server login changes:
SELECT event_time ,session_server_principal_name AS Changed_by ,target_server_principal_name AS LoginName ,server_instance_name ,statement FROM sys.fn_get_audit_file('C:\AUDITs\*.sqlaudit', DEFAULT, DEFAULT) WHERE action_id = 'G' OR action_id = 'APRL';
Unlike the database level auditing, the SQL Server Audit native feature on the server level is supported on all SQL Server editions, starting from SQL Server 2008. However, for viable reporting and querying of captured information, T-SQL knowledge is required. And again, a user with sufficient permissions can disable the audit specification, apply the login changes, and enable back the audit specification without a trace
Any security related changes applied to SQL Server login entities are potential security issue. Moreover, any compliance regulation requires login changes to be captured, properly documented, and made available on request
ApexSQL Audit is a SQL Server auditing tool, with a range of built-in and custom reports, which monitors changes and access on multiple SQL Server instances and their objects (databases, tables, stored procedures, functions, and views)
To audit login changes on a particular SQL Server instance:
Or by using the Advanced filter:
To audit security changes on multiple SQL Server instances, repeat the first two steps on each of SQL Server instance
Captured data about changes on login entities are reported within the Security configuration history report. The report provides all changes on the security entities (logins, users, and roles) including information about logins being created or dropped, permissions granted, revoked, or denied. Each event is documented with a time of execution, SQL Server instance name, login name, application (e.g. Microsoft SQL Server Management Studio), client computer name, operation, T-SQL that was executed, class (e.g. Audit add member to DB role event), and event sub class
In order to track and document changes on SQL Server login entities ApexSQL Audit provides:
To audit login changes on SQL Server, you can use either native SQL Server features and methods, or products like ApexSQL Audit. Even the native methods are built-in, they do not offer an out-of-the-box solution, and require advanced knowledge for configuring and reporting captured information. On the other hand, ApexSQL Audit provides value added features like centralized tamper-evident design, an out-of-the box solution for a number of compliance requirements, multiple SQL Server instances auditing, and more via user friendly interface
Please download the script(s) associated with this article on our GitHub repository.
Please contact us for any problems or questions with the script.
[1] Server-Level Roles
[2] SQL Server Audit Action Groups and Actions
Microsoft SQL Server 2012 Security Cookbook
Audit Login Change Property Event Class
Login Auditing (SQL Server Management Studio)
DDL_SERVER_SECURITY_EVENTS
© ALL RIGHTS RESERVED. Terms of Use Privacy Cookie Preference Center