forked from microsoft/Azure-Security-Center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzure-Security-Center.psm1
1643 lines (1444 loc) · 70.4 KB
/
Azure-Security-Center.psm1
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
#region ------------Internal Functions-------------------
function Show-Warning {
Write-Verbose "This module is an open-source project and not formally part of the Microsoft Azure Security Center product. In addition, this module is currently in development and may have bugs/issues; please use at your own risk."
}
function Set-Context {
if(-not (Get-Module AzureRm.Profile)) {
Import-Module AzureRm.Profile
}
Write-Verbose "Checking AzureRM.profile version"
$azureRmProfileModuleVersion = (Get-Module AzureRm.Profile).Version
# refactoring performed in AzureRm.Profile v3.0 or later
if($azureRmProfileModuleVersion.Major -ge 3) {
Write-Verbose "AzureRM.profile v3.x verified"
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
if(-not $azureRmProfile.Accounts.Count) {
Write-Error "Ensure you have logged in before calling this function."
}
}
else {
# AzureRm.Profile < v3.0
Write-Verbose "AzureRM.profile v2.x verified"
$azureRmProfile = [Microsoft.WindowsAzure.Commands.Common.AzureRmProfileProvider]::Instance.Profile
if(-not $azureRmProfile.Context.Account.Count) {
Write-Error "Ensure you have logged in before calling this function."
}
}
Write-Verbose "Checking AzureRM Context"
$currentAzureContext = Get-AzureRmContext
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
Write-Verbose "Getting access token"
Write-Debug ("Getting access token for tenant" + $currentAzureContext.Subscription.TenantId)
$token = $profileClient.AcquireAccessToken($currentAzureContext.Subscription.TenantId)
$token = $token.AccessToken
Write-Verbose "Extracting subscription and tenant ids"
$asc_subscriptionId = $currentAzureContext.Subscription.Id
$asc_tenantId = $currentAzureContext.Tenant.Id
Write-Verbose "Creating auth header"
Set-Variable -Name asc_requestHeader -Scope Script -Value @{"Authorization" = "Bearer $token"}
#2.x AzureRM outputs subscriptionid differently.
if($azureRmProfileModuleVersion.Major -le 2) {Set-Variable -Name asc_subscriptionId -Scope Script -Value $currentAzureContext.Subscription.SubscriptionId}
else{Set-Variable -Name asc_subscriptionId -Scope Script -Value $currentAzureContext.Subscription.Id}
Write-Verbose "Setting wellknown vars"
$Script:asc_clientId = "1950a258-227b-4e31-a9cf-717495945fc2" # Well-known client ID for Azure PowerShell
$Script:asc_redirectUri = "urn:ietf:wg:oauth:2.0:oob" # Redirect URI for Azure PowerShell
$Script:asc_resourceAppIdURI = "https://management.azure.com/" # Resource URI for REST API
$Script:asc_url = 'management.azure.com' # Well-known URL endpoint
$Script:asc_version = "2015-06-01-preview" # Default API Version
}
#endregion
<#
.Synopsis
Build-ASCPolicy creates the JSON format needed for Set-ASCPolicy.
.DESCRIPTION
When running the command, it will perform a GET request for your existing ASC policy configuraiton and will only update parameters you specify. The command currently has parameters for JIT Port Administration configuration, but the JIT commands are not currently in the module. These will be added later.
.EXAMPLE
Build-ASCPolicy -PolicyName Default
{
"properties": {
"policyLevel": "Subscription",
"name": "default",
"unique": "Off",
"logCollection": "On",
"recommendations": {
"patch": "On",
"baseline": "On",
"antimalware": "On",
"diskEncryption": "On",
"acls": "On",
"nsgs": "On",
"waf": "On",
"sqlAuditing": "On",
"sqlTde": "On",
"ngfw": "On",
"vulnerabilityAssessment": "On",
"storageEncryption": "On",
"jitNetworkAccess": "On"
},
"logsConfiguration": {
"storages": {
}
},
"omsWorkspaceConfiguration": {
"workspaces": {
}
},
"securityContactConfiguration": {
"securityContactEmails": [
],
"securityContactPhone": "867-5309",
"areNotificationsOn": true,
"sendToAdminOn": false
},
"pricingConfiguration": {
"selectedPricingTier": "Free",
"standardTierStartDate": "0001-01-01T00:00:00",
"premiumTierStartDate": "0001-01-01T00:00:00"
},
"lastStorageCreationTime": "1970-01-01T00:00:00Z"
}
}
The above example simply shows the existing configuration for the specified policy.
.EXAMPLE
Build-ASCPolicy -PolicyName Default -AllOff -DataCollection Off -SecurityContactEmail "[email protected]","[email protected]"
{
"properties": {
"policyLevel": "Subscription",
"name": "default",
"unique": "Off",
"logCollection": "Off",
"recommendations": {
"patch": "Off",
"baseline": "Off",
"antimalware": "Off",
"diskEncryption": "Off",
"acls": "Off",
"nsgs": "Off",
"waf": "Off",
"sqlAuditing": "Off",
"sqlTde": "Off",
"ngfw": "Off",
"vulnerabilityAssessment": "Off",
"storageEncryption": "Off",
"jitNetworkAccess": "Off"
},
"logsConfiguration": {
"storages": {
}
},
"omsWorkspaceConfiguration": {
"workspaces": {
}
},
"securityContactConfiguration": {
"securityContactEmails": [
],
"securityContactPhone": "867-5309",
"areNotificationsOn": true,
"sendToAdminOn": false
},
"pricingConfiguration": {
"selectedPricingTier": "Free",
"standardTierStartDate": "0001-01-01T00:00:00",
"premiumTierStartDate": "0001-01-01T00:00:00"
},
"lastStorageCreationTime": "1970-01-01T00:00:00Z"
}
}
The above example builds a policy that turns all recommendations off and log collection off and changes the security contact to "[email protected]" and "[email protected]"
#>
function Build-ASCPolicy {
[CmdletBinding()]
Param
(
# PolicyName - Specify policy name for configuration.Note: Currently only Default is supported for Policy configurations.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[string]$PolicyName,
# All Recommendations On. This turns all ASC recommendation flags to 'On'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[switch]$AllOn,
# All Recommendations Off. This turns all ASC recommendation flags to 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[switch]$AllOff,
# Patch. Specifies if Patch recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$Patch,
# Baseline. Specifies if Baseline recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$Baseline,
# AntiMalware. Specifies if AntiMalware recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$AntiMalware,
# DiskEncryption. Specifies if DiskEncryption recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$DiskEncryption,
# ACLS. Specifies if ACLS recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$ACLS,
# NSGS. Specifies if NSGS recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$NSGS,
# WAF. Specifies if WAF recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$WAF,
# SQLAuditing. Specifies if SQL Auditing recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$SQLAuditing,
# SQLTDE. Specifies if SQLTDE recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$SQLTDE,
# NGFW. Specifies if NGFW recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$NGFW,
# VulnerabilityAssessment. Specifies if Vulnerability Assessment recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$VulnerabilityAssessment,
# StorageEncryption. Specifies if Storage Encryption recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$StorageEncryption,
# JITNetworkAccess. Specifies if JIT Network Access recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$JITNetworkAccess,
# Application Whitelisting. Specifies if Application Whitelisting recommendation should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$ApplicationWhitelisting,
# DataCollection. Specifies if data collection for the resources in the subscription should be 'On' or 'Off'.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('On','Off')]
[string]$DataCollection,
# Security Contact Email. You may specify multiple names by comma separating. Example: "[email protected]", "[email protected]"
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[string[]]$SecurityContactEmail,
# Security Contact Phone Number.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[string]$SecurityContactPhone,
# Security Contact - Send notifications about alerts. This turns on automated notifications for the subscription.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('true','false')]
[string]$SecurityContactNotificationsOn,
# Security Contact - Send notifications to subscription owner as well.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('true','false')]
[string]$SecurityContactSendToAdminOn,
# Pricing Tier. Specifies the pricing tier for the subscription. Note, setting this to Standard may cause you to incur costs.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Policy')]
[ValidateSet('Free','StandardTrial','Standard')]
[string]$PricingTier,
# Security API version. By default this uses the $asc_version variable which this module pre-sets. Only specify this if necessary.
[Parameter(Mandatory=$false)]
[string]$Version
)
Begin {
Show-Warning
Set-Context
if (!$Version) {$Version = $asc_version}
$asc_APIVersion = "?api-version=$Version" #Build version syntax.
try{
# Additional parameter validations and mutual exclusions
If ($AllOn -and $AllOff) {Throw 'Cannot reconcile app parameters. Only use one of them at a time.'}
If (($AllOn -or $AllOff) -and ($Patch -or $Baseline -or $AntiMalware -or $DiskEncryption -or $ACLS -or $NSGS -or $WAF -or $SQLAuditing -or $SQLTDE -or $NGFW -or $VulnerabilityAssessment -or $StorageEncryption -or $JITNetworkAccess)) {Throw 'Cannot reconcile app parameters. Do not specify individual properties in addition to AllOn or AllOf.'}
#Retrieve existing policy and build hashtable
$a = Get-ASCPolicy -PolicyName $PolicyName
$json_policy = @{
properties = @{
policyLevel = $a.properties.policyLevel
policyName = $a.properties.name
unique = $a.properties.unique
logCollection = $a.properties.logCollection
recommendations = $a.properties.recommendations
logsConfiguration = $a.properties.logsConfiguration
omsWorkspaceConfiguration = $a.properties.omsWorkspaceConfiguration
securityContactConfiguration = $a.properties.securityContactConfiguration
pricingConfiguration = $a.properties.pricingConfiguration
}
}
if ($json_policy.properties.recommendations -eq $null){Write-Error "The specified policy does not exist."; return}
#Turn all recommendations off if specified
if ($AllOff){
#Set all params to off unless specified
$json_policy.properties.recommendations.patch = "Off"
$json_policy.properties.recommendations.baseline = "Off"
$json_policy.properties.recommendations.antimalware = "Off"
$json_policy.properties.recommendations.diskEncryption = "Off"
$json_policy.properties.recommendations.acls = "Off"
$json_policy.properties.recommendations.nsgs = "Off"
$json_policy.properties.recommendations.waf = "Off"
$json_policy.properties.recommendations.sqlAuditing = "Off"
$json_policy.properties.recommendations.sqlTde = "Off"
$json_policy.properties.recommendations.ngfw = "Off"
$json_policy.properties.recommendations.vulnerabilityAssessment = "Off"
$json_policy.properties.recommendations.storageEncryption = "Off"
$json_policy.properties.recommendations.jitNetworkAccess = "Off"
$json_policy.properties.recommendations.appWhitelisting = "Off"
}
#Turn all recommendations on if specified
if ($AllOn){
#Set all params to off unless specified
$json_policy.properties.recommendations.patch = "On"
$json_policy.properties.recommendations.baseline = "On"
$json_policy.properties.recommendations.antimalware = "On"
$json_policy.properties.recommendations.diskEncryption = "On"
$json_policy.properties.recommendations.acls = "On"
$json_policy.properties.recommendations.nsgs = "On"
$json_policy.properties.recommendations.waf = "On"
$json_policy.properties.recommendations.sqlAuditing = "On"
$json_policy.properties.recommendations.sqlTde = "On"
$json_policy.properties.recommendations.ngfw = "On"
$json_policy.properties.recommendations.vulnerabilityAssessment = "On"
$json_policy.properties.recommendations.storageEncryption = "On"
$json_policy.properties.recommendations.jitNetworkAccess = "On"
$json_policy.properties.recommendations.appWhitelisting = "On"
}
#Update recommendations if individual parameters are specified
If ($Patch){$json_policy.properties.recommendations.patch = $Patch}
If ($Baseline){$json_policy.properties.recommendations.baseline = $Baseline}
If ($AntiMalware){$json_policy.properties.recommendations.antimalware = $AntiMalware}
If ($DiskEncryption){$json_policy.properties.recommendations.diskEncryption = $DiskEncryption}
If ($ACLS){$json_policy.properties.recommendations.acls = $ACLS}
If ($NSGS){$json_policy.properties.recommendations.nsgs = $NSGS}
If ($WAF){$json_policy.properties.recommendations.waf = $WAF}
If ($SQLAuditing){$json_policy.properties.recommendations.sqlAuditing = $SQLAuditing}
If ($SQLTDE){$json_policy.properties.recommendations.sqlTde = $SQLTDE}
If ($NGFW){$json_policy.properties.recommendations.ngfw = $NGFW}
If ($VulnerabilityAssessment){$json_policy.properties.recommendations.vulnerabilityAssessment = $VulnerabilityAssessment}
If ($StorageEncryption){$json_policy.properties.recommendations.storageEncryption = $StorageEncryption}
If ($JITNetworkAccess){$json_policy.properties.recommendations.jitNetworkAccess = $JITNetworkAccess}
If ($ApplicationWhitelisting){$json_policy.properties.recommendations.appWhitelisting = $ApplicationWhitelisting}
#Update security contact information if specified
If ($SecurityContactEmail){
$SecurityContactEmailArray = @()
foreach ($i in $SecurityContactEmail){$SecurityContactEmailArray += $i}
$json_policy.properties.securityContactConfiguration.securityContactEmails = $SecurityContactEmailArray
}
If ($SecurityContactPhone){$json_policy.properties.securityContactConfiguration.securityContactPhone = $SecurityContactPhone}
If ($SecurityContactNotificationsOn){$json_policy.properties.securityContactConfiguration.areNotificationsOn = $SecurityContactNotificationsOn}
If ($SecurityContactSendToAdminOn){$json_policy.properties.securityContactConfiguration.sendToAdminOn = $SecurityContactSendToAdminOn}
#If ($SecurityContactEmail -or $SecurityContactPhone -or $SecurityContactNotifications -or $SecurityContactSendToAdmin) {$json_policy.properties.securityContactConfiguration.lastSaveDateTime = ((get-date -Format o) -replace '-\d{2}:\d{2}','Z')}
#Update data collection if specified
If ($DataCollection){$json_policy.properties.logCollection = $DataCollection}
#Update pricing tier if specified
If ($PricingTier){$json_policy.properties.pricingConfiguration.selectedPricingTier = $PricingTier}
#Convert hash table to JSON for Set-ASCPolicy cmdlet
$json_policy | ConvertTo-Json -Depth 3
}#end try block
catch{
Write-Error $_
}
}#end begin block
Process {
}
End {
}
}
<#
.Synopsis
Get-ASCPolicy
.DESCRIPTION
Get-ASCPolicy is used to retrieve either a list of set policies or a specific policy for a specified resource.
.EXAMPLE
Get-ASCPolicy | Format-List
id : /subscriptions/<subscriptionId>/providers/Microsoft.Security/policies/policy1
name : default
type : Microsoft.Security/policies
properties : @{policyLevel=Subscription; name=Default; unique=Off; logCollection=On; recommendations=; logsConfiguration=; omsWorkspaceConfiguration=; securityContactConfiguration=;
pricingConfiguration=}
id : /subscriptions/<subscriptionId>/providers/Microsoft.Security/policies/policy2
name : default
type : Microsoft.Security/policies
properties : @{policyLevel=Subscription; name=policy2; unique=On; logCollection=On; recommendations=; logsConfiguration=; omsWorkspaceConfiguration=; securityContactConfiguration=;
pricingConfiguration=}
Fetches details for all policies.
.EXAMPLE
(Get-ASCPolicy -PolicyName default).properties.recommendations
patch : On
baseline : On
antimalware : On
diskEncryption : On
acls : On
nsgs : On
waf : On
sqlAuditing : On
sqlTde : On
ngfw : On
vulnerabilityAssessment : On
storageEncryption : On
jitNetworkAccess : On
The above example fetches default policy and displays the current recommendations settings.
#>
function Get-ASCPolicy {
[CmdletBinding()]
Param
(
# Fetches a specific policy by name.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
Position=0,
ParameterSetName='Fetch')]
[String]$PolicyName,
# Security API version. By default this uses the $asc_version variable which this module pre-sets. Only specify this if necessary.
[Parameter(Mandatory=$false)]
[string]$Version
)
Begin {
Show-Warning
Set-Context
if (!$Version) {$Version = $asc_version}
$asc_APIVersion = "?api-version=$Version" #Build version syntax.
$asc_endpoint = 'policies' #Set endpoint.
}
Process {
If ($PSCmdlet.ParameterSetName -ne 'Fetch') {
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/$asc_endpoint$asc_APIVersion"
Try {
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Get -Headers $asc_requestHeader
}
Catch [System.Net.WebException] {
Write-Error $_
}
Finally {
$asc_request.value
}
}
If ($PolicyName) {
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/$asc_endpoint/$PolicyName$asc_APIVersion"
Try {
Write-Verbose "Retrieving data for $PolicyName..."
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Get -Headers $asc_requestHeader
}
Catch {
Write-Error $_
}
Finally {
$asc_request
}
}
}
End {
}
}
<#
.Synopsis
Set-ASCPolicy is used to update the current protection policy for your active subscription.
.DESCRIPTION
This cmdlet currently only works for the default policy in your active subscription. To change your active subscription either re-run Get-ASCCredential and select the desired subscription from the list, or run ($asc_subscriptionId = <your subscription id>) to change the global variable used by this module.
.EXAMPLE
Set-ASCPolicy -PolicyName default -JSON (Build-ASCJSON -Policy -DataCollection On -SecurityContactEmail [email protected], [email protected])
The above example uses the Set-ASCPolicy cmdlet against the default policy for the active subscriptionId and passes in the JSON configuration by running Build-ASCJSON within parentheses.
The Build-ASCJSON parameters specified will turn on data collection and replace the existing security contact email addresses with two new addresses.
The command should return a StatusCode: 200 (OK)
You can verify your updated configuration by running Get-ASCPolicy.
#>
function Set-ASCPolicy {
[CmdletBinding()]
Param
(
# Fetches a specific policy by name.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
Position=0)]
[String]$PolicyName,
# Fetches a specific policy by name.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$false)]
[string]$JSON,
# Security API version. By default this uses the $asc_version variable which this module pre-sets. Only specify this if necessary.
[Parameter(Mandatory=$false)]
[string]$Version
)
Begin {
Show-Warning
Set-Context
if (!$Version) {$Version = $asc_version}
$asc_APIVersion = "?api-version=$Version" #Build version syntax.
$asc_endpoint = 'policies' #Set endpoint.
}
Process {
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/$asc_endpoint/$PolicyName$asc_APIVersion"
$result = Invoke-WebRequest -Uri $asc_uri -Method Put -Headers $asc_requestHeader -Body $JSON -UseBasicParsing -ContentType "application/json"
$result.StatusDescription
}
End {
}
}
<#
.Synopsis
Get-ASCStatus retrieves the data collection status of all resources currently being protected in your active subscription.
.DESCRIPTION
This cmdlet will display the monitoring health and status of your azure resources for the active subscription. This data is based on data collection being enabled for your resources which can be set in your policy.
.EXAMPLE
(Get-ASCStatus | ?{$_.id -match 'Kali-01$'}).properties
vmAgent : On
dataCollector : Off
dataCollectorInstallationStatus : FailureDueToVmStopped
dataCollectorPolicy : On
antimalwareScannerData : @{antimalwareInstallationSecurityState=None; antimalwareSupportLogCollectionSecurityState=None; antimalwareHealthIssuesSecurityState=None; antimalwareComponentList=System.Object[];
dataType=Antimalware; isScannerDataValid=False; policy=On; dataExists=False; securityState=None; lastReportTime=0001-01-01T00:00:00}
baselineScannerData : @{failedRulesSecurityState=None; dataType=Baseline; isScannerDataValid=False; policy=On; dataExists=False; securityState=None; lastReportTime=0001-01-01T00:00:00}
patchScannerData : @{rebootPendingSecurityState=None; missingPatchesSecurityState=None; dataType=Patch; isScannerDataValid=False; policy=On; dataExists=False; securityState=None;
lastReportTime=0001-01-01T00:00:00}
vmInstallationsSecurityState : Medium
encryptionDataState : @{securityState=None; isSupported=False; isOsDiskEncrypted=False; isDataDiskEncrypted=False}
vulnerabilityAssessmentScannerStatus : @{isSupported=False; provider=Unknown; dataType=VulnerabilityAssessment; isScannerDataValid=False; policy=On; dataExists=False; securityState=None; lastReportTime=0001-01-01T00:00:00}
name : VirtualMachineHealthStateProperties
type : VirtualMachine
securityState : Medium
The above example retrieves the data collection status for the Kali-O1 VM and displays the properties.
#>
function Get-ASCStatus {
[CmdletBinding()]
Param
(
# Security API version. By default this uses the $asc_version variable which this module pre-sets. Only specify this if necessary.
[Parameter(Mandatory=$false)]
[string]$Version
)
Show-Warning
Set-Context
if (!$Version) {$Version = $asc_version}
$asc_APIVersion = "?api-version=$Version" #Build version syntax.
$asc_endpoint = 'securityStatuses' #Set endpoint.
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/$asc_endpoint$asc_APIVersion"
Try {
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Get -Headers $asc_requestHeader
$asc_request.value
}
Catch {
Write-Error $_
}
}
<#
.Synopsis
Get-ASCTask displays the current tasks in Azure Security Center.
.DESCRIPTION
This cmdlet displays the available tasks for your resources in the active subscription. These tasks are based on your set recommendations set in your policy.
.EXAMPLE
(Get-ASCTask).properties.securitytaskparameters | select storageaccountname, name
storageAccountName name
------------------ ----
defaultnetworkingdiag494 Enable encryption for Azure Storage Account
VirtualMachinesNsgShouldRestrictTrafficTaskParameters
VirtualMachinesNsgShouldRestrictTrafficTaskParameters
ProvisionNgfw
w10x6401disks523 Enable encryption for Azure Storage Account
NetworkSecurityGroupMissingOnSubnet
NetworkSecurityGroupMissingOnSubnet
122193westus2 Enable encryption for Azure Storage Account
EncryptionOnVm
ProvisionNgfw
UpgradePricingTierTaskParameters
defaultnetworking698 Enable encryption for Azure Storage Account
The above example retrives the available tasks displays the relevant storage account and task name only.
#>
function Get-ASCTask {
[CmdletBinding()]
Param
(
# Security API version. By default this uses the $asc_version variable which this module pre-sets. Only specify this if necessary.
[Parameter(Mandatory=$false)]
[string]$Version
)
Begin {
Show-Warning
Set-Context
if (!$Version) {$Version = $asc_version}
$asc_APIVersion = "?api-version=$Version" #Build version syntax.
$asc_endpoint = 'tasks' #Set endpoint.
}
Process {
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/$asc_endpoint$asc_APIVersion"
Try {
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Get -Headers $asc_requestHeader
}
Catch {
Write-Error $_
}
Finally {
$asc_request.value
}
}
End {
}
}
<#
.Synopsis
Set-ASCTask updates the status of a task
.DESCRIPTION
This cmdlet can be used to update task status to either dismiss or activate.
.EXAMPLE
Set-ASCTask -TaskID 09eb1b85-1b5b-c4b6-5ad3-b3c383b1a83d -Dismiss
(Get-ASCTask).properties | select -first 1
state
-----
Dismissed
The first command marks the set task as dismissed. The second command checks the status of the updated task and displays the state.
#>
function Set-ASCTask {
[CmdletBinding()]
Param
(
# Task ID
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[alias("name")]
[string[]]$TaskID,
# Dismiss Flag
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Dismiss')]
[switch]$Dismiss,
# Activate Flag
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Activate')]
[switch]$Activate,
# Security API version. By default this uses the $asc_version variable which this module pre-sets. Only specify this if necessary.
[Parameter(Mandatory=$false)]
[string]$Version = $asc_version
)
Begin {
Show-Warning
Set-Context
if (!$Version) {$Version = $asc_version}
$asc_APIVersion = "?api-version=$Version" #Build version syntax.
$asc_endpoint = 'tasks' #Set endpoint.
}
Process {
If ($PSCmdlet.ParameterSetName -eq 'Dismiss') {
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/locations/centralus/$asc_endpoint/$TaskID/dismiss$asc_APIVersion"
Try {
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Post -Headers $asc_requestHeader
$asc_request
}
Catch {
Write-Error $_
}
Finally {
}
}
If ($PSCmdlet.ParameterSetName -eq 'Activate') {
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/locations/centralus/$asc_endpoint/$TaskID/activate$asc_APIVersion"
Try {
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Post -Headers $asc_requestHeader
$asc_request
}
Catch {
Write-Error $_
}
Finally {
}
}
}
End {
}
}
<#
.Synopsis
Get-ASCAlert
.DESCRIPTION
This cmdlet receives a collection of alerts. Note, alerts are only avaible in Standart-tier subscriptions.
.EXAMPLE
Get-ASCAlert | select -First 20 @{N='Alert';E={$_.properties.alertdisplayname}}
Alert
-----
Potential SQL Injection
Deep Security Agent detected a malware
Possible outgoing spam activity detected
Modified system binary discovered in dump file 5bd767e4-2d08-4714-b744-aaed04b57107__391365252.hdmp
Security incident detected
Network communication with a malicious machine detected
Multiple Domain Accounts Queried
Suspicious SVCHOST process executed
Successful RDP brute force attack
Failed RDP Brute Force Attack
The above command retrieves the last 20 alerts and shows them in a table, renaming the alertdisplayname property to 'Alert'.
#>
function Get-ASCAlert {
[CmdletBinding()]
Param
(
# Specify Alert ID to fetch a specific ASC Alert. If this is not set, this cmdlet will retrieve a collection of all alerts.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=0,
ParameterSetName='Fetch')]
[alias("id")]
[string[]]$AlertID,
# Security API version. By default this uses the $asc_version variable which this module pre-sets. Only specify this if necessary.
[Parameter(Mandatory=$false)]
[string]$Version
)
Begin {
Show-Warning
Set-Context
if (!$Version) {$Version = $asc_version}
$asc_APIVersion = "?api-version=$Version" #Build version syntax.
$asc_endpoint = 'alerts' #Set endpoint.
}
Process {
Try {
if ($PSCmdlet.ParameterSetName -eq 'Fetch') {
foreach ($i in $AlertID) {
if ($i -match '^/') {$i = ($i -split '/alerts/' | ?{$_ -notmatch '^/'})}
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/locations/centralus/$asc_endpoint/$i$asc_APIVersion"
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Get -Headers $asc_requestHeader
$asc_request
}
}
else {
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/locations/centralus/$asc_endpoint$asc_APIVersion"
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Get -Headers $asc_requestHeader
$asc_request.value
}
}
Catch {
Write-Error $_
}
Finally {
}
}
End {
}
}
<#
.Synopsis
Set-ASCAlert changes the status of an alert.
.DESCRIPTION
Changes the status of alerts.
.EXAMPLE
<example>
#>
function Set-ASCAlert {
[CmdletBinding()]
Param
(
# Alert ID
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[alias('id')]
[string[]]$AlertID,
# Dismiss Flag
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Dismiss')]
[switch]$Dismiss,
# Activate Flag
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
ParameterSetName='Activate')]
[switch]$Activate,
# Security API version. By default this uses the $asc_version variable which this module pre-sets. Only specify this if necessary.
[Parameter(Mandatory=$false)]
[string]$Version
)
Begin {
Show-Warning
Set-Context
if (!$Version) {$Version = $asc_version}
$asc_APIVersion = "?api-version=$Version" #Build version syntax.
$asc_endpoint = 'alerts' #Set endpoint.
}
Process {
If ($Dismiss -and !$Activate) {
Try {
foreach ($i in $AlertID) {
if ($i -match '^/') { $i = ($i -split '/alerts/' | ?{$_ -notmatch '^/'})}
Write-Warning "Dismissing alert $i"
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/locations/centralus/$asc_endpoint/$i/dismiss$asc_APIVersion"
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Post -Headers $asc_requestHeader | Out-Null
}
}
Catch {
Write-Error $_
}
Finally {
}
}
If ($Activate -and !$Dismiss) {
Try {
foreach ($i in $AlertID) {
if ($i -match '^/') { $i = ($i -split '/alerts/' | ?{$_ -notmatch '^/'})}
Write-Warning "Activating alert $i"
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/locations/centralus/$asc_endpoint/$i/activate$asc_APIVersion"
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Post -Headers $asc_requestHeader | Out-Null
}
}
Catch {
Write-Error $_
}
Finally {
$asc_request
}
}
If ($Activate -and $Dismiss) {
Write-Warning "You may not specify -Activate and -Dismiss at the same time."
break
}
}
End {
}
}
<#
.Synopsis
Get-ASCLocation
.DESCRIPTION
Retrieves data center location information for Azure Security Center data.
.EXAMPLE
Get-ASCLocation | fl -Force
id : /subscriptions/6b1ceacd-5731-4780-8f96-2078dd96fd96/providers/Microsoft.Security/locations/centralus
name : centralus
type : Microsoft.Security/locations
properties : @{homeRegionName=centralus}
The above example retrieves datacenter region information for the ASC service.
#>
function Get-ASCLocation {
[CmdletBinding()]
Param
(
# Security API version. By default this uses the $asc_version variable which this module pre-sets. Only specify this if necessary.
[Parameter(Mandatory=$false)]
[string]$Version
)
Show-Warning
Set-Context
if (!$Version) {$Version = $asc_version}
$asc_APIVersion = "?api-version=$Version" #Build version syntax.
$asc_endpoint = 'locations' #Set endpoint.
$asc_uri = "https://$asc_url/subscriptions/$asc_subscriptionId/providers/microsoft.Security/$asc_endpoint$asc_APIVersion"
Try {
$asc_request = Invoke-RestMethod -Uri $asc_uri -Method Get -Headers $asc_requestHeader
$asc_request.value
}
Catch {
Write-Error $_
}
}
<#
.Synopsis
Get-ASCSecuritySolutionReferenceData
.DESCRIPTION
Retrieves list of available partner solutions and their corrosponding information.
.EXAMPLE
Get-ASCSecuritySolutionReferenceData | ?{$_.name -match 'barracuda'} | fl -Force
id : /subscriptions/6b1ceacd-5731-4780-8f96-2078dd96fd96/providers/Microsoft.Security/securitySolutionsReferenceData/barracudanetworks.wafbyol-ARM.FullyI
ntegrated
name : barracudanetworks.wafbyol-ARM.FullyIntegrated
type : Microsoft.Security/securitySolutionsReferenceData
properties : @{alertVendorName=BarracudaWAF; securityFamily=Waf; packageInfoUrl=www.azure.com; productName=Web Application Firewall;
provisionType=FullyIntegrated; publisher=barracudanetworks; publisherDisplayName=Barracuda Networks, Inc.; template=barracudanetworks/wafbyol-ARM}
id : /subscriptions/6b1ceacd-5731-4780-8f96-2078dd96fd96/providers/Microsoft.Security/securitySolutionsReferenceData/barracudanetworks.wafbyol-ARM