Guide
SQL Server Audit
SQL Server Audit is built into SQL Server. Logthread reads its events from the Windows Application log of the server, turns them into security fields, and offers detections for them: failed logins, role and permission changes, new or dropped logins, schema changes, changes to the audit itself, and reads of the tables you choose.
It works with SQL Server 2012 SP1 and later on Windows, in every edition, and was checked end to end on SQL Server 2022.
How it works#
- A server audit in SQL Server writes each audited action to the Windows Application log, as event 33205.
- The Logthread Agent's
windows-serverprofile already reads the Application log, so nothing else is installed. The event arrives as servicewindows-serverwithwindows.event.id33205. - The SQL Server Audit parser turns the audit message into fields such as
event.action,user.nameanddb.object. - The SQL Server Audit detections read those fields.
Turn on SQL Server Audit#
Open Integrations → Windows server and tick SQL Server Audit (it needs Event Log and performance, which is on by default). You can also pick sensitive tables (see below), and choose to audit successful logins. The connection page then gives you this T-SQL, filled in from your answers. Run it once on each instance, as a member of sysadmin, in SQL Server Management Studio or sqlcmd:
USE master;
CREATE SERVER AUDIT [Logthread_Audit]
TO APPLICATION_LOG
WITH (QUEUE_DELAY = 1000, ON_FAILURE = CONTINUE);
CREATE SERVER AUDIT SPECIFICATION [Logthread_Audit_Server]
FOR SERVER AUDIT [Logthread_Audit]
ADD (FAILED_LOGIN_GROUP),
ADD (SERVER_ROLE_MEMBER_CHANGE_GROUP),
ADD (DATABASE_ROLE_MEMBER_CHANGE_GROUP),
ADD (SERVER_PERMISSION_CHANGE_GROUP),
ADD (SERVER_OBJECT_PERMISSION_CHANGE_GROUP),
ADD (DATABASE_PERMISSION_CHANGE_GROUP),
ADD (DATABASE_OBJECT_PERMISSION_CHANGE_GROUP),
ADD (SCHEMA_OBJECT_PERMISSION_CHANGE_GROUP),
ADD (SCHEMA_OBJECT_CHANGE_GROUP),
ADD (DATABASE_OBJECT_CHANGE_GROUP),
ADD (SERVER_PRINCIPAL_CHANGE_GROUP),
ADD (DATABASE_PRINCIPAL_CHANGE_GROUP),
ADD (AUDIT_CHANGE_GROUP)
WITH (STATE = ON);
ALTER SERVER AUDIT [Logthread_Audit] WITH (STATE = ON);
GOTo audit every read of sensitive tables, add a database audit specification in each database that holds them. The setup writes these for the tables you name, for example:
USE [Sales];
CREATE DATABASE AUDIT SPECIFICATION [Logthread_Audit_Reads]
FOR SERVER AUDIT [Logthread_Audit]
ADD (SELECT ON OBJECT::[dbo].[Customers] BY [public])
WITH (STATE = ON);
GO- Successful logins (
SUCCESSFUL_LOGIN_GROUP) are off by default: a busy server logs in thousands of times an hour. Tick successful logins too in the setup to add them. Failed logins are always audited. - No extra rights. Writing to the Application log needs nothing from the SQL Server service account. Only the
SECURITY_LOGtarget needs the "Generate security audits" user right and object access auditing; Logthread does not read that target. - Already have an audit? A server can have several. Keep yours, and add this one, or change the names in the commands.
- To check the audit is on:
SELECT name, is_state_enabled FROM sys.server_audits;
Pick the sensitive tables#
Not released yet. You do not have to type table names. The Pick the sensitive tables step of the setup lists the server's databases and tables for you. The agent reads them with the monitoring login you gave for SQL Server: it lists names and approximate row counts only, never reads table data, and runs read-only statements.
- Tick SQL Server and SQL Server Audit and give the SQL Server sign-in as usual. The install command then also adds the agent profile
sqlserver-dam, which lists the tables every six hours. - Run the command the step shows, once, so the monitoring login can see the tables of each user database (it creates a database user for the login, with no access to data):
``sql EXEC sp_MSforeachdb N'IF DB_ID(''?'') > 4 BEGIN USE [?]; IF USER_ID(N''logthread_agent'') IS NULL CREATE USER [logthread_agent] FOR LOGIN [logthread_agent]; END'; ``
With Windows authentication the login is NT AUTHORITY\SYSTEM instead of logthread_agent.
- In Pick the sensitive tables, open a database and tick the tables that hold what you must protect, such as customers, payments and staff. You can tick up to 30 tables. A database the login cannot list says why.
- The T-SQL step then writes one database audit specification per database, for exactly the tables you ticked, like the example above.
The list refreshes every six hours. If a table you expect is missing, check that the login can connect to that database.
Reads far above usual#
Not released yet. Once the parser is on, the step Count reads per login and table opens a log metric, sql_table_reads, filled in: every SELECT on the audited tables, counted by user.name and db.object as the records arrive. Check its preview and create it. The detection SQL login reads a table far above its usual then learns each login and table pair from its own history (its usual reads at each hour of the week) and checks every closed hour: a login that usually reads a table a few times an hour and suddenly reads it hundreds of times is reported. Until the metric exists, the detection says so and links to this step.
Check that events arrive#
The setup's check counts event 33205 in the last 24 hours. To see one at once, sign in once with a wrong password: a failed login is audited. In Explore:
service:windows-server windows.event.id:33205Turn on the parser#
When events have arrived, choose Use parser: SQL Server Audit under Then add on the connection page. It previews the parser on your own events, then activates it for the server's records. The parser only changes Application log records from a SQL Server provider (any source starting with MSSQL, such as MSSQLSERVER$AUDIT or MSSQL$<instance>$AUDIT) with event id 33205; every other record is left alone. It changes nothing already stored. The second check of the setup then counts the parsed events by event.action.
The parser is also in Pipelines → Library under Security, as SQL Server Audit.
The fields#
| Field | From the audit |
|---|---|
event.action | action_id (and class_type for CREATE, ALTER and DROP): login_failed, login, logout, role_member_added, role_member_removed, permission_granted, permission_denied, permission_revoked, principal_created, principal_altered, principal_dropped (logins, users and roles), object_created, object_altered, object_dropped, audit_changed, audit_session_changed, audit_shutdown_on_failure, select, insert, update, delete, execute, impersonated, password_changed, password_reset. Other actions have no event.action and keep sqlserver.audit.action_id |
event.category | authentication, iam, configuration or database |
event.outcome | succeeded: success or failure |
user.name | server_principal_name, or session_server_principal_name when it is empty |
user.target.name | the login, user or role a permission or role membership was given to (target_server_principal_name or target_database_principal_name) |
source.address | client_ip; "local machine" and loopback are not an address. On versions without client_ip, the [CLIENT: address] of a failed login's message |
host.name | the server the agent runs on |
db.name, db.object | database_name; schema_name.object_name |
db.statement | statement, as SQL Server wrote it, cut at 4000 characters |
sqlserver.role.name, sqlserver.role.privileged | for role membership: the role, and whether it is one of sysadmin, securityadmin, serveradmin, setupadmin, processadmin, diskadmin, dbcreator, bulkadmin, db_owner, db_securityadmin, db_accessadmin or db_ddladmin |
sqlserver.audit.* | every other audit field, with Microsoft's names: action_id, class_type, session_id, application_name, host_name (the client's computer), server_instance_name, additional_information and the rest |
observer.vendor, observer.product | Microsoft, SQL Server |
The audited statement is stored as SQL Server wrote it, so it can contain the values in it, such as names in a WHERE clause. Audit reads only of tables whose statements you are allowed to keep, or remove db.statement with a pipeline step after the parser.
Some searches to start from:
sqlserver.audit.action_id:* | stats count() by event.action, user.nameevent.action:login_failed sqlserver.audit.action_id:* | stats count() by user.name, source.addresssqlserver.audit.action_id:* event.category:iamDetections#
Open Security → SIEM. The connection page's last steps link there.
| Detection | Fires on |
|---|---|
| SQL Server failed logins for one login | 10 or more failed logins for the same login in 10 minutes |
| Member added to a privileged SQL Server role | A member added to sysadmin, securityadmin, db_owner or another privileged fixed role (critical) |
| Member added to a SQL Server role | A member added to any other server or database role |
| SQL Server permission granted | A GRANT at the server or in a database (info) |
| SQL Server objects dropped | 3 or more objects dropped by one login in 15 minutes, outside tempdb |
| SQL Server Audit stopped, changed or dropped | An audit or audit specification altered (including STATE = OFF) or dropped (critical) |
| Mass reads of audited SQL Server tables | 100 or more SELECTs by one login on the tables you audit in 15 minutes |
| SQL login read a table it never read before | A login reads an audited table it never read before. Logthread learns the tables each login reads, and checks each closed hour (not released yet) |
| SQL login reads a table far above its usual | A login reads an audited table far more often in an hour than it usually does at that hour of the week, learned per login and table (needs the log metric above; not released yet) |
Failed SQL logins also count in Brute force from one address and Password spraying when they carry a client address.
Ask an AI agent or Pari#
- With MCP:
list_detectionswithquerysqlserverlists the SQL Server detections and whether their data arrives;search_logsruns the searches above, for example{"query": "sqlserver.audit.action_id:* event.category:iam", "from": "7d"}. - Pari answers "how do I turn on SQL Server Audit" from this page, and links to Integrations → Windows server with SQL Server Audit ticked.
Price: what SQL Server Audit costs#
Nothing extra. Audit events are billed as data ingested, ₹85 ($0.90) per GB on every plan, like any other data; the parser and the detections add no charge. Each audited action is one record, so auditing successful logins or the reads of a busy table can send many: check the volume on Administration → Usage, By service (windows-server), and audit only what you need. See Prices.
Troubleshooting#
- No event 33205 arrives. Check the audit is on:
SELECT name, is_state_enabled FROM sys.server_audits;andSELECT name, is_state_enabled FROM sys.server_audit_specifications;. Both must be1. Then look in Event Viewer, Windows Logs → Application, for a source starting withMSSQL(usuallyMSSQLSERVER$AUDIT, orMSSQL$<instance>$AUDIT) and event 33205. If they are there but not in Logthread, check that Event Log and performance is on for the server and that the agent runs (Data in → Host agents → Agents). - Events arrive but have no
event.action. The parser is not on: choose Use parser: SQL Server Audit on the connection page, or check Pipelines for its state. source.addressis empty. The client connected from the server itself (shared memory or loopback), which is not an address, or the SQL Server version writes noclient_ip.- Reads of a table are not audited. The database audit specification is created per database: run its T-SQL in the database that holds the table, and check
SELECT name, is_state_enabled FROM sys.database_audit_specifications;there. - Events are missing after a busy hour. The Application log wrapped before the agent read it: raise its size (see Limits).
Limits#
- Windows and the Application log only. Audits written to
.sqlauditfiles (TO FILE) or to the Security log (TO SECURITY_LOG) are not read, and SQL Server on Linux is not supported. - The Application log has a size limit. On a busy server with reads audited, raise it (Event Viewer, Application, Properties) so events are not overwritten before the agent reads them; the agent reads them within seconds.
- Database audit specifications (the reads of sensitive tables) need SQL Server 2016 SP1 or later in Standard, Web and Express editions; Enterprise has them in every version. Server audit specifications work in every edition.
Remove it#
USE master;
ALTER SERVER AUDIT SPECIFICATION [Logthread_Audit_Server] WITH (STATE = OFF);
DROP SERVER AUDIT SPECIFICATION [Logthread_Audit_Server];
ALTER SERVER AUDIT [Logthread_Audit] WITH (STATE = OFF);
DROP SERVER AUDIT [Logthread_Audit];Drop each database's Logthread_Audit_Reads specification first, in that database. Stopping or dropping the audit fires SQL Server Audit stopped, changed or dropped if it is on.