In order to audit an AZURE environment it's very important to know: which services are being used, what is being exposed, who has access to what, and how are internal Azure services and external services connected.
From a Red Team point of view, the first step to compromise an Azure environment is to manage to obtain some credentials for Azure AD. Here you have some ideas on how to do that:
The file accessTokens.json in az cli before 2.30 - Jan2022 - stored access tokens in clear text
The file azureProfile.json contains info about logged user.
az logout removes the token.
Older versions of Az PowerShell stored access tokens in clear text in TokenCache.dat. It also stores ServicePrincipalSecret in clear-text in AzureRmContext.json. The cmdlet Save-AzContext can be used to storetokens.
Use Disconnect-AzAccount to remove them.
After you have managed to obtain credentials, you need to know to who do those creds belong, and what they have access to, so you need to perform some basic enumeration:
Basic Enumeration
Remember that the noisiest part of the enumeration is the login, not the enumeration itself.
SSRF
If you found a SSRF in a machine inside Azure check this page for tricks:
Learn how to install az cli, AzureAD and Az PowerShell in the Az - AzureAD section.
One of the first things you need to know is who you are (in which environment you are):
azaccountlistazaccounttenantlist# Current tenant infoazaccountsubscriptionlist# Current subscription infoazadsigned-in-usershow# Current signed-in userazadsigned-in-userlist-owned-objects# Get owned objects by current userazaccountmanagement-grouplist#Not allowed by default
#Get the current session stateGet-AzureADCurrentSessionInfo#Get details of the current tenantGet-AzureADTenantDetail
# Get the information about the current context (Account, Tenant, Subscription etc.)Get-AzContext# List all available contextsGet-AzContext-ListAvailable# Enumerate subscriptions accessible by the current userGet-AzSubscription#Get Resource groupGet-AzResourceGroup# Enumerate all resources visible to the current userGet-AzResource# Enumerate all Azure RBAC role assignmentsGet-AzRoleAssignment# For all usersGet-AzRoleAssignment-SignInName test@corp.onmicrosoft.com # For current user
Oone of the most important commands to enumerate Azure is Get-AzResource from Az PowerShell as it lets you know the resources your current user has visibility over.
By default, any user should have enough permissions to enumerate things such us, users, groups, roles, service principals... (check default AzureAD permissions).
You can find here a guide:
Now that you have some information about your credentials (and if you are a red team hopefully you haven't been detected). It's time to figure out which services are being used in the environment.
In the following section you can check some ways to enumerate some common services.
Service Principal and Access Policy
An Azure service can have a System Identity (of the service itself) or use a User Assigned Managed Identity. This Identity can have Access Policy to, for example, a KeyVault to read secrets. These Access Policies should be restricted (least privilege principle), but might have more permissions than required. Typically an App Service would use KeyVault to retrieve secrets and certificates.
So it is useful to explore these identities.
App Service SCM
Kudu console to log in to the App Service 'container'.
Webshell
Use portal.azure.com and select the shell, or use shell.azure.com, for a bash or powershell. The 'disk' of this shell are stored as an image file in a storage-account.
Azure DevOps
Azure DevOps is separate from Azure. It has repositories, pipelines (yaml or release), boards, wiki, and more. Variable Groups are used to store variable values and secrets.
Import-Module monkey365Get-HelpInvoke-Monkey365Get-HelpInvoke-Monkey365-DetailedInvoke-Monkey365-IncludeAzureActiveDirectory -ExportTo HTML -Verbose -Debug -InformationAction ContinueInvoke-Monkey365- Instance Azure -Analysis All -ExportTo HTML
# Start Backendcd stormspotter\backend\pipenv shellpython ssbackend.pyz# Start Front-endcd stormspotter\frontend\dist\spa\quasar.cmd serve -p 9091--history# Run Stormcollectorcd stormspotter\stormcollector\pipenv shellaz login -u test@corp.onmicrosoft.com -p Welcome2022!python stormspotter\stormcollector\sscollector.pyz cli# This will generate a .zip file to upload in the frontend (127.0.0.1:9091)
# You need to use the Az PowerShell and Azure AD modules:$passwd =ConvertTo-SecureString"Welcome2022!"-AsPlainText -Force$creds =New-Object System.Management.Automation.PSCredential ("test@corp.onmicrosoft.com", $passwd)Connect-AzAccount-Credential $credsImport-Module AzureAD\AzureAD.psd1Connect-AzureAD-Credential $creds# Launch AzureHound. AzureHound\AzureHound.ps1Invoke-AzureHound-Verbose# Simple queries## All Azure UsersMATCH (n:AZUser) return n.name## All Azure ApplicationsMATCH (n:AZApp) return n.objectid## All Azure DevicesMATCH (n:AZDevice) return n.name## All Azure GroupsMATCH (n:AZGroup) return n.name## All Azure Key VaultsMATCH (n:AZKeyVault) return n.name## All Azure Resource GroupsMATCH (n:AZResourceGroup) return n.name## All Azure Service PrincipalsMATCH (n:AZServicePrincipal) return n.objectid## All Azure Virtual MachinesMATCH (n:AZVM) return n.name## All Principals with the ‘Contributor’ roleMATCH p = (n)-[r:AZContributor]->(g) RETURN p# Advanced queries## Get Global AdminsMATCH p =(n)-[r:AZGlobalAdmin*1..]->(m) RETURN p## Owners of Azure GroupsMATCH p = (n)-[r:AZOwns]->(g:AZGroup) RETURN p## All Azure Users and their GroupsMATCH p=(m:AZUser)-[r:MemberOf]->(n) WHERE NOT m.objectid CONTAINS 'S-1-5'RETURN p## Privileged Service PrincipalsMATCH p = (g:AZServicePrincipal)-[r]->(n) RETURN p## Owners of Azure ApplicationsMATCH p = (n)-[r:AZOwns]->(g:AZApp) RETURN p## Paths to VMsMATCH p = (n)-[r]->(g: AZVM) RETURN p## Paths to KeyVaultMATCH p = (n)-[r]->(g:AZKeyVault) RETURN p## Paths to Azure Resource GroupMATCH p = (n)-[r]->(g:AZResourceGroup) RETURN p## On-Prem users with edges to AzureMATCH p=(m:User)-[r:AZResetPassword|AZOwns|AZUserAccessAdministrator|AZContributor|AZAddMembers|AZGlobalAdmin|AZVMContributor|AZOwnsAZAvereContributor]->(n) WHERE m.objectid CONTAINS 'S-1-5-21' RETURN p
## All Azure AD Groups that are synchronized with On-Premise ADMATCH (n:Group) WHERE n.objectid CONTAINS 'S-1-5' AND n.azsyncid IS NOT NULL RETURN n
# You should use an account with at least read-permission on the assets you want to accessgitclonehttps://github.com/nccgroup/azucar.gitPS> Get-ChildItem-Recursec:\Azucar_V10|Unblock-FilePS> .\Azucar.ps1-AuthModeUseCachedCredentials-Verbose-WriteLog-Debug-ExportToPRINTPS> .\Azucar.ps1 -ExportTo CSV,JSON,XML,EXCEL -AuthMode Certificate_Credentials -Certificate C:\AzucarTest\server.pfx -ApplicationId 00000000-0000-0000-0000-000000000000 -TenantID 00000000-0000-0000-0000-000000000000
PS> .\Azucar.ps1 -ExportTo CSV,JSON,XML,EXCEL -AuthMode Certificate_Credentials -Certificate C:\AzucarTest\server.pfx -CertFilePassword MySuperP@ssw0rd! -ApplicationId 00000000-0000-0000-0000-000000000000 -TenantID 00000000-0000-0000-0000-000000000000
# resolve the TenantID for an specific usernamePS> .\Azucar.ps1-ResolveTenantUserNameuser@company.com