How the attack works
Run Command executes a script inside a machine through its VM agent, so no network path to the machine is needed. Action Run Command (Microsoft.Compute/virtualMachines/runCommand/action) runs a script once as SYSTEM or root; managed Run Command (.../runCommands/write) creates a run command resource that can run as a chosen user, in sequence, and on scale set instances. Azure Arc-enabled servers offer managed Run Command through the Connected Machine agent (Microsoft.HybridCompute/machines/runCommands/write).
Attackers who hold Virtual Machine Contributor, Contributor or a custom role with these actions use Run Command to turn Azure control-plane access into code execution: harvesting credentials on the host, requesting tokens for the machine's managed identity from the instance metadata service, or deploying tooling. The request is recorded in the Azure Activity log with its caller; the script and its output stay on the machine and, for managed Run Command, in the run command resource. A third rule counts executions per caller, because one identity reaching many machines in a short time is the pattern Microsoft Defender for Cloud describes for scripted code execution through Resource Manager.
What an attacker needs
- An identity with
Microsoft.Compute/virtualMachines/runCommand/actionor.../runCommands/write(Virtual Machine Contributor, Contributor, Owner or a custom role), orMicrosoft.HybridCompute/machines/runCommands/writefor Arc-enabled servers.
Impact
Code execution with the highest local privilege on the machine, access to everything the machine can reach on the network, and the permissions of its managed identity, which often reach well beyond the machine itself.
Detection rules
Azure VM Action Run Command Executed
Detects a successful action Run Command on an Azure virtual machine or a virtual machine scale set instance, which runs a script on the machine as SYSTEM or root through the VM agent.
title: Azure VM Action Run Command Executed
id: ecba41ff-1afa-4ee4-90d7-b9dfc0a742ed
status: experimental
description: >-
Detects a successful action Run Command on an Azure virtual machine or a
virtual machine scale set instance, which runs a script on the machine as
SYSTEM or root through the VM agent.
references:
- https://attack.mitre.org/techniques/T1651/
- https://learn.microsoft.com/en-us/azure/virtual-machines/run-command-overview
- https://learn.microsoft.com/en-us/azure/role-based-access-control/permissions/compute
author: Forestall
date: 2026-11-10
tags:
- attack.execution
- attack.t1651
logsource:
product: azure
service: activitylogs
detection:
selection:
OperationNameValue:
- MICROSOFT.COMPUTE/VIRTUALMACHINES/RUNCOMMAND/ACTION
- MICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/VIRTUALMACHINES/RUNCOMMAND/ACTION
ActivityStatusValue:
- Success
- Succeeded
condition: selection
falsepositives:
- Troubleshooting by platform administrators and management tooling that uses Run Command; add the expected Caller values to a filter_ selection once confirmed.
level: medium
False positives
- Troubleshooting by platform administrators and management tooling that uses Run Command; add the expected Caller values to a filter_ selection once confirmed.
Azure Managed Run Command Created or Updated
Detects a successful creation or update of a managed Run Command on an Azure virtual machine, a virtual machine scale set instance or an Azure Arc-enabled server. Creating the run command resource runs its script on the machine.
title: Azure Managed Run Command Created or Updated
id: 19d08ba0-f387-4608-80f4-c2b28b59ad68
status: experimental
description: >-
Detects a successful creation or update of a managed Run Command on an Azure
virtual machine, a virtual machine scale set instance or an Azure
Arc-enabled server. Creating the run command resource runs its script on the
machine.
references:
- https://attack.mitre.org/techniques/T1651/
- https://learn.microsoft.com/en-us/azure/virtual-machines/run-command-overview
- https://learn.microsoft.com/en-us/azure/azure-arc/servers/run-command
- https://learn.microsoft.com/en-us/azure/role-based-access-control/permissions/compute
author: Forestall
date: 2026-11-10
tags:
- attack.execution
- attack.t1651
logsource:
product: azure
service: activitylogs
detection:
selection:
OperationNameValue:
- MICROSOFT.COMPUTE/VIRTUALMACHINES/RUNCOMMANDS/WRITE
- MICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/VIRTUALMACHINES/RUNCOMMANDS/WRITE
- MICROSOFT.HYBRIDCOMPUTE/MACHINES/RUNCOMMANDS/WRITE
ActivityStatusValue:
- Success
- Succeeded
condition: selection
falsepositives:
- Provisioning templates and infrastructure-as-code pipelines that configure machines with managed Run Command; exclude their deployment identities in a filter_ selection by Caller.
level: medium
False positives
- Provisioning templates and infrastructure-as-code pipelines that configure machines with managed Run Command; exclude their deployment identities in a filter_ selection by Caller.
Run Command Executed Many Times by One Caller
Counts, per Caller, the successful Run Command executions (action Run Command and managed Run Command) on virtual machines, scale set instances and Arc-enabled servers. Microsoft Defender for Cloud describes attack tooling that runs a script on a list of virtual machines through Resource Manager. Five or more executions within 30 minutes by one identity is that pattern: one control-plane identity turned into code execution on many machines in a short time. The machine is named only in the resource ID of each event, which this rule does not count, so several runs on one machine count as well. The count and the window are starting points to tune.
Fires when one Caller has at least 5 matching events within 30 minutes.
title: Azure Run Command Executed (Threshold Base Event)
id: 0a0b2ffb-2ceb-4ae0-abe7-915fa88b2a9e
name: azure_run_command_executed
status: experimental
description: >-
Base event of the threshold rule in this file. A successful action Run
Command, or a successful creation or update of a managed Run Command, on an
Azure virtual machine, a scale set instance or an Azure Arc-enabled server.
logsource:
product: azure
service: activitylogs
detection:
selection:
OperationNameValue:
- MICROSOFT.COMPUTE/VIRTUALMACHINES/RUNCOMMAND/ACTION
- MICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/VIRTUALMACHINES/RUNCOMMAND/ACTION
- MICROSOFT.COMPUTE/VIRTUALMACHINES/RUNCOMMANDS/WRITE
- MICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/VIRTUALMACHINES/RUNCOMMANDS/WRITE
- MICROSOFT.HYBRIDCOMPUTE/MACHINES/RUNCOMMANDS/WRITE
ActivityStatusValue:
- Success
- Succeeded
condition: selection
level: low
---
title: Run Command Executed Many Times by One Caller
id: 920429cc-b979-43cf-9ce9-e6344f26f0d3
status: experimental
description: >-
Counts, per Caller, the successful Run Command executions (action Run
Command and managed Run Command) on virtual machines, scale set instances
and Arc-enabled servers. Microsoft Defender for Cloud describes attack
tooling that runs a script on a list of virtual machines through Resource
Manager. Five or more executions within 30 minutes by one identity is that
pattern: one control-plane identity turned into code execution on many
machines in a short time. The machine is named only in the resource ID of
each event, which this rule does not count, so several runs on one machine
count as well. The count and the window are starting points to tune.
references:
- https://attack.mitre.org/techniques/T1651/
- https://learn.microsoft.com/en-us/azure/virtual-machines/run-command-overview
- https://learn.microsoft.com/en-us/azure/role-based-access-control/permissions/compute
- https://learn.microsoft.com/en-us/azure/defender-for-cloud/alerts-resource-manager
author: Forestall
date: 2026-11-18
tags:
- attack.execution
- attack.t1651
correlation:
type: event_count
rules:
- azure_run_command_executed
group-by:
- Caller
timespan: 30m
condition:
gte: 5
falsepositives:
- Patch, inventory and configuration jobs that use Run Command across a fleet, and administrators who run a script on many machines at once. Exclude those identities by Caller, or raise the count for them.
- An administrator who runs several commands on one machine while troubleshooting. Read the resource IDs of the matching events to see how many machines were reached.
- The values of 5 executions and 30 minutes are starting points, not tested limits. Measure what your own operations produce and adjust both.
level: medium
False positives
- Patch, inventory and configuration jobs that use Run Command across a fleet, and administrators who run a script on many machines at once. Exclude those identities by Caller, or raise the count for them.
- An administrator who runs several commands on one machine while troubleshooting. Read the resource IDs of the matching events to see how many machines were reached.
- The values of 5 executions and 30 minutes are starting points, not tested limits. Measure what your own operations produce and adjust both.
Detection logic
Patterns that need more than one event, a baseline or knowledge of your own environment. Each is a hypothesis to build and tune in your SIEM, not a ready rule.
Run Command on a machine, then the machine's managed identity changes resources
Run Command gives SYSTEM or root on the machine, and from there a token for the machine's managed identity is one request to the instance metadata endpoint, which answers only inside the machine. Resource Manager operations by that identity shortly after a Run Command, from an address that is not the machine's or of a kind the identity never performed, show that its token was taken and used.
- In the Azure Activity log, take a successful Run Command event of the rules on this page. Keep the
Caller, the time and the machine from the resource ID. - Read the identities of that machine: the principal ID of its system-assigned managed identity and of each user-assigned identity attached to it. Microsoft documents that a managed identity is a service principal.
- Within 24 hours, take the Activity log events whose
Calleris one of these principal IDs. Microsoft's examples show the object ID inCallerwhen a service principal makes the request. Check it on an event of your own. - Report the events whose
OperationNameValuethe identity did not use in the 30 days before, and the events whoseCallerIpAddressis not an outbound address of the machine's network.
EventsMICROSOFT.COMPUTE/VIRTUALMACHINES/RUNCOMMAND/ACTIONMICROSOFT.COMPUTE/VIRTUALMACHINES/RUNCOMMANDS/WRITE
LimitsThe Activity log has no read operations and no data-plane requests, so an identity used only to read Key Vault secrets or storage data shows in those services' logs, not here. A token used from the machine itself carries the machine's own address. Identities that change resources as part of their job need a baseline per identity. The 24 hours and 30 days are starting points.
Role on a machine granted to a principal that then runs a command on it
An identity that can write role assignments but cannot run code gives a principal it controls a role that carries the Run Command actions, such as Virtual Machine Contributor or a custom role, and then uses it. A role assignment followed by the first Run Command of the new assignee on a machine in the assigned scope connects a permission change to code execution.
- In the Azure Activity log, take the start event of
MICROSOFT.AUTHORIZATION/ROLEASSIGNMENTS/WRITEand readPrincipalId,RoleDefinitionIdandScopefrom the request body inProperties. Confirm the Success event with the same correlation ID. - Within 24 hours, take the Run Command events of the rules on this page whose machine, read from the resource ID, lies under that
Scope. - Match the assignee with the caller: compare
PrincipalIdwith the object identifier claim (http://schemas.microsoft.com/identity/claims/objectidentifier) inClaims, which Microsoft describes as the identity of the user or service principal that made the request. - Report the matches where the assignee had not run a command on that machine in the 30 days before the assignment.
EventsMICROSOFT.AUTHORIZATION/ROLEASSIGNMENTS/WRITEMICROSOFT.COMPUTE/VIRTUALMACHINES/RUNCOMMAND/ACTIONMICROSOFT.COMPUTE/VIRTUALMACHINES/RUNCOMMANDS/WRITE
LimitsA new operator or a new automation identity that receives its role and starts working the same day looks the same, so check change records. A role received through group membership or activated in Privileged Identity Management is not an assignment to the principal itself and is not matched. The 24 hours and 30 days are starting points.
Triage
- Identify the Caller and the target machine from the event, and whether the caller normally administers it.
- On the machine, review the Run Command extension logs and process creation around the event time; for managed Run Command, read the script and output of the run command resource.
- Check for later use of the machine's managed identity (managed identity sign-ins, new Azure Resource Manager activity by its principal).
Response
Once the attack is confirmed:
- Remove the caller's Run Command rights (Virtual Machine Contributor, Contributor or the custom role), revoke its sessions and rotate its credentials.
- Treat the machine as compromised at SYSTEM or root level. Isolate it on the network, preserve it for analysis and plan a rebuild from a trusted image.
- Limit the machine's managed identity. Remove or reduce its role assignments until its sign-ins and Azure activity since the command are reviewed, and undo what was done with it.
- Rotate the credentials that were present on the machine, such as local and service accounts, application secrets, keys and certificates.
- For managed Run Command, save the script and output of the run command resources the caller created, then delete those resources.
- Check the Activity log for Run Command by the same caller on other virtual machines, scale set instances and Arc-enabled servers, and handle each of them the same way.
Mitigation
- Limit Virtual Machine Contributor, Contributor and custom roles that carry Run Command actions; scope them to individual machines and use Privileged Identity Management for just-in-time access.
- Keep managed identities on machines least privileged, so code execution on one machine does not become control of a subscription.
- On Arc-enabled servers that do not need it, block the Run Command extension through the Connected Machine agent's extension block list.
- Collect process creation and script logging on the machines so the executed script can be reviewed.
Frequently asked questions
Does the Activity log contain the script that was run?
No. The Activity log records the request and its caller. The script and its output stay on the machine and, for managed Run Command, in the run command resource. To learn what was executed, read the Run Command extension logs and process creation events on the machine, or the script and output of the run command resource.
Does blocking RDP and SSH to the machine stop Run Command?
No. Run Command executes the script inside the machine through its VM agent, so no network path to the machine is needed. What controls it is Azure authorization, that is the Run Command actions carried by roles such as Virtual Machine Contributor and Contributor.
What is the difference between the action and managed Run Command rules?
One matches action Run Command, which runs a script once as SYSTEM or root. The other matches managed Run Command, which creates a run command resource that can run as a chosen user and is also offered on Arc-enabled servers. Creating the resource runs its script, so both are execution events.
Are Azure Arc-enabled servers outside Azure covered as well?
The rule on managed Run Command includes the operation that the Connected Machine agent serves on Arc-enabled servers. On servers that do not need the feature, the Run Command extension can be blocked through the agent's extension block list.
References
- MITRE ATT&CK T1651: Cloud Administration Command
- Run scripts in your VM by using Run Command
- Run command on Azure Arc-enabled servers
- Azure permissions for Compute
- Azure Activity Log event schema
- AzureActivity table reference
- View activity logs for Azure RBAC changes
- How managed identities for Azure resources work with Azure virtual machines
- Access token claims reference
- Alerts for Resource Manager (Microsoft Defender for Cloud)