|
| 1 | +Function Get-HawkTenantAgentIdentity { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Collects Microsoft Entra Agent ID (AI agent) identities and flags those warranting review. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Enumerates Microsoft Entra Agent ID agent identities in the tenant using the Microsoft Graph |
| 8 | + agentIdentity service principal cast (GET /servicePrincipals/microsoft.graph.agentIdentity). |
| 9 | + AI agents - such as those created by Microsoft Copilot Studio and managed through Microsoft |
| 10 | + Agent 365 - are represented as a dedicated service principal subtype in Microsoft Entra, so |
| 11 | + this function gives responders an inventory of the agent identity attack surface alongside |
| 12 | + Hawk's existing application and service principal collection. |
| 13 | +
|
| 14 | + For each agent identity the function records core directory properties, a credential |
| 15 | + summary, and resolved owners, then flags agents that warrant review (no owner, |
| 16 | + password-secret credentials, creation inside the investigation window, or |
| 17 | + disabled-but-present) using the Test-SuspiciousAgentIdentity helper. |
| 18 | +
|
| 19 | + The function degrades gracefully: if the agent identity endpoint is not available in the |
| 20 | + tenant, or the AgentIdentity.Read.All permission has not been consented, it logs an |
| 21 | + informational message and returns without error. |
| 22 | +
|
| 23 | + .OUTPUTS |
| 24 | + File: AgentIdentities.csv / .json |
| 25 | + Path: \Tenant |
| 26 | + Description: Inventory of all Entra Agent ID agent identities and their properties. |
| 27 | +
|
| 28 | + File: _Investigate_AgentIdentities.csv / .json |
| 29 | + Path: \Tenant |
| 30 | + Description: Subset of agent identities flagged for investigation, with reasons. |
| 31 | +
|
| 32 | + .EXAMPLE |
| 33 | + Get-HawkTenantAgentIdentity |
| 34 | +
|
| 35 | + Enumerates all Entra Agent ID agent identities in the tenant and writes the inventory and any |
| 36 | + flagged agents to the Tenant output folder. |
| 37 | +
|
| 38 | + .NOTES |
| 39 | + Requires the following Microsoft Graph permission: |
| 40 | + - AgentIdentity.Read.All |
| 41 | +
|
| 42 | + .LINK |
| 43 | + https://learn.microsoft.com/en-us/graph/api/agentidentity-list |
| 44 | + #> |
| 45 | + [CmdletBinding()] |
| 46 | + param() |
| 47 | + |
| 48 | + BEGIN { |
| 49 | + # Check if Hawk object exists and is fully initialized |
| 50 | + if (Test-HawkGlobalObject) { |
| 51 | + Initialize-HawkGlobalObject |
| 52 | + } |
| 53 | + |
| 54 | + # Create Tenant folder path if it doesn't exist |
| 55 | + $tenantPath = Join-Path -Path $Hawk.FilePath -ChildPath "Tenant" |
| 56 | + if (-not (Test-Path -Path $tenantPath)) { |
| 57 | + New-Item -Path $tenantPath -ItemType Directory -Force | Out-Null |
| 58 | + } |
| 59 | + |
| 60 | + Out-LogFile "Initiating collection of Entra Agent ID identities from Microsoft Graph." -Action |
| 61 | + |
| 62 | + Test-GraphConnection |
| 63 | + Send-AIEvent -Event "CmdRun" |
| 64 | + } |
| 65 | + |
| 66 | + PROCESS { |
| 67 | + try { |
| 68 | + # Enumerate agent identities (a service principal subtype). GA on the v1.0 endpoint. |
| 69 | + $agentUri = "https://graph.microsoft.com/v1.0/servicePrincipals/microsoft.graph.agentIdentity" |
| 70 | + $agents = Get-HawkAllGraphResult -Uri $agentUri |
| 71 | + |
| 72 | + if ($null -eq $agents) { |
| 73 | + Out-LogFile "Entra Agent ID identities are not available in this tenant, or the AgentIdentity.Read.All permission has not been consented. Skipping." -Information |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + if ($agents.Count -eq 0) { |
| 78 | + Out-LogFile "No Entra Agent ID identities found in the tenant." -Information |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + Out-LogFile ("Found " + $agents.Count + " agent identities. Processing details.") -Information |
| 83 | + |
| 84 | + $agentResults = @() |
| 85 | + foreach ($agent in $agents) { |
| 86 | + |
| 87 | + # Resolve owners so we can spot orphaned / shadow agents. |
| 88 | + $ownerNames = $null |
| 89 | + if ($agent.id) { |
| 90 | + $owners = Get-HawkAllGraphResult -Uri ("https://graph.microsoft.com/v1.0/servicePrincipals/" + $agent.id + "/owners") |
| 91 | + if ($owners) { |
| 92 | + $ownerNames = ( |
| 93 | + $owners | ForEach-Object { |
| 94 | + if ($_.userPrincipalName) { $_.userPrincipalName } else { $_.displayName } |
| 95 | + } |
| 96 | + ) -join ';' |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + $passwordCount = ($agent.passwordCredentials | Measure-Object).Count |
| 101 | + $keyCount = ($agent.keyCredentials | Measure-Object).Count |
| 102 | + |
| 103 | + $agentResults += [PSCustomObject]@{ |
| 104 | + DisplayName = $agent.displayName |
| 105 | + Id = $agent.id |
| 106 | + AppId = $agent.appId |
| 107 | + ServicePrincipalType = $agent.servicePrincipalType |
| 108 | + AgentType = $agent.agentType |
| 109 | + BlueprintId = $agent.blueprintId |
| 110 | + AccountEnabled = $agent.accountEnabled |
| 111 | + CreatedDateTime = $agent.createdDateTime |
| 112 | + Owners = $ownerNames |
| 113 | + HasPasswordSecret = ($passwordCount -gt 0) |
| 114 | + HasKeyCredential = ($keyCount -gt 0) |
| 115 | + PasswordCredentialCount = $passwordCount |
| 116 | + KeyCredentialCount = $keyCount |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + # Write the full inventory |
| 121 | + $agentResults | Out-MultipleFileType -FilePrefix "AgentIdentities" -csv -json |
| 122 | + |
| 123 | + # Flag agents that warrant review |
| 124 | + $flagged = @() |
| 125 | + foreach ($record in $agentResults) { |
| 126 | + $reasons = @() |
| 127 | + if (Test-SuspiciousAgentIdentity -Agent $record -Reasons ([ref]$reasons)) { |
| 128 | + $flagged += ($record | Select-Object -Property *, @{Name = 'InvestigateReasons'; Expression = { $reasons -join '; ' } }) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if ($flagged.Count -gt 0) { |
| 133 | + Out-LogFile ("Found " + $flagged.Count + " agent identities that warrant review (orphaned, secret-based, newly created, or disabled).") -Notice |
| 134 | + Out-LogFile "Please review _Investigate_AgentIdentities.csv to ensure these agent identities are expected." -Notice |
| 135 | + $flagged | Out-MultipleFileType -FilePrefix "_Investigate_AgentIdentities" -csv -json -Notice |
| 136 | + } |
| 137 | + } |
| 138 | + catch { |
| 139 | + Out-LogFile "Error collecting Entra Agent ID identities: $($_.Exception.Message)" -isError |
| 140 | + Write-Error -ErrorRecord $_ -ErrorAction Continue |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + END { |
| 145 | + Out-LogFile "Completed collection of Entra Agent ID identities from Microsoft Graph." -Information |
| 146 | + } |
| 147 | +} |
0 commit comments