/
Azure.Storage.Rule.ps1
224 lines (199 loc) · 9.75 KB
/
Azure.Storage.Rule.ps1
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# Validation rules for Azure Storage Accounts
#
# Synopsis: Storage Accounts not using geo-replicated storage (GRS) or zone-redundant (ZRS) may be at risk.
Rule 'Azure.Storage.UseReplication' -Ref 'AZR-000195' -Type 'Microsoft.Storage/storageAccounts' -If { (ShouldStorageReplicate) } -Tag @{ release = 'GA'; ruleSet = '2020_06'; 'Azure.WAF/pillar' = 'Reliability'; } {
$Assert.In($TargetObject, 'sku.name', @(
'Standard_GRS'
'Standard_RAGRS'
'Standard_GZRS'
'Standard_RAGZRS'
'Premium_ZRS'
'Standard_GZRS'
'Standard_RAGZRS'
'Standard_ZRS'
));
}
# Synopsis: Enable soft delete on Storage Accounts
Rule 'Azure.Storage.SoftDelete' -Ref 'AZR-000197' -Type 'Microsoft.Storage/storageAccounts', 'Microsoft.Storage/storageAccounts/blobServices' -If { !(IsCloudShell) -and !(IsHnsStorage) -and !(IsFileStorage) } -Tag @{ release = 'GA'; ruleSet = '2020_06'; 'Azure.WAF/pillar' = 'Reliability'; } {
$services = @($TargetObject);
if ($PSRule.TargetType -eq 'Microsoft.Storage/storageAccounts') {
$services = @(GetSubResources -ResourceType 'Microsoft.Storage/storageAccounts/blobServices');
}
if ($services.Length -eq 0) {
return $Assert.Fail($LocalizedData.SubResourceNotFound, 'Microsoft.Storage/storageAccounts/blobServices');
}
foreach ($service in $services) {
$Assert.HasFieldValue($service, 'properties.deleteRetentionPolicy.enabled', $True);
}
}
# Synopsis: Use containers configured with a private access type that requires authorization.
Rule 'Azure.Storage.BlobAccessType' -Ref 'AZR-000199' -Type 'Microsoft.Storage/storageAccounts', 'Microsoft.Storage/storageAccounts/blobServices/containers' -If { !(IsFileStorage) } -Tag @{ release = 'GA'; ruleSet = '2020_06'; 'Azure.WAF/pillar' = 'Security'; } {
$containers = @($TargetObject);
if ($PSRule.TargetType -eq 'Microsoft.Storage/storageAccounts') {
$containers = @(GetSubResources -ResourceType 'Microsoft.Storage/storageAccounts/blobServices/containers');
}
if ($containers.Length -eq 0) {
return $Assert.Pass();
}
foreach ($container in $containers) {
$Assert.HasDefaultValue($container, 'Properties.publicAccess', 'None').
Reason($LocalizedData.PublicAccessStorageContainer, $container.name, $container.Properties.publicAccess);
}
}
# Synopsis: Use Storage naming requirements
Rule 'Azure.Storage.Name' -Ref 'AZR-000201' -Type 'Microsoft.Storage/storageAccounts' -Tag @{ release = 'GA'; ruleSet = '2020_06'; 'Azure.WAF/pillar' = 'Operational Excellence'; } {
# https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules#microsoftstorage
# Between 3 and 24 characters long
$Assert.GreaterOrEqual($TargetObject, 'Name', 3)
$Assert.LessOrEqual($TargetObject, 'Name', 24)
# Lowercase letters and numbers
Match 'Name' '^[a-z0-9]{3,24}$' -CaseSensitive
}
# Synopsis: Enable soft delete on Storage Accounts file shares.
Rule 'Azure.Storage.FileShareSoftDelete' -Ref 'AZR-000298' -Type 'Microsoft.Storage/storageAccounts', 'Microsoft.Storage/storageAccounts/fileServices' -If { (IsFileStorage) -and !(IsCloudShell) -and !(IsHnsStorage) } -Tag @{ release = 'GA'; ruleSet = '2022_09'; 'Azure.WAF/pillar' = 'Reliability'; } {
$services = @($TargetObject);
if ($PSRule.TargetType -eq 'Microsoft.Storage/storageAccounts') {
$services = @(GetSubResources -ResourceType 'Microsoft.Storage/storageAccounts/fileServices');
}
if ($services.Length -eq 0) {
return $Assert.Fail($LocalizedData.SubResourceNotFound, 'Microsoft.Storage/storageAccounts/fileServices');
}
foreach ($service in $services) {
$Assert.HasFieldValue($service, 'properties.shareDeleteRetentionPolicy.enabled', $True);
$Assert.HasFieldValue($service, 'properties.shareDeleteRetentionPolicy.days', 7);
}
}
# Synopsis: Enable container soft delete on Storage Accounts.
Rule 'Azure.Storage.ContainerSoftDelete' -Ref 'AZR-000289' -Type 'Microsoft.Storage/storageAccounts', 'Microsoft.Storage/storageAccounts/blobServices' -If { !(IsCloudShell) -and !(IsHnsStorage) -and !(IsFileStorage) } -Tag @{ release = 'GA'; ruleSet = '2022_09'; 'Azure.WAF/pillar' = 'Reliability'; } {
$services = @($TargetObject);
if ($PSRule.TargetType -eq 'Microsoft.Storage/storageAccounts') {
$services = @(GetSubResources -ResourceType 'Microsoft.Storage/storageAccounts/blobServices');
}
if ($services.Length -eq 0) {
return $Assert.Fail($LocalizedData.SubResourceNotFound, 'Microsoft.Storage/storageAccounts/blobServices');
}
foreach ($service in $services) {
$Assert.HasFieldValue($service, 'properties.containerDeleteRetentionPolicy.enabled', $True);
$Assert.GreaterOrEqual($service, 'properties.containerDeleteRetentionPolicy.days', 1);
}
}
# Synopsis: Enable Malware Scanning in Microsoft Defender for Storage.
Rule 'Azure.Storage.Defender.MalwareScan' -Alias 'Azure.Storage.DefenderCloud.MalwareScan' -Ref 'AZR-000384' -Type 'Microsoft.Storage/storageAccounts' -If { IsPublicNetworkAccessEnabled } -Tag @{ release = 'GA'; ruleSet = '2024_03'; 'Azure.WAF/pillar' = 'Security'; } -Labels @{ 'Azure.MCSB.v1/control' = @('DP-2', 'LT-1') } {
$malwareDisabled = @(GetSubResources -ResourceType 'Microsoft.Security/DefenderForStorageSettings' |
Where-Object { $_.properties.malwareScanning.onUpload.isEnabled -eq $False })
$Assert.Count($malwareDisabled, '.', 0).Reason($LocalizedData.ResStorageMalwareScanning, $PSRule.TargetName)
}
# Synopsis: Enable sensitive data threat detection in Microsoft Defender for Storage.
Rule 'Azure.Storage.Defender.DataScan' -Alias 'Azure.Storage.DefenderCloud.SensitiveData' -Ref 'AZR-000391' -Type 'Microsoft.Storage/storageAccounts' -If { IsPublicNetworkAccessEnabled } -Tag @{ release = 'preview'; ruleSet = '2023_06'; 'Azure.WAF/pillar' = 'Security'; } -Labels @{ 'Azure.MCSB.v1/control' = @('DP-2', 'LT-1') } {
$sensitiveDisabled = @(GetSubResources -ResourceType 'Microsoft.Security/DefenderForStorageSettings' |
Where-Object { $_.properties.sensitiveDataDiscovery.isEnabled -eq $False })
$Assert.Count($sensitiveDisabled, '.', 0).Reason($LocalizedData.ResStorageSensitiveDataThreatDetection, $PSRule.TargetName)
}
# Synopsis: Enable Microsoft Defender for Storage for storage accounts.
Rule 'Azure.Storage.DefenderCloud' -Ref 'AZR-000386' -Type 'Microsoft.Storage/storageAccounts' -If { $Configuration.AZURE_STORAGE_DEFENDER_PER_ACCOUNT -eq $True } -Tag @{ release = 'GA'; ruleSet = '2023_06'; 'Azure.WAF/pillar' = 'Security'; } -Labels @{ 'Azure.MCSB.v1/control' = @('DP-2', 'LT-1') } {
$defender = @(GetSubResources -ResourceType 'Microsoft.Security/DefenderForStorageSettings' |
Where-Object { $_.properties.isEnabled -eq $True })
$Assert.GreaterOrEqual($defender, '.', 1).Reason($LocalizedData.SubResourceNotFound, 'Microsoft.Security/DefenderForStorageSettings')
}
#region Helper functions
function global:ShouldStorageReplicate {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
return (IsStandardStorage) -and
!(IsCloudShell) -and
!(IsFunctionStorage) -and
!(IsMonitorStorage) -and
!(IsLargeFileSharesEnabled)
}
}
function global:IsStandardStorage {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
if ($PSRule.TargetType -ne 'Microsoft.Storage/storageAccounts') {
return $False;
}
return $TargetObject.sku.name -like 'Standard_*';
}
}
function global:IsCloudShell {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
if ($PSRule.TargetType -ne 'Microsoft.Storage/storageAccounts') {
return $False;
}
return $TargetObject.Tags.'ms-resource-usage' -eq 'azure-cloud-shell';
}
}
function global:IsFunctionStorage {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
if ($PSRule.TargetType -ne 'Microsoft.Storage/storageAccounts') {
return $False;
}
return $TargetObject.Tags.'resource-usage' -eq 'azure-functions';
}
}
function global:IsMonitorStorage {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
if ($PSRule.TargetType -ne 'Microsoft.Storage/storageAccounts') {
return $False;
}
return $TargetObject.Tags.'resource-usage' -eq 'azure-monitor';
}
}
function global:IsFileStorage {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
if ($PSRule.TargetType -ne 'Microsoft.Storage/storageAccounts') {
return $False;
}
return $Assert.HasFieldValue($TargetObject, 'Kind', 'FileStorage').Result;
}
}
# Some features are not supported with hierarchical namespace
function global:IsHnsStorage {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
if ($PSRule.TargetType -ne 'Microsoft.Storage/storageAccounts') {
return $False;
}
return $Assert.HasFieldValue($TargetObject, 'Properties.isHnsEnabled', $True).Result;
}
}
function global:IsLargeFileSharesEnabled {
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
process {
if ($PSRule.TargetType -ne 'Microsoft.Storage/storageAccounts') {
return $False;
}
return $Assert.HasFieldValue($TargetObject, 'Properties.largeFileSharesState', 'Enabled').Result;
}
}
function global:IsPublicNetworkAccessEnabled {
[CmdletBinding()]
param ()
process {
$Assert.HasDefaultValue($TargetObject, 'properties.publicNetworkAccess', 'Enabled').Result
}
}
#endregion Helper functions