Azure Pentesting

htARTE(HackTricks AWS Red Team Expert)でAWSハッキングをゼロからヒーローまで学ぶ htARTE(HackTricks AWS Red Team Expert)

HackTricks をサポートする他の方法:

Azure ペンテスト

基本情報

pageAz - Basic Information

Azure ペンテスター/Red Team メソドロジー

AZURE 環境を監査するためには、使用されているサービス公開されているもの誰が何にアクセス権を持っているか、および内部 Azure サービスと外部サービスがどのように接続されているかを知ることが非常に重要です。

Red Team の観点から、Azure 環境を侵害する最初のステップは、Azure AD の一部の資格情報を取得することです。以下は、それを行うためのいくつかのアイデアです:

  • github(または類似の)での情報漏洩 - OSINT

  • ソーシャルエンジニアリング

  • パスワード再利用(パスワード漏洩)

  • Azure ホストされたアプリケーションの脆弱性

  • サーバーサイドリクエストフォージェリ メタデータエンドポイントへのアクセス

  • ローカルファイル読み取り

  • /home/USERNAME/.azure

  • C:\Users\USERNAME\.azure

  • 2022年1月までのaz cliの**accessTokens.jsonファイル - 明文テキストでアクセストークンを保存**

  • azureProfile.jsonファイルには、ログインユーザーに関する情報が含まれています。

  • az logout はトークンを削除します。

  • 古いバージョンの**Az PowerShellTokenCache.datアクセストークン平文で保存していました。また、AzureRmContext.jsonServicePrincipalSecret平文で保存しています。Save-AzContextコマンドレットを使用してトークン**を保存できます。 Disconnect-AzAccount を使用して削除します。

  • 3者が侵害

  • 内部従業員

  • 一般的なフィッシング(資格情報またはOauthアプリ)

Azure テナント内のユーザーを侵害していなくても、いくつかの情報を収集できます:

pageAz - Unauthenticated Enum & Initial Entry

資格情報を取得した後は、それらの資格情報が誰に属しているか、および何にアクセス権を持っているかを知る必要があるため、基本的な列挙を実行する必要があります:

基本列挙

列挙の最も騒々しい部分はログインであり、列挙そのものではありません。

SSRF

Azure内のマシンでSSRFを見つけた場合は、トリックのためにこのページをチェックしてください:

ログイン条件のバイパス

有効な資格情報があるにもかかわらずログインできない場合、以下は一般的な保護措置です:

  • IPホワイトリスト -- 有効なIPを侵害する必要があります

  • 地理的制限 -- ユーザーの住所や会社のオフィスがどこにあるかを見つけ、同じ都市(または少なくとも同じ国)のIPを取得します

  • ブラウザ -- おそらく特定のOS(Windows、Linux、Mac、Android、iOS)からのみブラウザが許可されています。被害者/会社が使用しているOSを特定してください。

  • 通常は制限が少なく、そのログインが少なく見直されるため、Service Principal 資格情報を侵害することもできます

これをバイパスした後、元のセットアップに戻り、引き続きアクセスできるかもしれません。

サブドメインの乗っ取り

Whoami

Az - AzureAD セクションで az cli、AzureAD、Az PowerShell のインストール方法を学んでください。

最初に知る必要があることの1つは、自分が誰であるか(どの環境にいるか)です:

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

AzureAD

AzureAD is a cloud-based identity and access management service provided by Microsoft. It allows organizations to manage user identities and create access policies to secure their resources.

Security Best Practices

  • Enable Multi-Factor Authentication (MFA) for all users to add an extra layer of security.

  • Regularly review and audit user permissions to ensure least privilege access.

  • Implement Conditional Access policies to control access based on specific conditions.

  • Monitor sign-in logs and set up alerts for any suspicious activities.

  • Enable password protection and smart lockout to prevent password attacks.

Tools for Testing

  • Azure AD Identity Protection: Helps in detecting potential vulnerabilities and risky accounts.

  • Azure AD Privileged Identity Management (PIM): Manages, controls, and monitors access within Azure AD.

  • Azure AD Connect Health: Monitors and provides insights into the health of on-premises identity infrastructure.

Additional Resources

#Get the current session state
Get-AzureADCurrentSessionInfo
#Get details of the current tenant
Get-AzureADTenantDetail

Az PowerShell

Azure PowerShell is a set of cmdlets for managing Azure resources directly from the PowerShell command line. It provides a powerful and flexible command-line interface for interacting with Azure. You can use Azure PowerShell to automate common tasks, create and manage resources, and perform various administrative functions within your Azure environment.

Installation

To use Azure PowerShell, you need to install the Azure PowerShell module on your local machine. You can install it using the PowerShell Gallery by running the following command:

Install-Module -Name Az -AllowClobber -Scope CurrentUser

Connecting to Azure

Once you have installed the Azure PowerShell module, you can connect to your Azure account using the Connect-AzAccount cmdlet. This will prompt you to log in with your Azure credentials and establish a connection to your Azure subscription.

Connect-AzAccount

Managing Azure Resources

With Azure PowerShell, you can manage various Azure resources such as virtual machines, storage accounts, databases, and more. You can create, update, delete, start, stop, and perform other operations on these resources using the cmdlets provided by Azure PowerShell.

Scripting and Automation

Azure PowerShell is commonly used for scripting and automation tasks in Azure environments. You can write PowerShell scripts to automate repetitive tasks, configure resources, deploy applications, and more. Azure PowerShell provides a rich set of cmdlets and functionalities to help you streamline your workflows and improve efficiency.

Summary

Azure PowerShell is a powerful tool for managing Azure resources through the command line. By installing the Azure PowerShell module, connecting to your Azure account, and leveraging the cmdlets provided, you can automate tasks, manage resources, and perform administrative functions efficiently within your Azure environment.

# 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

Azureを列挙するための最も重要なコマンドの1つは、Az PowerShellの**Get-AzResource**であり、現在のユーザーが表示できるリソースを把握できます。

同じ情報をWebコンソールで取得することもできます。https://portal.azure.com/#view/HubsExtension/BrowseAllにアクセスするか、「All resources」を検索します。

AzureAD列挙

デフォルトでは、ユーザーはユーザー、グループ、ロール、サービス プリンシパルなどを列挙する十分な権限を持つはずです(デフォルトのAzureAD権限を確認してください)。 ここにガイドがあります:

pageAz - AzureAD (AAD)

今、資格情報に関する情報を持っている(そしてあなたがレッドチームである場合、検出されていないことを願います)。環境で使用されているサービスを特定する時が来ました。 次のセクションでは、一般的なサービスを列挙するいくつかの方法を確認できます。

サービス プリンシパルとアクセス ポリシー

Azureサービスには、システム ID(サービス自体)を持つことも、ユーザー割り当てのマネージド IDを使用することもできます。この ID には、たとえば KeyVault からシークレットを読み取るためのアクセス ポリシーが付与されることがあります。これらのアクセス ポリシーは制限されるべきです(最小特権原則)、しかし必要以上の権限を持っている可能性があります。通常、App Service は KeyVault を使用してシークレットや証明書を取得します。

そのため、これらの ID を調査することが役立ちます。

App Service SCM

App Service 'container' にログインするための Kudu コンソール。

Webshell

portal.azure.com を使用してシェルを選択するか、shell.azure.com を使用して、bash または powershell を使用します。このシェルの 'disk' は、ストレージ アカウント内のイメージ ファイルとして保存されます。

Azure DevOps

Azure DevOps は Azure とは別です。リポジトリ、パイプライン(yaml またはリリース)、ボード、ウィキなどがあります。Variable Groups は変数値とシークレットを格納するために使用されます。

Automated Recon Tools

cd ROADTools
pipenv shell
roadrecon auth -u test@corp.onmicrosoft.com -p "Welcome2022!"
roadrecon gather
roadrecon gui

Import-Module monkey365
Get-Help Invoke-Monkey365
Get-Help Invoke-Monkey365 -Detailed
Invoke-Monkey365 -IncludeAzureActiveDirectory -ExportTo HTML -Verbose -Debug -InformationAction Continue
Invoke-Monkey365 - Instance Azure -Analysis All -ExportTo HTML

# 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は、Azure AD環境内の権限エスカレーション経路を特定するためのツールです。BloodHoundの機能をAzure ADに拡張し、クラウド環境における攻撃経路を可視化します。

# 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は、Azure環境におけるセキュリティの自動化された評価を目的としたツールです。

# 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

Import-Module .\MicroBurst.psm1
Import-Module .\Get-AzureDomainInfo.ps1
Get-AzureDomainInfo -folder MicroBurst -Verbose

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
htARTE(HackTricks AWS Red Team Expert) を使って、ゼロからヒーローまでAWSハッキングを学ぶ

HackTricksをサポートする他の方法:

最終更新