forked from jehama/MSSQL-audit-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSSQL_Audit_Script.ps1
More file actions
1659 lines (1434 loc) · 72.4 KB
/
MSSQL_Audit_Script.ps1
File metadata and controls
1659 lines (1434 loc) · 72.4 KB
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
<#
.SYNOPSIS
Audits the MSSQL Server against the CIS-benchmark, and looks at all users, roles and their rights.
.DESCRIPTION
This scripts checks the recommendations of the CIS-benchmark for MSSQL Server 2016 and MSSQL Server 2012 against the current configuration of the MSSQL Server.
It will also display the following information from the database:
* The databases on the MSSQL Server, the date and time they were created, and the number of users each database has..
* The logins and their corresponding database accounts.
* The roles that are defined, both on server and database level.
* The rights granted or denied to users and roles, both on server and database level.
.PARAMETER Server
Specifies the MSSQL Server to connect to.
.PARAMETER Database
Specifies the database to connect to.
This parameter is optional. If no database is selected it will default to auditing all available databases.
.PARAMETER WindowsAuthentication
Specifies to use Windows Authentication when connecting to the MSSQL Server.
.PARAMETER SQLAuthentication
Specifies to use SQL Authentication when connecting to the MSSQL Server.
.PARAMETER Username
Specifies the username to use when authenticating to the MSSQL Server.
This parameter is only used when authenticating with SQL Authentication.
.PARAMETER Include
Specifies which sections of the script to run.
This parameter is optional. If it is not used the default 'All' will be used.
Valid options are: 'All','CIS','UserManagement'.
.INPUTS
None.
.OUTPUTS
The output is saved in a HTML file.
This file will be saved in the same folder the script is run from.
.EXAMPLE
.\MSSQL_Audit_Script.ps1 -Server "Servername" -WindowsAuthentication
.EXAMPLE
.\MSSQL_Audit_Script.ps1 -Server "Servername" -SQLAuthentication -Username "test"
.EXAMPLE
.\MSSQL_Audit_Script.ps1 -Server "Servername" -Database "DatabaseName" -WindowsAuthentication
.EXAMPLE
.\MSSQL_Audit_Script.ps1 -Server "Servername" -Include "CIS,UserManagement" -WindowsAuthentication
#>
[CmdletBinding()]
# This initializes the parameters which were present when the script was launched.
param(
# Specifies the MSSQL Server to connect to.
[parameter(ParameterSetName = "WindowsAuthentication", Mandatory = $true)]
[parameter(ParameterSetName = "SQLAuthentication", Mandatory = $true)]
[String]
$Server,
# Specifies the database to connect to.
# This parameter is optional. If no database is selected it will default to auditing all available databases.
[parameter(ParameterSetName = "WindowsAuthentication")]
[parameter(ParameterSetName = "SQLAuthentication")]
[String]
$Database,
# Specifies to use Windows Authentication when connecting to the MSSQL Server.
[parameter(ParameterSetName = "WindowsAuthentication", Mandatory = $true)]
[switch]
$WindowsAuthentication,
# Specifies to use SQL Authentication when connecting to the MSSQL Server.
[parameter(ParameterSetName = "SQLAuthentication", Mandatory = $true)]
[switch]
$SQLAuthentication,
# Specifies the username to use when authenticating to the MSSQL Server.
# This parameter is only used when authenticating with SQL Authentication.
[parameter(ParameterSetName = "SQLAuthentication", Mandatory = $true)]
[String]
$Username,
# Specifies the sections of the script to run.
# This parameter is optional. If it is not used every section will be ran.
[parameter(ParameterSetName = "WindowsAuthentication")]
[parameter(ParameterSetName = "SQLAuthentication")]
[ValidateSet('All', 'CIS', 'UserManagement')]
[String[]]
$Include = 'All'
)
function Startup {
<#
.SYNOPSIS
Method executed on startup.
.DESCRIPTION
This methods is called at the start of the program to verify it has started correctly.
It also makes any necessary preparations.
.EXAMPLE
Startup
#>
# This statement is used to signal the start of the script.
# It verifies that the script has started successfully.
[CmdletBinding()]
param()
Write-Host "#########################`nMSSQL audit tool`n#########################"
# A stopwatch is used to check how long a section of the script has needed to be completed.
# It is also used to check the total amount of time needed to complete the script.
$Script:Stopwatch = New-Object -TypeName System.Diagnostics.Stopwatch
$Script:TotalTime = $Script:Stopwatch.Elapsed
$Script:Stopwatch.Start()
# The password will not be visible while typing it in.
if($SQLAuthentication) {
$SecurePassword = Read-Host -AsSecureString "Enter password"
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$Script:Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
}
#Sets the output file. If the file already exists the user is prompted to override it or stop the script.
$Script:Outfile = "audit-MSSQL-" + $Script:Server + ".html"
if (Test-Path -Path $Script:Outfile) {
Write-Host "The output file already exists, would you like to overwrite it?"
Remove-Item $Script:Outfile -Confirm
if (Test-Path -Path $Script:Outfile) {
Write-Host "Please move the output file: $Script:Outfile"
exit
}
}
Write-Host "Using $Script:Server as target server"
if ($Script:Database -ne "") {
Write-Host "Using $Script:Database as target database"
$Script:AllDatabases = $false
}
else {
Write-Host "There Currently no database selected."
Write-Host "Selecting database `"master`" for the connection string"
$Script:Database = "master"
$Script:AllDatabases = $true
}
HTMLPrinter -OpeningTag "<h1>" -Content "Basic information" -ClosingTag "</h1>"
HTMLPrinter -OpeningTag "<p>" -Content "Using $Script:Server as target server." -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "Using $Script:Database as target database." -ClosingTag "</p>"
$Script:OriginalDatabase = $Script:Database
SqlConnectionBuilder
CheckFullVersion
GenerateDatabasesInfo
Write-Host "Setup completed in: " $Script:Stopwatch.Elapsed
$Script:TotalTime += $Script:Stopwatch.Elapsed
Write-Host "Total time elapsed: " $Script:TotalTime
$Script:Stopwatch.Restart()
Main
}
function Main {
<#
.SYNOPSIS
The main function.
.DESCRIPTION
The main function executes all methods.
.EXAMPLE
Main
#>
[CmdletBinding()]
param()
if ($Script:Include -eq 'All' -or $Script:Include -eq 'CIS') {
# Each function called corresponds to a different standard.
L1.1
L1.2
L1.3
L2.1
L2.2
L2.8
L3.3
L3.4
L3.5
L3.7
Write-Host "CIS Microsoft SQL Server 2016 benchmark completed in:" $Script:Stopwatch.Elapsed
$Script:TotalTime += $Script:Stopwatch.Elapsed
Write-Host "Total time elapsed: " $Script:TotalTime
$Script:Stopwatch.Restart()
}
if ($Script:Include -eq 'All' -or $Script:Include -eq 'UserManagement') {
# Used to obtain all users and their rights.
UserManagement
Write-Host "User management completed in: " $Script:Stopwatch.Elapsed
$Script:TotalTime += $Script:Stopwatch.Elapsed
Write-Host "Total time elapsed: " $Script:TotalTime
$Script:Stopwatch.Restart()
}
$Script:TotalTime += $Script:Stopwatch.Elapsed
Write-Host "Audit has finished, total time elapsed: " $Script:TotalTime
}
function SqlConnectionBuilder {
<#
.SYNOPSIS
Builds and returns the SqlConnection object.
.DESCRIPTION
Creates an ConnectionString based on the global script variables $Script:Server and $Script:Database.
.EXAMPLE
SqlConnectionBuilder
#>
[CmdletBinding()]
# "Integrated Security = True" means that the connection uses windows authentication.
# The supplied credentials will be the credentials of owner of the powershell session.
$Script:SqlConnection = New-Object System.Data.SqlClient.SqlConnection
if ($WindowsAuthentication) {
$Script:SqlConnection.ConnectionString = "Server = $Script:Server; Database = $Script:Database; Integrated Security = True;"
}
if ($SQLAuthentication) {
$Script:SqlConnection.ConnectionString = "Server = $Script:Server; Database = $Script:Database; User Id = $Script:Username; Password = $Script:Password;"
}
}
function DataCollector {
<#
.SYNOPSIS
Collects data from the MSSQL instance.
.DESCRIPTION
Creates an SqlAdapter based on the SQL query and fills it with data.
This dataset is then returned.
.EXAMPLE
SqlAdapter $SqlQuery
#>
[CmdletBinding()]
[OutputType([System.Data.Dataset])]
param (
# The SQL query to run.
[parameter(Mandatory = $true)]
[String[]]
$SqlQuery,
[parameter()]
[String]
$AllTables
)
$SQLCommand = New-Object System.Data.SqlClient.SqlCommand
$SQLCommand.CommandText = $SqlQuery
$SQLCommand.Connection = $Script:SqlConnection
$SQLAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SQLAdapter.SelectCommand = $SQLCommand
$Dataset = New-Object System.Data.DataSet
$SqlAdapter.Fill($Dataset) | Out-Null
if($AllTables -eq "y")
{
,$Dataset
}
else {
$DataTable = New-Object System.Data.DataTable
$DataTable = $Dataset.Tables[0]
,$DataTable
}
}
function CheckFullVersion {
<#
.SYNOPSIS
Check the full version of the MSSQL Server.
.DESCRIPTION
Checks and displays the full version info of the MSSQL server.
This includes the major version, service pack, build.
.EXAMPLE
CheckFullVersion
#>
[CmdletBinding()]
$SqlQuery = "SELECT
@@VERSION AS Version
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<h3>" -Content "Server version:" -ClosingTag "</h3>"
HTMLPrinter -Table $Dataset -Columns @("Version")
}
function GenerateDatabasesInfo {
<#
.SYNOPSIS
Generate list of databases.
.DESCRIPTION
Generates a list of databases on the server.
This list is used for queries that are used on every database on the server.
.EXAMPLE
GenerateDatabasesInfo
.NOTES
General notes
#>
[CmdletBinding()]
param ()
$SqlQuery = "SELECT
*
FROM
sys.databases AS DB
;"
$Script:DatabasesInfo = DataCollector $SqlQuery
$Script:DatabasesInfo.Columns.Add("number_of_users", "System.String") | Out-Null
$SqlQuery = "SELECT
COUNT(*) AS users
FROM
sys.database_principals AS DP
WHERE
DP.type IN (
'C',
'E',
'G',
'K',
'S',
'U',
'X'
)
;"
foreach ($db in $Script:DatabasesInfo) {
$Script:Database = $db.name
SqlConnectionBuilder
$Dataset = DataCollector $SqlQuery
$db.number_of_users = $Dataset.users
}
$Script:Database = $Script:OriginalDatabase
SqlConnectionBuilder
HTMLPrinter -OpeningTag "<h3>" -Content "This server contains the following databases:" -ClosingTag "</h3>"
HTMLPrinter -Table $Script:DatabasesInfo -Columns @("name", "create_date", "number_of_users")
}
function L1.1 {
<#
.SYNOPSIS
Checks control L1.1
.DESCRIPTION
Checks CIS Microsoft SQL Server 2012 benchmark section 4.2
Checks CIS Microsoft SQL Server 2016 benchmark section 4.2
.EXAMPLE
L1.1
.NOTES
Control L1.1 checks if passwords are periodically changed.
#>
[CmdletBinding()]
param()
Write-Host "###### Now checking Control L1.1"
HTMLPrinter -OpeningTag "<h3>" -Content "Control L1.1" -ClosingTag "</h3>"
# This check is based on CIS Microsoft SQL Server 2012 benchmark section 4.2.
# This check is based on CIS Microsoft SQL Server 2016 benchmark section 4.2.
# Checks if the 'CHECK_EXPIRATION' option is set to 'ON' for all SQL Authenticated Logins with the sysadmin role.
# Checks if the 'CHECK_EXPIRATION' option is set to 'ON' for all SQL Authenticated Logins who have been granted the control server permission.
# The second UNION ALL has been added to check users who have been granted the CONTROL SERVER permission through a server role.
$SqlQuery = "SELECT
L.name AS name,
'sysadmin membership' AS access_method,
L.is_expiration_checked AS is_expiration_checked
FROM
sys.sql_logins AS L
WHERE
IS_SRVROLEMEMBER('sysadmin', name) = 1
UNION ALL
SELECT
L.name AS name,
'CONTROL SERVER' AS 'access_method',
L.is_expiration_checked AS is_expiration_checked
FROM
sys.sql_logins AS L
JOIN sys.server_permissions AS P ON L.principal_id = P.grantee_principal_id
WHERE P.type = 'CL'
AND P.state IN (
'G',
'W'
)
UNION ALL
SELECT
L.name AS name,
P.name + ' membership' AS 'access_method',
L.is_expiration_checked AS is_expiration_checked
FROM
sys.sql_logins AS L
JOIN sys.server_role_members AS R ON L.principal_id = R.member_principal_id
JOIN sys.server_principals AS P ON P.principal_id = R.role_principal_id
WHERE R.role_principal_id IN (
SELECT
P.principal_id
FROM
sys.server_principals AS P
JOIN sys.server_permissions AS PE ON p.principal_id = pe.grantee_principal_id
WHERE
pe.type = 'CL'
AND p.type = 'R'
)
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if SQL Authenticated Logins have the 'CHECK_EXPIRATION' option set to on." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("name", "access_method", "is_expiration_checked")
}
function L1.2 {
<#
.SYNOPSIS
Checks control L1.2
.DESCRIPTION
Checks CIS Microsoft SQL Server 2012 benchmark section 3.4
Checks CIS Microsoft SQL Server 2012 benchmark section 4.3
Checks CIS Microsoft SQL Server 2016 benchmark section 3.4
Checks CIS Microsoft SQL Server 2016 benchmark section 4.3
.EXAMPLE
L1.2
.NOTES
Control L1.2 checks if password strength is adequately enough.
#>
[CmdletBinding()]
param()
Write-Host "###### Now checking Control L1.2"
HTMLPrinter -OpeningTag "<h3>" -Content "Control L1.2" -ClosingTag "</h3>"
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 3.4.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 3.4.
# Checks if SQL authentication is not used in contained databases.
$SqlQuery = "SELECT
DB_NAME() AS database_name,
P.name AS DB_user,
P.authentication_type AS authentication_type
FROM
sys.database_principals AS P
WHERE
P.type IN (
'U',
'S',
'G'
)
ORDER BY
authentication_type,
DB_user
;"
if ($Script:AllDatabases -and $Script:DatabasesInfo.containment -contains 1) {
foreach ($db in $Script:DatabasesInfo) {
if($db.containment -eq 1){
$Script:Database = $db.name
SqlConnectionBuilder
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if SQL authentication (authentication_type 2) is not used in this contained database." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("database_name", "DB_user", "authentication_type")
}
}
$Script:Database = $Script:OriginalDatabase
SqlConnectionBuilder
}
elseif ($Script:AllDatabases) {
HTMLPrinter -OpeningTag "<p>" -Content "There are no contained databases." -ClosingTag "</p>"
}
else {
$contained = $Script:DatabasesInfo | Where-Object name -eq $Database
if($contained.containment -eq 1){
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if SQL authentication (authentication_type 2) is not used in this contained database." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("database_name", "DB_user", "authentication_type")
}
else {
HTMLPrinter -OpeningTag "<p>" -Content "This database is not a contained database." -ClosingTag "</p>"
}
}
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 4.3.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 4.3.
# Checks if the 'CHECK_POLICY' Option is set to 'True' for all SQL Authenticated Logins.
$SqlQuery = "SELECT
SL.name AS name,
SL.is_disabled AS is_disabled,
SL.is_policy_checked AS is_policy_checked
FROM
sys.sql_logins AS SL
ORDER BY
Is_policy_checked,
Is_disabled
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if 'is_policy_checked' is set to 'True'." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("name", "is_disabled", "is_policy_checked")
}
function L1.3 {
<#
.SYNOPSIS
Checks control L1.3
.DESCRIPTION
Checks CIS Microsoft SQL Server 2012 benchmark section 3.1
Checks CIS Microsoft SQL Server 2016 benchmark section 3.1
.EXAMPLE
L1.3
.NOTES
Control L1.3 checks if two-factor authentication is used with untrusted zones.
#>
[CmdletBinding()]
param()
Write-Host "###### Now checking Control L1.3"
HTMLPrinter -OpeningTag "<h3>" -Content "Control L1.3" -ClosingTag "</h3>"
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 3.1.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 3.1.
# Checks if the 'Server Authentication' property is set to 'Windows Authentication Mode'.
$SqlQuery = "SELECT
SERVERPROPERTY('IsIntegratedSecurityOnly') AS [login_mode]
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if 'login_mode' is set to 'Windows Authentication Mode' only (1)." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("login_mode")
}
function L2.1 {
<#
.SYNOPSIS
Checks control L2.1
.DESCRIPTION
Checks CIS Microsoft SQL Server 2012 benchmark section 3.8
Checks CIS Microsoft SQL Server 2012 benchmark section 3.11
Checks CIS Microsoft SQL Server 2016 benchmark section 3.8
Checks CIS Microsoft SQL Server 2016 benchmark section 3.11
.EXAMPLE
L2.1
.NOTES
Control 2.1 Checks if accounts only have the necessary access rights.
#>
[CmdletBinding()]
param()
Write-Host "###### Now checking Control L2.1"
HTMLPrinter -OpeningTag "<h3>" -Content "Control L2.1" -ClosingTag "</h3>"
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 3.8.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 3.8.
# Checks if only the default permissions specified by Microsoft are granted to the public server role.
$SqlQuery = "SELECT
*
FROM
master.sys.server_permissions AS SP
WHERE
SP.grantee_principal_id = SUSER_SID(N'public')
ORDER BY
SP.class,
SP.permission_name,
SP.state,
SP.major_id
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "The 'public' server role has the following permissions." -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "These extra permissions apply to every login on the server. Therefore it should only have the default permissions." -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "These are:" -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "state_desc = 'GRANT' and [permission_name] = 'VIEW ANY DATABASE' and class_desc = 'SERVER')" -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "state_desc = 'GRANT' and [permission_name] = 'CONNECT' and class_desc = 'ENDPOINT' and major_id = 2)" -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "state_desc = 'GRANT' and [permission_name] = 'CONNECT' and class_desc = 'ENDPOINT' and major_id = 3)" -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "state_desc = 'GRANT' and [permission_name] = 'CONNECT' and class_desc = 'ENDPOINT' and major_id = 4)" -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "state_desc = 'GRANT' and [permission_name] = 'CONNECT' and class_desc = 'ENDPOINT' and major_id = 5)" -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("class", "class_desc", "major_id", "minor_id", "grantee_principal_id", "grantor_principal_id", "type", "permission_name", "state", "state_desc")
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 3.11.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 3.11.
# Checks if the 'public' server role does not have access to the SQL Agent proxies.
$SqlQuery = "SELECT
sp.name AS proxy_name
FROM
dbo.sysproxylogin AS SPL
JOIN sys.database_principals AS DP ON DP.sid = SPL.sid
JOIN sysproxies AS SP ON SP.proxy_id = SPL.proxy_id
WHERE
DP.principal_id = USER_ID('public')
;"
$Script:Database = "msdb"
SqlConnectionBuilder
$Dataset = DataCollector $SqlQuery
if ($Dataset.Rows.Count -gt 0) {
HTMLPrinter -OpeningTag "<p>" -Content "The 'public' serve role has been granted access to the sql agent following proxies." -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "These proxies may have higher privilages then the user calling the proxy. Therefore they should be removed.`n" -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("proxy_name")
}
else {
HTMLPrinter -OpeningTag "<p>" -Content "The 'msdb' database's 'public' role has not been granted access to proxies.`n" -ClosingTag "</p>"
}
$Script:Database = $Script:OriginalDatabase
SqlConnectionBuilder
}
function L2.2 {
<#
.SYNOPSIS
Checks control L2.2
.DESCRIPTION
Checks CIS Microsoft SQL Server 2012 benchmark section 3.9
Checks CIS Microsoft SQL Server 2012 benchmark section 3.10
Checks CIS Microsoft SQL Server 2016 benchmark section 3.9
Checks CIS Microsoft SQL Server 2016 benchmark section 3.10
.EXAMPLE
L2.2
.NOTES
Control 2.2 Checks if accounts and access rights are authorized.
#>
[CmdletBinding()]
param()
Write-Host "###### Now checking Control L2.2"
HTMLPrinter -OpeningTag "<h3>" -Content "Control L2.2" -ClosingTag "</h3>"
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 3.9
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 3.9
# Checks if the Windows 'BUILTIN' groups are not SQL Logins.
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 3.10.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 3.10.
# Checks if it is not allowed for 'WINDOWS_GROUP' users to be added to the server.
$SqlQuery = "SELECT
PR.[name] AS name,
PR.[type_desc] AS type_desc
FROM
sys.server_principals AS PR
ORDER BY
name,
type_desc
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "The following list contains all server principals." -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "Check if none of these principals are Windows BUILTIN groups or accounts." -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "Check if there are no WINDOWS_GROUP users. (type_desc = WINDOWS_GROUP and name contains the MachineName)`n" -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("name", "type_desc")
}
function L2.8 {
<#
.SYNOPSIS
Checks control L2.8
.DESCRIPTION
Checks CIS Microsoft SQL Server 2012 benchmark section 3.3
Checks CIS Microsoft SQL Server 2016 benchmark section 3.3
.EXAMPLE
L2.8
.NOTES
Control 2.8 Checks if useraccounts and administratoraccounts are periodically evaluated.
#>
[CmdletBinding()]
param()
Write-Host "###### Now checking Control L2.8"
HTMLPrinter -OpeningTag "<h3>" -Content "Control L2.8" -ClosingTag "</h3>"
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 3.3
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 3.3
# Checks if 'Orphaned Users' are dropped from SQL Server Databases.
$SqlQuery = "EXEC
sp_change_users_login
@Action = 'Report'
;"
$Dataset = DataCollector $SqlQuery
if ($Dataset.Rows.Count -gt 0) {
HTMLPrinter -OpeningTag "<p>" -Content "The following accounts are 'orphaned'." -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "These accounts should probably be removed.`n" -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("UserName", "UserSID")
}
else {
HTMLPrinter -OpeningTag "<p>" -Content "There are no accounts that are 'orphaned'.`n" -ClosingTag "</p>"
}
}
function L3.3 {
<#
.SYNOPSIS
Checks control L3.3
.DESCRIPTION
Checks CIS Microsoft SQL Server 2012 benchmark section 1.1.
Checks CIS Microsoft SQL Server 2016 benchmark section 1.1.
.EXAMPLE
L3.3
.NOTES
Control L3.3 checks if Systems are timely patched and updated.
#>
[CmdletBinding()]
param()
Write-Host "###### Now checking Control L3.3"
HTMLPrinter -OpeningTag "<h3>" -Content "Control L3.3" -ClosingTag "</h3>"
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 1.1.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 1.1.
# Checks the productlevel and productversion.
$SqlQuery = "SELECT
SERVERPROPERTY('ProductLevel') as SP_installed,
SERVERPROPERTY('ProductVersion') as version
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "The server contains the following Service Pack and Version." -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "Check if these match the expected versions." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("SP_installed", "version")
}
function L3.4 {
<#
.SYNOPSIS
Checks control 3.4
.DESCRIPTION
Checks CIS Microsoft SQL Server 2012 benchmark section 2.11
Checks CIS Microsoft SQL Server 2012 benchmark section 2.13
Checks CIS Microsoft SQL Server 2012 benchmark section 2.14
Checks CIS Microsoft SQL Server 2012 benchmark section 2.17
Checks CIS Microsoft SQL Server 2012 benchmark section 3.2
Checks CIS Microsoft SQL Server 2016 benchmark section 2.11
Checks CIS Microsoft SQL Server 2016 benchmark section 2.13
Checks CIS Microsoft SQL Server 2016 benchmark section 2.14
Checks CIS Microsoft SQL Server 2016 benchmark section 2.17
Checks CIS Microsoft SQL Server 2016 benchmark section 3.2
.EXAMPLE
L3.4
.NOTES
Control 3.4 checks if systems don't use default passwords or backdoor accounts.
The default port for MSSQL is als checked here since this seems the best place to do so.
#>
[CmdletBinding()]
param ()
Write-Host "###### Now checking Control L3.4"
HTMLPrinter -OpeningTag "<h3>" -Content "Control L3.4" -ClosingTag "</h3>"
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 2.11.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 2.11.
# Checks if the MSSQL Server does not use the default port 1433.
$SqlQuery = "DECLARE
@value nvarchar (256)
;
EXECUTE
master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE',
N'SOFTWARE\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IPALL',
N'TcpPort',
@value OUTPUT,
N'no_output'
;
SELECT
@value AS TCP_port
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check that the server does not use the default TCP_Port 1433." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("TCP_port")
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 2.13.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 2.13.
# Checks if the default 'sa' account is disabled.
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 2.14.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 2.14.
# Checks if the default 'sa' account has been renamed.
$SqlQuery = "SELECT
SP.sid AS SID,
SP.name AS name,
SP.is_disabled AS is_disabled
FROM
sys.server_principals AS SP
WHERE
SP.SID = 0x01
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if the default 'sa' account is disabled (True)" -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "Check if the default 'sa' account has been renamed." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("SID", "name", "is_disabled")
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 2.17.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 2.17.
# Checks if no login exists with the name 'sa'.
$SqlQuery = "SELECT
SP.principal_id AS principal_ID,
SP.name AS name,
SP.is_disabled AS is_disabled
FROM
sys.server_principals AS SP
WHERE
SP.type = 'S'
OR SP.type = 'U'
OR SP.type = 'G'
ORDER BY
SP.principal_ID
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if no login exists with the name 'sa', even if this is not the original 'sa' account." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("principal_ID", "name", "is_disabled")
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 3.2.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 3.2.
# Checks if the guest user has it's rights revoked on the databases, with the exception of the msdb
$SqlQuery = "SELECT
DB_NAME() AS database_name,
'guest' AS DB_user,
DP.[permission_name] AS permission_name,
DP.[state_desc] AS state_desc
FROM
sys.database_permissions AS DP
WHERE
DP.[grantee_principal_id] = DATABASE_PRINCIPAL_ID('guest')
;"
HTMLPrinter -OpeningTag "<p>" -Content "Check for each of the following databases if the 'CONNECT' permission has been revoked for the 'guest' user." -ClosingTag "</p>"
HTMLPrinter -OpeningTag "<p>" -Content "The connect permission is required for the 'master', 'tempdb', 'msdb' databases. Therefore they can be ignored." -ClosingTag "</p>"
if ($Script:AllDatabases) {
foreach ($db in $Script:DatabasesInfo) {
$Script:Database = $db.name
SqlConnectionBuilder
$Dataset = DataCollector $SqlQuery
HTMLPrinter -Table $Dataset -Columns @("database_name", "DB_user", "permission_name", "state_desc")
}
$Script:Database = $Script:OriginalDatabase
SqlConnectionBuilder
}
else {
$Dataset = DataCollector $SqlQuery
HTMLPrinter -Table $Dataset -Columns @("database_name", "DB_user", "permission_name", "state_desc")
}
}
function L3.5 {
<#
.SYNOPSIS
Checks control L3.5
.DESCRIPTION
Checks CIS Microsoft SQL Server 2012 benchmark section 2.1
Checks CIS Microsoft SQL Server 2012 benchmark section 2.2
Checks CIS Microsoft SQL Server 2012 benchmark section 2.3
Checks CIS Microsoft SQL Server 2012 benchmark section 2.4
Checks CIS Microsoft SQL Server 2012 benchmark section 2.5
Checks CIS Microsoft SQL Server 2012 benchmark section 2.6
Checks CIS Microsoft SQL Server 2012 benchmark section 2.7
Checks CIS Microsoft SQL Server 2012 benchmark section 2.8
Checks CIS Microsoft SQL Server 2012 benchmark section 2.9
Checks CIS Microsoft SQL Server 2012 benchmark section 2.12
Checks CIS Microsoft SQL Server 2012 benchmark section 2.15
Checks CIS Microsoft SQL Server 2012 benchmark section 2.16
Checks CIS Microsoft SQL Server 2012 benchmark section 6.2
Checks CIS Microsoft SQL Server 2012 benchmark section 7.1
Checks CIS Microsoft SQL Server 2012 benchmark section 7.2
Checks CIS Microsoft SQL Server 2016 benchmark section 2.1
Checks CIS Microsoft SQL Server 2016 benchmark section 2.2
Checks CIS Microsoft SQL Server 2016 benchmark section 2.3
Checks CIS Microsoft SQL Server 2016 benchmark section 2.4
Checks CIS Microsoft SQL Server 2016 benchmark section 2.5
Checks CIS Microsoft SQL Server 2016 benchmark section 2.6
Checks CIS Microsoft SQL Server 2016 benchmark section 2.7
Checks CIS Microsoft SQL Server 2016 benchmark section 2.8
Checks CIS Microsoft SQL Server 2016 benchmark section 2.9
Checks CIS Microsoft SQL Server 2016 benchmark section 2.12
Checks CIS Microsoft SQL Server 2016 benchmark section 2.15
Checks CIS Microsoft SQL Server 2016 benchmark section 2.16
Checks CIS Microsoft SQL Server 2016 benchmark section 6.2
Checks CIS Microsoft SQL Server 2016 benchmark section 7.1
Checks CIS Microsoft SQL Server 2016 benchmark section 7.2
.EXAMPLE
L3.5
.NOTES
Control 3.5 Checks if the OS does not run unnecessary services.
However since the MSSQL Server does not have access to this information it only checks its own services.
#>
[CmdletBinding()]
param()
Write-Host "###### Now checking Control L3.5"
HTMLPrinter -OpeningTag "<h3>" -Content "Control L3.5" -ClosingTag "</h3>"
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 2.1.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 2.1.
# Checks if the option 'Ad Hoc Distributed Queries' is disabled.
$SqlQuery = "SELECT name AS name,
CAST(value AS int) AS value_configured,
CAST(value_in_use AS int) AS value_in_use
FROM
sys.configurations AS C
WHERE
C.name = 'Ad Hoc Distributed Queries'
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if 'Add Hoc Distributed Queries' is disabled (0)." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("name", "value_configured", "value_in_use")
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 2.2.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 2.2.
# Checks if the option 'clr enabled' is disabled.
$SqlQuery = "SELECT name AS name,
CAST(value AS int) AS value_configured,
CAST(value_in_use AS int) AS value_in_use
FROM
sys.configurations AS C
WHERE
C.name = 'clr enabled'
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if 'clr enabled' is disabled (0)." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("name", "value_configured", "value_in_use")
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 2.3.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 2.3.
# Checks if the option 'cross db ownership chaining' is disabled.
$SqlQuery = "SELECT name AS name,
CAST(value AS int) AS value_configured,
CAST(value_in_use AS int) AS value_in_use
FROM
sys.configurations AS C
WHERE
C.name = 'cross db ownership chaining'
;"
$Dataset = DataCollector $SqlQuery
HTMLPrinter -OpeningTag "<p>" -Content "Check if 'cross db ownership chaining' is disabled (0)." -ClosingTag "</p>"
HTMLPrinter -Table $Dataset -Columns @("name", "value_configured", "value_in_use")
# This query is based on CIS Microsoft SQL Server 2012 benchmark section 2.4.
# This query is based on CIS Microsoft SQL Server 2016 benchmark section 2.4.
# Checks if the option 'Database Mail XPs' is disabled.
$SqlQuery = "SELECT name AS name,
CAST(value AS int) AS value_configured,
CAST(value_in_use AS int) AS value_in_use
FROM