1. Resources
  2. Detection rules
  3. Code Execution Through a VM Extension

Code Execution Through a VM Extension detection rules

Someone installs or updates a virtual machine extension, such as the Custom Script Extension, to run code inside an Azure VM, a scale set or an Arc-enabled server through its agent, with no network path to the machine.

Platform
Azure
Severity
High
Log source
Azure Activity log
Last updated
MITRE ATT&CK technique
T1651Cloud Administration Command
Tactic
Execution
Rule formats
Sigmaand queries for: SplunkMicrosoft SentinelIBM QRadar
What an attacker needs
An identity with the extension write action on the machine, through Virtual Machine Contributor, Contributor, Owner or a custom role that carries it.
Impact
Code execution in a privileged context on the machine, plus the permissions of its managed identity, which often reach well beyond the machine itself.
Response
Remove the caller's extension rights and revoke its sessions, save the extension's settings and logs, then isolate the machine and treat it as compromised.
Mitigation
Limit the roles that carry the extension write actions, and allow only approved extensions with Azure Policy or the Arc agent's allow list.
Operations
MICROSOFT.COMPUTE/VIRTUALMACHINES/EXTENSIONS/WRITEMICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/EXTENSIONS/WRITEMICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/VIRTUALMACHINES/EXTENSIONS/WRITEMICROSOFT.HYBRIDCOMPUTE/MACHINES/EXTENSIONS/WRITE
Threat groups
APT29VOID MANTICORE

How the attack works

VM extensions are small applications that the Azure platform installs and runs inside a machine through its VM agent, for configuration, monitoring and security. Some of them exist to run code: the Custom Script Extension downloads and runs scripts, and on Windows it runs under the LocalSystem account. An extension is created or updated with Microsoft.Compute/virtualMachines/extensions/write, with matching operations for scale sets and their instances, and with Microsoft.HybridCompute/machines/extensions/write on Azure Arc-enabled servers. Virtual Machine Contributor includes the Compute actions, and Contributor includes all of them.

For an attacker this is the same step as Run Command: control-plane rights on a machine become code execution inside it, with access to its data, its network and its managed identity. Microsoft's guidance for Arc-enabled servers says that extensions execute in privileged contexts and that the Custom Script Extension could be used by attackers to execute commands remotely. The write is recorded in the Azure Activity log with its caller, but the event does not say what the extension runs. The publisher, type and public settings are on the extension resource, protected settings are encrypted and decrypted only inside the machine, and the script and its output stay in the extension's folders on the machine.

What an attacker needs

  • An identity with Microsoft.Compute/virtualMachines/extensions/write (Virtual Machine Contributor, Contributor, Owner or a custom role), the matching scale set action, or Microsoft.HybridCompute/machines/extensions/write for Arc-enabled servers.
  • A machine with a running VM agent, on which no Azure Policy assignment and no agent allow list blocks the extension.

Impact

Code execution in a privileged context on the machine (LocalSystem for the Custom Script Extension on Windows), 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 Extension Created or Updated

Detects a successful create or update of an extension on an Azure virtual machine, a virtual machine scale set, a scale set instance or an Azure Arc-enabled server. Extensions run inside the machine through its agent, and the Custom Script Extension runs a script of the caller's choice. The Activity log event does not say which extension was written, and monitoring, security and backup extensions use the same operation, so the rule matches every extension write. Use it for review of unexpected callers and for correlation, not as an alert on its own. An extension that is set inside a scale set's own configuration (extensionProfile) arrives as a scale set write and is not matched.

LevelLowStatusExperimentalLog sourceAzure Activity log
title: Azure VM Extension Created or Updated
id: 1d4dfcd8-fad1-46d8-b5b2-83924c057ae4
status: experimental
description: >-
  Detects a successful create or update of an extension on an Azure virtual
  machine, a virtual machine scale set, a scale set instance or an Azure
  Arc-enabled server. Extensions run inside the machine through its agent, and
  the Custom Script Extension runs a script of the caller's choice. The
  Activity log event does not say which extension was written, and monitoring,
  security and backup extensions use the same operation, so the rule matches
  every extension write. Use it for review of unexpected callers and for
  correlation, not as an alert on its own. An extension that is set inside a
  scale set's own configuration (extensionProfile) arrives as a scale set
  write and is not matched.
references:
  - https://attack.mitre.org/techniques/T1651/
  - https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/overview
  - https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows
  - https://learn.microsoft.com/en-us/azure/role-based-access-control/permissions/compute
  - https://learn.microsoft.com/en-us/azure/role-based-access-control/permissions/hybrid-multicloud
  - https://learn.microsoft.com/en-us/azure/templates/microsoft.compute/virtualmachinescalesets
author: Forestall
date: 2026-11-16
tags:
  - attack.execution
  - attack.t1651
logsource:
  product: azure
  service: activitylogs
detection:
  selection:
    OperationNameValue:
      - MICROSOFT.COMPUTE/VIRTUALMACHINES/EXTENSIONS/WRITE
      - MICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/EXTENSIONS/WRITE
      - MICROSOFT.COMPUTE/VIRTUALMACHINESCALESETS/VIRTUALMACHINES/EXTENSIONS/WRITE
      - MICROSOFT.HYBRIDCOMPUTE/MACHINES/EXTENSIONS/WRITE
    ActivityStatusValue:
      - Success
      - Succeeded
  condition: selection
falsepositives:
  - Monitoring, antimalware, disk encryption and backup extensions installed by provisioning pipelines, Azure Policy and Microsoft Defender for Cloud. Exclude their deployment identities in a filter_ selection by Caller once confirmed.
  - Administrators who use the Custom Script Extension for planned configuration. Correlate with change records, and read the extension's publisher, type and settings on the machine.
level: low

False positives

  • Monitoring, antimalware, disk encryption and backup extensions installed by provisioning pipelines, Azure Policy and Microsoft Defender for Cloud. Exclude their deployment identities in a filter_ selection by Caller once confirmed.
  • Administrators who use the Custom Script Extension for planned configuration. Correlate with change records, and read the extension's publisher, type and settings on the machine.

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.

Extension written and removed again by the same caller shortly after

Detection hypothesisSequence of events

An extension that runs a script has done its work once the script has run, and deleting it takes the extension resource and its settings off the machine. A write and a delete of the same extension by the same caller within a short time is how a one-off script is run and then cleaned up. Microsoft Defender for Cloud lists an unusual deletion of a custom script extension among its alerts for VM extensions.

  1. In the Azure Activity log, take a successful extension write of the rule on this page. Keep the Caller, the time and the resource ID, which names the machine and the extension.
  2. Within 4 hours, look for a successful delete of the same resource ID with the delete operation that matches the write, such as MICROSOFT.COMPUTE/VIRTUALMACHINES/EXTENSIONS/DELETE or MICROSOFT.HYBRIDCOMPUTE/MACHINES/EXTENSIONS/DELETE. Scale sets and their instances have their own.
  3. Keep the pairs where the same Caller made both requests. Collect the extension's logs and downloaded files from the machine, because the extension resource with its settings is gone.

EventsMICROSOFT.COMPUTE/VIRTUALMACHINES/EXTENSIONS/WRITEMICROSOFT.COMPUTE/VIRTUALMACHINES/EXTENSIONS/DELETEMICROSOFT.HYBRIDCOMPUTE/MACHINES/EXTENSIONS/WRITEMICROSOFT.HYBRIDCOMPUTE/MACHINES/EXTENSIONS/DELETE

LimitsPipelines that remove a script extension to run a new script on the same machine, and image build processes, look the same and have to be excluded by caller. A delete made later or by another identity does not match. The 4 hours are a starting point.

Role on a machine granted to a principal that then writes an extension on it

Detection hypothesisSequence of eventsJoins two logsNeeds a baseline

An identity that can write role assignments but cannot run code gives a principal it controls a role that carries the extension write actions, such as Virtual Machine Contributor or a custom role, and then uses it. A role assignment followed by the first extension write of the new assignee on a machine in the assigned scope connects a permission change to code execution.

  1. In the Azure Activity log, take the start event of MICROSOFT.AUTHORIZATION/ROLEASSIGNMENTS/WRITE and read PrincipalId, RoleDefinitionId and Scope from the request body in Properties. Confirm the Success event with the same correlation ID.
  2. Within 24 hours, take the extension writes of the rule on this page whose machine, read from the resource ID, lies under that Scope.
  3. Match the assignee with the caller: compare PrincipalId with the object identifier claim (http://schemas.microsoft.com/identity/claims/objectidentifier) in Claims, which Microsoft describes as the identity of the user or service principal that made the request.
  4. Report the matches where the assignee had not written an extension on that machine in the 30 days before the assignment.

EventsMICROSOFT.AUTHORIZATION/ROLEASSIGNMENTS/WRITEMICROSOFT.COMPUTE/VIRTUALMACHINES/EXTENSIONS/WRITEMICROSOFT.HYBRIDCOMPUTE/MACHINES/EXTENSIONS/WRITE

LimitsA new operator or a new deployment 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 deploys extensions to it. Provisioning pipelines, Azure Policy and Microsoft Defender for Cloud do.
  • Read the extension resource on the machine. Its publisher and type say what it is, and its public settings may show the command and the script location. The types CustomScriptExtension (publisher Microsoft.Compute) and CustomScript (publisher Microsoft.Azure.Extensions) run a script.
  • On the machine, review the extension's logs and downloaded files and the process creation events around the event time. Microsoft documents the locations for the Custom Script Extension, for example the Plugins folders under C:\WindowsAzure\Logs and C:\Packages on Windows and /var/lib/waagent/custom-script on Linux.
  • Check for later use of the machine's managed identity, such as managed identity sign-ins and new Azure Resource Manager activity by its principal.

Response

Once the attack is confirmed:

  1. Remove the caller's extension rights (Virtual Machine Contributor, Contributor or the custom role), revoke its sessions and rotate its credentials.
  2. Save the evidence before you clean up. Export the extension resource with its settings, and copy the extension's logs and downloaded files from the machine, then delete the extension.
  3. Treat the machine as compromised at the privilege level of its agent. Isolate it on the network, preserve it for analysis and plan a rebuild from a trusted image.
  4. Limit the machine's managed identity. Remove or reduce its role assignments until its sign-ins and Azure activity since the extension ran are reviewed, and undo what was done with it.
  5. Rotate the credentials that were present on the machine, such as local and service accounts, application secrets, keys and certificates.
  6. Check the Activity log for extension writes and Run Command by the same caller on other virtual machines, scale sets and Arc-enabled servers, and handle each of them the same way.

Mitigation

  • Limit Virtual Machine Contributor, Contributor and custom roles that carry the extension write actions, scope them to individual machines and use Privileged Identity Management for just-in-time access.
  • Allow only the extensions you use. Assign the built-in Azure Policy definition Only approved VM extensions should be installed, or a policy that denies the Custom Script Extension where nobody needs it.
  • On Arc-enabled servers, set an extension allow list in the Connected Machine agent or run the agent in monitor mode. The lists are configured on the server and cannot be overridden from Azure.
  • Keep managed identities on machines least privileged, so code execution on one machine does not become control of a subscription.
  • Collect process creation and script logging on the machines so that executed code can be reviewed.

Frequently asked questions

Does the Activity log show which extension was installed or what it ran?

Not in a field you can rely on. The rule reads the operation, its status and the caller, and Microsoft's Activity log schema documents no field for the type or the settings of an extension. Read the extension resource on the machine. Its publisher and type identify it, and its public settings may contain the command. Protected settings are encrypted and decrypted only inside the machine, so a command passed there has to be recovered from the logs on the machine.

Why is the rule's level low when the attack is rated high?

The same operation installs monitoring agents, antimalware, disk encryption and backup extensions, often automatically through Azure Policy or Microsoft Defender for Cloud. The event alone cannot tell these from an extension that runs a script. Allow-list the identities that deploy your standard extensions and review the writes made by anyone else, starting with the extension's publisher and type.

How does this differ from Run Command?

The result is the same, code running inside the machine through its agent, but the permission and the log record differ. Run Command is requested with the `runCommand/action` or `runCommands/write` operations. An extension is written with `extensions/write`, so a role restriction or a detection that covers only the Run Command operations does not cover it.

Can extensions be blocked on machines that should never run scripts this way?

Yes. For Azure VMs, Azure Policy can deny extensions, for example with the built-in definition Only approved VM extensions should be installed. For Arc-enabled servers, the Connected Machine agent supports allow lists and block lists that are configured on the server itself. Microsoft documents that nobody, not even a user with Owner permissions in Azure, can override them by trying to install an unauthorized extension.

References

We respect your privacy

We use cookies to keep this site secure and working properly. With your permission, we also use optional cookies to understand usage and improve the experience. Cookie Policy

You can change your choice at any time.