-
Notifications
You must be signed in to change notification settings - Fork 214
/
MFA.ps1
949 lines (803 loc) · 31.8 KB
/
MFA.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
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
## MFA functions utilizing provisioning API
# Mar 3rd 2020
function Set-UserMFA
{
<#
.SYNOPSIS
Sets user's MFA settings
.DESCRIPTION
Sets user's MFA settings using Provisioning API
.Parameter AccessToken
Access Token of the user accessing Azure Active Directory to find the given user to get the SID
.Parameter UserPrincipalName
User's principal name.
.Parameter State
State of user's MFA: Disabled, Enabled, or Enforced.
.Parameter StartTime
Remembers devices issued after the given time. Note! Applied only if State equals Enabled or Enfoced.
.Parameter PhoneNumber
User's phone number used for MFA. Must in the following format "+CCC NNNNN" where CCC is country code and NNNN the phone number without leading zero.
.Parameter AlternativePhoneNumber
User's alternative phone number used for MFA. Must in the following format "+CCC NNNNN" where CCC is country code and NNNN the phone number without leading zero.
.Parameter Email
User's phone number used for MFA. Should be correct email address.
.Parameter DefaultMethod
User's default MFA method: PhoneAppNotification, PhoneAppOTP, or OneWaySMS. TwoWayVoiceOffice and TwoWayVoiceMobile won't work in TRIAL tenants.
In audit log: PhoneAppNotification=0, PhoneAppOTP=6, OneWaySMS=7, TwoWayVoiceOffice=5, TwoWayVoiceMobile=2
.Example
PS C:\>$at=Get-AADIntAccessTokenForAADGraph
PS C:\>Set-AADIntUserMFA -AccessToken $at -UserPrincipalName user@company.com -PhoneNumber "+1 123456789" -DefaultMethod PhoneAppNotification
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
$UserPrincipalName,
[Parameter(Mandatory=$False)]
[ValidateSet('Disabled','Enabled','Enforced')]
$State,
[Parameter(Mandatory=$False)]
[ValidateSet('PhoneAppOTP','PhoneAppNotification','OneWaySMS','TwoWayVoiceOffice','TwoWayVoiceMobile')]
$DefaultMethod,
[Parameter(Mandatory=$False)]
[DateTime]$StartTime=(Get-Date),
[Parameter(Mandatory=$False)]
[String]$PhoneNumber,
[Parameter(Mandatory=$False)]
[String]$AlternativePhoneNumber,
[Parameter(Mandatory=$False)]
[String]$Email
)
Process
{
# Validation for phone numbers
function valPho
{
Param([String]$PhoneNumber)
if(![String]::IsNullOrEmpty($PhoneNumber))
{
$regex="^[+]\d{1,3} [1-9]\d{1,11}$" # 1-3 digits (country code), space, non-zero digit, and 1 to 11 digits.
return [regex]::Match($PhoneNumber,$regex).success
}
else
{
return $true
}
}
# Check the phone numbers
if(!((valPho $PhoneNumber) -and (valPho $AlternativePhoneNumber)))
{
Write-Error 'Invalid phone number format! Use the following format: "+CCC NNNNNNN" where CCC is the country code and NNNN the phonenumber without the leading zero.'
return
}
$command="SetUser"
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -ClientID "1b730954-1685-4b74-9bfd-dac224a7b894" -Resource "https://graph.windows.net"
# Get user name from access token if empty
if([string]::IsNullOrEmpty($UserPrincipalName))
{
$UserPrincipalName = (Read-Accesstoken -AccessToken $AccessToken).unique_name
}
# Convert time to text
$startText = $StartTime.ToUniversalTime().toString("yyyy-MM-ddTHH:mm:ss+00:00").Replace(".",":")
# Set StrongAuthenticationRequirements
$StrongAuthenticationRequirements="<c:StrongAuthenticationRequirements/>"
if([string]::IsNullOrEmpty($State))
{
$StrongAuthenticationRequirements='<c:StrongAuthenticationRequirements i:nil="true"/>'
}
elseif($State -ne "Disabled")
{
$StrongAuthenticationRequirements=@"
<c:StrongAuthenticationRequirements>
<c:StrongAuthenticationRequirement>
$(Add-CElement -Parameter "RelyingParty" -Value "*")
$(Add-CElement -Parameter "RememberDevicesNotIssuedBefore" -Value "$startText")
$(Add-CElement -Parameter "State" -Value "$State")
</c:StrongAuthenticationRequirement>
</c:StrongAuthenticationRequirements>
"@
}
# Set the default method
$StrongAuthenticationMethods='<c:StrongAuthenticationMethods i:nil="true"/>'
if(![String]::IsNullOrEmpty($DefaultMethod))
{
$StrongAuthenticationMethods=@"
<c:StrongAuthenticationMethods>
<c:StrongAuthenticationMethod>
<c:IsDefault>$($DefaultMethod.Equals("PhoneAppOTP").ToString().ToLower())</c:IsDefault>
<c:MethodType>PhoneAppOTP</c:MethodType>
</c:StrongAuthenticationMethod>
<c:StrongAuthenticationMethod>
<c:IsDefault>$($DefaultMethod.Equals("PhoneAppNotification").ToString().ToLower())</c:IsDefault>
<c:MethodType>PhoneAppNotification</c:MethodType>
</c:StrongAuthenticationMethod>
<c:StrongAuthenticationMethod>
<c:IsDefault>$($DefaultMethod.Equals("OneWaySMS").ToString().ToLower())</c:IsDefault>
<c:MethodType>OneWaySMS</c:MethodType>
</c:StrongAuthenticationMethod>
<c:StrongAuthenticationMethod>
<c:IsDefault>$($DefaultMethod.Equals("TwoWayVoiceOffice").ToString().ToLower())</c:IsDefault>
<c:MethodType>TwoWayVoiceOffice</c:MethodType>
</c:StrongAuthenticationMethod>
<c:StrongAuthenticationMethod>
<c:IsDefault>$($DefaultMethod.Equals("TwoWayVoiceMobile").ToString().ToLower())</c:IsDefault>
<c:MethodType>TwoWayVoiceMobile</c:MethodType>
</c:StrongAuthenticationMethod>
</c:StrongAuthenticationMethods>
"@
}
# Create the body for setting MFA
$request_elements=@"
<b:User xmlns:c="http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration">
$StrongAuthenticationMethods
<c:StrongAuthenticationPhoneAppDetails i:nil="true"/>
<c:StrongAuthenticationProofupTime i:nil="true"/>
$StrongAuthenticationRequirements
<c:StrongAuthenticationUserDetails>
$(Add-CElement -Parameter "AlternativePhoneNumber" -Value "$AlternativePhoneNumber")
$(Add-CElement -Parameter "Email" -Value "$Email")
<c:OldPin i:nil="true"/>
$(Add-CElement -Parameter "PhoneNumber" -Value "$PhoneNumber")
<c:Pin i:nil="true"/>
</c:StrongAuthenticationUserDetails>
$(Add-CElement -Parameter "UserPrincipalName" -Value "$UserPrincipalName")
</b:User>
"@
# Create the envelope and call the API
$response=Call-ProvisioningAPI(Create-Envelope $AccessToken $command $request_elements)
# Get the results
$results = Parse-SOAPResponse($Response)
# Return
$results
}
}
# Sets user's MFA app details
# Jun 29th 2020
function Set-UserMFAApps
{
<#
.SYNOPSIS
Sets user's MFA Apps settings
.DESCRIPTION
Sets user's MFA Apps settings using Azure AD Graph
.Parameter AccessToken
Access Token of the user accessing Azure Active Directory to find the given user to get the SID
.Parameter UserPrincipalName
User's principal name.
.Parameter Id
Id of the device.
.Parameter AuthenticationType
Comma separated list of authentication types of the device. For example, "Notification, OTP" or just "OTP".
In audit log: OTP=1, Notification=2.
.Parameter DeviceName
Name of the device
.Parameter DeviceTag
Tag. Usually "SoftwareTokenActivated".
.Parameter DeviceToken
Device token of MFA Authenticator App.
.Parameter NotificationType
Notification type of the app. Can be GCM (notification through app) or Invalid (just OTP).
In audit log: OTP=1, GCM=4
.Parameter OathTokenTimeDrift
Time drift of Oath token in seconds. Should be 0 or close to it.
.Parameter OathSecretKey
Secret key for calculating OTPs.
.Parameter PhoneAppVersion
Version of the app.
.Parameter TimeInterval
Time interval.
.Example
PS C:\>$at=Get-AADIntAccessTokenForAADGraph
PS C:\>Get-AADIntUserMFAApps -AccessToken $at -UserPrincipalName user@company.com
AuthenticationType : Notification, OTP
DeviceName : SM-R2D2
DeviceTag : SoftwareTokenActivated
DeviceToken : APA91...
Id : 454b8d53-d97e-4ead-a69c-724166394334
NotificationType : GCM
OathTokenTimeDrift : 0
OathSecretKey :
PhoneAppVersion : 6.2001.0140
TimeInterval :
AuthenticationType : OTP
DeviceName : NO_DEVICE
DeviceTag : SoftwareTokenActivated
DeviceToken : NO_DEVICE_TOKEN
Id : aba89d77-0a69-43fa-9e5d-6f41c7b9bb16
NotificationType : Invalid
OathTokenTimeDrift : 0
OathSecretKey :
PhoneAppVersion : NO_PHONE_APP_VERSION
TimeInterval :
PS C:\>Set-AADIntUserMFAApps -AccessToken $at -Id 454b8d53-d97e-4ead-a69c-724166394334 -DeviceName "SM-3CPO"
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$UserPrincipalName,
[Parameter(Mandatory=$True)]
[guid]$Id,
[Parameter(Mandatory=$False)]
[String]$AuthenticationType,
[Parameter(Mandatory=$False)]
[String]$DeviceName,
[Parameter(Mandatory=$False)]
[String]$DeviceTag,
[Parameter(Mandatory=$False)]
[String]$DeviceToken,
[Parameter(Mandatory=$False)]
[ValidateSet('Invalid','GCM')]
[String]$NotificationType,
[Parameter(Mandatory=$False)]
[int]$OathTokenTimeDrift,
[Parameter(Mandatory=$False)]
[String]$OathSecretKey,
[Parameter(Mandatory=$False)]
[String]$PhoneAppVersion,
[Parameter(Mandatory=$False)]
[String]$TimeInterval
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -ClientID "1b730954-1685-4b74-9bfd-dac224a7b894" -Resource "https://graph.windows.net"
# Get user name from access token if empty
if([string]::IsNullOrEmpty($UserPrincipalName))
{
$UserPrincipalName = (Read-Accesstoken -AccessToken $AccessToken).unique_name
}
# Get user's current configuration and get the app details
$MFAApps = Get-UserMFAApps -UserPrincipalName $UserPrincipalName -AccessToken $AccessToken
# If only one element, add it to array
if(!$MFAApps.Count -gt 0)
{
$MFAApp = $MFAApps
Remove-Variable MFAApps
$MFAApps = @($MFAApp)
}
$found = $false
$pos=0
foreach($app in $MFAApps)
{
if($app.id -eq ($id.ToString()))
{
$found = $true
break
}
$pos++
}
if(!$found)
{
Throw "Authentication app $id not found from user $UserPrincipalName"
}
# Apply the new information
if($AuthenticationType)
{
$MFAApps[$pos].AuthenticationType=$AuthenticationType
}
if($DeviceName)
{
$MFAApps[$pos].DeviceName=$DeviceName
}
if($DeviceTag)
{
$MFAApps[$pos].DeviceTag=$DeviceTag
}
if($DeviceToken)
{
$MFAApps[$pos].DeviceToken=$DeviceToken
}
if($NotificationType)
{
$MFAApps[$pos].NotificationType=$NotificationType
}
if($OathTokenTimeDrift -ne $MFAApps[$pos].OathTokenTimeDrift)
{
$MFAApps[$pos].OathTokenTimeDrift=$OathTokenTimeDrift
}
if($OathSecretKey)
{
$MFAApps[$pos].OathSecretKey=$OathSecretKey
}
if($PhoneAppVersion)
{
$MFAApps[$pos].PhoneAppVersion=$PhoneAppVersion
}
if($TimeInterval)
{
$MFAApps[$pos].TimeInterval=$TimeInterval
}
# Create the body
$body = '{ "strongAuthenticationDetail": {"phoneAppDetails": ['
# We need to reverse so that it doesn't look weird in audit log.
for($a=$MFAApps.count-1; $a -ge 0; $a--)
{
$app=$MFAApps[$a]
$body+="{"
$body += """authenticationType"": ""$($app.AuthenticationType)"","
$body += """deviceName"": ""$($app.DeviceName)"","
$body += """deviceTag"": ""$($app.DeviceTag)"","
$body += """deviceToken"": ""$($app.DeviceToken)"","
$body += """id"": ""$($app.Id)"","
$body += """notificationType"": ""$($app.NotificationType)"","
$body += """oathTokenTimeDrift"": $($app.OathTokenTimeDrift),"
if([string]::IsNullOrEmpty($app.OathSecretKey))
{
$body += """oathSecretKey"": null,"
}
else
{
$body += """oathSecretKey"": ""$($app.oathSecretKey)"","
}
$body += """phoneAppVersion"": ""$($app.PhoneAppVersion)"","
$body += """timeInterval"": $(if([string]::IsNullOrEmpty($app.TimeInterval)){'null'}else{$app.TimeInterval})"
$body += "},"
}
# Strip the last comma
$body=$body.Substring(0,$body.Length-1)
$body += "]}}";
# Set the user agent
$headers=@{
"User-Agent" = ""
}
try
{
# Set app details
$results=Call-GraphAPI -AccessToken $AccessToken -Command "users/$UserPrincipalName" -Method PATCH -Body $body -Headers $headers
}
catch
{
# Get the error
$err = $_.ErrorDetails.Message | ConvertFrom-Json
# Insufficient privileges etc.
if($err.'odata.error'.message.value)
{
Write-Error $err.'odata.error'.message.value
}
else # Other errors
{
$property = $err.'odata.error'.values[0].value
$error = $err.'odata.error'.values[1].value
Write-Error "$($property): $error"
}
}
}
}
# Mar 3rd 2020
# Deprecated old version
function Get-UserMFA2
{
<#
.SYNOPSIS
Gets user's MFA settings
.DESCRIPTION
Gets user's MFA settings using Provisioning API
.Parameter AccessToken
Access Token of the user accessing Azure Active Directory to find the given user to get the SID
.Parameter UserPrincipalName
User's principal name.
.Example
PS C:\>$at=Get-AADIntAccessTokenForAADGraph
PS C:\>Get-AADIntUserMFA -AccessToken $at -UserPrincipalName user@company.com
UserPrincipalName : user@company.com
State : Enforced
PhoneNumber : +1 123456789
AlternativePhoneNumber : +358 123456789
Email : someone@hotmail.com
DefaultMethod : OneWaySMS
Pin :
OldPin :
StartTime :
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
$UserPrincipalName
)
Process
{
# Get the user
$user = Get-UserByUpn -AccessToken $AccessToken -UserPrincipalName $UserPrincipalName
# Get user name from access token if empty
if([string]::IsNullOrEmpty($UserPrincipalName))
{
$UserPrincipalName = (Read-Accesstoken -AccessToken $AccessToken).unique_name
}
# Get the details and requirements
$details = $user.StrongAuthenticationUserDetails
$requirements = $user.StrongAuthenticationRequirements
$appDetails = $user.StrongAuthenticationPhoneAppDetails
# Construct the attributes hashtable
$attributes = [ordered]@{
"UserPrincipalName" = $UserPrincipalName
"State" = "Disabled"
"PhoneNumber" = $details.PhoneNumber
"AlternativePhoneNumber" = $details.AlternativePhoneNumber
"Email" = $details.Email
"DefaultMethod" =""
"Pin" = $details.Pin
"OldPin" = $details.OldPin
"StartTime" = $null
}
if(![string]::IsNullOrEmpty($requirements))
{
$attributes["State"]=$requirements.StrongAuthenticationRequirement.State
$attributes["StartTime"]=[DateTime]$requirements.StrongAuthenticationRequirement.RememberDevicesNotIssuedBefore
}
$count=0
foreach($app in $appDetails.StrongAuthenticationPhoneAppDetail)
{
$count++
#$app=$appDetails.StrongAuthenticationPhoneAppDetail
$attributes["App$count-AppAuthenticationType"]=$app.AuthenticationType
$attributes["App$count-AppDeviceId"]=$app.DeviceId
$attributes["App$count-AppDeviceName"]=$app.DeviceName
$attributes["App$count-AppDeviceTag"]=$app.DeviceTag
$attributes["App$count-AppDeviceToken"]=$app.DeviceToken
$attributes["App$count-AppId"]=$app.Id
$attributes["App$count-AppNotificationType"]=$app.NotificationType
$attributes["App$count-AppOathTokenTimeDrift"]=$app.OathTokenTimeDrift
$attributes["App$count-AppPhoneAppVersion"]=$app.PhoneAppVersion
$attributes["App$count-AppTimeInterval"]=$app.TimeInterval
}
# Get the default method
foreach($method in $user.StrongAuthenticationMethods.StrongAuthenticationMethod)
{
if($method.IsDefault.equals("true"))
{
$attributes["DefaultMethod"]=$method.Methodtype
}
}
# Return
New-Object PSObject -Property $attributes
}
}
# Jun 24th 2020
function Get-UserMFA
{
<#
.SYNOPSIS
Gets user's MFA settings
.DESCRIPTION
Gets user's MFA settings using Provisioning API
.Parameter AccessToken
Access Token of the user accessing Azure Active Directory to find the given user to get the SID
.Parameter UserPrincipalName
User's principal name.
.Example
PS C:\>$at=Get-AADIntAccessTokenForAADGraph
PS C:\>Get-AADIntUserMFA -AccessToken $at -UserPrincipalName user@company.com
UserPrincipalName : user@company.com
State : Enforced
PhoneNumber : +1 123456789
AlternativePhoneNumber : +358 123456789
Email : someone@hotmail.com
DefaultMethod : OneWaySMS
Pin :
OldPin :
StartTime :
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
$UserPrincipalName
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -ClientID "1b730954-1685-4b74-9bfd-dac224a7b894" -Resource "https://graph.windows.net"
# Get user name from access token if empty
if([string]::IsNullOrEmpty($UserPrincipalName))
{
$UserPrincipalName = (Read-Accesstoken -AccessToken $AccessToken).unique_name
}
# Get the user information
$user=Call-GraphAPI -AccessToken $AccessToken -Command "users/$UserPrincipalName" -QueryString "`$select=strongAuthenticationDetail"
# Get the details and requirements
$details = $user.strongAuthenticationDetail.verificationDetail
$requirements = $user.strongAuthenticationDetail.Requirements
$appDetails = $user.strongAuthenticationDetail.PhoneAppDetails
# Construct the attributes hashtable
$attributes = [ordered]@{
"UserPrincipalName" = $UserPrincipalName
"State" = $null
"PhoneNumber" = $details.PhoneNumber
"AlternativePhoneNumber" = $details.AlternativePhoneNumber
"Email" = $details.Email
"DefaultMethod" =""
"Pin" = $details.Pin
"OldPin" = $details.OldPin
"StartTime" = $null
"RelyingParty" = $null
}
# Check if we got details. If so, default the State to Disabled
if($details)
{
$attributes["State"]="Disabled"
}
# Check if we got requirements and update.
if($requirements)
{
$attributes["State"]=$requirements.state
$attributes["StartTime"]=[DateTime]$requirements.rememberDevicesNotIssuedBefore
$attributes["RelyingParty"]=$requirements.relyingParty
}
$attributes["AppDetails"]=Parse-AuthApps -appDetails $appDetails
# Get the default method
foreach($method in $user.strongAuthenticationDetail.methods)
{
if($method.IsDefault -eq "True")
{
$attributes["DefaultMethod"]=$method.Methodtype
}
}
# Return
New-Object PSObject -Property $attributes
}
}
# Jun 30th 2020
function Get-UserMFAApps
{
<#
.SYNOPSIS
Gets user's MFA Authentication App settings
.DESCRIPTION
Gets user's MFA Authentication App settings using Azure AD Graph
.Parameter AccessToken
Access Token of the user accessing Azure Active Directory to find the given user to get the SID
.Parameter UserPrincipalName
User's principal name.
.Example
PS C:\>$at=Get-AADIntAccessTokenForAADGraph
PS C:\>Get-AADIntUserMFAApps -AccessToken $at -UserPrincipalName user@company.com
AuthenticationType : Notification, OTP
DeviceName : SM-R2D2
DeviceTag : SoftwareTokenActivated
DeviceToken : APA91...
Id : 454b8d53-d97e-4ead-a69c-724166394334
NotificationType : GCM
OathTokenTimeDrift : 0
OathSecretKey :
PhoneAppVersion : 6.2001.0140
TimeInterval :
LastAuthTime : 16/08/2020 10.12.17
AuthenticationType : OTP
DeviceName : NO_DEVICE
DeviceTag : SoftwareTokenActivated
DeviceToken : NO_DEVICE_TOKEN
Id : aba89d77-0a69-43fa-9e5d-6f41c7b9bb16
NotificationType : Invalid
OathTokenTimeDrift : 0
OathSecretKey :
PhoneAppVersion : NO_PHONE_APP_VERSION
TimeInterval :
LastAuthTime : 06/08/2019 11.07.05
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
$UserPrincipalName
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -ClientID "1b730954-1685-4b74-9bfd-dac224a7b894" -Resource "https://graph.windows.net"
# Get user name from access token if empty
if([string]::IsNullOrEmpty($UserPrincipalName))
{
$UserPrincipalName = (Read-Accesstoken -AccessToken $AccessToken).unique_name
}
# Get the user information
$MFAinfo=Get-UserMFA -AccessToken $AccessToken -UserPrincipalName $UserPrincipalName
# Return
return $MFAinfo.AppDetails
}
}
# Generates a new One-Time-Password for MFA with the given secret
# Jun 26th 2020
function New-OTP
{
<#
.SYNOPSIS
Generates a one-time-password (OTP) using the given secret.
.DESCRIPTION
Generates a one-time-password (OTP) using the given secret. Can be used for MFA if the user's secret is known.
.Example
New-AADIntOTP -SecretKey "rrc2 wntz dkbu iikb"
OTP Valid
--- -----
502 109 26s
.Example
New-AADIntOTP -SecretKey "rrc2 wntz dkbu iikb" -Clipboard
OTP copied to clipboard, valid for 28s
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$SecretKey,
[switch]$Clipboard
)
Process
{
# Strip the spaces
$SecretKey=$SecretKey.Replace(" ","")
# Get the current time in seconds from 1.1.1970
$now = [int]((Get-Date).ToUniversalTime() -$epoch).TotalSeconds
# Generate the OTP
$OTP = Generate-tOTP -SecretKey $SecretKey -Seconds $now -TimeShift -15
# Copy to clipboard
if($Clipboard)
{
"{0:000000}" -f $OTP | Set-Clipboard
Write-Host "OTP copied to clipboard, valid for $(30-($now % 30))s"
return
}
# Return
$otpFormatted = "{0:000 000}" -f $OTP
return New-Object psobject -Property ([ordered]@{"OTP" = $otpFormatted; "Valid" = "$(30-($now % 30))s"})
}
}
# Generates a new One-Time-Password secret
# Jun 27th 2020
function New-OTPSecret
{
<#
.SYNOPSIS
Generates a one-time-password (OTP) secret.
.DESCRIPTION
Generates a one-time-password (OTP) secret.
.Example
New-AADIntOTPSecret
njny7gdb6tnfihy3
.Example
New-AADIntOTPSecret -Clipboard
OTP secret copied to clipboard.
#>
[cmdletbinding()]
Param(
[switch]$Clipboard
)
Process
{
$RNG = [Security.Cryptography.RNGCryptoServiceProvider]::Create()
[Byte[]]$x=1
for($secret=''; $secret.length -lt 16)
{
$RNG.GetBytes($x);
if([char]$x[0] -clike '[2-7a-z]')
{
$secret+=[char]$x[0]
}
}
# Copy to clipboard
if($Clipboard)
{
$secret | Set-Clipboard
Write-Host "OTP secret copied to clipboard"
return
}
# Return
return $secret
}
}
# Registers an authenticator app
# Jul 1st 2020
function Register-MFAApp
{
<#
.SYNOPSIS
Registers AADInternals Authenticator App or OTP app for the user.
.DESCRIPTION
Registers AADInternals Authenticator App or OTP appfor the user.
Requirements for App:
* AADInternals Authentication app is installed.
* Device Token is copied from the app.
* The user have registered at least one MFA method, e.g. SMS. This is because Access Token creation performs MFA.
* Registration is done through https://mysignins.microsoft.com so "Users can use the combined security information registration experience" MUST be activated for the tenant.
.Example
$deviceToken = "APA91bEGIvk1CCg1VIj_YQ_L8fn59UD6...mvXYxlWM6s90_Ct_fpo7iE3uF8hTb"
PS C:\>Get-AADIntAccessTokenForMySignins -SaveToCache
Tenant User Resource Client
------ ---- -------- ------
9a79b12c-f563-4bdc-9d18-6e6d0d52f73b user@company.com 0000000c-0000-0000-c000-000000000000 19db86c3-b2b9-44cc-b339-36da233a3be2
PS C:\>Register-AADIntMFAApp -DeviceToken -$deviceToken -DeviceName "My MFA App" -Type APP
DefaultMethodOptions : 1
DefaultMethod : 0
Username : user@company.com
TenantId : 9a79b12c-f563-4bdc-9d18-6e6d0d52f73b
AzureObjectId : dce60ee2-d907-4478-9f36-de3d74708381
ConfirmationCode : 1481770594613653
OathTokenSecretKey : dzv5osvdx6dhtly4av2apcts32eqh4bg
OathTokenEnabled : true
.Example
PS C:\>Get-AADIntAccessTokenForMySignins -SaveToCache
Tenant User Resource Client
------ ---- -------- ------
9a79b12c-f563-4bdc-9d18-6e6d0d52f73b user@company.com 0000000c-0000-0000-c000-000000000000 19db86c3-b2b9-44cc-b339-36da233a3be2
PS C:\>Register-AADIntMFAApp -Type OTP
OathSecretKey DefaultMethodOptions DefaultMethod
------------- -------------------- -------------
5bhbqsrb6ft5rxdx 1 0
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[String]$DeviceToken,
[Parameter(Mandatory=$False)]
[String]$DeviceName="AADInternals",
[ValidateSet("APP","OTP")]
[String]$Type="APP"
)
Begin
{
# Define some variables
$PfPaWs = "PfPaWs.asmx"
$Version = "6.2001.0140" # Don't change this or Android version number. It should match the auth app version.
}
Process
{
try
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "0000000c-0000-0000-c000-000000000000" -ClientId "1b730954-1685-4b74-9bfd-dac224a7b894"
}
catch
{
Throw "Access token not found! Call Get-AADIntAccessTokenForMySignins with SaveToCache switch."
}
# Check that DeviceCode exists for APP
if($Type -eq "APP" -and [string]::IsNullOrEmpty($DeviceToken))
{
Throw "Type APP requires DeviceToken"
}
# Phase 1: Get the registration info (url, activation code, session context)
$regInfo = Get-MFAAppRegistrationInfo -AccessToken $AccessToken -Type $Type
if(!$regInfo) {
Throw "Registration failed (phase 1)"
}
if($Type -eq "APP")
{
# Phase 2: Send a new activation request
$actInfo = Send-MFAAppNewActivation -AccessToken $AccessToken -RegistrationInfo $regInfo -DeviceToken $DeviceToken -DeviceName $DeviceName
if(!$actInfo) {
Throw "Registration failed (phase 2)"
}
# Phase 3: Send confirmation
$confResult = Send-MFAAppNewActivationConfirmation -AccessToken $AccessToken -ActivationInfo $actInfo -RegistrationInfo $regInfo
if(!$confResult) {
Throw "Registration failed (phase 3)"
}
}
else
{
$actInfo = New-Object psobject -Property @{ "OathSecretKey" = $regInfo.SecretKey}
}
# Phase 4: Add the device to the user
$verContext = Add-MFAAppAddDevice -AccessToken $AccessToken -RegistrationInfo $regInfo -Type $Type
if(!$verContext) {
Throw "Registration failed (phase 4)"
}
# Phase 5: Get data updates (not needed)
$updates = Verify-MFAAppAddDevice -AccessToken $AccessToken -RegistrationInfo $regInfo -VerificationContext $verContext -Type $Type
if(!$updates) {
Write-Warning "Couldn't get data updates."
}
# Insert data update info to return value
$actInfo | Add-Member -NotePropertyName "DefaultMethodOptions" -NotePropertyValue $updates.DefaultMethodOptions
$actInfo | Add-Member -NotePropertyName "DefaultMethod" -NotePropertyValue $updates.DefaultMethod
# Return
return $actInfo
}
}