-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsp_CheckSecurity.sql
More file actions
1908 lines (1668 loc) · 64.1 KB
/
sp_CheckSecurity.sql
File metadata and controls
1908 lines (1668 loc) · 64.1 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
IF OBJECT_ID('dbo.sp_CheckSecurity') IS NULL
EXEC ('CREATE PROCEDURE dbo.sp_CheckSecurity AS RETURN 0;');
GO
ALTER PROCEDURE dbo.sp_CheckSecurity
@Mode TINYINT = 99
, @CheckLocalAdmin BIT = 0
, @PreferredDBOwner NVARCHAR(255) = NULL
, @Override BIT = 0
, @Help BIT = 0
, @VersionCheck BIT = 0
WITH RECOMPILE
AS
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
DECLARE
@Version VARCHAR(10) = NULL
, @VersionDate DATETIME = NULL
SELECT
@Version = '2026.4.1'
, @VersionDate = '20260420';
/* Version check */
IF @VersionCheck = 1 BEGIN
SELECT
@Version AS VersionNumber
, @VersionDate AS VersionDate
RETURN;
END;
/* @Help = 1 */
IF @Help = 1 BEGIN
PRINT '
/*
sp_CheckSecurity from https://straightpathsql.com/
This script checks your SQL Server for several dozen possible vulnerabilities
and gives you an order list with explanations and action items.
Known limitations of this version:
- sp_CheckSecurity only works Microsoft-supported versions of SQL Server, so
that means SQL Server 2016 or later.
- sp_CheckSecurity will work with SQL Server 2012 and 2014, but it will skip
a few checks. The results should still be valid and helpful, but you should
really considering upgrading to a newer version by now.
- If you attempt to execute sp_CheckSecurity on SQL Server 2008 R2 or older,
then you will only receive an output message saying this will not run on your
version. I''m sorry. No really, I''m sorry you have to support a version of
SQL Server that old.
- sp_CheckSecurity is designed only for database administrators, so the user
must be a member of the sysadmin role to complete the checks.
- If a database name has a question mark in it, then certain checks will fail
due to the usage of sp_MSforeachdb.
Parameters:
@Mode 0=All discovered vulnerabilities
1=Only high vulnerability items will be shown
99=Information and all discovered vulnerabilities shown(DEFAULT)
@CheckLocalAdmin 1=Check members of local Administrators
0=Do NOT check members of local Administrators(DEFAULT)
@PreferredDBOwner (This can be whatever login you prefer to have as the owner
of your databases. By default, it will be NULL and will
not check unless you provide a preferred owner login.)
@Override for allowing checks on instances of over 50 databases
*** WARNING FOR @CheckLocalAdmin usage ***
If you execute sp_CheckSecurity with @CheckLocalAdmin = 1, then sp_CheckSecurity
will attempt to read and record the members of the BUILTIN\Administrators
group. If BUILTIN\Administrators is not currently a member of the Logins,
then sp_CheckSecurity will proceed with the following logic.
1. BEGIN an explicit transaction.
2. Add BUILTIN\Administrators as a Login.
3. Read and record the members of BUILTIN\Administrators.
4. ROLLBACK the transaction, removing BUILTIN\Administrators from Logins.
If you have ANY database level triggers or other fun features enabled to track
the addition of members to Logins then you, dear user, assume any responsibility
for any subsequent action from this brief addition.
Please don''t say we didn''t warn you.
*** End of WARNING FOR @CheckLocalAdmin usage ***
MIT License
Copyright for portions of sp_CheckSecurity are held by Microsoft as part of project
tigertoolbox and are provided under the MIT license:
https://github.com/Microsoft/tigertoolbox
Copyright for portions of sp_CheckSecurity are also held by Brent Ozar Unlimited
as part of sp_Blitz and are provided under the MIT license:
https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/
All other copyrights for sp_CheckSecurity are held by Straight Path Solutions.
Copyright 2026 Straight Path IT Solutions, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/';
RETURN;
END;
/* Check if @Override needed for too many databases */
IF @Override = 0 AND ((SELECT COUNT(database_id) from sys.databases where source_database_id IS NULL) > 50) BEGIN
PRINT '
You have over 50 databases, so you could have a lot of backup history.
If you want to proceed, and you understand this procedure could use
substantial resources to get your backup history, use @Override = 1.
Godspeed, my friend.
'
RETURN;
END;
/* check that user is in the sysadmin role */
IF IS_SRVROLEMEMBER ('sysadmin') = 0 BEGIN
PRINT '
/*
*** Executing user is NOT in the sysadmin role ***
sp_CheckSecurity is designed only for database administrators, so the user
must be a member of the sysadmin role to complete the checks.
For more information about the limitations of sp_CheckSecurity, execute
using @Help = 1
*** EXECUTION ABORTED ***
*/';
RETURN;
END;
DECLARE
@SQL NVARCHAR(4000)
, @SQLVersion NVARCHAR(128)
, @SQLVersionMajor DECIMAL(10,2)
, @SQLVersionMinor DECIMAL(10,2)
, @ComputerNamePhysicalNetBIOS NVARCHAR(128)
, @ServerZeroName sysname
, @SQLServerService NVARCHAR(256)
, @SQLAgentService NVARCHAR(256)
, @InstanceName NVARCHAR(128)
, @Edition NVARCHAR(128);
IF OBJECT_ID('tempdb..#Results') IS NOT NULL
DROP TABLE #Results;
CREATE TABLE #Results (
CategoryID TINYINT
, CheckID INT
, [Importance] TINYINT
, CheckName VARCHAR(50)
, Issue NVARCHAR(MAX)
, DatabaseName NVARCHAR(255)
, Details NVARCHAR(MAX)
, ActionStep NVARCHAR(MAX)
, ReadMoreURL XML
);
IF OBJECT_ID('tempdb..#Category') IS NOT NULL
DROP TABLE #Category;
CREATE TABLE #Category (
CategoryID TINYINT
, CategoryName VARCHAR(50)
);
INSERT #Category (CategoryID, CategoryName)
VALUES
(1, 'Discovery')
, (2, 'Recoverability')
, (3, 'Security')
, (4, 'Availability')
, (5, 'Integrity')
, (6, 'Reliability')
, (7, 'Performance')
, (8, 'Troubleshooting');
IF OBJECT_ID('tempdb..#SQLVersions') IS NOT NULL
DROP TABLE #SQLVersions;
CREATE TABLE #SQLVersions (
VersionName VARCHAR(10)
, VersionNumber DECIMAL(10,2)
);
INSERT #SQLVersions
VALUES
('2008', 10)
, ('2012', 11)
, ('2014', 12)
, ('2016', 13)
, ('2017', 14)
, ('2019', 15)
, ('2022', 16)
, ('2025', 17);
/* SQL Server version */
SELECT @SQLVersion = CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128));
SELECT
@SQLVersionMajor = SUBSTRING(@SQLVersion, 1,CHARINDEX('.', @SQLVersion) + 1 )
, @SQLVersionMinor = PARSENAME(CONVERT(varchar(32), @SQLVersion), 2);
/* check for unsupported version */
IF @Override = 0 AND @SQLVersionMajor < 11 BEGIN
PRINT '
/*
*** Unsupported SQL Server Version ***
sp_CheckSecurity is supported only for execution on SQL Server 2012 and later.
For more information about the limitations of sp_CheckSecurity, execute
using @Help = 1
*** EXECUTION ABORTED ***
*/';
RETURN;
END;
SELECT
@ComputerNamePhysicalNetBIOS = CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS NVARCHAR(128))
, @InstanceName = CAST(SERVERPROPERTY('InstanceName') AS NVARCHAR(128))
, @Edition = CAST(SERVERPROPERTY('Edition') AS NVARCHAR(128));
SELECT @ServerZeroName = [name]
FROM sys.servers
WHERE server_id = 0;
SELECT @SQLServerService = service_account
FROM sys.dm_server_services
WHERE servicename LIKE 'SQL Server (%';
SELECT @SQLAgentService = service_account
FROM sys.dm_server_services
WHERE servicename LIKE 'SQL Server Agent%';
/*
Discovery
*/
IF @Mode IN (99) BEGIN /* Collect instance info */
/* capture date */
INSERT #Results
SELECT
1
, 100
, 0
, ' Capture date'
, 'Capture date'
, NULL
, '(Information captured on ' + CONVERT(VARCHAR(100), GETDATE(), 101) + ' using version ' + @Version + ')'
, NULL
, '';
/* server name */
INSERT #Results
SELECT
1
, 101
, 0
, ' Server Name'
, 'Server Name'
, NULL
, COALESCE(@ComputerNamePhysicalNetBIOS,'')
, NULL
, '';
/* instance name */
INSERT #Results
SELECT
1
, 102
, 0
, ' Instance Name'
, 'Instance Name'
, NULL
, COALESCE(@InstanceName, '(default instance)')
, NULL
, '';
/* instance version */
INSERT #Results
SELECT
1
, 103
, 0
, ' Instance Version'
, 'Instance Version'
, NULL
, 'SQL Server ' + VersionName
, NULL
, ''
FROM #SQLVersions
WHERE VersionNumber = @SQLVersionMajor;
/* instance edition */
INSERT #Results
SELECT
1
, 104
, 0
, 'Instance Edition'
, 'Instance Edition'
, NULL
, @Edition
, NULL
, '';
/* instance build */
INSERT #Results
SELECT
1
, 105
, 0
, 'Instance Build'
, 'Instance Build'
, NULL
, @SQLVersion
, NULL
, '';
/* SQL Server service account */
INSERT #Results
SELECT
1
, 116
, 0
, 'SQL Server Service Account'
, 'SQL Server Service Account'
, NULL
, service_account
, NULL
, ''
FROM sys.dm_server_services
WHERE servicename like 'SQL Server (%';
/* SQL Agent service account */
INSERT #Results
SELECT
1
, 117
, 0
, 'SQL Agent Service Account'
, 'SQL Agent Service Account'
, NULL
, service_account
, NULL
, ''
FROM sys.dm_server_services
WHERE servicename like 'SQL Server Agent%';
/* IP address */
INSERT #Results
SELECT
1
, 119
, 0
, 'IP address'
, 'IP address'
, NULL
, COALESCE(CONVERT(VARCHAR(15), CONNECTIONPROPERTY('local_net_address')), 'UNKNOWN')
, 'Check to make sure is not an externally-facing server and this IP address cannot be reached outside your network.'
, 'https://straightpathsql.com/check/ip-address';
END;
IF @Mode IN (0, 1, 99) BEGIN /* Collect issues info */
/* remote admin connections */
INSERT #Results
SELECT
3
, 310
, 2
, 'Configuration: Remote admin connections'
, 'Remote admin connections are currently '
+ CASE value_in_use
WHEN 1 THEN 'ENABLED.'
ELSE 'DISABLED.'
END
, NULL
, 'Remote admin connections are currently '
+ CASE value_in_use
WHEN 1 THEN 'ENABLED.'
ELSE 'DISABLED.'
END
, 'We recommend ''remote admin connections'' be ENABLED as a troubleshooting option for sysadmin role members.'
, 'https://straightpathsql.com/check/remote-dedicated-admin-connections'
FROM sys.configurations
WHERE [name] = 'remote admin connections';
/* Database Mail XPs */
INSERT #Results
SELECT
3
, 311
, 2
, 'Configuration: Database Mail XPs'
, 'Database Mail XPs'
, NULL
, 'Database Mail XPs are currently '
+ CASE value_in_use
WHEN 1 THEN 'ENABLED.'
ELSE 'DISABLED.'
END
, 'Enabling Database Mail XPs can be useful, but be aware if your instance is breached they could be used to initiate a Denial Of Service attack.'
, 'https://straightpathsql.com/check/database-mail-xps'
FROM sys.configurations
WHERE [name] = 'Database Mail XPs';
/* check for supported versions */
IF SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances */ BEGIN
IF @SQLVersionMajor < 13 BEGIN
INSERT #Results
SELECT
6
, 602
, 1
, '*Unsupported version of SQL Server'
, 'This version of SQL Server is no longer supported by Microsoft.'
, NULL
, 'SQL Server ' + VersionName + ' is no longer supported, so there will be no future security updates.'
, 'Upgrade to SQL Server 2016 or higher.'
, 'https://straightpathsql.com/check/unsupported-versions'
FROM #SQLVersions
WHERE VersionNumber = @SQLVersionMajor
END;
END;
/* check for security update */
IF SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances */ BEGIN
IF ((@SQLVersionMajor = 11 AND @SQLVersionMinor < 7507) OR
(@SQLVersionMajor = 12 AND @SQLVersionMinor < 6449) OR
(@SQLVersionMajor = 13 AND @SQLVersionMinor < 6485) OR
(@SQLVersionMajor = 14 AND @SQLVersionMinor < 3525) OR
(@SQLVersionMajor = 15 AND @SQLVersionMinor < 4465) OR
(@SQLVersionMajor = 16 AND @SQLVersionMinor < 4250) OR
(@SQLVersionMajor = 17 AND @SQLVersionMinor < 4030) )
INSERT #Results
SELECT
6
, 601
, 1
, '*Security update available'
, 'There are security updates from Microsoft that have not been applied to this instance.'
, NULL
, 'There is at least one security update available for SQL Server ' + VersionName + ' that has not been applied.'
, 'Apply the most recent cumulative update or GDR for SQL Server ' + VersionName + '.'
, 'https://straightpathsql.com/check/security-update'
FROM #SQLVersions
WHERE VersionNumber = @SQLVersionMajor
END;
/* check for encrypted databases */
INSERT #Results
SELECT
3
, 312
, 2
, 'Encrypted database'
, 'One or more databases are using SQL Server encryption.'
, NULL
, 'This instance has ' + CONVERT(VARCHAR(10), COUNT(database_id)) + ' encrypted databases using ' + key_algorithm + ' ' + CONVERT(VARCHAR(5), key_length) + '.'
, 'Having encrypted databases is good, but make sure you have backed up your encryption keys to a secure location.'
, 'https://straightpathsql.com/check/encrypted-databases'
FROM sys.dm_database_encryption_keys
WHERE database_id > 4
GROUP BY
key_algorithm
, key_length;
/* check for unencrypted databases */
INSERT #Results
SELECT
3
, 312
, 2
, 'Unencrypted database'
, 'One or more databases are not using SQL Server encryption.'
, NULL
, 'This instance has ' + CONVERT(VARCHAR(10), COUNT(database_id)) + ' unencrypted databases.'
, 'Having unencrypted databases isn''t necessarily bad, but make sure you don''t need to have these user databases encrypted.'
, 'https://straightpathsql.com/check/unencrypted-databases'
FROM sys.databases d
WHERE database_id > 4
AND NOT EXISTS (SELECT 1 from sys.dm_database_encryption_keys dek where d.database_id = dek.database_id)
AND d.source_database_id IS NULL; /* exclude snapshot databases */
/***** Login settings *****/
/* sa is enabled */
INSERT #Results
SELECT
3
, 307
, 1
, 'Enabled sa account'
, 'The sa login is enabled.'
, NULL
, 'The sa account is enabled for connections. Hackers commonly use the [sa] account for malicious activity since it is in the [sysadmin] role.'
, 'Disable the sa account. Disabling only prevents sa from being used as a login for connections, as it can still own databases, jobs, etc.'
, 'https://straightpathsql.com/check/enabled-sa-login'
FROM sys.sql_logins
WHERE sid = 0x01
AND is_disabled = 0;
/* sa has been renamed */
INSERT #Results
SELECT
3
, 340
, 2
, 'Renamed sa account'
, 'The sa login has been renamed.'
, NULL
, 'The sa account is has been renamed, but the new name can be easily determined by any other login.'
, 'Did you mean to rename this? It''s more important that this login is disabled than renamed.'
, 'https://straightpathsql.com/check/sa-login-renamed'
FROM sys.sql_logins
WHERE sid = 0x01
AND [name] <> 'sa';
/* local Administrators group members */
DECLARE @LocalAdmin TABLE (
AccountName VARCHAR(1000)
, AccountType VARCHAR(8)
, AccountPrivilege VARCHAR(9)
, MappedLoginName VARCHAR(1000)
, PermissionPath VARCHAR(1000)
);
IF @CheckLocalAdmin = 1 BEGIN
BEGIN TRAN
IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE [name] = 'BUILTIN\Administrators')
CREATE LOGIN [BUILTIN\Administrators] FROM WINDOWS WITH DEFAULT_DATABASE=[master];
INSERT @LocalAdmin (AccountName, AccountType, AccountPrivilege, MappedLoginName, PermissionPath)
EXEC xp_logininfo 'BUILTIN\Administrators', 'members'
ROLLBACK
INSERT #Results
SELECT
3
, 315
, 1
, 'local Administrators'
, 'Members of the local Administrators Windows group.'
, NULL
, 'The ' + AccountType + ' ' + AccountName + ' is in the local Administrator group. They can add themselves to the sysadmin role and then do anything in SQL Server, including dropping databases or changing other permissions.'
, 'Review all users and groups in the local Administrators group to verify they require these elevated permissions.'
, 'https://straightpathsql.com/cs/local-administrators'
FROM @LocalAdmin
INSERT #Results
SELECT
3
, 351
, 1
, 'Service account in local Administrators'
, 'The SQL Server service account is a member of the local Administrators Windows group.'
, NULL
, 'The account [' + AccountName + '] is in the local Administrator group, so anyone with xp_cmdshell access can do anything on this server.'
, 'We recommend removing the service account from the local Administrators group to adhere to the principle of least privilege.'
, 'https://straightpathsql.com/check/service-account-permissions'
FROM @LocalAdmin
WHERE AccountName = @SQLServerService;
INSERT #Results
SELECT
3
, 351
, 1
, 'Service account in local Administrators'
, 'The SQL Agent service account is a member of the local Administrators Windows group.'
, NULL
, 'The account [' + AccountName + '] is in the local Administrator group, so anyone with xp_cmdshell access can do anything on this server.'
, 'We recommend removing the service account from the local Administrators group to adhere to the principle of least privilege.'
, 'https://straightpathsql.com/check/service-account-permissions'
FROM @LocalAdmin
WHERE AccountName = @SQLAgentService;
END;
/* sysadmin members */
INSERT #Results
SELECT
3
, 301
, 1
, 'sysadmin role members'
, 'Members of the sysadmin role.'
, NULL
, 'Login [' + l.name + '] is a sysadmin. They can do anything in SQL Server, including dropping databases or changing other permissions.'
, 'Review the list of logins and groups in the sysadmin role to verify they require these elevated permissions.'
, 'https://straightpathsql.com/check/sysadmin'
FROM master.sys.syslogins l
WHERE l.sysadmin = 1
AND l.name <> SUSER_SNAME(0x01)
AND l.denylogin = 0
AND l.name NOT LIKE 'NT SERVICE\%'
AND l.name <> 'l_certSignSmDetach'; /* Added in SQL 2016 */
/* securityadmin members */
INSERT #Results
SELECT
3
, 302
, 1
, 'securityadmin role members'
, 'Members of the securityadmin role.'
, NULL
, 'Login [' + l.name + '] is a security admin. They can create other logins that do anything in SQL Server, including dropping databases or changing other permissions.'
, 'Review the list of logins and groups in the securityadmin role to verify they require these elevated permissions.'
, 'https://straightpathsql.com/check/securityadmin'
FROM master.sys.syslogins l
WHERE l.securityadmin = 1
AND l.name <> SUSER_SNAME(0x01)
AND l.denylogin = 0;
/* CONTROL SERVER permissions */
INSERT #Results
SELECT
3
, 303
, 1
, 'CONTROL SERVER permissions'
, 'Logins with CONTROL SERVER permissions.'
, NULL
, 'Login [' + pri.[name]+ '] has the CONTROL SERVER permission. They can do anything in SQL Server, including dropping databases or changing permissions.'
, 'Review the list of logins and groups with CONTROL SERVER permission to verify they require these elevated permissions.'
, 'https://straightpathsql.com/check/control-server'
FROM sys.server_principals AS pri
WHERE pri.[principal_id] IN (
SELECT p.[grantee_principal_id]
FROM sys.server_permissions AS p
WHERE p.[state] IN ( 'G', 'W' )
AND p.[class] = 100
AND p.[type] = 'CL' )
AND pri.[name] NOT LIKE '##%##';
/* check for invalid Windows logins */
IF OBJECT_ID('tempdb..#InvalidLogins') IS NOT NULL
DROP TABLE #InvalidLogins;
CREATE TABLE #InvalidLogins (
LoginSID VARBINARY(85)
, LoginName VARCHAR(256)
);
INSERT INTO #InvalidLogins
EXEC sp_validatelogins;
INSERT #Results
SELECT
3
, 304
, 3
, 'Invalid login with Windows Authentication'
, 'There is a login with permissions that is no longer mapped to a valid Windows user.'
, NULL
, QUOTENAME(LoginName) + ' is an invalid Windows user or group that is mapped to a SQL Server principal.'
, 'Verify the account no longer exists and carefully remove all SQL Server permissions.'
, 'https://straightpathsql.com/check/invalid-windows-login'
FROM #InvalidLogins;
/* blank passwords */
INSERT #Results
SELECT
3
, 316
, 1
, 'Password blank'
, 'A SQL Server login has a blank password.'
, NULL
, 'Login [' + name + '] has a blank password.'
, 'Change the password to something more secure. Logins with blank passwords are easy to hack.'
, 'https://straightpathsql.com/check/password-vulnerabilities'
FROM sys.sql_logins
WHERE PWDCOMPARE('',password_hash)=1;
/* password same as login */
INSERT #Results
SELECT
3
, 317
, 1
, 'Password is same as login'
, 'A SQL Server login has a password that is the same as the login.'
, NULL
, 'Login [' + name + '] has a password that is the same as the login.'
, 'Change the password to something more secure. Logins with matching passwords are easy to hack.'
, 'https://straightpathsql.com/check/password-vulnerabilities'
FROM sys.sql_logins
WHERE PWDCOMPARE(name,password_hash)=1;
/* passwords is password */
INSERT #Results
SELECT
3
, 318
, 1
, 'Password is password'
, 'A SQL Server login has a password that is the word "password".'
, NULL
, 'Login [' + name + '] has a password that is the word "password".'
, 'Change the password to this login to something more secure.'
, 'https://straightpathsql.com/check/password-vulnerabilities'
FROM sys.sql_logins
WHERE PWDCOMPARE('password',password_hash)=1;
/***** Instance settings *****/
/* CLR enabled */
INSERT #Results
SELECT
3
, 309
, 2
, 'Configuration: clr enabled'
, 'The "clr enabled" configuration is enabled.'
, NULL
, 'Having the ''clr enabled'' setting enabled allows for the execution of assemblies in the context of the SQL Server account.'
, CASE
WHEN @SQLVersionMajor >= 14 THEN 'Starting with SQL Server 2017, use the configuration option ''clr strict security'' instead of ''clr enabled''.'
ELSE 'A CLR assembly created with PERMISSION_SET = SAFE may be able to access external system resources, call unmanaged code, and acquire sysadmin privileges.'
END
, 'https://straightpathsql.com/check/clr-enabled'
FROM master.sys.configurations
WHERE [name] = 'clr enabled'
AND value_in_use = 1
AND NOT EXISTS (SELECT 1 FROM sys.databases WHERE [name] = 'SSISDB');
/* xp_cmdshell enabled */
INSERT #Results
SELECT
3
, 319
, 2
, 'Configuration: xp_cmdshell'
, 'The "xp_cmdshell" configuration is enabled.'
, NULL
, 'xp_cmdshell allows for the execution of operating system commands in the context of the SQL Server account by members of the sysadmin role.'
, 'If you do not have any code requiring xp_cmdshell, disable this configuration option.'
, 'https://straightpathsql.com/check/xp-cmdshell'
FROM master.sys.configurations
WHERE [name] = 'xp_cmdshell'
AND value_in_use = 1;
/* Ole Automation Procedures enabled */
INSERT #Results
SELECT
3
, 320
, 2
, 'Configuration: Ole Automation Procedures'
, 'The "Ole Automation Procedures" configuration is enabled.'
, NULL
, 'The ''Ole Automation Procedures'' configuration allows a call to create and execute functions in the context of SQL Server.'
, 'If you do not have any code requiring OLE Automation objects, disable this configuration option.'
, 'https://straightpathsql.com/check/ole-automation-procedures'
FROM master.sys.configurations
WHERE [name] = 'Ole Automation Procedures'
AND value_in_use = 1;
/* cross-database ownership chaining */
INSERT #Results
SELECT
3
, 321
, 2
, 'Configuration: Cross-database ownership chaining'
, 'The "cross-database ownership chaining" configuration is enabled.'
, NULL
, 'Cross-database ownership chaining allows database owners and members of the db_ddladmin and db_owners database roles to create objects that are owned by other users.'
, 'Since enabling this setting allows certain users to create objects that can potentially target objects in other databases, this configuration option should be enabled only at the database level.'
, 'https://straightpathsql.com/check/cross-database-ownership-chaining'
FROM master.sys.configurations
WHERE [name] = 'cross db ownership chaining'
AND value_in_use = 1;
/* ad hoc distributed queries */
INSERT #Results
SELECT
3
, 322
, 2
, 'Configuration: Ad Hoc Distributed Queries'
, 'The "Ad Hoc Distributed Queries" configuration is enabled.'
, NULL
, 'Ad hoc distributed queries use the OPENROWSET and OPENDATASOURCE functions to connect to remote data sources that use OLE DB.'
, 'If a malicious user was able to utilize SQL injection, having this option enabled would allow them to read data files of their choosing.'
, 'https://straightpathsql.com/check/ad-hoc-distributed-queries'
FROM sys.configurations
WHERE [name] = 'Ad Hoc Distributed Queries'
AND value_in_use = 1;
/* jobs owned by users */
INSERT #Results
SELECT
6
, 611
, 2
, 'SQL Agent jobs owned by users'
, 'There are jobs that appear to be owned by user logins.'
, NULL
, 'Job [' + j.name + '] is owned by [' + SUSER_SNAME(j.owner_sid) + ']. If this login is disabled or not available due to Active Directory problems, the job will stop working.'
, 'Verify this login is the correct owner for the job. If possible, see if the job can be owned by sa.'
, 'https://straightpathsql.com/check/sql-agent-jobs-owned-by-users'
FROM msdb.dbo.sysjobs j
WHERE j.enabled = 1
AND SUSER_SNAME(j.owner_sid) <> SUSER_SNAME(0x01)
AND SUSER_SNAME(j.owner_sid) not like '##%';
/* stored procedures that run at startup */
INSERT #Results
SELECT
3
, 313
, 2
, 'Stored procedure run at Startup'
, 'There is a stored procedure scheduled to run on startup.'
, NULL
, 'Stored procedure [master].[' + r.SPECIFIC_SCHEMA + '].[' + r.SPECIFIC_NAME + '] runs automatically when SQL Server starts up.'
, 'Verify you and your team know exactly what this stored procedure is doing, because if not then it could pose a security risk.'
, 'https://straightpathsql.com/check/stored-procedures-that-run-at-startup'
FROM master.INFORMATION_SCHEMA.ROUTINES r
WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME), 'ExecIsStartup') = 1;
/* jobs that run at startup */
INSERT #Results
SELECT
3
, 314
, 2
, 'SQL Agent job run at Startup'
, 'There is a job scheduled to run on startup.'
, NULL
, 'Job [' + j.name + '] runs automatically when SQL Server Agent starts up.'
, 'Verify you and your team know exactly what this job is doing, because it could pose a security risk.'
, 'https://straightpathsql.com/check/sql-agent-jobs-that-run-at-startup'
FROM msdb.dbo.sysschedules s
JOIN msdb.dbo.sysjobschedules js ON s.schedule_id = js.schedule_id
JOIN msdb.dbo.sysjobs j ON js.job_id = j.job_id
WHERE s.freq_type = 64
AND s.enabled = 1
AND j.enabled = 1;
/* check for TDE certificate backup */
INSERT #Results
SELECT
2
, 211
, 1
, 'TDE certificate never backed up'
, 'There is a certificate used for TDE that has never been backed up.'
, db_name(d.database_id)
, 'The certificate ' + c.name + ' used to encrypt database ' + db_name(d.database_id) + ' has never been backed up.'
, 'Make a backup of your current certificate and store it in a secure location in case you need to restore this encrypted database.'
, 'https://straightpathsql.com/check/no-recent-tde-certificate-backup'
FROM sys.certificates c
INNER JOIN sys.dm_database_encryption_keys d
ON c.thumbprint = d.encryptor_thumbprint
WHERE c.pvt_key_last_backup_date IS NULL;
INSERT #Results
SELECT
2
, 211
, 2
, 'TDE certificate not backed up recently'
, 'There is a certificate used for TDE that has not been backed up recently.'
, db_name(d.database_id)
, 'The certificate ' + c.name + ' used to encrypt database ' + db_name(d.database_id) + ' has not been backed up since: ' + CAST(c.pvt_key_last_backup_date AS VARCHAR(100))
, 'Make sure you have a recent backup of your certificate in a secure location in case you need to restore your encrypted database.'
, 'https://straightpathsql.com/check/no-recent-tde-certificate-backup'
FROM sys.certificates c
INNER JOIN sys.dm_database_encryption_keys d
ON c.thumbprint = d.encryptor_thumbprint
WHERE c.pvt_key_last_backup_date <= DATEADD(dd, -90, GETDATE());
/* check TDE certificate expiration dates */
INSERT #Results
SELECT
2
, 213
, 2
, 'TDE certificate set to expire'
, 'There is a certificate used for TDE that is set to expire.'
, db_name(d.database_id)
, 'The certificate ' + c.name + ' used to encrypt database ' + db_name(d.database_id) + ' is set to expire on: ' + CAST(c.expiry_date AS VARCHAR(100))
, 'Although you will still be able to backup or restore your encrypted database with an expired certificate, these should be changed regularly like passwords.'
, 'https://straightpathsql.com/check/tde-certificate-expiration-date'
FROM sys.certificates c
INNER JOIN sys.dm_database_encryption_keys d
ON c.thumbprint = d.encryptor_thumbprint;
/* check for database backup certificate backup */
IF @SQLVersionMajor >= 12 BEGIN
SET @SQL = '
SELECT DISTINCT
2
, 210
, 1
, ''Database backup certificate never been backed up.''
, ''There is a certificate used for backups that has never been backed up.''
, b.[database_name]
, ''The certificate '' + c.name + '' used to encrypt database backups for '' + b.[database_name] + '' has never been backed up.''
, ''Make sure you have a recent backup of your certificate in a secure location in case you need to restore encrypted database backups.''
, ''https://straightpathsql.com/check/missing-database-backup-certificate-backup''
FROM sys.certificates c
INNER JOIN msdb.dbo.backupset b
ON c.thumbprint = b.encryptor_thumbprint
WHERE c.pvt_key_last_backup_date IS NULL
AND b.encryptor_thumbprint IS NOT NULL;';
INSERT #Results
EXEC sp_executesql @SQL;
SET @SQL = '
SELECT DISTINCT
2
, 210
, 2
, ''Database backup certificate not backed up recently.''
, ''There is a certificate used for backups that has not been backed up recently.''
, b.[database_name]
, ''The certificate '' + c.name + '' used to encrypt database backups for '' + b.[database_name] + '' has not been backed up since: '' + CAST(c.pvt_key_last_backup_date AS VARCHAR(100))
, ''Make sure you have a recent backup of your certificate in a secure location in case you need to restore encrypted database backups.''
, ''https://straightpathsql.com/check/missing-database-backup-certificate-backup''
FROM sys.certificates c
INNER JOIN msdb.dbo.backupset b
ON c.thumbprint = b.encryptor_thumbprint
WHERE c.pvt_key_last_backup_date <= DATEADD(dd, -90, GETDATE());';
INSERT #Results
EXEC sp_executesql @SQL;
/* check for database backup certificate expiration dates */
SET @SQL = '
SELECT DISTINCT
2
, 212
, 2
, ''Database backup certificate set to expire.''