AZ_EXECUTE_COMMAND
Summary
Forestall ACL Alias
AZ_EXECUTE_COMMAND
Azure Alias
Execute Command (VM Run Command, managed Run Commands, extensions, AKS Run Command, Web App SSH/Kudu, ACR Tasks)
Affected Object Types
Virtual Machines, VM Scale Sets (Uniform mode), AKS Managed Clusters, App Services, Function Apps, Container Registries
Exploitation Certainty
Certain
Permission / Role
Azure resource permissions that allow running code on the guest or in a build/task runner (often Owner, Contributor, Virtual Machine Contributor, AKS Contributor Role, Website Contributor). See Execution mechanisms for the exact API actions.
Built-in Roles (VM):
Owner
Contributor
Virtual Machine Contributor
Avere Contributor
Azure Center For SAP Solutions Service Role
Defender Servers P1/P2
Defender SQL Servers On Machines
VM Restore Operator
Desktop Virtualization VM Contributor
Built-in Roles (AKS):
Contributor
Azure Kubernetes Service Contributor Role
Built-in Roles (VMSS - Uniform Orchestration Mode):
Owner
Contributor
Virtual Machine Contributor
Custom Role Actions (VM):
Microsoft.Compute/virtualMachines/runCommand/actionMicrosoft.Compute/virtualMachines/runCommands/writeMicrosoft.Compute/virtualMachines/extensions/write
Custom Role Actions (AKS):
Microsoft.ContainerService/managedClusters/runCommand/action
Custom Role Actions (VMSS):
Microsoft.Compute/virtualMachineScaleSets/virtualMachines/runCommand/action
Built-in Roles (App Services / Function Apps):
Owner
Contributor
Website Contributor
Custom Role Actions (App Services / Function Apps):
Microsoft.Web/sites/publish/Action
Built-in Roles (Container Registry — ACR Tasks):
Owner
Contributor
Container Registry Tasks Contributor (
fb382eab-e894-4461-af04-94435c366c3f) — purpose-built for Tasks management; grants the entire poison/trigger/read-log loop.
Custom Role Actions (Container Registry — ACR Tasks):
Microsoft.ContainerRegistry/registries/tasks/writeMicrosoft.ContainerRegistry/registries/scheduleRun/action
Pre-condition (real attack): an existing Task on the registry already has a privileged managed identity attached (system-assigned or user-assigned).
tasks/writethen lets a principal rewrite the Task'sstep.cmdto dump that identity's token - noassign/actionis required because the Task already carries the identity.Not a separate escalation: "create a new Task and attach a UAMI" requires
Microsoft.ManagedIdentity/userAssignedIdentities/<UAMI>/assign/actionon the target UAMI - which is the actual privilege primitive. With that permission an attacker can attach the UAMI to any compute resource (VM, Function App, ACI, etc.); ACR Tasks isn't doing any work in that case. See the AZACR section below.
Description
AZ_EXECUTE_COMMAND is the risk that an attacker (or insider) with the right Azure resource permissions can run commands inside Azure compute resources. This includes Virtual Machines, VM Scale Sets, AKS clusters, App Services, Function Apps, and Azure Container Registry build/task runners.
Execution mechanisms
Microsoft.Compute/virtualMachines/runCommand/action
Run a one-off command on the VM through the agent (the usual "Run Command").
Invoke-AzVMRunCommand, az vm run-command invoke
Microsoft.Compute/virtualMachines/runCommands/write
Save a named Run Command on the VM (script stored on the VM resource).
Set-AzVMRunCommand, az vm run-command create
Microsoft.Compute/virtualMachines/extensions/write (and related VM writes)
Install or change a VM extension (for example Custom Script Extension), which runs code on the guest.
Set-AzVMExtension, az vm extension set
Microsoft.Compute/virtualMachineScaleSets/virtualMachines/runCommand/action
Run a one-off command on a VMSS instance.
Invoke-AzVmssVMRunCommand, az vmss run-command invoke
Microsoft.ContainerService/managedClusters/runCommand/action
Run commands through AKS Run Command in the cluster context.
az aks command invoke
Microsoft.Web/sites/publish/Action
SSH or execute commands on App Services/Function Apps via Web SSH or Kudu API.
az webapp ssh, Kudu REST API, Web SSH portal
Microsoft.ContainerRegistry/registries/tasks/write
Create or poison an ACR Task that executes on an ephemeral build VM with the registry's managed identity available (unlike sandboxed az acr build/run).
az acr task create, az acr task update, ARM template
Microsoft.ContainerRegistry/registries/scheduleRun/action
Trigger a quick run against an ACR Task / Dockerfile that runs as the registry's managed identity.
az acr task run, az acr build (control plane)
Run Command on Windows normally runs as SYSTEM; on Linux it often runs as root. Extensions also run with high privilege on the guest. Web SSH on Linux App Services runs as root.
Why this matters for security:
Stealing credentials - for example memory/cached credential attacks where your policy allows them.
Moving to other systems - especially if the VM is joined to on-premises Active Directory.
Taking data - files, databases, app data on the VM.
Stealing managed identity tokens - calling the instance metadata service (IMDS) for tokens.
Staying on the box - malware, scheduled tasks, bad extensions, and similar.
Resource Sections
Use the resource-specific sections below as standalone mini-docs. Each target type has its own identification, exploitation, and detection guidance.
AZVM - Virtual Machines
Identification
Look for identities whose roles allow any of these actions on the VM (or on a parent scope such as resource group or subscription):
Microsoft.Compute/virtualMachines/runCommand/actionMicrosoft.Compute/virtualMachines/runCommands/writeMicrosoft.Compute/virtualMachines/extensions/write
Built-in roles like Owner, Contributor, and Virtual Machine Contributor usually include one or more of the above (often via broad * or Microsoft.Compute/virtualMachines/* patterns). Custom roles must be checked against this list.
PowerShell (Az module)
To scope to one VM: run Get-AzRoleAssignment -Scope '/subscriptions/<subId>/resourceGroups/<rg>/providers/Microsoft.Compute/virtualMachines/<vmName>' (fill in your IDs) and filter with the same $riskyRoleNames.
Azure portal
In the Azure Portal, open the subscription, resource group, or virtual machine you want to check.
Open Access control (IAM) -> Role assignments.
Open each relevant role (especially custom roles) and confirm whether Permissions include the three actions above (or a wildcard that covers them, such as
*orMicrosoft.Compute/virtualMachines/*).
Exploitation
Replace <RGName>, <VMName>, <Location>, and the script text with your own values. These examples are for authorized testing only.
Invoke-AzVmExecutionMethods (all Az methods)
Usage
For Linux guests, add -LinuxGuest (the function selects RunShellScript and the Linux Custom Script extension publisher/type).
VM Method 1: Microsoft.Compute/virtualMachines/runCommand/action - action Run Command
PowerShell (Windows guest)

Azure CLI (Windows guest)

PowerShell (Linux guest)

VM Method 2: Microsoft.Compute/virtualMachines/runCommands/write - managed Run Command
Creates a named Run Command on the VM. That is handy when you want a script saved on the VM and run again later. For lab demos, keep --async-execution false so the command finishes before the CLI returns. See Microsoft docs if you need async (run in the background).
Azure CLI

PowerShell (Az.Compute)

VM Method 3: Microsoft.Compute/virtualMachines/extensions/write - Custom Script Extension
Adding or updating Custom Script Extension runs your script on the guest as part of the extension install/update. If an extension with the same name already exists, you may need a new name, a force-update flag, or to remove the old extension first - see Microsoft's docs for your case.
Azure CLI (settings file)
You need read access to the VM for this flow (Microsoft.Compute/virtualMachines/extensions/read), not only write.

PowerShell (Az module)
Same read permission on the VM applies: Microsoft.Compute/virtualMachines/extensions/read.

PowerShell - Invoke-AzVmExecutionMethods
Same as the Set-AzVMExtension approach inside the helper script; you need -Location (Azure region) for this method.

Detection
Use the Azure Portal:
In the Azure Portal, open the subscription, resource group, or virtual machine you want to review.
Open Activity log.
Filter Operation for VM execution and extension activity, such as Run Command, Create or Update Virtual Machine Run Command, and Create or Update Virtual Machine Extension.
Review the Event initiated by, Timestamp, Resource, Status, and Change history fields for unexpected administrators, unusual timing, or suspicious command/extension changes.
On the VM blade, review Run command and Extensions + applications for saved run commands or unexpected extensions.
AZAKS - Azure Kubernetes Service (AKS)
Identification
Look for identities whose roles allow Microsoft.ContainerService/managedClusters/runCommand/action on an AKS cluster, resource group, or subscription.
Built-in roles that commonly grant this access:
Contributor
Azure Kubernetes Service Contributor Role
PowerShell (Az module)
Azure portal
In the Azure Portal, open the subscription, resource group, or AKS cluster you want to check.
Open Access control (IAM) -> Role assignments.
Review assignments for Contributor, Azure Kubernetes Service Contributor Role, and custom roles that include
Microsoft.ContainerService/managedClusters/runCommand/action.
Exploitation
AKS Run Command - Microsoft.ContainerService/managedClusters/runCommand/action
AKS Run Command enables code execution within the AKS cluster context, allowing access to Kubernetes resources and the cluster's managed identity.
Azure CLI

AKS Run Command is particularly dangerous because:
It bypasses standard Kubernetes RBAC - the caller only needs Azure RBAC permissions.
It provides access to all cluster resources including secrets.
It can access the cluster's managed identity for Azure pivoting.
Detection
Use the Azure Portal:
In the Azure Portal, open the subscription, resource group, or AKS cluster you want to review.
Open Activity log.
Filter Operation for AKS Run Command activity, such as Run command on managed cluster.
Review the Event initiated by, Timestamp, Resource, Status, and Change history fields for unexpected users, unusual timing, or suspicious command execution.
On the AKS cluster blade, review recent Run command activity where available.
AZVMSS - VM Scale Sets
Identification
Look for identities whose roles allow Microsoft.Compute/virtualMachineScaleSets/virtualMachines/runCommand/action on a VM Scale Set, resource group, or subscription.
Built-in roles that commonly grant this access:
Owner
Contributor
Virtual Machine Contributor
PowerShell (Az module)
Azure portal
In the Azure Portal, open the subscription, resource group, or Virtual Machine Scale Set you want to check.
Open Access control (IAM) -> Role assignments.
Review assignments for Owner, Contributor, Virtual Machine Contributor, and custom roles that include
Microsoft.Compute/virtualMachineScaleSets/virtualMachines/runCommand/action.
Exploitation
VMSS Run Command - Microsoft.Compute/virtualMachineScaleSets/virtualMachines/runCommand/action
Run Command on VM Scale Sets (Uniform orchestration mode only) enables code execution across multiple VM instances simultaneously.
Azure CLI
PowerShell

Note: VMSS Run Command only applies to Uniform orchestration mode scale sets. Flexible orchestration mode uses standard VM Run Command per instance.
Detection
Use the Azure Portal:
In the Azure Portal, open the subscription, resource group, or Virtual Machine Scale Set you want to review.
Open Activity log.
Filter Operation for VMSS Run Command activity, such as Run Command on scale set VM instances.
Review the Event initiated by, Timestamp, Resource, Status, and Change history fields for commands across many instances, unexpected administrators, or runs outside approved change windows.
On the scale set blade, review Instances and the affected instance activity for unexpected run command usage.
AZWEBAPP - App Services & Function Apps
Identification
Look for identities whose roles allow Microsoft.Web/sites/publish/Action on App Services, Function Apps, resource groups, or subscriptions.
Built-in roles that commonly grant this access:
Owner
Contributor
Website Contributor
PowerShell (Az module)
Azure portal
In the Azure Portal, open the subscription, resource group, App Service, or Function App you want to check.
Open Access control (IAM) -> Role assignments.
Review assignments for Owner, Contributor, Website Contributor, and custom roles that include
Microsoft.Web/sites/publish/Action.
Exploitation
Web App Execute Command - Microsoft.Web/sites/publish/Action
App Services and Function Apps support command execution through SSH and the Kudu API. With Microsoft.Web/sites/publish/Action permission, an attacker can:
SSH as root (Linux) - full root access inside the container via Web SSH.
Execute commands via Kudu - run commands through the Kudu REST API.
Steal managed identity tokens - access tokens for Azure resources the app identity can reach.
Built-in Roles with this permission:
Website Contributor
Contributor
Owner
Method 1: SSH as Root via Web (Linux App Services)
For Linux App Services, SSH directly into the container as root via the web interface.
Web SSH Endpoints
Authentication: Requires Microsoft.Web/sites/publish/Action permission. Access via:
Browser session (Microsoft Entra authenticated)
Injecting
Authorization: Bearer <token>header using tools like Burp Suite, ModHeader extension, or curl

Method 2: Execute Command via Kudu API (Windows/Linux)
Kudu commands execute as a sandboxed user (not root). Use Method 1 for root access on Linux.
Azure CLI

PowerShell

Method 3: Steal Managed Identity Token
App Services use a local identity endpoint (not the standard VM IMDS at 169.254.169.254).
Important: The managed identity environment variables (
IDENTITY_ENDPOINT,IDENTITY_HEADER) are only available when executing as root via Web SSH (Method 1). The Kudu sandbox (Method 2) runs in a separate container without these variables.
Linux (via Web SSH as root)

Windows (via Kudu console)
On Windows App Services, the Kudu console has access to identity environment variables.
Request tokens for different resources:
Azure Management API
https://management.azure.com/
Key Vault
https://vault.azure.net
Storage
https://storage.azure.com/
Microsoft Graph
https://graph.microsoft.com
Detection
Use the Azure Portal:
In the Azure Portal, open the subscription, resource group, App Service, or Function App you want to review.
Open Activity log.
Filter Operation for publishing and configuration access, such as Publish Web App, List Publishing Credentials, and List Web App Config.
Review the Event initiated by, Timestamp, Resource, Status, and Change history fields for unexpected identities, unusual publishing activity, or suspicious configuration access.
On the App Service or Function App blade, use Log stream from the Azure Portal for real-time review when investigating active activity.
AZACR - Azure Container Registry (ACR Tasks)
Microsoft.ContainerRegistry/registries/tasks/write + scheduleRun/action enables code execution via ACR Tasks.
Attack Paths
Poison existing Task
If a Task already has a managed identity attached, update its step.cmd or redirect --context to attacker YAML. No assign/action needed.
Inject identity into Task
If you have AZ_MANAGED_IDENTITY_ASSIGNABLE_BY on a privileged UAMI, attach it to a new or existing Task to steal its token.
Exploitation
If Tasks with assigned identities already exist — poison them to steal the token:
If you can assign identities — see AZ_MANAGED_IDENTITY_ASSIGNABLE_BY. ACR Tasks is one execution venue among many (VMs, Logic Apps, Function Apps, etc.).
References
Operational Security Notes
Azure Activity Log often shows who ran Run Command or changed extensions (exact rows depend on your log setup).
On the guest, these actions are still SYSTEM / root - your usual VM logs, EDR, and PowerShell logging still matter.
Saved Run Commands and extensions leave artifacts on the VM definition. A one-shot
run-command invokeis different for investigators and for cleanup.ACR Tasks leave a Task resource in the registry (
az acr task list) and run logs inaz acr task list-runs- short-lived, but persistent enough to forensically attribute.
Mitigation
Limit execution-capable role assignments
Grant Owner, Contributor, Virtual Machine Contributor, AKS Contributor Role, and Website Contributor only where they are truly needed.
Prefer narrow scopes such as a single VM, AKS cluster, App Service, resource group, or subscription segment.
Use custom roles and policy guardrails
Remove
runCommand/action,runCommands/write, extension writes, AKS Run Command, and Web publish actions where teams do not need command execution.
Enable workload-level telemetry
Use EDR on VMs, Kubernetes audit logging for AKS, and App Service logging or Kudu review where supported.
Enable command and script logging
Turn on PowerShell script block and module logging on Windows VMs where your team supports it.
References
Last updated
Was this helpful?