-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdeploy-azuresecurityscenarios.ps1
309 lines (270 loc) · 12.1 KB
/
deploy-azuresecurityscenarios.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
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
<#
.SYNOPSIS
Short description
.DESCRIPTION
Copyright (c) Avyan Consulting Corp. All rights reserved.
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 ONINFRINGEMENT. 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.
.EXAMPLE
.\deploy-azuresecurityscenarios.ps1 -Help
Run this command to get list of supported scenarios
.EXAMPLE
.\deploy-azuresecurityscenarios.ps1 -Scenario "virus-attack-on-vm" -Command Deploy -Verbose
If you are using Cloud Shell you can simply pass 2 parameters to run the deployment.
.EXAMPLE
.\deploy-azuresecurityscenarios.ps1 -SubscriptionId <subscriptionId> -UserName <username> -Password <securePassword> -Scenario <scenario> -Command Deploy -Verbose
If you are running on a local machine pass additional parameters to connect to subscription and run the deployment.
.INPUTS
Inputs to this cmdlet (if any)
.OUTPUTS
Output from this cmdlet (if any)
.NOTES
General notes
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false,
ParameterSetName = "Deployment"
)]
[Parameter(Mandatory = $false,
ParameterSetName = "Cleanup"
)]
[ValidateScript( {
if ( (Get-Content -Path $PSScriptRoot\azure-security-poc.json | ConvertFrom-Json).PSObject.Properties.Name -contains "$_") {
$true
}
else {
throw "Invalid input. Run deploy-azuresecurityscenarios.ps1 -Help to view supported scenario names."
}
})]
[string]
$Scenario,
[Parameter(Mandatory = $false,
ParameterSetName = "Deployment"
)]
[ValidateSet("Deploy", "Attack", "Mitigate", "Remediate")]
[string]
$Command = "Deploy",
# Enter Subscription Id for deployment.
[Parameter(Mandatory = $false,
ParameterSetName = "Deployment"
)]
[Alias("subscription")]
[guid]
$SubscriptionId,
# Enter AAD Username with Owner permission at subscription level and Global Administrator at AAD level.
[Parameter(Mandatory = $false,
ParameterSetName = "Deployment"
)]
[Alias("user")]
[string]
$UserName,
# Enter AAD Username password as securestring.
[Parameter(Mandatory = $true,
ParameterSetName = "Deployment"
)]
[Alias("pwd")]
[securestring]
$Password,
# Enter AAD Username password as securestring.
[Parameter(Mandatory = $false,
ParameterSetName = "Deployment"
)]
[string]
$Location = "East US",
# use this switch for help information.
[Parameter(Mandatory = $false,
ParameterSetName = "Help"
)]
[switch]
$Help,
# use this switch for help cleanup deployed resources.
[Parameter(Mandatory = $false,
ParameterSetName = "Cleanup"
)]
[switch]
$Cleanup,
# use this switch to delete common deployed resources.
[Parameter(Mandatory = $false,
ParameterSetName = "Cleanup"
)]
[switch]
$DeleteCommonResources,
# use this switch for help cleanup deployed resources.
[Parameter(Mandatory = $false,
ParameterSetName = "AzureSecurityCenter"
)]
[switch]
$ConfigureASC,
# provide email address for alerts from security center.
[Parameter(Mandatory = $false,
ParameterSetName = "AzureSecurityCenter",
HelpMessage = "Provide email address for recieving alerts from Azure Security Center.")]
[Alias("email")]
[string]
$EmailAddressForAlerts = "dummy@contoso.com",
# Use this switch to skip OMS deployment
[Parameter(Mandatory = $false,
ParameterSetName = "Deployment"
)]
[switch]
$SkipOMSDeployment = $false,
# Use this switch to skip artifacts upload.
[Parameter(Mandatory = $false,
ParameterSetName = "Deployment"
)]
[switch]
$SkipArtifactsUpload = $false,
# use this switch to install prerequisites.
[Parameter(Mandatory = $false,
ParameterSetName = "Cleanup"
)]
[switch]
$InstallPreRequisites
)
$ErrorActionPreference = 'Stop'
$scenarios = Get-Content -Path $PSScriptRoot\azure-security-poc.json | ConvertFrom-Json
if ($Help) {
$scenarios.PSObject.Properties | Select-Object -Property Name , @{Name = "Description"; Expression = {$_.value.description}} | Format-Table
Break
}
$moduleFolderPath = "$PSScriptRoot\common\modules\powershell\asc.poc.psd1"
Import-Module $moduleFolderPath
$storageContainerName = "artifacts"
$artifactStagingDirectories = @(
"$PSScriptRoot\common"
"$PSScriptRoot\resources"
)
# Checking for required modules and importing modules
if ($InstallPreRequisites) {
& "$PSScriptRoot\common\scripts\install-modules.ps1"
}
if($UserName -eq $null){
$UserName = (Get-AzureRmContext).Account.Id
}
$commonDeploymentResourceGroupName = "azuresecuritypoc-common-resources"
$tmp = [System.IO.Path]::GetTempFileName()
if ((Get-AzureRmContext).Subscription -eq $null) {
if ($SubscriptionId -eq $null -or $UserName -eq $null -or $Password -eq $null) {
throw "Kindly make sure SubscriptionID, Username and Password parameters are provided during the deployment."
}
### Create the credential object
$credential = New-Object System.Management.Automation.PSCredential($UserName, $Password)
try {
Write-Verbose "Setting AzureRM context to Subscription Id - $SubscriptionId."
Set-AzureRmContext -Subscription $SubscriptionId
}
catch {
Write-Verbose "Login to Subscription - $SubscriptionId"
Login-AzureRmAccount -Subscription $SubscriptionId -Credential $credential
}
}
else {
$subscriptionId = (Get-AzureRmContext).Subscription.Id
}
$deploymentHash = (Get-StringHash $SubscriptionId).substring(0, 10)
$storageAccountName = 'azsecstage' + $deploymentHash
if ($ConfigureASC) {
Write-Verbose "Enabling Azure Security Center and Policies."
& "$PSScriptRoot\common\scripts\Configure-AzureSecurityCenter.ps1" -EmailAddressForAlerts $EmailAddressForAlerts -Verbose
Break
}
$prefix = ($scenarios | Select-Object -expandproperty $Scenario).prefix
if ($Cleanup) {
Write-Verbose "Intiating Cleanup for $Scenario"
& "$PSScriptRoot\scenarios\$Scenario\scripts\cleanup.ps1" -Prefix $prefix -Verbose
Break
}
if ($DeleteCommonResources) {
try {
Write-Verbose "Deleting ResourceGroup - $commonDeploymentResourceGroupName"
Remove-AzureRmResourceGroup -Name $commonDeploymentResourceGroupName -Force
}
catch {
Throw $_
}
Write-Host "ResourceGroup - $commonDeploymentResourceGroupName deleted successfully."
}
if ($SkipArtifactsUpload) {
Write-Verbose "Check if artifacts storage account exists."
$storageAccount = (Get-AzureRmStorageAccount | Where-Object {$_.StorageAccountName -eq $storageAccountName})
if ($storageAccount -eq $null) {
Throw "Artifacts storage account does not exists. Please run deployment without SkipArtifactsUpload switch."
}
else {
Write-Verbose "Skipped artifacts upload."
}
}
else {
# Create Resourcegroup
New-AzureRmResourceGroup -Name $commonDeploymentResourceGroupName -Location $Location -Force
Write-Verbose "Check if artifacts storage account exists."
$storageAccount = (Get-AzureRmStorageAccount | Where-Object {$_.StorageAccountName -eq $storageAccountName})
# Create the storage account if it doesn't already exist
if ($storageAccount -eq $null) {
Write-Verbose "Artifacts storage account does not exists."
Write-Verbose "Provisioning artifacts storage account."
$storageAccount = New-AzureRmStorageAccount -StorageAccountName $storageAccountName -Type 'Standard_LRS' `
-ResourceGroupName $commonDeploymentResourceGroupName -Location $Location
Write-Verbose "Artifacts storage account provisioned."
Write-Verbose "Creating storage container to upload a blobs."
New-AzureStorageContainer -Name $storageContainerName -Context $storageAccount.Context -ErrorAction SilentlyContinue *>&1
}
else {
New-AzureStorageContainer -Name $storageContainerName -Context $storageAccount.Context -ErrorAction SilentlyContinue *>&1
}
# Copy files from the local storage staging location to the storage account container
foreach ($artifactStagingDirectory in $artifactStagingDirectories) {
$ArtifactFilePaths = Get-ChildItem $ArtifactStagingDirectory -Recurse -File | ForEach-Object -Process {$_.FullName}
foreach ($SourcePath in $ArtifactFilePaths) {
Set-AzureStorageBlobContent -File $SourcePath -Blob $SourcePath.Substring((Split-Path($ArtifactStagingDirectory)).length + 1) `
-Container $storageContainerName -Context $storageAccount.Context -Force
}
}
}
if ($SkipOMSDeployment) {
$omsWorkspaceName = (Get-AzureRmResourceGroupDeployment -ResourceGroupName azuresecuritypoc-common-resources | Where-Object DeploymentName -match 'oms').Outputs.workspaceName.Value
if ($omsWorkspaceName -eq $null) {
Throw "OMS workspace does not exist. Please run the deployment without SkipOMSDeployment switch"
}
else {
Write-Verbose "Skipped OMS deployment"
}
}
else {
Write-Verbose "Generate the value for artifacts location & 1 hour SAS token for the artifacts location."
$artifactsLocation = $storageAccount.Context.BlobEndPoint + $storageContainerName
$artifactsLocationSasToken = New-AzureStorageContainerSASToken -Container $storageContainerName -Context $storageAccount.Context -Permission r -ExpiryTime (Get-Date).AddHours(1)
Write-Verbose "SAS token for artifacts storage account generated successfully."
# Update parameter file with deployment values.
Write-Verbose "Updating parameter file."
$parametersObj = Get-Content -Path "$PSScriptRoot\scenarios\common-deployments\azuredeploy.parameters.json" | ConvertFrom-Json
$parametersObj.parameters.commonReference.value._artifactsLocation = $artifactsLocation
$parametersObj.parameters.commonReference.value._artifactsLocationSasToken = $artifactsLocationSasToken
( $parametersObj | ConvertTo-Json -Depth 10 ) -replace "\\u0027", "'" | Out-File $tmp
# Create Resourcegroup
New-AzureRmResourceGroup -Name $commonDeploymentResourceGroupName -Location $Location -Force
Write-Verbose "Initiate deployment for common resources"
New-AzureRmResourceGroupDeployment -ResourceGroupName $commonDeploymentResourceGroupName `
-TemplateFile "$PSScriptRoot\scenarios\common-deployments\azuredeploy.json" `
-TemplateParameterFile $tmp -Name $commonDeploymentResourceGroupName -Mode Incremental `
-DeploymentDebugLogLevel All -Verbose -Force
}
$omsWorkspaceResourceGroupName = $commonDeploymentResourceGroupName
$omsWorkspaceName = (Get-AzureRmResourceGroupDeployment -ResourceGroupName azuresecuritypoc-common-resources | Where-Object DeploymentName -match 'oms').Outputs.workspaceName.Value
switch ($Command) {
"Deploy" {
& "$PSScriptRoot\scenarios\$Scenario\deploy.ps1" -Prefix $prefix -artifactsStorageAccountName $storageAccountName -omsWorkspaceResourceGroupName $omsWorkspaceResourceGroupName -omsWorkspaceName $omsWorkspaceName -UserName $UserName -Verbose
}
"Remediate" {
}
"Attack" {
}
}