mirror of
https://github.com/HackTricks-wiki/hacktricks-cloud.git
synced 2025-12-22 23:26:41 -08:00
424 lines
18 KiB
Markdown
424 lines
18 KiB
Markdown
# Azure Pentesting
|
||
|
||
{% hint style="success" %}
|
||
Learn & practice AWS Hacking:<img src="../../.gitbook/assets/image (1) (1) (1) (1).png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="../../.gitbook/assets/image (1) (1) (1) (1).png" alt="" data-size="line">\
|
||
Learn & practice GCP Hacking: <img src="../../.gitbook/assets/image (2) (1).png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="../../.gitbook/assets/image (2) (1).png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
||
|
||
<details>
|
||
|
||
<summary>Support HackTricks</summary>
|
||
|
||
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
|
||
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks_live)**.**
|
||
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
||
|
||
</details>
|
||
{% endhint %}
|
||
|
||
## Basic Information
|
||
|
||
{% content-ref url="az-basic-information/" %}
|
||
[az-basic-information](az-basic-information/)
|
||
{% endcontent-ref %}
|
||
|
||
## Azure Pentester/Red Team Methodology
|
||
|
||
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:
|
||
|
||
* **Leaks** in github (or similar) - OSINT
|
||
* **Social** Engineering
|
||
* **Password** reuse (password leaks)
|
||
* Vulnerabilities in Azure-Hosted Applications
|
||
* [**Server Side Request Forgery**](https://book.hacktricks.xyz/pentesting-web/ssrf-server-side-request-forgery/cloud-ssrf) with access to metadata endpoint
|
||
* **Local File Read**
|
||
* `/home/USERNAME/.azure`
|
||
* `C:\Users\USERNAME\.azure`
|
||
* 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 **store** **tokens**.\
|
||
Use `Disconnect-AzAccount` to remove them.
|
||
* 3rd parties **breached**
|
||
* **Internal** Employee
|
||
* [**Common Phishing**](https://book.hacktricks.xyz/generic-methodologies-and-resources/phishing-methodology) (credentials or Oauth App)
|
||
* [Device Code Authentication Phishing](az-unauthenticated-enum-and-initial-entry/az-device-code-authentication-phishing.md)
|
||
* [Azure **Password Spraying**](az-unauthenticated-enum-and-initial-entry/az-password-spraying.md)
|
||
|
||
Even if you **haven't compromised any user** inside the Azure tenant you are attacking, you can **gather some information** from it:
|
||
|
||
{% content-ref url="az-unauthenticated-enum-and-initial-entry/" %}
|
||
[az-unauthenticated-enum-and-initial-entry](az-unauthenticated-enum-and-initial-entry/)
|
||
{% endcontent-ref %}
|
||
|
||
{% hint style="info" %}
|
||
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:
|
||
{% endhint %}
|
||
|
||
## Basic Enumeration
|
||
|
||
{% hint style="info" %}
|
||
Remember that the **noisiest** part of the enumeration is the **login**, not the enumeration itself.
|
||
{% endhint %}
|
||
|
||
### SSRF
|
||
|
||
If you found a SSRF in a machine inside Azure check this page for tricks:
|
||
|
||
{% embed url="https://book.hacktricks.xyz/pentesting-web/ssrf-server-side-request-forgery/cloud-ssrf" %}
|
||
|
||
### Bypass Login Conditions
|
||
|
||
<figure><img src="../../.gitbook/assets/image (268).png" alt=""><figcaption></figcaption></figure>
|
||
|
||
In cases where you have some valid credentials but you cannot login, these are some common protections that could be in place:
|
||
|
||
* **IP whitelisting** -- You need to compromise a valid IP
|
||
* **Geo restrictions** -- Find where the user lives or where are the offices of the company and get a IP from the same city (or contry at least)
|
||
* **Browser** -- Maybe only a browser from certain OS (Windows, Linux, Mac, Android, iOS) is allowed. Find out which OS the victim/company uses.
|
||
* You can also try to **compromise Service Principal credentials** as they usually are less limited and its login is less reviewed
|
||
|
||
After bypassing it, you might be able to get back to your initial setup and you will still have access.
|
||
|
||
### Subdomain Takeover
|
||
|
||
* [https://godiego.co/posts/STO-Azure/](https://godiego.co/posts/STO-Azure/)
|
||
|
||
### Whoami
|
||
|
||
{% hint style="danger" %}
|
||
Learn **how to install** az cli, AzureAD and Az PowerShell in the [**Az - Entra ID**](az-services/az-azuread.md) section.
|
||
{% endhint %}
|
||
|
||
One of the first things you need to know is **who you are** (in which environment you are):
|
||
|
||
{% tabs %}
|
||
{% tab title="az cli" %}
|
||
```bash
|
||
az account list
|
||
az account tenant list # Current tenant info
|
||
az account subscription list # Current subscription info
|
||
az ad signed-in-user show # Current signed-in user
|
||
az ad signed-in-user list-owned-objects # Get owned objects by current user
|
||
az account management-group list #Not allowed by default
|
||
```
|
||
{% endtab %}
|
||
|
||
{% tab title="AzureAD" %}
|
||
```powershell
|
||
#Get the current session state
|
||
Get-AzureADCurrentSessionInfo
|
||
#Get details of the current tenant
|
||
Get-AzureADTenantDetail
|
||
```
|
||
{% endtab %}
|
||
|
||
{% tab title="Az PowerShell" %}
|
||
```powershell
|
||
# Get the information about the current context (Account, Tenant, Subscription etc.)
|
||
Get-AzContext
|
||
# List all available contexts
|
||
Get-AzContext -ListAvailable
|
||
# Enumerate subscriptions accessible by the current user
|
||
Get-AzSubscription
|
||
#Get Resource group
|
||
Get-AzResourceGroup
|
||
# Enumerate all resources visible to the current user
|
||
Get-AzResource
|
||
# Enumerate all Azure RBAC role assignments
|
||
Get-AzRoleAssignment # For all users
|
||
Get-AzRoleAssignment -SignInName test@corp.onmicrosoft.com # For current user
|
||
```
|
||
{% endtab %}
|
||
{% endtabs %}
|
||
|
||
{% hint style="danger" %}
|
||
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**.
|
||
|
||
You can get the same info in the **web console** going to [https://portal.azure.com/#view/HubsExtension/BrowseAll](https://portal.azure.com/#view/HubsExtension/BrowseAll) or searching for "All resources"
|
||
{% endhint %}
|
||
|
||
### ENtra ID Enumeration
|
||
|
||
By default, any user should have **enough permissions to enumerate** things such us, users, groups, roles, service principals... (check [default AzureAD permissions](az-basic-information/#default-user-permissions)).\
|
||
You can find here a guide:
|
||
|
||
{% content-ref url="az-services/az-azuread.md" %}
|
||
[az-azuread.md](az-services/az-azuread.md)
|
||
{% endcontent-ref %}
|
||
|
||
{% hint style="info" %}
|
||
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.**
|
||
{% endhint %}
|
||
|
||
## 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.
|
||
|
||
## Debug | MitM az cli
|
||
|
||
Using the parameter **`--debug`** it's possible to see all the requests the tool **`az`** is sending:
|
||
|
||
```bash
|
||
az account management-group list --output table --debug
|
||
```
|
||
|
||
In order to do a **MitM** to the tool and **check all the requests** it's sending manually you can do:
|
||
|
||
{% tabs %}
|
||
{% tab title="Bash" %}
|
||
```bash
|
||
export ADAL_PYTHON_SSL_NO_VERIFY=1
|
||
export AZURE_CLI_DISABLE_CONNECTION_VERIFICATION=1
|
||
export HTTPS_PROXY="http://127.0.0.1:8080"
|
||
export HTTP_PROXY="http://127.0.0.1:8080"
|
||
|
||
# If this is not enough
|
||
# Download the certificate from Burp and convert it into .pem format
|
||
# And export the following env variable
|
||
openssl x509 -in ~/Downloads/cacert.der -inform DER -out ~/Downloads/cacert.pem -outform PEM
|
||
export REQUESTS_CA_BUNDLE=/Users/user/Downloads/cacert.pem
|
||
```
|
||
{% endtab %}
|
||
|
||
{% tab title="PS" %}
|
||
```bash
|
||
$env:ADAL_PYTHON_SSL_NO_VERIFY=1
|
||
$env:AZURE_CLI_DISABLE_CONNECTION_VERIFICATION=1
|
||
$env:HTTPS_PROXY="http://127.0.0.1:8080"
|
||
$env:HTTP_PROXY="http://127.0.0.1:8080"
|
||
```
|
||
{% endtab %}
|
||
{% endtabs %}
|
||
|
||
## Automated Recon Tools
|
||
|
||
### [**ROADRecon**](https://github.com/dirkjanm/ROADtools)
|
||
|
||
```powershell
|
||
cd ROADTools
|
||
pipenv shell
|
||
roadrecon auth -u test@corp.onmicrosoft.com -p "Welcome2022!"
|
||
roadrecon gather
|
||
roadrecon gui
|
||
```
|
||
|
||
### [Monkey365](https://github.com/silverhack/monkey365)
|
||
|
||
{% code overflow="wrap" %}
|
||
```powershell
|
||
Import-Module monkey365
|
||
Get-Help Invoke-Monkey365
|
||
Get-Help Invoke-Monkey365 -Detailed
|
||
Invoke-Monkey365 -IncludeEntraID -ExportTo HTML -Verbose -Debug -InformationAction Continue
|
||
Invoke-Monkey365 - Instance Azure -Analysis All -ExportTo HTML
|
||
```
|
||
{% endcode %}
|
||
|
||
### [**Stormspotter**](https://github.com/Azure/Stormspotter)
|
||
|
||
```powershell
|
||
# Start Backend
|
||
cd stormspotter\backend\
|
||
pipenv shell
|
||
python ssbackend.pyz
|
||
|
||
# Start Front-end
|
||
cd stormspotter\frontend\dist\spa\
|
||
quasar.cmd serve -p 9091 --history
|
||
|
||
# Run Stormcollector
|
||
cd stormspotter\stormcollector\
|
||
pipenv shell
|
||
az 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)
|
||
```
|
||
|
||
### [**AzureHound**](https://github.com/BloodHoundAD/AzureHound)
|
||
|
||
```powershell
|
||
# 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 $creds
|
||
|
||
Import-Module AzureAD\AzureAD.psd1
|
||
Connect-AzureAD -Credential $creds
|
||
|
||
# Launch AzureHound
|
||
. AzureHound\AzureHound.ps1
|
||
Invoke-AzureHound -Verbose
|
||
|
||
# Simple queries
|
||
## All Azure Users
|
||
MATCH (n:AZUser) return n.name
|
||
## All Azure Applications
|
||
MATCH (n:AZApp) return n.objectid
|
||
## All Azure Devices
|
||
MATCH (n:AZDevice) return n.name
|
||
## All Azure Groups
|
||
MATCH (n:AZGroup) return n.name
|
||
## All Azure Key Vaults
|
||
MATCH (n:AZKeyVault) return n.name
|
||
## All Azure Resource Groups
|
||
MATCH (n:AZResourceGroup) return n.name
|
||
## All Azure Service Principals
|
||
MATCH (n:AZServicePrincipal) return n.objectid
|
||
## All Azure Virtual Machines
|
||
MATCH (n:AZVM) return n.name
|
||
## All Principals with the ‘Contributor’ role
|
||
MATCH p = (n)-[r:AZContributor]->(g) RETURN p
|
||
|
||
# Advanced queries
|
||
## Get Global Admins
|
||
MATCH p =(n)-[r:AZGlobalAdmin*1..]->(m) RETURN p
|
||
## Owners of Azure Groups
|
||
MATCH p = (n)-[r:AZOwns]->(g:AZGroup) RETURN p
|
||
## All Azure Users and their Groups
|
||
MATCH p=(m:AZUser)-[r:MemberOf]->(n) WHERE NOT m.objectid CONTAINS 'S-1-5' RETURN p
|
||
## Privileged Service Principals
|
||
MATCH p = (g:AZServicePrincipal)-[r]->(n) RETURN p
|
||
## Owners of Azure Applications
|
||
MATCH p = (n)-[r:AZOwns]->(g:AZApp) RETURN p
|
||
## Paths to VMs
|
||
MATCH p = (n)-[r]->(g: AZVM) RETURN p
|
||
## Paths to KeyVault
|
||
MATCH p = (n)-[r]->(g:AZKeyVault) RETURN p
|
||
## Paths to Azure Resource Group
|
||
MATCH p = (n)-[r]->(g:AZResourceGroup) RETURN p
|
||
## On-Prem users with edges to Azure
|
||
MATCH 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 AD
|
||
MATCH (n:Group) WHERE n.objectid CONTAINS 'S-1-5' AND n.azsyncid IS NOT NULL RETURN n
|
||
```
|
||
|
||
### [Azucar](https://github.com/nccgroup/azucar)
|
||
|
||
```bash
|
||
# You should use an account with at least read-permission on the assets you want to access
|
||
git clone https://github.com/nccgroup/azucar.git
|
||
PS> Get-ChildItem -Recurse c:\Azucar_V10 | Unblock-File
|
||
|
||
PS> .\Azucar.ps1 -AuthMode UseCachedCredentials -Verbose -WriteLog -Debug -ExportTo PRINT
|
||
PS> .\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 username
|
||
PS> .\Azucar.ps1 -ResolveTenantUserName user@company.com
|
||
```
|
||
|
||
### [**MicroBurst**](https://github.com/NetSPI/MicroBurst)
|
||
|
||
```
|
||
Import-Module .\MicroBurst.psm1
|
||
Import-Module .\Get-AzureDomainInfo.ps1
|
||
Get-AzureDomainInfo -folder MicroBurst -Verbose
|
||
```
|
||
|
||
### [**PowerZure**](https://github.com/hausec/PowerZure)
|
||
|
||
```powershell
|
||
Connect-AzAccount
|
||
ipmo C:\Path\To\Powerzure.psd1
|
||
Get-AzureTarget
|
||
|
||
# Reader
|
||
$ Get-Runbook, Get-AllUsers, Get-Apps, Get-Resources, Get-WebApps, Get-WebAppDetails
|
||
|
||
# Contributor
|
||
$ Execute-Command -OS Windows -VM Win10Test -ResourceGroup Test-RG -Command "whoami"
|
||
$ Execute-MSBuild -VM Win10Test -ResourceGroup Test-RG -File "build.xml"
|
||
$ Get-AllSecrets # AllAppSecrets, AllKeyVaultContents
|
||
$ Get-AvailableVMDisks, Get-VMDisk # Download a virtual machine's disk
|
||
|
||
# Owner
|
||
$ Set-Role -Role Contributor -User test@contoso.com -Resource Win10VMTest
|
||
|
||
# Administrator
|
||
$ Create-Backdoor, Execute-Backdoor
|
||
```
|
||
|
||
### [**GraphRunner**](https://github.com/dafthack/GraphRunner/wiki/Invoke%E2%80%90GraphRunner)
|
||
|
||
```powershell
|
||
|
||
#Get-GraphTokens
|
||
#A good place to start is to authenticate with the Get-GraphTokens module. This module will launch a device-code login, allowing you to authenticate the session from a browser session. Access and refresh tokens will be written to the global $tokens variable. To use them with other GraphRunner modules use the Tokens flag (Example. Invoke-DumpApps -Tokens $tokens)
|
||
Import-Module .\GraphRunner.ps1
|
||
Get-GraphTokens
|
||
|
||
#Invoke-GraphRecon
|
||
#This module gathers information about the tenant including the primary contact info, directory sync settings, and user settings such as if users have the ability to create apps, create groups, or consent to apps.
|
||
Invoke-GraphRecon -Tokens $tokens -PermissionEnum
|
||
|
||
#Invoke-DumpCAPS
|
||
#A module to dump conditional access policies from a tenant.
|
||
Invoke-GraphRecon -Tokens $tokens -PermissionEnum
|
||
|
||
#Invoke-DumpCAPS
|
||
#A module to dump conditional access policies from a tenant.
|
||
Invoke-DumpCAPS -Tokens $tokens -ResolveGuids
|
||
|
||
#Invoke-DumpApps
|
||
#This module helps identify malicious app registrations. It will dump a list of Azure app registrations from the tenant including permission scopes and users that have consented to the apps. Additionally, it will list external apps that are not owned by the current tenant or by Microsoft's main app tenant. This is a good way to find third-party external apps that users may have consented to.
|
||
Invoke-DumpApps -Tokens $tokens
|
||
|
||
#Get-AzureADUsers
|
||
#Gather the full list of users from the directory.
|
||
Get-AzureADUsers -Tokens $tokens -OutFile users.txt
|
||
|
||
#Get-SecurityGroups
|
||
#Create a list of security groups along with their members.
|
||
Get-SecurityGroups -AccessToken $tokens.access_token
|
||
|
||
G#et-UpdatableGroups
|
||
#Gets groups that may be able to be modified by the current user
|
||
Get-UpdatableGroups -Tokens $tokens
|
||
|
||
#Get-DynamicGroups
|
||
#Finds dynamic groups and displays membership rules
|
||
Get-DynamicGroups -Tokens $tokens
|
||
|
||
#Get-SharePointSiteURLs
|
||
#Gets a list of SharePoint site URLs visible to the current user
|
||
Get-SharePointSiteURLs -Tokens $tokens
|
||
|
||
#Invoke-GraphOpenInboxFinder
|
||
#This module attempts to locate mailboxes in a tenant that have allowed other users to read them. By providing a userlist the module will attempt to access the inbox of each user and display if it was successful. The access token needs to be scoped to Mail.Read.Shared or Mail.ReadWrite.Shared for this to work.
|
||
Invoke-GraphOpenInboxFinder -Tokens $tokens -Userlist users.txt
|
||
|
||
#Get-TenantID
|
||
#This module attempts to gather a tenant ID associated with a domain.
|
||
Get-TenantID -Domain
|
||
|
||
#Invoke-GraphRunner
|
||
#Runs Invoke-GraphRecon, Get-AzureADUsers, Get-SecurityGroups, Invoke-DumpCAPS, Invoke-DumpApps, and then uses the default_detectors.json file to search with Invoke-SearchMailbox, Invoke-SearchSharePointAndOneDrive, and Invoke-SearchTeams.
|
||
Invoke-GraphRunner -Tokens $tokens
|
||
```
|
||
|
||
{% hint style="success" %}
|
||
Learn & practice AWS Hacking:<img src="../../.gitbook/assets/image (1) (1) (1) (1).png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="../../.gitbook/assets/image (1) (1) (1) (1).png" alt="" data-size="line">\
|
||
Learn & practice GCP Hacking: <img src="../../.gitbook/assets/image (2) (1).png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="../../.gitbook/assets/image (2) (1).png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
||
|
||
<details>
|
||
|
||
<summary>Support HackTricks</summary>
|
||
|
||
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
|
||
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks_live)**.**
|
||
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
||
|
||
</details>
|
||
{% endhint %}
|