- Audit on SQL Server (Server Audit)
- How to Create an Audit to Monitor Job Creation, Modification, and Deletion in SQL Server
- How to create an Audit trigger to log object manipulation in SQL Server
- SQL Server - How to implement audit and control of logins (Logon Trigger)
- Monitoring DDL and DCL Operations Using SQL Server fn_trace_gettable
- Using SQL Server Standard Trace to Audit Events (fn_trace_gettable)
- SQL Server - Permissions and privileges audit trigger at database and instance level (GRANT and REVOKE)
- SQL Server - How to Monitor and Audit Data Changes in Tables Using Change Data Capture (CDC)
- SQL Server 2016 - How to “Time Travel” Using the Temporal Tables Feature
- SQL Server - How to use auditing to map actual required permissions on a user
- SQL Server - Trigger to prevent and prevent changes to tables
- SQL Server - How to Create a Data Change History for Your Tables (Audit Logs)
- SQL Server - How to Avoid Brute Force Attacks on Your Database
- SQL Server Security Checklist - An SP with more than 70 security items to validate your database.
- SQL Server - How to know the date of a user's last login
- SQL Server - How to Avoid and Protect Against Ransomware Attacks Like WannaCry on Your Database Server
- SQL Server – Beware of server role securityadmin! Using privilege elevation to become sysadmin
- SQL Server - How to Avoid SQL Injection? Stop using Dynamic Query as EXEC (@Query). Now.
- SQL Server - Understanding the Risks of TRUSTWORTHY Property Enabled in a Database
- SQL Server - Password Policies, Password Expiration, Mandatory Password Change, and Login Lockout after N attempts
Hello people,
Good day.
Introduction
Today I come to bring you a very useful resource in the life of DBA's SQL Server, both for auditing alteration / creation / deletion of objects and for a “versioning” of SP's, Functions, etc., which is the use of triggers to log all DDL level changes to objects, including being able to record the query used for that. This is especially useful in the case of programming objects, such as Stored Procedures and Functions, where you can view each change made to these objects.
As an added point, I added a warning message for when someone creates an object in a system database (model, master and msdb). This alert can be incremented to send an email to the database team as well as visually notify the user who created the object, among others.
Implementation for a specific database
View sourceFor all users to have access to write to this table, you will need to execute the following commands:
1 2 3 4 | USE [Auditoria] GO GRANT CONNECT TO [guest] GRANT INSERT ON dbo.Alteracao_Objetos TO PUBLIC |
Another note in this code is that this trigger writes data to some tables in the database, meaning that the user who changes any object in the database will need permissions to write the data to that table. For this reason, I added the grant command to the table for role public.
Another way to get around this is to use the EXECUTE AS clause 'login_com_permissao', so that the trigger will be executed with the permission of that EXECUTE AS user, but will record the data of the real user who is counting, avoiding the need to having to create all users in the database and release the permissions, looking like this:
1 2 3 4 5 | CREATE TRIGGER [trgAlteracao_Objetos] ON DATABASE WITH EXECUTE AS 'dirceu.resende' FOR DDL_DATABASE_LEVEL_EVENTS [...] |
Implementation for all databases
The solution presented above must be applied to each database of your server where you wish to log DDL operations. In the code snippet below, I will demonstrate how to apply this trigger to all server databases.
View sourceAs with database Trigger, for all users to have access to write to this table, you will need to execute the following commands:
1 2 3 4 | USE [Auditoria] GO GRANT CONNECT TO [guest] GRANT INSERT ON dbo.Alteracao_Objetos TO PUBLIC |
Another note in this code is that this trigger writes data to some tables in the database, that is, the user who changes objects in the database will need permissions to write the data to that table, as well as having the user created in the database of that table. For this reason, I added the grant command to the table for role public.
Another way to get around this is to use the EXECUTE AS clause 'login_com_permissao', so that the trigger will be executed with the permission of that EXECUTE AS user, but will record the data of the real user who is counting, avoiding the need to having to create all users in the database and release the permissions, looking like this:
1 2 3 4 5 | CREATE TRIGGER [trgAlteracao_Objetos] ON ALL SERVER WITH EXECUTE AS 'dirceu.resende' FOR DDL_DATABASE_LEVEL_EVENTS [...] |
Dirceu, is this trg valid for SQL Server 2005n Standard Edition SP1?
I get some errors.