> 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/enumeration/az-powershell.md).

# Az PowerShell

{% hint style="warning" %}
Need credentials or some kind or token from here on out.
{% endhint %}

## Az PowerShell

Az PowerShell is a module from Microsoft for managing Azure resources. Please note that if the module is not present on your machine, you can use Install-Module Az command (needs internet).

```powershell
Install-Module Az
```

"The Azure Az PowerShell module is a rollup module. Installing it downloads the generally available Az PowerShell modules, and makes their cmdlets available for use."

Using credentials from command line (PSCredential object and access tokens can be used too)

<pre class="language-powershell"><code class="lang-powershell">$passwd = ConvertTo-SecureString "SuperVeryEasytoGuessPassword@1234" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential("test@defcorphq.onmicrosoft.com", $passwd)
Connect-AzAccount -Credential $creds <a data-footnote-ref href="#user-content-fn-1">-Tenant</a>
</code></pre>

Az PowerShell can enumerate both Entra ID and Azure Resources. All the Entra ID cmdlets have the format *-AzAD.*

```powershell
Get-Command *azad*
Get-AzADUser
```

Cmdlets for other Azure resources have the format *Az*

```powershell
Get-Command *az*
Get-AzResource
```

Find cmdlets for a particular resource. For example, VMs

```powershell
Get-Command *azvm*
Get-Command -Noun *vm* -Verb Get
Get-Command *vm*
```

***

## Az PowerShell — Enumeration

{% stepper %}
{% step %}
Get the information about the current context (Account, Tenant, Subscription etc.)

```powershell
Get-AzContext
```

{% endstep %}

{% step %}
List all available contexts

```powershell
Get-AzContext -ListAvailable
```

{% endstep %}

{% step %}
Enumerate subscriptions accessible by the current user

```powershell
Get-AzSubscription
```

{% endstep %}

{% step %}
Enumerate all resources visible to the current user

```powershell
Get-AzResource
```

{% endstep %}

{% step %}
Enumerate all Azure RBAC role assignments

```powershell
Get-AzRoleAssignment
```

{% endstep %}
{% endstepper %}

***

## Az PowerShell — AAD Users

{% stepper %}
{% step %}
Enumerate all users

```powershell
Get-AzADUser
```

{% endstep %}

{% step %}
Enumerate a specific user

```powershell
Get-AzADUser -UserPrincipalName test@defcorphq.onmicrosoft.com
```

{% endstep %}

{% step %}
Search for a user based on string in first characters of DisplayName (wildcard not supported)

```powershell
Get-AzADUser -SearchString "admin"
```

{% endstep %}

{% step %}
Search for users who contain the word “admin” in their Display name:

```powershell
Get-AzADUser |?{$_.Displayname -match "admin"}
```

{% endstep %}
{% endstepper %}

***

## Az PowerShell — AAD Groups

{% stepper %}
{% step %}
List all groups

```powershell
Get-AzADGroup
```

{% endstep %}

{% step %}
Enumerate a specific group

```powershell
Get-AzADGroup -ObjectId 783a312d-0de2-4490-92e4-539b0e4ee03e
```

{% endstep %}

{% step %}
Search for a group based on string in first characters of DisplayName (wildcard not supported)

```powershell
Get-AzADGroup -SearchString "admin" | fl *
```

{% endstep %}

{% step %}
To search for groups which contain the word "admin" in their name

```powershell
Get-AzADGroup |?{$_.Displayname -match "admin"}
```

{% endstep %}

{% step %}
Get members of a group

```powershell
Get-AzADGroupMember -ObjectId 783a312d-0de2-4490-92e4-
539b0e4ee03e
```

{% endstep %}
{% endstepper %}

***

## Az PowerShell — AAD Apps

{% stepper %}
{% step %}
Get all the application objects registered with the current tenant (visible in App Registrations in Azure portal). An application object is the global representation of an app.

```powershell
Get-AzADApplication
```

{% endstep %}

{% step %}
Get all details about an application

```powershell
Get-AzADApplication -ObjectId a1333e88-1278-41bf-8145-155a069ebed0
```

{% endstep %}

{% step %}
Get an application based on the display name

```powershell
Get-AzADApplication | ?{$_.DisplayName -match "app"}
```

{% endstep %}

{% step %}
The <mark style="color:orange;">Get-AzADAppCredential</mark> will show the applications with an application password, but password value is not shown. List all the apps with an application password.

```powershell
Get-AzADApplication | %{if(Get-AzADAppCredential -ObjectID $_.ID){$_}}
```

{% endstep %}
{% endstepper %}

***

## Az PowerShell — AAD Service Principals

Enumerate Service Principals (visible as Enterprise Applications in Azure Portal). Service principal is local representation for an app in a specific tenant, and it is the security object that has privileges. This is the 'service account'! Service Principals can be assigned Azure roles.

{% stepper %}
{% step %}
Get all service principals

```powershell
Get-AzADServicePrincipal
```

{% endstep %}

{% step %}
Get all details about a service principal

```powershell
Get-AzADServicePrincipal -ObjectId cdddd16e-2611-4442-8f45-053e7c37a264
```

{% endstep %}

{% step %}
Get a service principal based on the display name

```powershell
Get-AzADServicePrincipal | ?{$_.DisplayName -match "app"}
```

{% endstep %}
{% endstepper %}

***

## Az PowerShell — Using Tokens with CLI

Both Az PowerShell and AzureAD modules allow the use of Access tokens for authentication. Usually, tokens contain all the claims (including that for MFA and Conditional Access etc.) so they are useful in bypassing such security controls.

If you are already connected to a tenant, request an access token for resource manager (ARM)

```powershell
Get-AzAccessToken
(Get-AzAccessToken).Token
```

Request an access token for Microsoft Graph to access Entra ID. Supported tokens - AadGraph, AnalysisServices, Arm, Attestation, Batch, DataLake, KeyVault, MSGraph, OperationalInsights, ResourceManager, Storage, Synapse

```powershell
Get-AzAccessToken -ResourceTypeName MSGraph
```

From older versions of Az PowerShell, get a token for Microsoft Graph

```powershell
(Get-AzAccessToken -Resource "https://graph.microsoft.com").Token
```

{% stepper %}
{% step %}
Use the access token

```powershell
Connect-AzAccount -AccountId test@defcorphq.onmicrosoft.com -AccessToken eyJ0eXA..
```

{% endstep %}

{% step %}
Get all details about a service principal

```powershell
Get-AzADServicePrincipal -ObjectId cdddd16e-2611-4442-8f45-053e7c37a264
```

{% endstep %}

{% step %}
Use other access tokens. In the below command, use the one for MSGraph (access token is still required) for accessing Entra ID

```powershell
Connect-AzAccount -AccountId test@defcorphq.onmicrosoft.com -AccessToken eyJ0eXA... -MicrosoftGraphAccessToken eyJ0eXA...
```

{% endstep %}
{% endstepper %}

## Az PowerShell — Stealing Tokens

Az PowerShell (older versions) stores access tokens in clear text in TokenCache.dat in the directory <mark style="color:orange;">C:\Users\[username].Azure.</mark> It also stores ServicePrincipalSecret in clear-text in AzureRmContext.json if a service principal secret is used to authenticate. Another interesting method is to take a process dump of PowerShell and looking for tokens in it! Users can save tokens using <mark style="color:orange;">Save-AzContext</mark>, look out for them! Search for <mark style="color:orange;">Save-AzContext</mark> in PowerShell console history!

Always use Disconnect-AzAccount!!

```powershell
Disconnect-AzAccount
```

If you are already connected to a tenant, request an access token for resource manager (ARM)

```powershell
Get-AzAccessToken
(Get-AzAccessToken).Token
```

Request an access token for Microsoft Graph to access Entra ID. Supported tokens - AadGraph, AnalysisServices, Arm, Attestation, Batch, DataLake, KeyVault, MSGraph, OperationalInsights, ResourceManager, Storage, Synapse

```powershell
Get-AzAccessToken -ResourceTypeName MSGraph
```

From older versions of Az PowerShell, get a token for Microsoft Graph

```powershell
(Get-AzAccessToken -Resource "https://graph.microsoft.com").Token
```

{% stepper %}
{% step %}
Use the access token

```powershell
Connect-AzAccount -AccountId test@defcorphq.onmicrosoft.com -AccessToken eyJ0eXA..
```

{% endstep %}

{% step %}
Get all details about a service principal

```powershell
Get-AzADServicePrincipal -ObjectId cdddd16e-2611-4442-8f45-053e7c37a264
```

{% endstep %}

{% step %}
Use other access tokens. In the below command, use the one for MSGraph (access token is still required) for accessing Entra ID

```powershell
Connect-AzAccount -AccountId test@defcorphq.onmicrosoft.com -AccessToken eyJ0eXA... 
```

{% endstep %}
{% endstepper %}

[^1]: optional, only if you know
