-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAZ-BetterSecDefaults.ps1
More file actions
870 lines (751 loc) · 41.9 KB
/
AZ-BetterSecDefaults.ps1
File metadata and controls
870 lines (751 loc) · 41.9 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
################!#################################################################################################################
###############!##################################################################################################################
##############!#INTRO#############################################################################################################
###############!##################################################################################################################
#################!################################################################################################################
# Script Name: Azure BetterSecDefaults
# Description: Creates a set of conditional access policies that will provide improved security over Microsofts "Security Defaults".
# This script is primarily for less mature orgs that are perhaps still using Microsofts "Security Defaults"
# or only have very basic conditional access policies in place.
# Author - https://github.com/biffalo/
# Credits - SecOps-Institute for TOR IP List //// X4BNet for the VPN IP List
Write-Host " ____ _ _ ____ ____ __ _ _ " -BackgroundColor DarkBlue -ForegroundColor Black
Write-Host " | __ ) ___| |_| |_ ___ _ __/ ___| ___ ___| _ \ ___ / _| __ _ _ _| | |_ ___ " -BackgroundColor DarkBlue -ForegroundColor Black
Write-Host " | _ \ / _ \ __| __/ _ \ '__\___ \ / _ \/ __| | | |/ _ \ |_ / _` | | | | | __/ __|" -BackgroundColor DarkBlue -ForegroundColor Black
Write-Host " | |_) | __/ |_| || __/ | ___) | __/ (__| |_| | __/ _| (_| | |_| | | |_\__ \" -BackgroundColor DarkBlue -ForegroundColor Black
Write-Host " |____/ \___|\__|\__\___|_| |____/ \___|\___|____/ \___|_| \__,_|\__,_|_|\__|___/" -BackgroundColor DarkBlue -ForegroundColor Black
Write-Host " " -BackgroundColor DarkBlue -ForegroundColor Black
Write-Host "This script creates 7 conditional access policies." -BackgroundColor DarkBlue -ForegroundColor White
Write-Host "MFA for All Apps with trusted location/hybrid joined devices excluded" -BackgroundColor DarkGreen -ForegroundColor White
Write-Host "Block outside of USA (excludes global admin)" -BackgroundColor DarkGreen -ForegroundColor White
Write-Host "Deny logon from device types Mac/Osx (excludes global admin)" -BackgroundColor DarkGreen -ForegroundColor White
Write-Host "Deny logon from device types Linux (excludes global admin)" -BackgroundColor DarkGreen -ForegroundColor White
Write-Host "Block Legacy Auth Except for Trusted Locations" -BackgroundColor DarkGreen -ForegroundColor White
Write-Host "Blocks logins from known VPN Providers/TOR Exit Nodes (excludes global admin) " -BackgroundColor DarkGreen -ForegroundColor White
Write-Host "Sign In Risk Policy (medium and high) (excludes global admin)(excludes trust locations)" -BackgroundColor DarkGreen -ForegroundColor White
Write-Host "Be sure to have TENANTID, GLOBAL ADMIN CREDS, and TRUSTED IPs IN CIDR FORMAT" -BackgroundColor DarkYellow -ForegroundColor Black
############!#####################################################################################################################
##########!#######################################################################################################################
###########!Connect##################################################################################################
#############!####################################################################################################################
##############!###################################################################################################################
Write-Host "CONNECTING TO AZURE/GRAPH" -BackgroundColor DarkBlue -ForegroundColor White
# Import Other Modules
Import-Module Microsoft.Graph.Reports
Import-Module Microsoft.Graph.Identity.SignIns
Import-Module Microsoft.Graph.Authentication
# Prompt for tenant ID
$TenantId = Read-Host "Enter Azure TenantID"
# Authenticate to Azure AD and Microsoft Graph
Connect-MgGraph -TenantId $TenantId -Scopes "User.Read.All, Policy.ReadWrite.ConditionalAccess, Directory.Read.All, Policy.Read.All, RoleManagement.Read.All" -NoWelcome
##########!#######################################################################################################################
###########!######################################################################################################################
##########!#Lic Check##################################################################################################
###########!######################################################################################################################
############!#####################################################################################################################
Write-Host "CHECKING FOR AzureAD/Entra P2 LIC" -BackgroundColor DarkBlue -ForegroundColor White
# Check for AzureAD/Entra Premium P2 license
$licenses = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/subscribedSkus"
$p2License = $licenses.value | Where-Object { $_.skuPartNumber -eq "AAD_PREMIUM_P2" }
if (-not $p2License) {
Write-Host "You have the wrong license type. AzureAD/Entra P2 license required. Exiting script." -BackgroundColor DarkRed -ForegroundColor White
exit
} else {
Write-Host "AzureAD/Entra P2 license is present! Continuing..." -BackgroundColor DarkGreen -ForegroundColor White
}
Write-Host "Caching existing Conditional Access policies and Global Administrator role..." -BackgroundColor DarkBlue -ForegroundColor White
$ExistingCAPolicies = Get-MgIdentityConditionalAccessPolicy -All
$GlobalAdminRoleIds = Get-MgRoleManagementDirectoryRoleDefinition -All |
Where-Object { $_.DisplayName -eq "Global Administrator" } |
Select-Object -ExpandProperty Id
function Refresh-ConditionalAccessPolicyCache {
$script:ExistingCAPolicies = Get-MgIdentityConditionalAccessPolicy -All
}
function Wait-NamedLocationAvailable {
param(
[Parameter(Mandatory)]
[string]$NamedLocationId,
[int]$MaxAttempts = 12,
[int]$DelaySeconds = 5
)
for ($attempt = 1; $attempt -le $MaxAttempts; $attempt++) {
try {
Get-MgIdentityConditionalAccessNamedLocation -NamedLocationId $NamedLocationId -ErrorAction Stop | Out-Null
return $true
}
catch {
if ($attempt -eq $MaxAttempts) {
return $false
}
Start-Sleep -Seconds $DelaySeconds
}
}
}
function Wait-ConditionalAccessPolicyAvailable {
param(
[string]$PolicyId,
[Parameter(Mandatory)]
[string]$DisplayName,
[int]$MaxAttempts = 12,
[int]$DelaySeconds = 5
)
for ($attempt = 1; $attempt -le $MaxAttempts; $attempt++) {
try {
if (-not [string]::IsNullOrWhiteSpace($PolicyId)) {
$policy = Get-MgIdentityConditionalAccessPolicy -ConditionalAccessPolicyId $PolicyId -ErrorAction Stop
}
else {
$policy = Get-MgIdentityConditionalAccessPolicy -All -ErrorAction Stop |
Where-Object { $_.DisplayName -eq $DisplayName } |
Select-Object -First 1
}
if ($policy) {
return $true
}
}
catch {
if ($attempt -eq $MaxAttempts) {
return $false
}
}
Start-Sleep -Seconds $DelaySeconds
}
return $false
}
##########!#######################################################################################################################
###########!######################################################################################################################
##########!#Check for Sec Defaults################################################################################################
###########!######################################################################################################################
############!#####################################################################################################################
# Check for Sec Defaults - Disable if Enabled
Write-Host "Checking if 'Security Defaults' is enabled..." -BackgroundColor DarkBlue -ForegroundColor White
$getSD = Get-MgPolicyIdentitySecurityDefaultEnforcementPolicy
if ($getSD.IsEnabled -eq $true) {
Write-Host "'Security Defaults' is enabled. Disabling it now...." -BackgroundColor DarkRed -ForegroundColor White
$params = @{
IsEnabled = $false
}
Update-MgPolicyIdentitySecurityDefaultEnforcementPolicy -BodyParameter $params
}
else {
Write-Host "'Security Defaults' is disabled. Continuing..." -BackgroundColor DarkGreen -ForegroundColor White
}
#!################################################################################################################################
##!###############################################################################################################################
###!####MFA FOR ALL CAP###########################################################################################################
####!#############################################################################################################################
#####!############################################################################################################################
Write-Host "POLICY - MFA FOR ALL" -BackgroundColor DarkBlue -ForegroundColor White
# Named Location Name
$locationName = "Trusted"
# Helper: Get named location reliably (server-side filter + explicit select)
function Get-TrustedNamedLocation {
param([string]$Name)
$loc = Get-MgIdentityConditionalAccessNamedLocation `
-Filter "displayName eq '$Name'" `
-Property "id,displayName" `
-All |
Select-Object -First 1
# Some SDK shapes place fields in AdditionalProperties
if ($loc -and [string]::IsNullOrWhiteSpace($loc.Id) -and $loc.AdditionalProperties) {
$maybeId = $loc.AdditionalProperties["id"]
if (-not [string]::IsNullOrWhiteSpace($maybeId)) {
$loc | Add-Member -NotePropertyName Id -NotePropertyValue $maybeId -Force
}
}
return $loc
}
# Check if the named location already exists
$existingNamedLocation = Get-TrustedNamedLocation -Name $locationName
if ($null -ne $existingNamedLocation) {
Write-Host "Named location 'Trusted' already exists with ID: $($existingNamedLocation.Id)" -BackgroundColor DarkBlue -ForegroundColor White
# Verify the location is retrievable
try {
Get-MgIdentityConditionalAccessNamedLocation -NamedLocationId $existingNamedLocation.Id -ErrorAction Stop | Out-Null
Write-Host "Verified named location 'Trusted' is accessible." -BackgroundColor DarkGreen -ForegroundColor White
}
catch {
Write-Host "WARNING: Named location exists but cannot be retrieved. Recreating..." -BackgroundColor DarkYellow -ForegroundColor Black
$existingNamedLocation = $null
}
}
if ($null -eq $existingNamedLocation) {
# Get WAN IP from User only when the trusted location must be created
$ipRanges = Read-Host "Enter Trusted Location IP Address in CIDR format."
# Define the named location
$namedLocationParams = @{
"@odata.type" = "#microsoft.graph.ipNamedLocation"
displayName = "Trusted"
isTrusted = $true
ipRanges = @(
@{
"@odata.type" = "#microsoft.graph.iPv4CidrRange"
cidrAddress = $ipRanges
}
)
}
# Create the named location
try {
New-MgIdentityConditionalAccessNamedLocation -BodyParameter $namedLocationParams -ErrorAction Stop | Out-Null
Write-Host "Named location 'Trusted' created successfully." -BackgroundColor DarkGreen -ForegroundColor White
}
catch {
Write-Host "ERROR: Failed to create named location: $($_.Exception.Message)" -BackgroundColor DarkRed -ForegroundColor White
}
}
# Check if Conditional Access Policy already exists
$existingPolicies = $ExistingCAPolicies |
Where-Object { $_.DisplayName -eq "MFA for All" }
if ($null -ne $existingPolicies) {
Write-Host "Conditional Access Policy 'MFA for All' already exists. Skipping creation." -BackgroundColor DarkBlue -ForegroundColor White
}
else {
# Define Conditional Access Policy - excludes ALL trusted named locations
$conditionalAccessPolicy = @{
displayName = "MFA for All"
state = "enabled"
conditions = @{
users = @{
includeUsers = @("all")
}
applications = @{
includeApplications = @("all")
}
locations = @{
includeLocations = @("All")
excludeLocations = @("AllTrusted")
}
}
grantControls = @{
operator = "OR"
builtInControls = @("mfa", "domainJoinedDevice")
}
}
try {
$createdMfaPolicy = New-MgIdentityConditionalAccessPolicy -BodyParameter $conditionalAccessPolicy -ErrorAction Stop
if (Wait-ConditionalAccessPolicyAvailable -PolicyId $createdMfaPolicy.Id -DisplayName "MFA for All") {
Write-Host "Conditional Access Policy 'MFA for All' created successfully." -BackgroundColor DarkGreen -ForegroundColor White
}
else {
Write-Host "WARNING: Graph accepted 'MFA for All', but it is not visible in policy lookups yet." -BackgroundColor DarkYellow -ForegroundColor Black
}
Refresh-ConditionalAccessPolicyCache
}
catch {
Write-Host "ERROR: Failed to create Conditional Access Policy: $($_.Exception.Message)" -BackgroundColor DarkRed -ForegroundColor White
}
}
#!################################################################################################################################
#!################################################################################################################################
##!##### BLOCK OUTSIDE USA CAP — AUTO MODE ########################################################################################
###!##############################################################################################################################
####!#############################################################################################################################
Write-Host "AUTO: Checking Block Outside USA policy…" -BackgroundColor DarkBlue -ForegroundColor White
# ======================================================================
# Variables
# ======================================================================
$locationName = "Outside USA"
$policyName = "Block Outside USA"
# List of all country codes except the United States
$countryCodes = @(
"AD","AE","AF","AG","AI","AL","AM","AO","AQ","AR","AS","AT","AU","AW","AX","AZ","BA","BB","BD","BE","BF","BG","BH","BI","BJ","BL","BM","BN","BO","BQ","BR","BS","BT",
"BV","BW","BY","BZ","CA","CC","CD","CF","CG","CH","CI","CK","CL","CM","CN","CO","CR","CU","CV","CW","CX","CY","CZ","DE","DJ","DK","DM","DO","DZ","EC","EE","EG","EH",
"ER","ES","ET","FI","FJ","FK","FM","FO","FR","GA","GB","GD","GE","GF","GG","GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT","GU","GW","GY","HK","HM","HN","HR","HT",
"HU","ID","IE","IL","IM","IN","IO","IQ","IR","IS","IT","JE","JM","JO","JP","KE","KG","KH","KI","KM","KN","KP","KR","KW","KY","KZ","LA","LB","LC","LI","LK","LR","LS",
"LT","LU","LV","LY","MA","MC","MD","ME","MF","MG","MH","MK","ML","MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV","MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI",
"NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF","PG","PH","PK","PL","PM","PN","PR","PS","PT","PW","PY","QA","RE","RO","RS","RU","RW","SA","SB","SC","SD","SE","SG",
"SH","SI","SJ","SK","SL","SM","SN","SO","SR","SS","ST","SV","SX","SY","SZ","TC","TD","TF","TG","TH","TJ","TK","TL","TM","TN","TO","TR","TT","TV","TW","TZ","UA","UG",
"UM","UY","UZ","VA","VC","VE","VG","VI","VN","VU","WF","WS","YE","YT","ZA","ZM","ZW"
)
# ======================================================================
# Named Location — Check / Create
# ======================================================================
$existingLocation = Get-MgIdentityConditionalAccessNamedLocation -All |
Where-Object { $_.DisplayName -eq $locationName } |
Select-Object -First 1
if (-not $existingLocation) {
Write-Host "Creating named location '$locationName'…" -BackgroundColor DarkBlue -ForegroundColor White
$params = @{
"@odata.type" = "#microsoft.graph.countryNamedLocation"
DisplayName = $locationName
CountriesAndRegions = $countryCodes
IncludeUnknownCountriesAndRegions = $false
}
try {
$existingLocation = New-MgIdentityConditionalAccessNamedLocation -BodyParameter $params -ErrorAction Stop
Write-Host "Named location created with ID: $($existingLocation.Id)" -BackgroundColor DarkGreen -ForegroundColor White
}
catch {
Write-Host "ERROR: Failed to create named location '$locationName': $($_.Exception.Message)" -BackgroundColor DarkRed -ForegroundColor White
$existingLocation = $null
}
}
else {
Write-Host "Named location '$locationName' already exists with ID: $($existingLocation.Id)" -BackgroundColor DarkGreen -ForegroundColor White
}
# ======================================================================
# Conditional Access Policy — Check / Create
# ======================================================================
$existingPolicy = $ExistingCAPolicies |
Where-Object { $_.DisplayName -eq $policyName }
if (-not $existingPolicy) {
# Verify we have a valid ID
if ([string]::IsNullOrWhiteSpace($existingLocation.Id)) {
Write-Host "ERROR: Named Location ID is empty. Cannot create policy." -BackgroundColor DarkRed -ForegroundColor White
}
elseif (-not (Wait-NamedLocationAvailable -NamedLocationId $existingLocation.Id)) {
Write-Host "ERROR: Named location '$locationName' was created, but Graph is not returning it yet. Skipping '$policyName' for this run." -BackgroundColor DarkRed -ForegroundColor White
}
else {
Write-Host "Creating Conditional Access policy '$policyName'…" -BackgroundColor DarkBlue -ForegroundColor White
$adminRolesIds = $GlobalAdminRoleIds
$policy = @{
displayName = $policyName
state = "enabled"
conditions = @{
users = @{
includeUsers = @("All")
excludeRoles = $adminRolesIds
}
locations = @{
includeLocations = @($existingLocation.Id)
}
clientAppTypes = @("All")
applications = @{
includeApplications = @("All")
}
}
grantControls = @{
operator = "OR"
builtInControls = @("block")
}
}
$policyCreated = $false
$maxPolicyCreateAttempts = 18
$policyCreateDelaySeconds = 10
for ($attempt = 1; $attempt -le $maxPolicyCreateAttempts; $attempt++) {
try {
$createdOutsidePolicy = New-MgIdentityConditionalAccessPolicy -BodyParameter $policy -ErrorAction Stop
if (Wait-ConditionalAccessPolicyAvailable -PolicyId $createdOutsidePolicy.Id -DisplayName $policyName) {
Write-Host "Conditional Access policy '$policyName' created successfully." -BackgroundColor DarkGreen -ForegroundColor White
}
else {
Write-Host "WARNING: Graph accepted '$policyName', but it is not visible in policy lookups yet." -BackgroundColor DarkYellow -ForegroundColor Black
}
Refresh-ConditionalAccessPolicyCache
$policyCreated = $true
break
}
catch {
$message = $_.Exception.Message
$namedLocationNotReady = $message -match "1040|NamedLocation.*does not exist"
if ($namedLocationNotReady -and $attempt -lt $maxPolicyCreateAttempts) {
Write-Host ("Named location '$locationName' is not ready for policy validation yet. Retrying in {0} seconds ({1}/{2})..." -f $policyCreateDelaySeconds, $attempt, $maxPolicyCreateAttempts) -BackgroundColor DarkYellow -ForegroundColor Black
Start-Sleep -Seconds $policyCreateDelaySeconds
continue
}
Write-Host "ERROR: Failed to create Conditional Access policy '$policyName': $message" -BackgroundColor DarkRed -ForegroundColor White
break
}
}
if (-not $policyCreated) {
Write-Host "Conditional Access policy '$policyName' was not created in this run." -BackgroundColor DarkRed -ForegroundColor White
}
}
}
else {
Write-Host "Conditional Access policy '$policyName' already exists. Skipping." -BackgroundColor DarkGreen -ForegroundColor White
}
Write-Host "Block Outside USA — Completed" -BackgroundColor DarkBlue -ForegroundColor White
#######!##########################################################################################################################
###########!######################################################################################################################
####!### BLOCK MACOS CAP — AUTO MODE ############################################################################################
#####!############################################################################################################################
######!###########################################################################################################################
Write-Host "AUTO: Checking Block MacOS Sign-Ins policy…" -BackgroundColor DarkBlue -ForegroundColor White
$policyName = "Block MAC OS"
# ======================================================================
# Check for existing policy
# ======================================================================
$existingPolicy = $ExistingCAPolicies |
Where-Object { $_.DisplayName -eq $policyName }
if ($existingPolicy) {
Write-Host "Conditional Access policy '$policyName' already exists. Skipping." -BackgroundColor DarkGreen -ForegroundColor White
}
else {
# ======================================================================
# Create the policy (only if missing)
# ======================================================================
Write-Host "Creating Conditional Access policy '$policyName'…" -BackgroundColor DarkBlue -ForegroundColor White
$adminRolesIds = $GlobalAdminRoleIds
$conditions = @{
users = @{
includeUsers = @("All")
excludeRoles = $adminRolesIds
}
clientAppTypes = @("All")
platforms = @{
includePlatforms = @("macOS")
}
applications = @{
includeApplications = @("All")
}
locations = @{
includeLocations = @("All")
excludeLocations = @("AllTrusted")
}
}
$grantControls = @{
operator = "OR"
builtInControls = @("block")
}
$policy = @{
displayName = $policyName
state = "enabled"
conditions = $conditions
grantControls = $grantControls
}
try {
New-MgIdentityConditionalAccessPolicy -BodyParameter $policy -ErrorAction Stop | Out-Null
Write-Host "Conditional Access policy '$policyName' created successfully." -BackgroundColor DarkGreen -ForegroundColor White
Refresh-ConditionalAccessPolicyCache
}
catch {
Write-Host "ERROR: Failed to create Conditional Access policy '$policyName': $($_.Exception.Message)" -BackgroundColor DarkRed -ForegroundColor White
}
}
#!################################################################################################################################
#!################################################################################################################################
#!###### BLOCK LINUX OS CAP — AUTO MODE ##########################################################################################
#!################################################################################################################################
#!################################################################################################################################
Write-Host "AUTO: Checking Block Linux Sign-Ins policy…" -BackgroundColor DarkBlue -ForegroundColor White
$policyName = "Block Linux OS"
# ======================================================================
# Check for existing policy
# ======================================================================
$existingPolicy = $ExistingCAPolicies |
Where-Object { $_.DisplayName -eq $policyName }
if ($existingPolicy) {
Write-Host "Conditional Access policy '$policyName' already exists. Skipping." -BackgroundColor DarkGreen -ForegroundColor White
}
else {
# ======================================================================
# Create the policy (only if missing)
# ======================================================================
Write-Host "Creating Conditional Access policy '$policyName'…" -BackgroundColor DarkBlue -ForegroundColor White
$adminRolesIds = $GlobalAdminRoleIds
$params = @{
displayName = $policyName
state = "enabled"
conditions = @{
applications = @{
includeApplications = @("All")
}
users = @{
includeUsers = @("All")
excludeRoles = $adminRolesIds
}
platforms = @{
includePlatforms = @("Linux")
}
locations = @{
includeLocations = @("All")
excludeLocations = @("AllTrusted")
}
}
grantControls = @{
operator = "OR"
builtInControls = @("block")
}
}
try {
New-MgIdentityConditionalAccessPolicy -BodyParameter $params -ErrorAction Stop | Out-Null
Write-Host "Conditional Access policy '$policyName' created successfully." -BackgroundColor DarkGreen -ForegroundColor White
Refresh-ConditionalAccessPolicyCache
}
catch {
Write-Host "ERROR: Failed to create Conditional Access policy '$policyName': $($_.Exception.Message)" -BackgroundColor DarkRed -ForegroundColor White
}
}
#!################################################################################################################################
#!################################################################################################################################
#!######BLOCK LEGACY AUTH CAP######################################################################################################
#!################################################################################################################################
#!################################################################################################################################
Write-Host "Auto- Block Legacy Authentication" -BackgroundColor DarkBlue -ForegroundColor White
$policyName = "Block Legacy Auth"
# Define the Conditional Access policy body
$params = @{
displayName = $policyName
state = "enabled"
conditions = @{
users = @{
includeUsers = @("all")
}
clientAppTypes = @("ExchangeActiveSync","Other")
applications = @{
includeApplications = @("all")
}
locations = @{
includeLocations = @("All")
excludeLocations = @("AllTrusted")
}
}
grantControls = @{
operator = "OR"
builtInControls = @("block")
}
}
# Check if the policy already exists
$existingPolicy = $ExistingCAPolicies | Where-Object { $_.DisplayName -eq $policyName }
if ($null -ne $existingPolicy) {
Write-Host "Conditional Access Policy '$policyName' already exists. Skipping creation." -BackgroundColor DarkBlue -ForegroundColor White
} else {
try {
New-MgIdentityConditionalAccessPolicy -BodyParameter $params -ErrorAction Stop | Out-Null
Write-Host "Conditional Access Policy '$policyName' created successfully." -BackgroundColor DarkBlue -ForegroundColor White
Refresh-ConditionalAccessPolicyCache
}
catch {
Write-Host "ERROR: Failed to create Conditional Access Policy '$policyName': $($_.Exception.Message)" -BackgroundColor DarkRed -ForegroundColor White
}
}
##########!##################################################################
#########!###################################################################
########!#######Get Bad IPs and Split Due to AZ Limits#######################
############!################################################################
#############!###############################################################
Write-Host "POLICY - BLOCK BAD IPs" -BackgroundColor DarkBlue -ForegroundColor White
$existingBadRepPolicy = $ExistingCAPolicies |
Where-Object { $_.DisplayName -eq "BadRep IP Block" }
if ($null -ne $existingBadRepPolicy) {
Write-Host "Conditional Access Policy 'BadRep IP Block' already exists - skipping BadRep location and policy creation." -BackgroundColor DarkBlue -ForegroundColor White
}
else {
Write-Host "Conditional Access Policy 'BadRep IP Block' not found. Continuing..." -BackgroundColor DarkGreen -ForegroundColor White
Write-Host "Getting Bad IP Lists (VPN + TOR)" -BackgroundColor DarkBlue -ForegroundColor White
# -----------------------------------------------------------------------------
# 1. Download and prepare source lists
# -----------------------------------------------------------------------------
$outputDir = "C:\temp"
if (-not (Test-Path -Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir | Out-Null
}
# VPN list from X4BNet
$fileUrlVpn = "https://github.com/X4BNet/lists_vpn/raw/main/output/vpn/ipv4.txt"
$tempVpn = Join-Path $outputDir "vpn_ipv4.txt"
Invoke-WebRequest -Uri $fileUrlVpn -OutFile $tempVpn -UseBasicParsing
# TOR exit nodes
$fileUrlTor = "https://raw.githubusercontent.com/SecOps-Institute/Tor-IP-Addresses/master/tor-exit-nodes.lst"
$tempTor = Join-Path $outputDir "tor_exit_nodes.txt"
Invoke-WebRequest -Uri $fileUrlTor -OutFile $tempTor -UseBasicParsing
# Append /32 to TOR entries (normalize format)
(Get-Content $tempTor | ForEach-Object { "$_/32" }) | Set-Content $tempTor -Encoding ASCII
# -----------------------------------------------------------------------------
# 2. Helper: chunked named-location creation
# -----------------------------------------------------------------------------
function New-ChunkedNamedLocation {
param(
[string]$DisplayName,
[string]$FilePath,
[int]$ChunkSize = 800
)
Write-Host "Processing $DisplayName from $FilePath ..." -BackgroundColor DarkBlue -ForegroundColor White
if (-not (Test-Path $FilePath)) {
Write-Host "File not found: $FilePath" -BackgroundColor DarkRed -ForegroundColor White
return @()
}
$allIPs = Get-Content -Path $FilePath | Where-Object { $_ -match '\d+\.\d+\.\d+\.\d+' }
if ($allIPs.Count -eq 0) {
Write-Host "No valid IPs found in $FilePath." -BackgroundColor DarkYellow -ForegroundColor Black
return @()
}
$chunks = foreach ($i in 0..([Math]::Floor(($allIPs.Count - 1) / $ChunkSize))) {
$start = $i * $ChunkSize
$end = [Math]::Min($start + $ChunkSize - 1, $allIPs.Count - 1)
,($allIPs[$start..$end])
}
$createdIds = @()
$part = 1
foreach ($chunk in $chunks) {
$chunkName = "$DisplayName-Part$part"
$existing = Get-MgIdentityConditionalAccessNamedLocation -All |
Where-Object { $_.DisplayName -eq $chunkName } |
Select-Object -First 1
if ($null -ne $existing) {
# Verify the location is still valid
try {
Get-MgIdentityConditionalAccessNamedLocation -NamedLocationId $existing.Id -ErrorAction Stop | Out-Null
Write-Host ("Named location '{0}' already exists and is valid - skipping." -f $chunkName) -BackgroundColor DarkBlue -ForegroundColor White
$createdIds += $existing.Id
$part++
continue
}
catch {
Write-Host ("WARNING: Named location '{0}' exists but is invalid. Recreating..." -f $chunkName) -BackgroundColor DarkYellow -ForegroundColor Black
$existing = $null
}
}
if ($null -eq $existing) {
$ipArray = foreach ($ip in $chunk) {
@{
"@odata.type" = "#microsoft.graph.iPv4CidrRange"
CidrAddress = $ip
}
}
$params = @{
"@odata.type" = "#microsoft.graph.ipNamedLocation"
DisplayName = $chunkName
IsTrusted = $false
IpRanges = $ipArray
}
try {
$created = New-MgIdentityConditionalAccessNamedLocation -BodyParameter $params -ErrorAction Stop
$countText = "$($chunk.Count) IPs"
Write-Host ("Created named location '{0}' ({1}) with ID: {2}" -f $chunkName, $countText, $created.Id) -BackgroundColor DarkGreen -ForegroundColor White
# Verify the ID is not empty
if (-not [string]::IsNullOrWhiteSpace($created.Id)) {
if (Wait-NamedLocationAvailable -NamedLocationId $created.Id) {
$createdIds += $created.Id
}
else {
Write-Host ("WARNING: Created location '{0}', but Graph is not returning it yet. It will be excluded from this run." -f $chunkName) -BackgroundColor DarkYellow -ForegroundColor Black
}
}
else {
Write-Host ("WARNING: Created location '{0}' but ID is empty!" -f $chunkName) -BackgroundColor DarkYellow -ForegroundColor Black
}
}
catch {
Write-Host ("Failed to create '{0}': {1}" -f $chunkName, $_.Exception.Message) -BackgroundColor DarkRed -ForegroundColor White
}
}
$part++
}
return $createdIds
}
# -----------------------------------------------------------------------------
# 3. Create named locations for each list
# -----------------------------------------------------------------------------
$BadRep1_Locations = New-ChunkedNamedLocation -DisplayName "BadRep1" -FilePath $tempVpn
$BadRep3_Locations = New-ChunkedNamedLocation -DisplayName "BadRep3" -FilePath $tempTor
# Clean up temporary files
Remove-Item -Path $tempVpn, $tempTor -Force -ErrorAction SilentlyContinue
# -----------------------------------------------------------------------------
# 4. Create the Conditional Access Policy
# -----------------------------------------------------------------------------
Write-Host "Creating BadRep IP Block Conditional Access Policy..." -BackgroundColor DarkBlue -ForegroundColor White
# Combine all IDs and filter out any empty/null values
$AllBadRepIDs = @()
$AllBadRepIDs += $BadRep1_Locations | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
$AllBadRepIDs += $BadRep3_Locations | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
Write-Host "Total valid named location IDs collected: $($AllBadRepIDs.Count)" -BackgroundColor DarkBlue -ForegroundColor White
if ($AllBadRepIDs.Count -eq 0) {
Write-Host "No BadRep named locations found - skipping policy creation." -BackgroundColor DarkYellow -ForegroundColor Black
}
else {
# Verify all collected IDs are still valid before creating policy
Write-Host "Verifying all named location IDs are valid..." -BackgroundColor DarkBlue -ForegroundColor White
$validIDs = @()
foreach ($id in $AllBadRepIDs) {
if (Wait-NamedLocationAvailable -NamedLocationId $id) {
$validIDs += $id
}
else {
Write-Host "WARNING: Named location ID $id is invalid and will be excluded from policy." -BackgroundColor DarkYellow -ForegroundColor Black
}
}
Write-Host "Verified $($validIDs.Count) valid named location IDs." -BackgroundColor DarkGreen -ForegroundColor White
if ($validIDs.Count -eq 0) {
Write-Host "No valid named location IDs found - skipping policy creation." -BackgroundColor DarkYellow -ForegroundColor Black
}
else {
$repblockparams = @{
displayName = "BadRep IP Block"
state = "enabled"
conditions = @{
applications = @{
includeApplications = @("All")
}
users = @{
includeUsers = @("all")
excludeRoles = $GlobalAdminRoleIds
}
locations = @{
includeLocations = $validIDs
}
}
grantControls = @{
operator = "OR"
builtInControls = @("block")
}
}
try {
New-MgIdentityConditionalAccessPolicy -BodyParameter $repblockparams -ErrorAction Stop | Out-Null
Write-Host "Conditional Access Policy 'BadRep IP Block' created successfully." -BackgroundColor DarkGreen -ForegroundColor White
Refresh-ConditionalAccessPolicyCache
}
catch {
Write-Host "Failed to create 'BadRep IP Block' policy: $($_.Exception.Message)" -BackgroundColor DarkRed -ForegroundColor White
}
}
}
}
##############################!###################################################################################################
##############################!###################################################################################################
###########################!#########Create Sign-in Risk Block CAP################################################################
############################!#####################################################################################################
##############################!###################################################################################################
Write-Host "Creating Sign-In Risk Block Policy" -BackgroundColor DarkBlue -ForegroundColor White
# Get Global Administrator role ID
$AdminRolesIds = $GlobalAdminRoleIds
# Create the Conditional Access Policy
$conditions = @{
Users = @{
IncludeUsers = @("all")
ExcludeRoles = $AdminRolesIds
}
Applications = @{
IncludeApplications = @("all")
}
Locations = @{
IncludeLocations = @("All")
ExcludeLocations = @("AllTrusted")
}
SignInRiskLevels = @("medium", "high")
}
$controls = @{
Operator = "OR"
BuiltInControls = @("block")
}
$policyName = "Sign-In Risk Block"
$policyExists = $ExistingCAPolicies | Where-Object { $_.displayName -eq $policyName }
if ($null -ne $policyExists) {
Write-Host "Conditional Access Policy 'Sign-In Risk Block' already exists. Skipping creation." -BackgroundColor DarkBlue -ForegroundColor White
}
else {
try {
New-MgIdentityConditionalAccessPolicy -DisplayName $policyName -State "Enabled" -Conditions $conditions -GrantControls $controls -ErrorAction Stop | Out-Null
Write-Host "Conditional Access Policy 'Sign-In Risk Block' created successfully." -BackgroundColor DarkBlue -ForegroundColor White
Refresh-ConditionalAccessPolicyCache
}
catch {
Write-Host "ERROR: Failed to create Conditional Access Policy 'Sign-In Risk Block': $($_.Exception.Message)" -BackgroundColor DarkRed -ForegroundColor White
}
}
#############################!#########################################################
########################!##############################################################
###!###########Show Results#!###########################################################
############################!##########################################################
##############################!########################################################
$caps = Get-MgIdentityConditionalAccessPolicy -All | Select DisplayName, CreatedDateTime, State
Write-Host "The Following Conditional Access Policies Were Created or Already Exist:" -BackgroundColor DarkBlue -ForegroundColor White
$caps | Format-Table -AutoSize | Out-Host
Write-Host "Now disconnecting from AzureAD/Graph" -BackgroundColor DarkBlue -ForegroundColor White
#################################!#####################################################
################################!######################################################
##########!######Disconnect###########################################################
##################################!####################################################
###################################!###################################################
Disconnect-MgGraph | Out-Null