-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-Install-DShield-Database.sql
2725 lines (2571 loc) · 85.6 KB
/
1-Install-DShield-Database.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
USE [master]
GO
/*
Installation Instructions
1. Run this script
2. Create a local or domain users named sans_isc_svc and grafana_svc.
Replace 'EXAMPLE' with either your ADSI domain name for an
Active Directory integrated account, or local machine name
for a locally-bound account
a. Remember the passwords...
b. EXAMPLE\sans_isc_svc
c. EXAMPLE\grafana_svc
d. EXAMPLE can be your domain or your machine name
3. In order (1,2,3...n) execute the scripts in this project
a. Repeat for project DShield-Utilities-IPAddress
b. Repeat for project DShield-Utilities-Fingerprints
c. Repeat for project DShield-Utilities-PublicKeys
d. Repeat for project DShield-Utilities-SessionIds
e. Repeat for project DShield-Schema-Reference
f. Repeat for project DShield-Schema-Rolodex
g. Repeat for project DShield-Schema-Logs
h. Repeat for project DShield-Schema-Cowrie
*/
/****** Object: Database [DShield] Script Date: 12/4/2024 2:50:47 AM ******/
CREATE DATABASE [DShield]
CONTAINMENT = NONE
ON PRIMARY (
NAME = N'DShield',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\DShield.mdf' ,
SIZE = 8192KB ,
MAXSIZE = UNLIMITED,
FILEGROWTH = 65536KB
)
LOG ON
(
NAME = N'DShield_log',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\DShield_log.ldf' ,
SIZE = 8192KB ,
MAXSIZE = 2048GB ,
FILEGROWTH = 65536KB
)
WITH CATALOG_COLLATION = DATABASE_DEFAULT
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
BEGIN
EXEC [DShield].[dbo].[sp_fulltext_database] @action = 'enable'
END
GO
ALTER DATABASE [DShield] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [DShield] SET ANSI_NULLS OFF
GO
ALTER DATABASE [DShield] SET ANSI_PADDING OFF
GO
ALTER DATABASE [DShield] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [DShield] SET ARITHABORT OFF
GO
ALTER DATABASE [DShield] SET AUTO_CLOSE ON
GO
ALTER DATABASE [DShield] SET AUTO_SHRINK ON
GO
ALTER DATABASE [DShield] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [DShield] SET CURSOR_CLOSE_ON_COMMIT ON
GO
ALTER DATABASE [DShield] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [DShield] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [DShield] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [DShield] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [DShield] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [DShield] SET DISABLE_BROKER
GO
ALTER DATABASE [DShield] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [DShield] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [DShield] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [DShield] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [DShield] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [DShield] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [DShield] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [DShield] SET RECOVERY FULL
GO
ALTER DATABASE [DShield] SET MULTI_USER
GO
ALTER DATABASE [DShield] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [DShield] SET DB_CHAINING OFF
GO
ALTER DATABASE [DShield] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [DShield] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [DShield] SET DELAYED_DURABILITY = DISABLED
GO
ALTER DATABASE [DShield] SET ACCELERATED_DATABASE_RECOVERY = OFF
GO
ALTER DATABASE [DShield] SET QUERY_STORE = OFF
GO
ALTER DATABASE [DShield] SET READ_WRITE
GO
USE [DShield]
GO
-- =============================================
-- Drop Backwards Compatible Server Roles/Schemas
-- =============================================
/*
Source:
https://learn.microsoft.com/en-us/sql/relational-databases/security/authentication-access/ownership-and-user-schema-separation?view=sql-server-ver16
SQL Server ships with nine predefined schemas that have
the same names as the built-in database users and roles:
- db_accessadmin
- db_backupoperator
- db_datareader
- db_datawriter
- db_ddladmin
- db_denydatareader
- db_denydatawriter
- db_owner
- db_securityadmin
These exist for backward compatibility.
The recommendation is to not use them for user objects.
You can drop the schemas that have the same names as the
fixed database roles - unless they're already in use,
in which case the drop-command returns an error and block
the drop of the used schema.
If you drop these schemas from the model database,
they won't appear in new databases.
Schemas that contain objects can't be dropped.
The following schemas can't be dropped:
- dbo
- guest
- sys
- INFORMATION_SCHEMA
*/
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'db_accessadmin')
DROP SCHEMA [db_accessadmin]
GO
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'db_backupoperator')
DROP SCHEMA [db_backupoperator]
GO
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'db_datareader')
DROP SCHEMA [db_datareader]
GO
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'db_datawriter')
DROP SCHEMA [db_datawriter]
GO
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'db_ddladmin')
DROP SCHEMA [db_ddladmin]
GO
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'db_denydatareader')
DROP SCHEMA [db_denydatareader]
GO
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'db_denydatawriter')
DROP SCHEMA [db_denydatawriter]
GO
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'db_owner')
DROP SCHEMA [db_owner]
GO
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'db_securityadmin')
DROP SCHEMA [db_securityadmin]
GO
-- =============================================
-- Guest Account
-- Can't drop the account, but I can prevent
-- it from connecting
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/authentication-access/principals-database-engine?view=sql-server-ver16
-- =============================================
REVOKE CONNECT FROM GUEST;
GO
USE [master]
GO
IF OBJECT_ID('dbo.sp_hexadecimal') IS NOT NULL
DROP PROCEDURE dbo.sp_hexadecimal
GO
-- =============================================
-- Title: sp_hexadecimal
-- Author: Microsoft
-- Date: 1/3/2025
-- Database: master
-- Schema: dbo
-- Type: Stored Procedure
-- Description:
-- Handles binary and hex conversions as a
-- helper method for dbo.sp_help_revlogin
-- Source:
-- https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/security/transfer-logins-passwords-between-instances
-- =============================================
CREATE PROCEDURE dbo.sp_hexadecimal
@binvalue [varbinary](256)
,@hexvalue [nvarchar] (514) OUTPUT
AS
BEGIN
DECLARE @i [smallint]
DECLARE @length [smallint]
DECLARE @hexstring [nchar](16)
SELECT @hexvalue = N'0x'
SELECT @i = 1
SELECT @length = DATALENGTH(@binvalue)
SELECT @hexstring = N'0123456789ABCDEF'
WHILE (@i < = @length)
BEGIN
DECLARE @tempint [smallint]
DECLARE @firstint [smallint]
DECLARE @secondint [smallint]
SELECT @tempint = CONVERT([smallint], SUBSTRING(@binvalue, @i, 1))
SELECT @firstint = FLOOR(@tempint / 16)
SELECT @secondint = @tempint - (@firstint * 16)
SELECT @hexvalue = @hexvalue
+ SUBSTRING(@hexstring, @firstint + 1, 1)
+ SUBSTRING(@hexstring, @secondint + 1, 1)
SELECT @i = @i + 1
END
END
GO
IF OBJECT_ID('dbo.sp_help_revlogin') IS NOT NULL
DROP PROCEDURE dbo.sp_help_revlogin
GO
-- =============================================
-- Title: sp_help_revlogin
-- Author: Microsoft
-- Date: 1/3/2025
-- Database: master
-- Schema: dbo
-- Type: Stored Procedure
-- Description:
-- Stored procedure that will help generate
-- necessary scripts to transfer logins and
-- their passwords from one server to a new
-- one.
-- Help:
-- Execute the command 'EXEC sp_help_revlogin'
-- in SSMS and output results to text, and
-- in the window, a full migration script
-- will appear to migrate users, 1 for 1
-- to the new machine
-- Warning:
-- READ THE REMARKS SECTION OF THE LINK BELOW
-- Source:
-- https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/security/transfer-logins-passwords-between-instances
-- =============================================
CREATE PROCEDURE dbo.sp_help_revlogin
@login_name [sysname] = NULL
AS
BEGIN
DECLARE @name [sysname]
DECLARE @type [nvarchar](1)
DECLARE @hasaccess [int]
DECLARE @denylogin [int]
DECLARE @is_disabled [int]
DECLARE @PWD_varbinary [varbinary](256)
DECLARE @PWD_string [nvarchar](514)
DECLARE @SID_varbinary [varbinary](85)
DECLARE @SID_string [nvarchar](514)
DECLARE @tmpstr [nvarchar](4000)
DECLARE @is_policy_checked [nvarchar](3)
DECLARE @is_expiration_checked [nvarchar](3)
DECLARE @Prefix [nvarchar](4000)
DECLARE @defaultdb [sysname]
DECLARE @defaultlanguage [sysname]
DECLARE @tmpstrRole [nvarchar](4000)
IF @login_name IS NULL
BEGIN
DECLARE login_curs CURSOR
FOR
SELECT p.[sid],p.[name],p.[type],p.is_disabled,p.default_database_name,l.hasaccess,l.denylogin,default_language_name = ISNULL(p.default_language_name,@@LANGUAGE)
FROM sys.server_principals p
LEFT JOIN sys.syslogins l ON l.[name] = p.[name]
WHERE p.[type] IN ('S' /* SQL_LOGIN */,'G' /* WINDOWS_GROUP */,'U' /* WINDOWS_LOGIN */)
AND p.[name] <> 'sa'
AND p.[name] not like '##%'
ORDER BY p.[name]
END
ELSE
DECLARE login_curs CURSOR
FOR
SELECT p.[sid],p.[name],p.[type],p.is_disabled,p.default_database_name,l.hasaccess,l.denylogin,default_language_name = ISNULL(p.default_language_name,@@LANGUAGE)
FROM sys.server_principals p
LEFT JOIN sys.syslogins l ON l.[name] = p.[name]
WHERE p.[type] IN ('S' /* SQL_LOGIN */,'G' /* WINDOWS_GROUP */,'U' /* WINDOWS_LOGIN */)
AND p.[name] <> 'sa'
AND p.[name] NOT LIKE '##%'
AND p.[name] = @login_name
ORDER BY p.[name]
OPEN login_curs
FETCH NEXT FROM login_curs INTO @SID_varbinary,@name,@type,@is_disabled,@defaultdb,@hasaccess,@denylogin,@defaultlanguage
IF (@@fetch_status = - 1)
BEGIN
PRINT '/* No login(s) found for ' + QUOTENAME(@login_name) + N'. */'
CLOSE login_curs
DEALLOCATE login_curs
RETURN - 1
END
SET @tmpstr = N'/* sp_help_revlogin script
** Generated ' + CONVERT([nvarchar], GETDATE()) + N' on ' + @@SERVERNAME + N'
*/'
PRINT @tmpstr
WHILE (@@fetch_status <> - 1)
BEGIN
IF (@@fetch_status <> - 2)
BEGIN
PRINT ''
SET @tmpstr = N'/* Login ' + QUOTENAME(@name) + N' */'
PRINT @tmpstr
SET @tmpstr = N'IF NOT EXISTS (
SELECT 1
FROM sys.server_principals
WHERE [name] = N''' + @name + N'''
)
BEGIN'
PRINT @tmpstr
IF @type IN ('G','U') -- NT-authenticated Group/User
BEGIN -- NT authenticated account/group
SET @tmpstr = N' CREATE LOGIN ' + QUOTENAME(@name) + N'
FROM WINDOWS
WITH DEFAULT_DATABASE = ' + QUOTENAME(@defaultdb) + N'
,DEFAULT_LANGUAGE = ' + QUOTENAME(@defaultlanguage)
END
ELSE
BEGIN -- SQL Server authentication
-- obtain password and sid
SET @PWD_varbinary = CAST(LOGINPROPERTY(@name, 'PasswordHash') AS [varbinary](256))
EXEC dbo.sp_hexadecimal @PWD_varbinary, @PWD_string OUT
EXEC dbo.sp_hexadecimal @SID_varbinary, @SID_string OUT
-- obtain password policy state
SELECT @is_policy_checked = CASE is_policy_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END
FROM sys.sql_logins
WHERE [name] = @name
SELECT @is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END
FROM sys.sql_logins
WHERE [name] = @name
SET @tmpstr = NCHAR(9) + N'CREATE LOGIN ' + QUOTENAME(@name) + N'
WITH PASSWORD = ' + @PWD_string + N' HASHED
,SID = ' + @SID_string + N'
,DEFAULT_DATABASE = ' + QUOTENAME(@defaultdb) + N'
,DEFAULT_LANGUAGE = ' + QUOTENAME(@defaultlanguage)
IF @is_policy_checked IS NOT NULL
BEGIN
SET @tmpstr = @tmpstr + N'
,CHECK_POLICY = ' + @is_policy_checked
END
IF @is_expiration_checked IS NOT NULL
BEGIN
SET @tmpstr = @tmpstr + N'
,CHECK_EXPIRATION = ' + @is_expiration_checked
END
END
IF (@denylogin = 1)
BEGIN -- login is denied access
SET @tmpstr = @tmpstr
+ NCHAR(13) + NCHAR(10) + NCHAR(9) + N''
+ NCHAR(13) + NCHAR(10) + NCHAR(9) + N'DENY CONNECT SQL TO ' + QUOTENAME(@name)
END
ELSE IF (@hasaccess = 0)
BEGIN -- login exists but does not have access
SET @tmpstr = @tmpstr
+ NCHAR(13) + NCHAR(10) + NCHAR(9) + N''
+ NCHAR(13) + NCHAR(10) + NCHAR(9) + N'REVOKE CONNECT SQL TO ' + QUOTENAME(@name)
END
IF (@is_disabled = 1)
BEGIN -- login is disabled
SET @tmpstr = @tmpstr
+ NCHAR(13) + NCHAR(10) + NCHAR(9) + N''
+ NCHAR(13) + NCHAR(10) + NCHAR(9) + N'ALTER LOGIN ' + QUOTENAME(@name) + N' DISABLE'
END
SET @Prefix =
NCHAR(13) + NCHAR(10) + NCHAR(9) + N''
+ NCHAR(13) + NCHAR(10) + NCHAR(9) + N'EXEC [master].dbo.sp_addsrvrolemember @loginame = N'''
SET @tmpstrRole = N''
SELECT @tmpstrRole = @tmpstrRole
+ CASE WHEN sysadmin = 1 THEN @Prefix + LoginName + N''', @rolename = N''sysadmin''' ELSE '' END
+ CASE WHEN securityadmin = 1 THEN @Prefix + LoginName + N''', @rolename = N''securityadmin''' ELSE '' END
+ CASE WHEN serveradmin = 1 THEN @Prefix + LoginName + N''', @rolename = N''serveradmin''' ELSE '' END
+ CASE WHEN setupadmin = 1 THEN @Prefix + LoginName + N''', @rolename = N''setupadmin''' ELSE '' END
+ CASE WHEN processadmin = 1 THEN @Prefix + LoginName + N''', @rolename = N''processadmin''' ELSE '' END
+ CASE WHEN diskadmin = 1 THEN @Prefix + LoginName + N''', @rolename = N''diskadmin''' ELSE '' END
+ CASE WHEN dbcreator = 1 THEN @Prefix + LoginName + N''', @rolename = N''dbcreator''' ELSE '' END
+ CASE WHEN bulkadmin = 1 THEN @Prefix + LoginName + N''', @rolename = N''bulkadmin''' ELSE '' END
FROM (
SELECT
SUSER_SNAME([sid])AS LoginName
,sysadmin
,securityadmin
,serveradmin
,setupadmin
,processadmin
,diskadmin
,dbcreator
,bulkadmin
FROM sys.syslogins
WHERE ( sysadmin <> 0
OR securityadmin <> 0
OR serveradmin <> 0
OR setupadmin <> 0
OR processadmin <> 0
OR diskadmin <> 0
OR dbcreator <> 0
OR bulkadmin <> 0
)
AND [name] = @name
) L
IF @tmpstr <> '' PRINT @tmpstr
IF @tmpstrRole <> '' PRINT @tmpstrRole
PRINT 'END'
END
FETCH NEXT FROM login_curs INTO @SID_varbinary,@name,@type,@is_disabled,@defaultdb,@hasaccess,@denylogin,@defaultlanguage
END
CLOSE login_curs
DEALLOCATE login_curs
RETURN 0
END
GO
USE [DShield]
GO
-- =============================================
-- DShield Database Audit
-- Drops the Database audit objects
-- =============================================
IF EXISTS (SELECT name FROM [sys].[database_audit_specifications] WHERE name = N'DShield_Essential_Database_Audit_Specification')
ALTER DATABASE AUDIT SPECIFICATION DShield_Essential_Database_Audit_Specification
WITH (STATE = OFF);
GO
IF EXISTS (SELECT name FROM [sys].[database_audit_specifications] WHERE name = N'DShield_Essential_Database_Audit_Specification')
DROP DATABASE AUDIT SPECIFICATION DShield_Essential_Database_Audit_Specification;
GO
USE [master]
GO
-- =============================================
-- DShield Database Server Audit
-- Drops the base server audit objects
-- =============================================
IF EXISTS (SELECT name FROM [sys].[server_audit_specifications] WHERE name = N'DShield_Essential_Server_Audit_Specification')
ALTER SERVER AUDIT SPECIFICATION DShield_Essential_Server_Audit_Specification
WITH (STATE = OFF);
GO
IF EXISTS (SELECT name FROM [sys].[server_audit_specifications] WHERE name = N'DShield_Essential_Server_Audit_Specification')
DROP SERVER AUDIT SPECIFICATION DShield_Essential_Server_Audit_Specification;
GO
IF EXISTS (SELECT name FROM [sys].[server_audits] WHERE name = N'DShield_Essential_Server_Audit')
ALTER SERVER AUDIT DShield_Essential_Server_Audit
WITH (STATE = OFF);
GO
IF EXISTS (SELECT name FROM [sys].[server_audits] WHERE name = N'DShield_Essential_Server_Audit')
DROP SERVER AUDIT DShield_Essential_Server_Audit;
GO
-- =============================================
-- Title: Audit - Create
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Server
-- Description:
-- Create a server level audit for the database
-- And assign to non-default location for
-- ease of access + avoiding versions conflicts
-- Reason:
-- This database will capture live scripts and
-- other nastiness from the open internet. Using
-- extensive built-in auditing features is one
-- way to ensure there is a starting point
-- to find out what happened in the event of
-- something bad happening. More importantly,
-- using server level objects allows us to
-- keep an eye on additional databases or systems
-- installed parallel to the DShield Database.
-- Note:
-- See below link for instructions to record
-- audit logs elsewhere
-- https://learn.microsoft.com/en-us/sql/t-sql/statements/create-server-audit-transact-sql?view=sql-server-ver16
-- =============================================
CREATE SERVER AUDIT DShield_Essential_Server_Audit
TO FILE (
-- The path of the audit log.
-- The file name is generated based on the audit name and audit GUID
FILEPATH = 'C:\DShield Database Audits\', -- make sure this path exists!
-- Specifies the maximum size to which the audit file can grow.
-- The max_size value must be an integer followed by MB, GB, TB,
-- or UNLIMITED.
-- The minimum size that you can specify for max_size is 2 MB
-- and the maximum is 2,147,483,647 TB.
-- When UNLIMITED is specified, the file grows until the disk is full.
-- (0 also indicates UNLIMITED.) Specifying a value lower than 2 MB
-- raises the error MSG_MAXSIZE_TOO_SMALL.
-- The default value is UNLIMITED.
MAXSIZE = 16 MB,
-- Specifies the maximum number of files to retain in the file
-- system in addition to the current file.
-- The MAX_ROLLOVER_FILES value must be an integer or UNLIMITED.
-- The default value is UNLIMITED. This parameter is evaluated
-- whenever the audit restarts (which can happen when the instance
-- of the Database Engine restarts or when the audit is turned off
-- and then on again) or when a new file is needed because the MAXSIZE is reached.
MAX_ROLLOVER_FILES = 128,
-- This option preallocates the file on the disk to the MAXSIZE value.
-- It applies only if MAXSIZE isn't equal to UNLIMITED. The default value is OFF.
RESERVE_DISK_SPACE = ON
)
WITH (
-- Determines the time, in milliseconds,
-- that can elapse before audit actions are forced to be processed.
-- A value of 0 indicates synchronous delivery. The minimum settable
-- query delay value is 1000 (1 second), which is the default.
-- The maximum is 2147483647
-- (2,147,483.647 seconds or 24 days, 20 hours, 31 minutes, 23.647 seconds).
-- Specifying an invalid number raises the MSG_INVALID_QUEUE_DELAY error.
QUEUE_DELAY = 15000,
-- Indicates whether the instance writing to the target should
-- fail, continue, or stop SQL Server if the target can't write to the audit log.
-- The default value is CONTINUE.
ON_FAILURE = CONTINUE
);
GO
-- =============================================
-- Title: Audit - Enable
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Server
-- Description:
-- Enable the Server Audit
-- =============================================
ALTER SERVER AUDIT DShield_Essential_Server_Audit WITH (STATE = ON);
GO
-- =============================================
-- Title: Audit Specification - Create
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Server
-- Description:
-- Create and record audit logs for failed login
-- attempts at the server level
-- Notes:
-- Indicates that a principal tried to log on to SQL Server and failed.
-- Events in this class are raised by new connections or by connections
-- that are reused from a connection pool.
-- Equivalent to the Audit Login Failed Event Class.
-- This audit doesn't apply to Azure SQL Database.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
CREATE SERVER AUDIT SPECIFICATION DShield_Essential_Server_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (FAILED_LOGIN_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Modify
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Server
-- Description:
-- Create and record audit logs for application
-- role password changes at the server level
-- Notes:
-- This event is raised whenever a password is
-- changed for an application role.
-- Equivalent to the Audit App Role Change Password Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER SERVER AUDIT SPECIFICATION DShield_Essential_Server_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (APPLICATION_ROLE_CHANGE_PASSWORD_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Modify
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Server
-- Description:
-- Create and record audit logs for changes
-- to database permissions at the server level
-- Notes:
-- This event is raised whenever a
-- GRANT, REVOKE, or DENY is issued for a statement
-- permission by any principal in SQL Server
-- (This applies to database-only events, such
-- as granting permissions on a database).
--
-- This event is raised for any database permission
-- change (GDR) for any database in the server.
-- Equivalent to the Audit Database Scope GDR Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER SERVER AUDIT SPECIFICATION DShield_Essential_Server_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (DATABASE_PERMISSION_CHANGE_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Modify
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Server
-- Description:
-- Create and record audit logs for CRUD of
-- database principals at the server level
-- Notes:
-- This event is raised when principals,
-- such as users, are created, altered, or
-- dropped from a database. Equivalent to the
-- Audit Database Principal Management Event Class.
-- (Also equivalent to the Audit Add DB Principal
-- Event Class, which occurs on the deprecated
-- sp_grantdbaccess, sp_revokedbaccess, sp_addPrincipal,
-- and sp_dropPrincipal stored procedures.)
--
-- This event is raised whenever a database role is
-- added to or removed by using the sp_addrole,
-- sp_droprole stored procedures.
-- This event is raised whenever any database principals
-- are created, altered, or dropped from any database.
-- Equivalent to the Audit Add Role Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER SERVER AUDIT SPECIFICATION DShield_Essential_Server_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (DATABASE_PRINCIPAL_CHANGE_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Modify
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Server
-- Description:
-- Create and record audit logs for CRUD of
-- logins for roles at the server level
-- Notes:
-- This event is raised whenever a login is
-- added to or removed from a database role.
-- This event class is raised for the sp_addrolemember,
-- sp_changegroup, and sp_droprolemember stored procedures.
-- This event is raised on any Database role
-- member change in any database.
-- Equivalent to the Audit Add Member to DB Role Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER SERVER AUDIT SPECIFICATION DShield_Essential_Server_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (DATABASE_ROLE_MEMBER_CHANGE_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Modify
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Server
-- Description:
-- Create and record audit logs for changes to
-- server principals
-- Notes:
-- This event is raised when server principals are
-- created, altered, or dropped.
-- Equivalent to the Audit Server Principal Management Event Class.
--
-- This event is raised when a principal issues
-- the sp_defaultdb or sp_defaultlanguage stored
-- procedures or ALTER LOGIN statements.
-- Equivalent to the Audit Addlogin Event Class.
--
-- This event is raised on the sp_addlogin and
-- sp_droplogin stored procedures.
-- Also equivalent to the Audit Login Change Property Event Class.
--
-- This event is raised for the sp_grantlogin or
-- sp_revokelogin stored procedures.
-- Equivalent to the Audit Login GDR Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER SERVER AUDIT SPECIFICATION DShield_Essential_Server_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (SERVER_PRINCIPAL_CHANGE_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Modify
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Server
-- Description:
-- Enable the recording of custom user-defined
-- auditable events to be written to the
-- server audit log
-- Notes:
-- This group monitors events raised by using sp_audit_write.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER SERVER AUDIT SPECIFICATION DShield_Essential_Server_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (USER_DEFINED_AUDIT_GROUP)
WITH (STATE=ON);
GO
USE [DShield]
GO
-- =============================================
-- Title: Audit Specification - Create
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Database
-- Description:
-- Create and record audit logs for application
-- role password changes at the server level
-- Notes:
-- This event is raised whenever a password is
-- changed for an application role.
-- Equivalent to the Audit App Role Change Password Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
CREATE DATABASE AUDIT SPECIFICATION DShield_Essential_Database_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (APPLICATION_ROLE_CHANGE_PASSWORD_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Create
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Database
-- Description:
-- Create and record audit logs for changes
-- to database permissions at the server level
-- Notes:
-- This event is raised whenever a
-- GRANT, REVOKE, or DENY is issued for a statement
-- permission by any principal in SQL Server
-- (This applies to database-only events, such
-- as granting permissions on a database).
--
-- This event is raised for any database permission
-- change (GDR) for any database in the server.
-- Equivalent to the Audit Database Scope GDR Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER DATABASE AUDIT SPECIFICATION DShield_Essential_Database_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (DATABASE_PERMISSION_CHANGE_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Create
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Database
-- Description:
-- Create and record audit logs for CRUD of
-- database principals at the server level
-- Notes:
-- This event is raised when principals,
-- such as users, are created, altered, or
-- dropped from a database. Equivalent to the
-- Audit Database Principal Management Event Class.
-- (Also equivalent to the Audit Add DB Principal
-- Event Class, which occurs on the deprecated
-- sp_grantdbaccess, sp_revokedbaccess, sp_addPrincipal,
-- and sp_dropPrincipal stored procedures.)
--
-- This event is raised whenever a database role is
-- added to or removed by using the sp_addrole,
-- sp_droprole stored procedures.
-- This event is raised whenever any database principals
-- are created, altered, or dropped from any database.
-- Equivalent to the Audit Add Role Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER DATABASE AUDIT SPECIFICATION DShield_Essential_Database_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (DATABASE_PRINCIPAL_CHANGE_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Create
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Database
-- Description:
-- Create and record audit logs for CRUD of
-- logins for roles at the server level
-- Notes:
-- This event is raised whenever a login is
-- added to or removed from a database role.
-- This event class is raised for the sp_addrolemember,
-- sp_changegroup, and sp_droprolemember stored procedures.
-- This event is raised on any Database role
-- member change in any database.
-- Equivalent to the Audit Add Member to DB Role Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER DATABASE AUDIT SPECIFICATION DShield_Essential_Database_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (DATABASE_ROLE_MEMBER_CHANGE_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Create
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Database
-- Description:
-- Create and record audit logs for changes to
-- server principals
-- Notes:
-- This event is raised when server principals are
-- created, altered, or dropped.
-- Equivalent to the Audit Server Principal Management Event Class.
--
-- This event is raised when a principal issues
-- the sp_defaultdb or sp_defaultlanguage stored
-- procedures or ALTER LOGIN statements.
-- Equivalent to the Audit Addlogin Event Class.
--
-- This event is raised on the sp_addlogin and
-- sp_droplogin stored procedures.
-- Also equivalent to the Audit Login Change Property Event Class.
--
-- This event is raised for the sp_grantlogin or
-- sp_revokelogin stored procedures.
-- Equivalent to the Audit Login GDR Event Class.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER DATABASE AUDIT SPECIFICATION DShield_Essential_Database_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (DATABASE_PRINCIPAL_CHANGE_GROUP)
WITH (STATE=OFF);
GO
-- =============================================
-- Title: Audit Specification - Create
-- Author: Curtis Dibble
-- Date: 1/4/2025
-- Schema:
-- Type: Database
-- Description:
-- Enable the recording of custom user-defined
-- auditable events to be written to the
-- server audit log
-- Notes:
-- This group monitors events raised by using sp_audit_write.
-- Source:
-- https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-action-groups-and-actions?view=sql-server-ver16
-- =============================================
ALTER DATABASE AUDIT SPECIFICATION DShield_Essential_Database_Audit_Specification
FOR SERVER AUDIT DShield_Essential_Server_Audit
ADD (USER_DEFINED_AUDIT_GROUP)
WITH (STATE=ON);
GO
USE [DShield]
GO
-- =============================================
-- Core Schema
-- Long tail, update only
-- destination of many FK constraints
-- =============================================
IF EXISTS (SELECT * FROM sys.schemas WHERE name = N'Core')
DROP SCHEMA [Core]
GO
CREATE SCHEMA [Core]
GO
-- =============================================
-- Title: Login - Create
-- Author: Curtis Dibble
-- Date: 12/14/2024
-- Schema: Utility
-- Type: Service Account
-- Description:
-- Create a login for the SANS ISC Service
-- Account using a Windows user
-- =============================================
CREATE LOGIN [EXAMPLE\sans_isc_svc] FROM WINDOWS WITH DEFAULT_DATABASE = DShield
GO
-- =============================================
-- Title: User - Create
-- Author: Curtis Dibble
-- Date: 12/14/2024
-- Schema: Utility
-- Type: Service Account
-- Description:
-- Create a user for login using the SANS ISC
-- Service Account from a Windows user. Assign
-- default Schema as 'Core'
-- =============================================
CREATE USER [EXAMPLE\sans_isc_svc]
FOR LOGIN [EXAMPLE\sans_isc_svc]
WITH DEFAULT_SCHEMA = [Core]
GO
USE [DShield]
GO
-- =============================================
-- Core - Handle Roles
-- =============================================
-- =============================================
-- Title: Drop Role - Manager
-- Author: Curtis Dibble
-- Date: 12/14/2024
-- Schema: Core
-- Type: Role