TraceID to Audit Group Map

28 06 2014

I will update this and make it pretty when I get more time but here is the information for those who need or want it regarding the Trace ID’s and their correlation to SQL Auditing groups

 

Trace ID Audit Equivelant SQL Auditing Group
109 Audit Add DB User Event DATABASE_PRINCIPAL_CHANGE_GROUP
108 Audit Add Login to Server Role Event SERVER_ROLE_MEMBER_CHANGE_GROUP
110 Audit Add Member to DB Role Event DATABASE_ROLE_MEMBER_CHANGE_GROUP
111 Audit Add Role Event DATABASE_PRINCIPAL_CHANGE_GROUP
104 Audit Addlogin Event SERVER_PRINCIPAL_CHANGE_GROUP
112 Audit App Role Change Password Event APPLICATION_ROLE_CHANGE_PASSWORD_GROUP
115 Audit Backup/Restore Event BACKUP_RESTORE_GROUP
117 Audit Change Audit Event AUDIT_CHANGE_GROUP
152 Audit Change Database Owner DATABASE_OWNERSHIP_CHANGE_GROUP
128 Audit Database Management Event DATABASE_CHANGE_GROUP
172 Audit Database Object GDR Event DATABASE_OBJECT_PERMISSION_CHANGE_GROUP
129 Audit Database Object Management Event DATABASE_OBJECT_CHANGE_GROUP
135 Audit Database Object Take Ownership Event DATABASE_OBJECT_OWNERSHIP_CHANGE_GROUP
178 Audit Database Operation Event DATABASE_OPERATION_GROUP
133 Audit Database Principal Impersonation Event DATABASE_PRINCIPAL_IMPERSONATION_GROUP
130 Audit Database Principal Management Event DATABASE_PRINCIPAL_CHANGE_GROUP
102 Audit Database Scope GDR Event DATABASE_PERMISSION_CHANGE_GROUP
116 Audit DBCC Event DBCC_GROUP
14 Audit Login SUCCESSFUL_LOGIN_GROUP
107 Audit Login Change Password Event LOGIN_CHANGE_PASSWORD_GROUP
106 Audit Login Change Property Event SERVER_PRINCIPAL_CHANGE_GROUP
20 Audit Login Failed FAILED_LOGIN_GROUP
105 Audit Login GDR Event SERVER_PRINCIPAL_CHANGE_GROUP
15 Audit Logout LOGOUT_GROUP
118 Audit Object Derived Permission Event SCHEMA_OBJECT_CHANGE_GROUP
103 Audit Schema Object GDR Event SCHEMA_OBJECT_PERMISSION_CHANGE_GROUP
131 Audit Schema Object Management Event SCHEMA_OBJECT_CHANGE_GROUP
153 Audit Schema Object Take Ownership Event SCHEMA_OBJECT_OWNERSHIP_CHANGE_GROUP
175 Audit Server Alter Trace Event T RACE_CHANGE_GROUP
171 Audit Server Object GDR Event SERVER_OBJECT_PERMISSION_CHANGE_GROUP
176 Audit Server Object Management Event SERVER_OBJECT_CHANGE_GROUP
134 Audit Server Object Take Ownership Event SERVER_OBJECT_OWNERSHIP_CHANGE_GROUP
173 Audit Server Operation Event SERVER_OPERATION_GROUP
132 Audit Server Principal Impersonation Event SERVER_PRINCIPAL_IMPERSONATION_GROUP
177 Audit Server Principal Management Event SERVER_PRINCIPAL_CHANGE_GROUP
170 Audit Server Scope GDR Event SERVER_PERMISSION_CHANGE_GROUP
18 Audit Server Starts And Stops SERVER_STATE_CHANGE_GROUP
113 Audit Statement Permission Event SCHEMA_OBJECT_CHANGE_GROUP

 

Advertisements




SQL Server 2012 Master Query

10 06 2014

Sorry it took so long to get this query completed. Fo those that have managed to find this article know there is a ton of information in the STIG’s for SQL Server 2012! I ended up rewriting about half of the queries provided in the STIG as they either did not work as is or they could have been implemented better, so now they are! There is alot of duplication of queries going on in this script, I decided to write it this way so that the information returned is in the same order as the STIG is to make matching up everything a little easier. Also please note that this is my first crack at all the code and while it runs, I may have missed something somewhere, please let me know!

Download a docx file with the code (wordpress doesn’t allow .txt)

SQL Server 2012 Master Query Word Document

*** Prior to running the query set your output to text and change the query options for text output to Tab Delimited ***


/* Declare variables used in script */

DECLARE @id Int --Used for traceid loop queries

PRINT 'SQL Server 2012 DB V1R2 STIG Checks'

PRINT 'Rule Title: SQL Server must be protected from unauthorized access by developers.';
PRINT 'STIG ID: SQL2-00-009200'

SELECT name AS 'Account Name'
, create_date AS 'Account Create Date'
, LOGINPROPERTY(name, 'PasswordLastSetTime') AS 'Password Last Set on'
FROM sys.server_principals
WHERE NOT TYPE IN ('C', 'R', 'U') -- ('C', 'G', 'K', 'R', 'S', 'U')
AND NOT name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##')
AND sid CONVERT(VARBINARY(85), 0x01) -- no 'sa' account
AND is_disabled 1
ORDER BY name;

PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server utilizing Discretionary Access Control (DAC) must enforce a policy that limits propagation of access rights.'
PRINT 'STIG ID: SQL2-00-011050'
Select * from sys.server_permissions
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must provide audit record generation capability for organization-defined auditable events within the database.'
PRINT 'STIG ID: SQL2-00-011200'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')
OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c

PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must be monitored to discover unauthorized changes to functions.'
PRINT 'STIG ID: SQL2-00-014900'

PRINT 'This STIG item will require the name of a job that checks for changes to functions.'
PRINT 'use the code below if you know what your job name is'
PRINT '/* Start code block */
DECLARE @Job_title varchar(20)
-- user-defined VARIABLE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
SET @Job_title = ''''
-- user-defined VARIABLE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

EXEC sp_help_job
@job_name = @Job_title
GO
/* end of code block */'

PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must be monitored to discover unauthorized changes to triggers.'
PRINT 'STIG ID: SQL2-00-015100'

PRINT 'This STIG item will require the name of a job that checks for changes to functions.'
PRINT 'use the code below if you know what your job name is'
PRINT '/* Start code block */
DECLARE @Job_title varchar(20)
-- user-defined VARIABLE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
SET @Job_title = ''''
-- user-defined VARIABLE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

EXEC sp_help_job
@job_name = @Job_title
GO
/* end of code block */'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must be monitored to discover unauthorized changes to stored procedures.'
PRINT 'STIG ID: SQL2-00-015200'

PRINT 'This STIG item will require the name of a job that checks for changes to functions.'
PRINT 'use the code below if you know what your job name is'
PRINT '/* Start code block */

DECLARE @Job_title varchar(20)
-- user-defined VARIABLE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
SET @Job_title = ''''
-- user-defined VARIABLE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

EXEC sp_help_job
@job_name = @Job_title
GO
/* end of code block */'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must encrypt information stored in the database.'
PRINT 'STIG ID: SQL2-00-019300'

EXEC sp_MSforeachdb '
SELECT ''?'' AS ''database ?'', *
FROM ?.sys.symmetric_keys
ORDER BY name, algorithm_desc
'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must implement required cryptographic protections using cryptographic modules complying with applicable federal laws, Executive Orders, directives, policies, regulations, standards, and guidance.'
PRINT 'STIG ID: SQL2-00-019500'

EXEC sp_MSforeachdb '
SELECT ''?'' AS ''database ?'', *
FROM ?.sys.symmetric_keys
ORDER BY name, algorithm_desc
'

PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must employ cryptographic mechanisms preventing the unauthorized disclosure of information at rest, unless the data is otherwise protected by alternative physical measures.'
PRINT 'STIG ID: SQL2-00-021400'
SELECT name AS 'Database Name', is_encrypted AS 'Encryption Status' FROM [master].sys.databases
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must protect against or limit the effects of the organization-defined types of Denial of Service (DoS) attacks.'
PRINT 'STIG ID: SQL2-00-022000'
PRINT '*** CHANGES ARE BEING MADE TO YOUR SYSTEM ***'
PRINT 'Use the below code to configure your system to required STIG settings'

PRINT '/* start code block */
USE MASTER;
EXEC sys.sp_configure N''show advanced options'', N''1'';
RECONFIGURE WITH OVERRIDE;
EXEC sys.sp_configure N''user connections'';
EXEC sys.sp_configure N''show advanced options'', N''0'';
RECONFIGURE WITH OVERRIDE;
/* end code block */'

PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server default account public must be removed from each database.'
PRINT 'STIG ID: SQL2-00-023400'
EXEC sp_MSforeachdb '

IF NOT ''?'' IN (''master'', ''tempdb'', ''model'', ''msdb'')
BEGIN
USE ?
SELECT ''?'' AS ''Database'', su.name AS ''db Account Name'', s.name AS ''SQL Server Account Name''
FROM sys.sysusers AS su
LEFT JOIN sys.server_principals AS s ON su.sid = s.sid
WHERE ( su.name like ''publ%'' OR s.name like ''publ%'') AND NOT su.sid = CONVERT(VARBINARY(85), 0x)
END'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server job/batch queues must be reviewed regularly to detect unauthorized SQL Server job submissions.'
PRINT 'STIG ID: SQL2-00-023500'

SELECT name
FROM master.sys.procedures
WHERE is_auto_executed = 1
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server default account guest must be removed from each database.'
PRINT 'STIG ID: SQL2-00-023800'

EXEC sp_MSforeachdb '
IF NOT ''?'' IN (''master'', ''tempdb'', ''model'', ''msdb'')
BEGIN
USE ?
SELECT ''?'' AS ''Database''
, su.name AS ''db Account Name''
, sp.name AS ''SQL Server Account Name''
FROM sys.sysusers su
LEFT JOIN sys.server_principals sp
ON su.sid = sp.sid
WHERE ( su.name like ''gues%''
OR sp.name like ''gues%'' )
AND NOT su.sid = CONVERT(VARBINARY(85), 0x00)
END '
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: The Database Master Key encryption password must meet DoD password complexity requirements.'
PRINT 'STIG ID: SQL2-00-024000'

EXEC sp_MSforeachdb '
USE ?
SELECT COUNT(name)
FROM sys.symmetric_keys s, sys.key_encryptions k
WHERE s.name = ''##MS_DatabaseMasterKey##''
AND s.symmetric_key_id = k.key_id
AND k.crypt_type = ''ESKP'''
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: The Database Master Key must be encrypted by the Service Master Key where required.'
PRINT 'STIG ID: SQL2-00-024100'
SELECT name
FROM [master].sys.databases
WHERE is_master_key_encrypted_by_server = 1
AND owner_sid 1
AND state = 0
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: Database Master Key passwords must not be stored in credentials within the database.'
PRINT 'STIG ID: SQL2-00-024200'
SELECT COUNT(credential_id)
FROM [master].sys.master_key_passwords
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: Symmetric keys must use a DoD certificate to encrypt the key.'
PRINT 'STIG ID: SQL2-00-024300 '
EXEC sp_MSforeachdb '
USE ?
SELECT s.name, k.crypt_type_desc
FROM sys.symmetric_keys s, sys.key_encryptions k
WHERE s.symmetric_key_id = k.key_id
AND k.crypt_type IN (''KSKP'', ''ESKS'')
AND s.principal_id 1
ORDER BY s.name, k.crypt_type_desc'
PRINT '---------------------------------------------------------------------------------------';
PRINT 'Complete SQL Server 2012 DB V1R2 STIG Checks'
PRINT '';
PRINT '';

PRINT 'SQL Server 2012 Instance V1R2 STIG Checks'
PRINT 'Rule Title: SQL Server must ensure that remote sessions that access an organization-defined list of security functions and security-relevant information are audited.'
PRINT 'STIG ID: SQL2-00-001600'

DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c

PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must automatically audit account modification.'
PRINT 'STIG ID: SQL2-00-001900'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce non-DAC policies over users and resources where the policy rule set for each policy specifies access control information (i.e., position, nationality, age, project, time of day).'
PRINT 'STIG ID: SQL2-00-002200'
DECLARE @admin_Account_name sysname
SET @admin_Account_name = 'NO admin ACCOUNT found'
DECLARE @server_name sysname
SET @server_name = 'NO Server found'

SELECT @server_name = name FROM sys.servers
WHERE server_id = 0
SET @admin_Account_name = @server_name + '\Administrator'

SELECT pe.grantee_principal_id
, pr.type AS 'Grantee_Type'
, pr.name AS 'Grantee_Name'
, pe.type
, pe.permission_name
, pe.state
, pe.state_desc
FROM sys.server_permissions pe
JOIN sys.server_principals pr
ON pe.grantee_principal_id = pr.principal_id
JOIN sys.server_principals ps
ON pe.grantor_principal_id = ps.principal_id
LEFT JOIN sys.server_principals us
ON us.principal_id = pe.major_id
WHERE pr.type IN ('K', 'S', 'U')
AND pe.grantee_principal_id > 10
AND NOT pr.name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##',
'NT AUTHORITY\NETWORK SERVICE', 'NT AUTHORITY\SYSTEM', 'NT SERVICE\MSSQLSERVER',
'NT SERVICE\SQLSERVERAGENT', 'NT SERVICE\SQLWriter', 'NT SERVICE\Winmgmt')
AND NOT pr.name = @admin_Account_name
ORDER BY CASE pr.type
WHEN 'K' THEN 1
WHEN 'S' THEN 2
WHEN 'U' THEN 3
ELSE 4
END

SELECT @server_name = name FROM sys.servers
WHERE server_id = 0
SET @admin_Account_name = @server_name + '\Administrator'

SELECT pe.grantee_principal_id
, pr.type AS 'Grantee_Type'
, pr.name AS 'Grantee_Name'
, pe.type
, pe.permission_name
, pe.state
, pe.state_desc
FROM sys.server_permissions pe
JOIN sys.server_principals pr
ON pe.grantee_principal_id = pr.principal_id
JOIN sys.server_principals ps
ON pe.grantor_principal_id = ps.principal_id
LEFT JOIN sys.server_principals us
ON us.principal_id = pe.major_id
WHERE pr.type IN ('R')
AND pe.grantee_principal_id > 10
AND NOT pr.name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##',
'NT AUTHORITY\NETWORK SERVICE', 'NT AUTHORITY\SYSTEM', 'NT SERVICE\MSSQLSERVER',
'NT SERVICE\SQLSERVERAGENT', 'NT SERVICE\SQLWriter', 'NT SERVICE\Winmgmt')
AND NOT pr.name = @admin_Account_name
ORDER BY CASE pe.state
WHEN 'D' THEN 1
WHEN 'W' THEN 2
WHEN 'G' THEN 3
ELSE 4
END
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict Alter server state permissions to only authorized roles.'
PRINT 'STIG ID: SQL2-00-002300'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter Server State'

PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any event session permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-002400'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any event session'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any event notification permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-002500'
Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any event notification'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any endpoint permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-002600'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any endpoint'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any database permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-002700'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any database'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any credential permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-002800'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any credential'

PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any connection permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-002900'
Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any connection'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the View any definition permission.'
PRINT 'STIG ID: SQL2-00-003000'
Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'view any definition'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any server role permission.'
PRINT 'STIG ID: SQL2-00-003100 '
Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any server role'

PRINT '---------------------------------------------------------------------------------------';
PRINT 'Rule Title: SQL Server must not grant users direct access control to the View server state permission.'
PRINT 'STIG ID: SQL2-00-003200'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'view server state'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Create any database permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-003300'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Create any database'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Authenticate server permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-003400'
Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Authenticate server'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter Settings permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-003500'
Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter settings'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any server role permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-003600'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any server role'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Create server role permission.'
PRINT 'STIG ID: SQL2-00-003700'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'create server role'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Control server permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-003800'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Control server'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Unsafe assembly permission.'
PRINT 'STIG ID: SQL2-00-003900'

Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Unsafe assembly'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Unsafe assembly permission.'
PRINT 'STIG ID: SQL2-00-003900'
Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Control Server'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter trace permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-004000'
Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter trace'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the View server state permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-004100'
Select prin.name as 'Name' from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'View server state'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Shutdown permission.'
PRINT 'STIG ID: SQL2-00-004200'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'shutdown'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any linked server permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-004300'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any linked server'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any login permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-004500'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any logon'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any availability group permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-004600'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any availability group'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any login permission.'
PRINT 'STIG ID: SQL2-00-00470'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any login'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the External access assembly permission.'
PRINT 'STIG ID: SQL2-00-004800'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'External access assembly'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter resources permission.'
PRINT 'STIG ID: SQL2-00-004900'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter resources'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Create trace event notification permission.'
PRINT 'STIG ID: SQL2-00-005000'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Create trace event notification'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter Settings permission.'
PRINT 'STIG ID: SQL2-00-005100'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter Settings'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter trace permission.'
PRINT 'STIG ID: SQL2-00-005200'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter trace'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any event session permission.'
PRINT 'STIG ID: SQL2-00-005300'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any event session'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Connect SQL permission.'
PRINT 'STIG ID: SQL2-00-005400'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Connect SQL'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any linked server permission.'
PRINT 'STIG ID: SQL2-00-005500'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any linked server'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter resources permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-005600'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter resources'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Administer bulk operations permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-005700'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Administer bulk operations'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Control server permission.'
PRINT 'STIG ID: SQL2-00-005800'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Control server'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Create any database permission.'
PRINT 'STIG ID: SQL2-00-005900'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Create any database'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Create availability group permission.'
PRINT 'STIG ID: SQL2-00-006000'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Create availability group'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Create DDL event notification permission.'
PRINT 'STIG ID: SQL2-00-006100'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Create DDL event notification'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Create endpoint permission.'
PRINT 'STIG ID: SQL2-00-006200'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Create endpoint'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Administer bulk operations permission.'
PRINT 'STIG ID: SQL2-00-006300'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Administer bulk operations'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Authenticate server permission.'
PRINT 'STIG ID: SQL2-00-006400 '
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Authenticate server'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the View any definition permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-006500'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'View any definition'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Alter any server audit permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-006600 '
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Alter any server audit'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Create availability group permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-006700'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Create availability group'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Create DDL event notification permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-006800'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Create DDL event notification'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Create endpoint permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-006900'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Create endpoint'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Create server role permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-007000'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Create server role'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Create trace event notification permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-007100'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Create trace event notification'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the External access assembly permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-007200'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'External access assembly'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Shutdown permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-007300'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Shutdown'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any server audit permission.'
PRINT 'STIG ID: SQL2-00-007400'
Select prin.name as 'Name', perm.permission_name from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any server audit'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the View any database permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-007500'
Select prin.name as 'Name', perm.permission_name as 'Permission', perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'View any database'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any event notification permission.'
PRINT 'STIG ID: SQL2-00-007600'
Select prin.name as 'Name', perm.permission_name, perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any event notification'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Connect SQL permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-007700'
Select prin.name as 'Name', perm.permission_name as 'Permission', perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Connect SQL'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter server state permission.'
PRINT 'STIG ID: SQL2-00-007800'
Select prin.name as 'Name', perm.permission_name, perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter server state'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any availability group permission.'
PRINT 'STIG ID: SQL2-00-007900'
Select prin.name as 'Name', perm.permission_name, perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any availability group'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any connection permission.'
PRINT 'STIG ID: SQL2-00-008000'
Select prin.name as 'Name', perm.permission_name, perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any connection'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any credential permission.'
PRINT 'STIG ID: SQL2-00-008100'
Select prin.name as 'Name', perm.permission_name, perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any credential'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any database permission.'
PRINT 'STIG ID: SQL2-00-008200 '
Select prin.name as 'Name', perm.permission_name, perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any database'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must not grant users direct access control to the Alter any endpoint permission.'
PRINT 'STIG ID: SQL2-00-008300'
Select prin.name as 'Name', perm.permission_name, perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where perm.permission_name = 'Alter any endpoint'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce access control policies to restrict the Unsafe assembly permission to only authorized roles.'
PRINT 'STIG ID: SQL2-00-008400'
Select prin.name as 'Name', perm.permission_name as 'Permission', perm.state_desc as 'State'
from sys.server_principals prin
inner join sys.server_permissions perm on prin.principal_id = perm.grantee_principal_id
where prin.type = 'R' AND perm.permission_name = 'Unsafe assembly'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce DAC policy allowing users to specify and control sharing by named individuals, groups of individuals, or by both; limiting propagation of access rights; and including or excluding access to the granularity of a single user.'
PRINT 'STIG ID: SQL2-00-008500'
PRINT 'Check 1 Users'
SELECT @server_name = name FROM sys.servers
WHERE server_id = 0
SET @admin_Account_name = @server_name + '\Administrator'

SELECT pe.grantee_principal_id
, pr.type AS 'Grantee_Type'
, pr.name AS 'Grantee_Name'
, pe.type
, pe.permission_name
, pe.state
, pe.state_desc
FROM sys.server_permissions pe
JOIN sys.server_principals pr
ON pe.grantee_principal_id = pr.principal_id
JOIN sys.server_principals ps
ON pe.grantor_principal_id = ps.principal_id
LEFT JOIN sys.server_principals us
ON us.principal_id = pe.major_id
WHERE pr.type IN ('K', 'S', 'U')
AND pe.grantee_principal_id > 10
AND NOT pr.name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##',
'NT AUTHORITY\NETWORK SERVICE', 'NT AUTHORITY\SYSTEM', 'NT SERVICE\MSSQLSERVER',
'NT SERVICE\SQLSERVERAGENT', 'NT SERVICE\SQLWriter', 'NT SERVICE\Winmgmt')
AND NOT pr.name = @admin_Account_name
ORDER BY CASE pr.type
WHEN 'K' THEN 1
WHEN 'S' THEN 2
WHEN 'U' THEN 3
ELSE 4
END

PRINT 'Check 2 Roles'
SELECT @server_name = name FROM sys.servers
WHERE server_id = 0
SET @admin_Account_name = @server_name + '\Administrator'

SELECT pe.grantee_principal_id
, pr.type AS 'Grantee_Type'
, pr.name AS 'Grantee_Name'
, pe.type
, pe.permission_name
, pe.state
, pe.state_desc
FROM sys.server_permissions pe
JOIN sys.server_principals pr
ON pe.grantee_principal_id = pr.principal_id
JOIN sys.server_principals ps
ON pe.grantor_principal_id = ps.principal_id
LEFT JOIN sys.server_principals us
ON us.principal_id = pe.major_id
WHERE pr.type IN ('R')
AND pe.grantee_principal_id > 10
AND NOT pr.name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##',
'NT AUTHORITY\NETWORK SERVICE', 'NT AUTHORITY\SYSTEM', 'NT SERVICE\MSSQLSERVER',
'NT SERVICE\SQLSERVERAGENT', 'NT SERVICE\SQLWriter', 'NT SERVICE\Winmgmt')
AND NOT pr.name = @admin_Account_name
ORDER BY CASE pe.state
WHEN 'D' THEN 1
WHEN 'W' THEN 2
WHEN 'G' THEN 3
ELSE 4
END
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must enforce separation of duties through assigned information access authorizations.'
PRINT 'STIG ID: SQL2-00-008800'
PRINT 'Check 1 Users'
SELECT @server_name = name FROM sys.servers
WHERE server_id = 0
SET @admin_Account_name = @server_name + '\Administrator'

SELECT pe.grantee_principal_id
, pr.type AS 'Grantee_Type'
, pr.name AS 'Grantee_Name'
, pe.type
, pe.permission_name
, pe.state
, pe.state_desc
FROM sys.server_permissions pe
JOIN sys.server_principals pr
ON pe.grantee_principal_id = pr.principal_id
JOIN sys.server_principals ps
ON pe.grantor_principal_id = ps.principal_id
LEFT JOIN sys.server_principals us
ON us.principal_id = pe.major_id
WHERE pr.type IN ('K', 'S', 'U')
AND pe.grantee_principal_id > 10
AND NOT pr.name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##',
'NT AUTHORITY\NETWORK SERVICE', 'NT AUTHORITY\SYSTEM', 'NT SERVICE\MSSQLSERVER',
'NT SERVICE\SQLSERVERAGENT', 'NT SERVICE\SQLWriter', 'NT SERVICE\Winmgmt')
AND NOT pr.name = @admin_Account_name
ORDER BY CASE pr.type
WHEN 'K' THEN 1
WHEN 'S' THEN 2
WHEN 'U' THEN 3
ELSE 4
END

PRINT 'Check 2 Roles'
SELECT @server_name = name FROM sys.servers
WHERE server_id = 0
SET @admin_Account_name = @server_name + '\Administrator'

SELECT pe.grantee_principal_id
, pr.type AS 'Grantee_Type'
, pr.name AS 'Grantee_Name'
, pe.type
, pe.permission_name
, pe.state
, pe.state_desc
FROM sys.server_permissions pe
JOIN sys.server_principals pr
ON pe.grantee_principal_id = pr.principal_id
JOIN sys.server_principals ps
ON pe.grantor_principal_id = ps.principal_id
LEFT JOIN sys.server_principals us
ON us.principal_id = pe.major_id
WHERE pr.type IN ('R')
AND pe.grantee_principal_id > 10
AND NOT pr.name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##',
'NT AUTHORITY\NETWORK SERVICE', 'NT AUTHORITY\SYSTEM', 'NT SERVICE\MSSQLSERVER',
'NT SERVICE\SQLSERVERAGENT', 'NT SERVICE\SQLWriter', 'NT SERVICE\Winmgmt')
AND NOT pr.name = @admin_Account_name
ORDER BY CASE pe.state
WHEN 'D' THEN 1
WHEN 'W' THEN 2
WHEN 'G' THEN 3
ELSE 4
END
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must restrict access to sensitive information to authorized user roles.'
PRINT 'STIG ID: SQL2-00-009000'

SELECT @server_name = name FROM sys.servers
WHERE server_id = 0
SET @admin_Account_name = @server_name + '\Administrator'

SELECT pe.grantee_principal_id
, pr.type AS 'Grantee_Type'
, pr.name AS 'Grantee_Name'
, pe.type
, pe.permission_name
, pe.state
, pe.state_desc
FROM sys.server_permissions pe
JOIN sys.server_principals pr
ON pe.grantee_principal_id = pr.principal_id
JOIN sys.server_principals ps
ON pe.grantor_principal_id = ps.principal_id
LEFT JOIN sys.server_principals us
ON us.principal_id = pe.major_id
WHERE pr.type IN ('R')
AND pe.grantee_principal_id > 10
AND NOT pr.name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##',
'NT AUTHORITY\NETWORK SERVICE', 'NT AUTHORITY\SYSTEM', 'NT SERVICE\MSSQLSERVER',
'NT SERVICE\SQLSERVERAGENT', 'NT SERVICE\SQLWriter', 'NT SERVICE\Winmgmt')
AND NOT pr.name = @admin_Account_name
ORDER BY CASE pe.state
WHEN 'D' THEN 1
WHEN 'W' THEN 2
WHEN 'G' THEN 3
ELSE 4
END
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: Administrators must utilize a separate, distinct administrative account when performing administrative activities, accessing database security functions, or accessing security-relevant information within SQL Server.'
PRINT 'STIG ID: SQL2-00-009600'
SELECT SP1.[name] AS 'Login', 'Role: ' + SP2.[name] COLLATE DATABASE_DEFAULT AS 'ServerPermission'
FROM sys.server_principals SP1
JOIN sys.server_role_members SRM
ON SP1.principal_id = SRM.member_principal_id
JOIN sys.server_principals SP2
ON SRM.role_principal_id = SP2.principal_id
UNION ALL
SELECT SP.[name] AS 'Login' , SPerm.state_desc + ' ' + SPerm.permission_name COLLATE DATABASE_DEFAULT AS 'ServerPermission'
FROM sys.server_principals SP
JOIN sys.server_permissions SPerm
ON SP.principal_id = SPerm.grantee_principal_id
ORDER BY [Login], [ServerPermission]
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server auditing configuration maximum file size must be configured to reduce the likelihood of storage capacity being exceeded, while meeting organization-defined auditing requirements.'
PRINT 'STIG ID: SQL2-00-010400'
Select SUM(max_size) from sys.traces
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server auditing configuration maximum number of files must be configured to reduce the likelihood of storage capacity being exceeded, while meeting organization-defined auditing requirements.'
PRINT 'STIG ID: SQL2-00-010500'
Select SUM(max_size * max_files) from sys.traces
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server utilizing Discretionary Access Control (DAC) must enforce a policy that limits propagation of access rights.'
PRINT 'STIG ID: SQL2-00-011000'
Select * from sys.server_permissions where state_desc != 'GRANT'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server utilizing Discretionary Access Control (DAC) must enforce a policy that includes or excludes access to the granularity of a single user.'
PRINT 'STIG ID: SQL2-00-011100'
PRINT 'Check 1 Users'
SELECT pe.grantee_principal_id
, pr.type AS 'Grantee_Type'
, pr.name AS 'Grantee_Name'
, pe.type
, pe.permission_name
, pe.state
, pe.state_desc
FROM sys.server_permissions pe
JOIN sys.server_principals pr
ON pe.grantee_principal_id = pr.principal_id
JOIN sys.server_principals ps
ON pe.grantor_principal_id = ps.principal_id
LEFT JOIN sys.server_principals us
ON us.principal_id = pe.major_id
WHERE pr.type IN ('K', 'S', 'U')
AND pe.grantee_principal_id > 10
AND NOT pr.name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##',
'NT AUTHORITY\NETWORK SERVICE', 'NT AUTHORITY\SYSTEM', 'NT SERVICE\MSSQLSERVER',
'NT SERVICE\SQLSERVERAGENT', 'NT SERVICE\SQLWriter', 'NT SERVICE\Winmgmt')
AND NOT pr.name = @admin_Account_name
ORDER BY CASE pr.type
WHEN 'K' THEN 1
WHEN 'S' THEN 2
WHEN 'U' THEN 3
ELSE 4
END

PRINT 'Check 2 Roles'
SELECT @server_name = name FROM sys.servers
WHERE server_id = 0
SET @admin_Account_name = @server_name + '\Administrator'

SELECT pe.grantee_principal_id
, pr.type AS 'Grantee_Type'
, pr.name AS 'Grantee_Name'
, pe.type
, pe.permission_name
, pe.state
, pe.state_desc
FROM sys.server_permissions pe
JOIN sys.server_principals pr
ON pe.grantee_principal_id = pr.principal_id
JOIN sys.server_principals ps
ON pe.grantor_principal_id = ps.principal_id
LEFT JOIN sys.server_principals us
ON us.principal_id = pe.major_id
WHERE pr.type IN ('R')
AND pe.grantee_principal_id > 10
AND NOT pr.name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##',
'NT AUTHORITY\NETWORK SERVICE', 'NT AUTHORITY\SYSTEM', 'NT SERVICE\MSSQLSERVER',
'NT SERVICE\SQLSERVERAGENT', 'NT SERVICE\SQLWriter', 'NT SERVICE\Winmgmt')
AND NOT pr.name = @admin_Account_name
ORDER BY CASE pe.state
WHEN 'D' THEN 1
WHEN 'W' THEN 2
WHEN 'G' THEN 3
ELSE 4
END
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must generate audit records for the DoD-selected list of auditable events.'
PRINT 'STIG ID: SQL2-00-011400'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must produce audit records containing sufficient information to establish what type of events occurred.'
PRINT 'STIG ID: SQL2-00-011800'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must produce audit records containing sufficient information to establish when (date and time) the events occurred.'
PRINT 'STIG ID: SQL2-00-011900'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must produce audit records containing sufficient information to establish where the events occurred.'
PRINT 'STIG ID: SQL2-00-012000'
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must produce audit records containing sufficient information to establish the sources (origins) of the events.'
PRINT 'STIG ID: SQL2-00-012100'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must produce audit records containing sufficient information to establish the outcome (success or failure) of the events.'
PRINT 'STIG ID: SQL2-00-012200'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must produce audit records containing sufficient information to establish the identity of any user/subject associated with the event.'
PRINT 'STIG ID: SQL2-00-012300'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must include organization-defined additional, more detailed information in the audit records for audit events identified by type, location, or subject.'
PRINT 'STIG ID: SQL2-00-012400'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must shutdown immediately in the event of an audit failure, unless an alternative audit capability exists.'
PRINT 'STIG ID: SQL2-00-012800'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must audit attempts to bypass access controls.'
PRINT 'STIG ID: SQL2-00-013400'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must protect audit information from any type of unauthorized access.'
PRINT 'STIG ID: SQL2-00-013600'
select path from sys.traces
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must protect audit information from unauthorized modification.'
PRINT 'STIG ID: SQL2-00-013700'
select path from sys.traces
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must protect audit information from unauthorized deletion.'
PRINT 'STIG ID: SQL2-00-013800'
select path from sys.traces
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must have the publicly available NorthWind sample database removed.'
PRINT 'STIG ID: SQL2-00-016200'
SELECT name from sysdatabases where name like 'Northwind%'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must have the publicly available AdventureWorks sample database removed.'
PRINT 'STIG ID: SQL2-00-016300'
SELECT name from sysdatabases where name like 'AdventureWorks%'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server default account sa must be disabled.'
PRINT 'STIG ID: SQL2-00-017100'
-- since account was renamed, query will look for account status of sid 0x01 the default sid for sa
Select name, is_disabled as 'Disabled' from sys.sql_logins where sid=0x01
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: Access to xp_cmdshell must be disabled.'
PRINT 'STIG ID: SQL2-00-017200'

Print 'Run the following query to configure your server for this requirement'
PRINT '/* start code block */
EXEC SP_CONFIGURE ''show advanced option'', ''1'';
RECONFIGURE WITH OVERRIDE;
EXEC SP_CONFIGURE ''xp_cmdshell'';
/* end code block */'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must specifically prohibit or restrict the use of unauthorized functions and services in each instance.'
PRINT 'STIG ID: SQL2-00-017300'
EXEC sp_MSforeachdb '
DECLARE @nCount integer

SELECT @nCount = Count(*)
FROM ?.sys.objects
WHERE type in (''FN'', ''P'')
AND is_ms_shipped 1

IF @nCount > 0
SELECT ''?'' AS ''Database Name'', *
FROM ?.sys.objects
WHERE type in (''FN'', ''P'')
AND is_ms_shipped 1 '
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must recover to a known state that is verifiable.'
PRINT 'STIG ID: SQL2-00-017500'
EXEC sp_MSforeachdb '
SELECT ''?'' AS ''database name''
, name AS ''log file name''
, physical_name AS ''log file location and name''
, state_desc
, size
, max_size
, growth
, is_percent_growth
FROM ?.sys.database_files
WHERE type_desc = ''LOG''
AND state = 0
'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must have transaction logging enabled.'
PRINT 'STIG ID: SQL2-00-017600'
Select * FROM sys.database_files
WHERE type_desc = 'LOG'
AND state_desc = 'online'
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must uniquely identify and authenticate organizational users (or processes acting on behalf of organizational users).'
PRINT 'STIG ID: SQL2-00-018400'
SELECT name AS 'Account Name'
, CASE is_disabled WHEN 1 THEN 'Yes' ELSE '' END AS 'Is Disabled'
, create_date AS 'Account Create Date'
, LOGINPROPERTY(name, 'PasswordLastSetTime') AS 'Password Last Set on'
FROM sys.server_principals
WHERE NOT TYPE IN ('C', 'R', 'U') -- ('C', 'G', 'K', 'R', 'S', 'U')
AND NOT name IN ('##MS_PolicyEventProcessingLogin##', '##MS_PolicyTsqlExecutionLogin##')
AND sid CONVERT(VARBINARY(85), 0x01) -- no 'sa' account
ORDER BY name
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must ensure users are authenticated with an individual authenticator prior to using a group authenticator.'
PRINT 'STIG ID: SQL2-00-018500'
Select uid, name, roles from sys.sysusers
where uid > 16000
and name not in
('db_owner', 'db_accessadmin',
'db_securityadmin', 'db_ddladmin',
'db_backupoperator', 'db_datareader',
'db_datawriter', 'db_denydatareader','db_denydatawriter')
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server default account sa must have its password changed.'
PRINT 'STIG ID: SQL2-00-018800'
SELECT name AS 'Account'
, LOGINPROPERTY(name, 'PasswordLastSetTime') AS 'Time of Last Password Change'
FROM sys.sql_logins
WHERE sid = 0x01
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must protect data at rest and ensure confidentiality and integrity of data.'
PRINT 'STIG ID: SQL2-00-021300'
SELECT name as 'Database Name', is_encrypted as 'Encrypted' FROM [master].sys.databases
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must notify appropriate individuals when accounts are modified.'
PRINT 'STIG ID: SQL2-00-023300'
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT DISTINCT traceid FROM ::FN_TRACE_GETINFO('0')

OPEN c
FETCH NEXT FROM c INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT N'Showing results for Trace ID: ' + CAST(@id as nvarchar(3));
SELECT DISTINCT(eventid) FROM ::FN_TRACE_GETEVENTINFO(@id)
FETCH NEXT FROM c INTO @id
END
CLOSE c
DEALLOCATE c
PRINT '---------------------------------------------------------------------------------------';

PRINT 'Rule Title: SQL Server must protect against an individual using a group account from falsely denying having performed a particular action.'
PRINT 'STIG ID: SQL2-00-023700'
Select name, principal_id, type_desc from sys.server_principals
PRINT '---------------------------------------------------------------------------------------';





Error initializing transaction logs on restore

8 03 2014

An issue created through the use of Transparent Data Encryption (TDE)

To sandbox a set of databases for a specific application a named instance was created on the SQL server. The Original instance had TDE encryption enabled and all databases were encrypted. After creating the new instance, the databases to be moved were decrypted.

ALTER DATABASE <DATABASE NAME>  SET ENCRYPTION OFF

A backup of each database was then taken to be restored on to the new instance. This practice was used over a detach / re-attach method as testing was needed against the new instance in preparation to migrate the application to the new instance.
Attempting to restore the databases I received the following errors in the SQL error log, the restored databases did not become functional due to this issue.

Error: 3283, Severity: 16, State: 1.
The file “XXXXX_log” failed to initialize correctly. Examine the error logs for more details.

After investigation it was reported that this was a known error that occurred on databases that were encrypted by TDE upon restoring them to a new server or instance. Microsoft recommends applying Cumulative Update 4 to resolve the issue. After applying the latest Cumulative Update from Microsoft, Update 11, the problem still persisted.
Further research indicated that although the databases had been decrypted they would require the original certificate they were encrypted with to be restored into the new instance or server. The solution is to implement TDE on the new instance. In implementing TDE on the new instance the standard procedure was followed except with the cvaveat of the certificate. A new one was not genereated, instead the originating database servers certificate was was imported during the configuration.
It is important to note that when TDE is used, backups of keys and certificates are necessary. Store those backups in multiple locations. Failure to take appropriate precautions will result in catastrophic data loss.

/* create a backup of the service master key */
BACKUP SERVICE MASTER KEY TO FILE = 'C:\temp\service_master_key_YYYY-MM-DD.bak' ENCRYPTION BY PASSWORD = ''
GO

/* Create master key for the new instance */
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '';
GO

/* Backup the new master key */
BACKUP MASTER KEY TO FILE = ' C:\temp\master_key_YYYY-MM-DD.bak' ENCRYPTION BY PASSWORD = ''
GO

/* Here we import the certificate from the original server */

CREATE CERTIFICATE
FROM FILE = ' C:\temp\certificate.cer'

/* We need to use the original private key we used to encrypt the certificate */
WITH PRIVATE KEY (FILE = ' C:\temp\private_key.pvk', DECRYPTION BY PASSWORD = '');
GO

/* Backup the certificate for the new instance or server */
BACKUP CERTIFICATE TO FILE = ' C:\temp\certificate_YYYY-MM-DD.cer' WITH PRIVATE KEY (FILE = ' C:\temp\private_key_YYYY-MM-DD.pvk', ENCRYPTION BY PASSWORD = '')
GO

After configuring TDE and using the original certificate restores from the original database server to the new instance work without any issue.





Enable and configure auditing in SQL Server 2008 R2

2 11 2013

Since I mentioned the auditing feature in my prior post I decided to post a quick script to setup and start database auditing in SQL Server 2008 R2.

/* 
Created By: Christopher Provolt
Created: August 2, 2013
Description: Create and intialize database auditing (SQL Server 2008 R2)

**** NOTES ****
Replace ServerName With the name of the server if it is a named instance also include the instance name i.e. ServerName-InstanceName-Audit

Replace Server Audit Name with the name generated for the intial Server Audit object

Make sure to set appropriate file paths to match your environment  */

USE [master] GO

/* Create the initial Server Audit Object */ 
CREATE SERVER AUDIT [ServerName-Audit] TO FILE(FILEPATH = 'E:\Audit\', MAXSIZE=50MB, MAX_ROLLOVER_FILES=200) WITH (QUEUE_DELAY = 1000, ON_FAILURE=CONTINUE) GO

/* Create the Server Audit Specification */  
CREATE SERVER AUDIT SPECIFICATION [ServerName-ServerAudit_Specification] FOR SERVER AUDIT [ServerName-Audit] ADD (APPLICATION_ROLE_CHANGE_PASSWORD_GROUP), ADD (AUDIT_CHANGE_GROUP), ADD (BACKUP_RESTORE_GROUP), ADD (DATABASE_CHANGE_GROUP), ADD (DATABASE_OBJECT_ACCESS_GROUP), ADD (DATABASE_OBJECT_CHANGE_GROUP), ADD (DATABASE_OBJECT_OWNERSHIP_CHANGE_GROUP), ADD (DATABASE_OBJECT_PERMISSION_CHANGE_GROUP), ADD (DATABASE_OPERATION_GROUP), ADD (DATABASE_OWNERSHIP_CHANGE_GROUP), ADD (DATABASE_PERMISSION_CHANGE_GROUP), ADD (DATABASE_PRINCIPAL_CHANGE_GROUP), ADD (DATABASE_PRINCIPAL_IMPERSONATION_GROUP), ADD (DATABASE_ROLE_MEMBER_CHANGE_GROUP), ADD (FAILED_LOGIN_GROUP), ADD (LOGIN_CHANGE_PASSWORD_GROUP), ADD (LOGOUT_GROUP), ADD (SCHEMA_OBJECT_ACCESS_GROUP), ADD (SCHEMA_OBJECT_CHANGE_GROUP), ADD (SCHEMA_OBJECT_OWNERSHIP_CHANGE_GROUP), ADD (SCHEMA_OBJECT_PERMISSION_CHANGE_GROUP), ADD (SERVER_OBJECT_CHANGE_GROUP), ADD (SERVER_OBJECT_OWNERSHIP_CHANGE_GROUP), ADD (SERVER_OBJECT_PERMISSION_CHANGE_GROUP), ADD (SERVER_OPERATION_GROUP), ADD (SERVER_PERMISSION_CHANGE_GROUP), ADD (SERVER_PRINCIPAL_CHANGE_GROUP), ADD (SERVER_PRINCIPAL_IMPERSONATION_GROUP), ADD (SERVER_ROLE_MEMBER_CHANGE_GROUP), ADD (SERVER_STATE_CHANGE_GROUP), ADD (SUCCESSFUL_LOGIN_GROUP), ADD (TRACE_CHANGE_GROUP) GO

/* Enable the Server Audit */ 
ALTER SERVER AUDIT [ServerName-Audit] WITH (STATE = ON)  GO   

 /* Enable the Server Specification Audit */  
ALTER SERVER AUDIT SPECIFICATION [ServerName-ServerAudit_Specification]  WITH (STATE = ON)  GO




Revoke Public / Guest Permissions and tempdb is created from model

2 11 2013

Probably the worst and maybe most important step to STIGing a SQL Server is to remove public and guest permissions, I found a great script over at Fort SQL, http://blogs.technet.com/b/fort_sql/archive/2010/02/04/remove-public-and-guest-permissions.aspx, to perform this action. I would suggest if using this script first ensure that you run it as script only, there is a variable that can be set. This will allow you to roll back any all of the changes that get made when the script is ran in execute mode.

If you use this script it will break your applications that use the SQL Server! It is not a question of if but more of how badly! I recommend that prior to running this script you enable the built-in auditing features of MSSQL Server 2008, as the information collected is invaluable to resolving the permissions issues that will occur.

That being said I ran this script against a database server that hosted the database for SCCM 2007. I knew that SCCM was going to break and was very prepared to deal with that. It wasn’t until several days later that the SQL Server was rebooted and SCCM stopped deploying patches. Looking at the failures in the audit log I quickly realized that guest required the select permissions to sys.sysobjects in the tempdb.

use tempdb
grant select on sys.sysobjects to guest;

executing this resolved the issue and SCCM happily began chugging away with deploying software!

This resolution only worked as long as the SQL Server did not shutdown, as the tempdb is re-created at every startup.  Looking for solutions to this issue many places I looked advised to just create a job to run using the SQL Agent service everytime it started. This seemed like ghetto fix for the problem, what happens when that service fails to start or the script fails to execute? I know the answer is I’m going to have to manually update permissions again! After some more research I found that tempdb is created in the eyes of the model and if I set the permissions in the model database they would translate to the tempdb upon reboot.

use model
grant select on sys.sysobjects to guest;

The problem is now resolved and no longer or will ever require intervention from me.

 





Easy Backup Script

11 09 2013

Here is a handy backup script for those that do not like to type much. It automatically enumerates the database names and creates a backup of that database in a user set location. Make sure the @path variable is set!


/* Backup all database except for tempdb
Created By: Christopher Provolt
Date: 11 September 2013 */

DECLARE @database VARCHAR(255)
DECLARE @cmd varchar(1000)
DECLARE @path varchar(255)

--set @path variable to the root of where backup files will be created. Ensure following backslash is used.

SET @path = 'E:\Backup\'

DECLARE DatabaseCursor

CURSOR FOR SELECT name FROM master.sys.databases WHERE name NOT IN ('tempdb') AND state = 0

OPEN DataBaseCursor FETCH NEXT FROM DatabaseCursor INTO @database
WHILE @@FETCH_STATUS = 0

BEGIN  select 'Database: ', @database

SET @cmd = 'BACKUP DATABASE ['+@database+'] TO DISK = N'''+@path+@database+'.bak'''
EXEC(@cmd)

FETCH NEXT FROM DatabaseCursor INTO @database
END
CLOSE DatabaseCursor
DEALLOCATE DatabaseCursor





SQL Server Audit action_id List

2 08 2013

While reviewing some audit logs genereated in SQL Server 2008 auditing I came across a a few action_id values that were not completely obvious to me. I began the great google search in hopes of finding a table that mapped out this information and was somewhat unsuccessful. What I did manage to put together was a a query to determine what they all meant.

Select DISTINCT action_id,name,class_desc,parent_class_desc from sys.dm_audit_actions

The above query will list all the action_id’s and what they actually mean in the world of SQL.  In order to save time for everyone the trimmed down list is below.

action_id name
ACDO DATABASE_OBJECT_ACCESS_GROUP
ACO SCHEMA_OBJECT_ACCESS_GROUP
ADBO BULK ADMIN
ADDP DATABASE_ROLE_MEMBER_CHANGE_GROUP
ADSP SERVER_ROLE_MEMBER_CHANGE_GROUP
AL ALTER
ALCN ALTER CONNECTION
ALRS ALTER RESOURCES
ALSS ALTER SERVER STATE
ALST ALTER SETTINGS
ALTR ALTER TRACE
APRL ADD MEMBER
AS ACCESS
AUSC AUDIT SESSION CHANGED
AUSF AUDIT SHUTDOWN ON FAILURE
AUTH AUTHENTICATE
BA BACKUP
BAL BACKUP LOG
BRDB BACKUP_RESTORE_GROUP
C2OF TRACE AUDIT C2OFF
C2ON TRACE AUDIT C2ON
CCLG CHANGE LOGIN CREDENTIAL
CMLG CREDENTIAL MAP TO LOGIN
CNAU AUDIT_CHANGE_GROUP
CO CONNECT
CP CHECKPOINT
CR CREATE
D DENY
DBCC DBCC
DBCG DBCC_GROUP
DL DELETE
DPRL DROP MEMBER
DR DROP
DWC DENY WITH CASCADE
EX EXECUTE
FT FULLTEXT
FTG FULLTEXT_GROUP
G GRANT
GRDB DATABASE_PERMISSION_CHANGE_GROUP
GRDO DATABASE_OBJECT_PERMISSION_CHANGE_GROUP
GRO SCHEMA_OBJECT_PERMISSION_CHANGE_GROUP
GRSO SERVER_OBJECT_PERMISSION_CHANGE_GROUP
GRSV SERVER_PERMISSION_CHANGE_GROUP
GWG GRANT WITH GRANT
IMDP DATABASE_PRINCIPAL_IMPERSONATION_GROUP
IMP IMPERSONATE
IMSP SERVER_PRINCIPAL_IMPERSONATION_GROUP
IN INSERT
LGB BROKER LOGIN
LGBG BROKER_LOGIN_GROUP
LGDA DISABLE
LGDB CHANGE DEFAULT DATABASE
LGEA ENABLE
LGFL FAILED_LOGIN_GROUP
LGIF LOGIN FAILED
LGIS LOGIN SUCCEEDED
LGLG CHANGE DEFAULT LANGUAGE
LGM DATABASE MIRRORING LOGIN
LGMG DATABASE_MIRRORING_LOGIN_GROUP
LGNM NAME CHANGE
LGO LOGOUT
LGSD SUCCESSFUL_LOGIN_GROUP
LO LOGOUT_GROUP
MNDB DATABASE_CHANGE_GROUP
MNDO DATABASE_OBJECT_CHANGE_GROUP
MNDP DATABASE_PRINCIPAL_CHANGE_GROUP
MNO SCHEMA_OBJECT_CHANGE_GROUP
MNSO SERVER_OBJECT_CHANGE_GROUP
MNSP SERVER_PRINCIPAL_CHANGE_GROUP
NMLG NO CREDENTIAL MAP TO LOGIN
OP OPEN
OPDB DATABASE_OPERATION_GROUP
OPSV SERVER_OPERATION_GROUP
PWAR APPLICATION_ROLE_CHANGE_PASSWORD_GROUP
PWC CHANGE PASSWORD
PWCG LOGIN_CHANGE_PASSWORD_GROUP
PWCS CHANGE OWN PASSWORD
PWEX PASSWORD EXPIRATION
PWMC MUST CHANGE PASSWORD
PWPL PASSWORD POLICY
PWR RESET PASSWORD
PWRS RESET OWN PASSWORD
PWU UNLOCK ACCOUNT
R REVOKE
RC RECEIVE
RF REFERENCES
RS RESTORE
RWC REVOKE WITH CASCADE
RWG REVOKE WITH GRANT
SL SELECT
SN SEND
SPLN SHOW PLAN
STSV SERVER_STATE_CHANGE_GROUP
SUQN SUBSCRIBE QUERY NOTIFICATION
SVCN SERVER CONTINUE
SVPD SERVER PAUSED
SVSD SERVER SHUTDOWN
SVSR SERVER STARTED
TASA TRACE AUDIT START
TASP TRACE AUDIT STOP
TO TAKE OWNERSHIP
TODB DATABASE_OWNERSHIP_CHANGE_GROUP
TODO DATABASE_OBJECT_OWNERSHIP_CHANGE_GROUP
TOO SCHEMA_OBJECT_OWNERSHIP_CHANGE_GROUP
TOSO SERVER_OBJECT_OWNERSHIP_CHANGE_GROUP
TRCG TRACE_CHANGE_GROUP
TRO TRANSFER
UP UPDATE
USAF CHANGE USERS LOGIN AUTO
USLG CHANGE USERS LOGIN
VDST VIEW DATABASE STATE
VSST VIEW SERVER STATE
VWCT VIEW CHANGETRACKING
XA EXTERNAL ACCESS ASSEMBLY
XU UNSAFE ASSEMBLY