Setting Permissions for Scanning Azure Virtual Machines
Table of Contents
To scan Azure virtual machines by using the Azure Inventory data provider, you need to set additional permissions for subscriptions to be able to use the Run Command feature.
Assigning the Virtual Machine Contributor Role to Your Subscriptions
- Open Subscriptions from the Microsoft Azure home page and go to a subscription that contains the virtual machines to be scanned.
- Open Access Control (IAM) and use the Add button to add role assignment.
- On the Role > Job function roles tab, select the Virtual Machine Contributor role and click Next.
- On the Members tab, choose the Assign access to radio button and select your registered application as a member of this role.
- Click Next and then Review + assign to save your changes.
Now you can see the new role assignment under Access Control (IAM) > Role assignments.
Frequently Asked Questions regarding Security
VM scanning via Azure Run Command
Is Azure VM scanning actually performed via Azure Run Command?
Yes. The workflow sends PUT {AzureApiBaseUrl}/subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Compute/virtualMachines/{vm}/runcommands/{name}?api-version=2023-03-01. This is the managed Run Command resource, authenticated through the service connection.
The request body contains only:
location-
source.scriptUri:Matrix42.Inventory.AzureVMs.ps1(Windows) orMatrix42.Inventory.AzureLinuxVMs.sh(Linux), both from the storage account -
parameters(timestamp, VmId) -
outputBlobUrianderrorBlobUri, pointing to the blob container with the SAS token
Under which permissions are scripts executed inside the VMs?
Matrix42 does not select an account. The request has no runAsUser or runAsPassword field (neither string appears anywhere in the repo), so Azure uses its default context:
- Windows:
NT AUTHORITY\SYSTEM - Linux:
root
Do they run with SYSTEM on Windows and root on Linux by default?
Yes.
If a less privileged account is used, which one and what permissions?
Not applicable. No less privileged account is used, and the workflow does not support running as another user. That would require a workflow change.
Least privilege for Azure Roles
Which Azure permissions are strictly required for VM scanning?
- Read data: Reader at subscription level. This covers
Microsoft.Compute/virtualMachines/read,.../instanceView/read,Microsoft.Compute/skus/read,Microsoft.SqlVirtualMachine/sqlVirtualMachines/readandMicrosoft.ResourceGraph/resources/read. - Run Command: the workflow only issues PUT on
virtualMachines/runcommands, soMicrosoft.Compute/virtualMachines/runCommands/writeis the key action.
Can a restricted custom role be used instead of Virtual Machine Contributor?
Very likely yes. Virtual Machine Contributor is much broader than needed (it also allows start, stop, delete, resize and extension changes).
Minimum permissions the custom role should include (in addition to Reader):
Microsoft.Compute/virtualMachines/runCommands/writeMicrosoft.Compute/virtualMachines/runCommands/read-
Microsoft.Compute/virtualMachines/runCommands/delete(only if cleanup of the run command resource is wanted) Microsoft.Compute/virtualMachines/readMicrosoft.Compute/virtualMachines/instanceView/read
To validate on a test subscription:
- Whether the legacy
Microsoft.Compute/virtualMachines/runCommand/actionis also needed. - Whether the managed run-command write also checks
Microsoft.Compute/virtualMachines/write.
Storage: the SAS token should grant only read on the scripts and write/create on the output container.