-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-UnattestedPasskeys.ps1
More file actions
162 lines (133 loc) · 6.64 KB
/
Copy pathGet-UnattestedPasskeys.ps1
File metadata and controls
162 lines (133 loc) · 6.64 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
<#
.SYNOPSIS
Reports device-bound passkeys registered in Entra ID that are not attested.
.DESCRIPTION
This script efficiently finds all device-bound passkeys that lack attestation
by first filtering users who have passkeys registered (via userRegistrationDetails),
then checking their FIDO2 authentication methods for attestation status.
This avoids querying all users in the tenant.
.PARAMETER OutputPath
Optional path for CSV export. If not specified, outputs to console only.
.EXAMPLE
.\Get-UnattestedPasskeys.ps1
.EXAMPLE
.\Get-UnattestedPasskeys.ps1 -OutputPath "C:\Reports\UnattestedPasskeys.csv"
.NOTES
Requires Microsoft.Graph.Reports and Microsoft.Graph.Authentication modules
Required permissions: UserAuthenticationMethod.Read.All, Reports.Read.All
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$OutputPath
)
# Function to ensure required modules are loaded
function Test-GraphModule {
$requiredModules = @('Microsoft.Graph.Authentication', 'Microsoft.Graph.Reports', 'Microsoft.Graph.Users')
foreach ($module in $requiredModules) {
if (-not (Get-Module -Name $module -ListAvailable)) {
Write-Error "Required module '$module' is not installed. Install it with: Install-Module $module -Scope CurrentUser"
return $false
}
if (-not (Get-Module -Name $module)) {
Write-Verbose "Importing module: $module"
Import-Module $module -ErrorAction Stop
}
}
return $true
}
# Main script execution
try {
Write-Host "Starting unattested device-bound passkey report..." -ForegroundColor Cyan
# Check modules
if (-not (Test-GraphModule)) {
throw "Required modules are missing"
}
# Connect to Microsoft Graph if not already connected
$context = Get-MgContext
if (-not $context) {
Write-Host "Connecting to Microsoft Graph..." -ForegroundColor Yellow
Connect-MgGraph -Scopes "UserAuthenticationMethod.Read.All", "Reports.Read.All" -NoWelcome
} else {
Write-Host "Already connected to Microsoft Graph as $($context.Account)" -ForegroundColor Green
}
# Step 1: Find users with device-bound passkeys registered
# Filtering for passKeyDeviceBound and passKeyDeviceBoundAuthenticator
Write-Host "`nStep 1: Querying users with device-bound passkeys..." -ForegroundColor Cyan
$uri = "https://graph.microsoft.com/beta/reports/authenticationMethods/userRegistrationDetails"
$filter = "methodsRegistered/any(m:m eq 'passKeyDeviceBound' or m eq 'passKeyDeviceBoundAuthenticator')"
$select = "userPrincipalName,userDisplayName,id"
$usersWithPasskeys = Invoke-MgGraphRequest -Method GET -Uri "$uri`?`$filter=$filter&`$select=$select" -Headers @{"ConsistencyLevel" = "eventual" }
$totalUsers = $usersWithPasskeys.value.Count
Write-Host "Found $totalUsers users with device-bound passkeys registered" -ForegroundColor Green
if ($totalUsers -eq 0) {
Write-Host "No users with device-bound passkeys found. Exiting." -ForegroundColor Yellow
return
}
# Step 2: Check each user's FIDO2 methods for attestation status
Write-Host "`nStep 2: Checking attestation status for each user..." -ForegroundColor Cyan
$results = @()
$currentUser = 0
foreach ($user in $usersWithPasskeys.value) {
$currentUser++
Write-Progress -Activity "Checking FIDO2 methods" -Status "Processing user $currentUser of $totalUsers : $($user.userPrincipalName)" -PercentComplete (($currentUser / $totalUsers) * 100)
try {
# Get FIDO2 authentication methods for this user
$fido2Uri = "https://graph.microsoft.com/beta/users/$($user.id)/authentication/fido2Methods"
$fido2Methods = Invoke-MgGraphRequest -Method GET -Uri $fido2Uri
# Filter for device-bound passkeys that are not attested
$unattestedKeys = $fido2Methods.value | Where-Object {
$_.passkeyType -eq 'deviceBound' -and $_.attestationLevel -eq 'notAttested'
}
if ($unattestedKeys.Count -gt 0) {
foreach ($key in $unattestedKeys) {
$results += [PSCustomObject]@{
UserPrincipalName = $user.userPrincipalName
UserDisplayName = $user.userDisplayName
PasskeyDisplayName = $key.displayName
PasskeyId = $key.id
AAGUID = $key.aaGuid
Model = $key.model
CreatedDateTime = $key.createdDateTime
LastUsedDateTime = $key.lastUsedDateTime
AttestationLevel = $key.attestationLevel
PasskeyType = $key.passkeyType
HasAttestationCerts = ($key.attestationCertificates.Count -gt 0)
}
}
}
}
catch {
Write-Warning "Error processing user $($user.userPrincipalName): $($_.Exception.Message)"
}
}
Write-Progress -Activity "Checking FIDO2 methods" -Completed
# Step 3: Display and optionally export results
Write-Host "`n=== RESULTS ===" -ForegroundColor Cyan
Write-Host "Total users checked: $totalUsers" -ForegroundColor White
Write-Host "Unattested device-bound passkeys found: $($results.Count)" -ForegroundColor $(if ($results.Count -gt 0) { 'Yellow' } else { 'Green' })
if ($results.Count -gt 0) {
Write-Host "`nDetails:" -ForegroundColor Cyan
$results | Format-Table UserPrincipalName, PasskeyDisplayName, Model, AttestationLevel, CreatedDateTime -AutoSize
# Export to CSV if path specified
if ($OutputPath) {
$results | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8
Write-Host "`nResults exported to: $OutputPath" -ForegroundColor Green
}
# Group by user for summary
Write-Host "`nSummary by user:" -ForegroundColor Cyan
$results | Group-Object UserPrincipalName |
Select-Object @{N='User';E={$_.Name}}, @{N='UnattestedKeys';E={$_.Count}} |
Format-Table -AutoSize
}
else {
Write-Host "`nNo unattested device-bound passkeys found. All device-bound passkeys are properly attested!" -ForegroundColor Green
}
}
catch {
Write-Error "An error occurred: $($_.Exception.Message)"
Write-Error $_.ScriptStackTrace
}
finally {
Write-Host "`nScript completed." -ForegroundColor Cyan
}