Az - Automation Accounts

Reading time: 16 minutes

tip

AWSハッキングを学び、実践する:HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE) Azureハッキングを学び、実践する:HackTricks Training Azure Red Team Expert (AzRTE)

HackTricksをサポートする

基本情報

Azure Automation Accountsは、Microsoft Azureのクラウドベースのサービスで、リソース管理、構成、更新などのタスクを自動化するのに役立ちます。これにより、Runbooks(実行される自動化用のスクリプト)、スケジュール、およびハイブリッドワーカーグループを提供し、自動化ジョブを実行できるようにし、インフラストラクチャをコード(IaC)として扱い、クラウドリソースの管理における効率性と一貫性を向上させます。

設定

  • 資格情報: パスワードは自動化アカウント内のRunbook内でのみアクセス可能で、ユーザー名とパスワードを安全に保存するために使用されます。
  • 変数: Runbook内で使用できる構成データを保存するために使用されます。これにはAPIキーのような機密情報も含まれる可能性があります。変数が暗号化されて保存されている場合、それは自動化アカウント内のRunbook内でのみ利用可能です。
  • 証明書: Runbook内で使用できる証明書を保存するために使用されます。
  • 接続: 外部サービスへの接続情報を保存するために使用されます。これには機密情報が含まれる可能性があります。
  • ネットワークアクセス: 公開またはプライベートに設定できます。

Runbooks & Jobs

Azure AutomationのRunbookは、クラウド環境内でタスクを自動的に実行するスクリプトです。RunbookはPowerShell、Python、またはグラフィカルエディタで記述できます。これにより、VM管理、パッチ適用、コンプライアンスチェックなどの管理タスクを自動化できます。

Runbooks内のコードには機密情報(資格情報など)が含まれる可能性があります。

ジョブはRunbook実行のインスタンスです。Runbookを実行すると、実行を追跡するためにジョブが作成されます。各ジョブには以下が含まれます:

  • ステータス: キュー待ち、実行中、完了、失敗、保留。
  • 出力: Runbook実行の結果。
  • 開始時刻と終了時刻: ジョブが開始された時刻と完了した時刻。

ジョブにはRunbook実行の出力が含まれます。ジョブ読み取ることができる場合は、実行の出力(潜在的な機密情報)が含まれているため、必ず行ってください。

スケジュール & Webhooks

Runbookを実行する主な方法は3つあります:

  • スケジュール: 特定の時間または間隔でRunbookをトリガーするために使用されます。
  • Webhooks: 外部サービスからRunbookをトリガーするために使用できるHTTPエンドポイントです。作成後、Webhook URLは表示されません
  • 手動トリガー: AzureポータルやCLIからRunbookを手動でトリガーできます。

ソース管理

Github、Azure Devops (Git)、およびAzure Devops (TFVC)からRunbooksをインポートすることができます。リポジトリのRunbooksをAzure Automationアカウントに公開するように指示することも、リポジトリからAzure Automationアカウントに変更を同期するように指示することも可能です。

同期が有効になっている場合、GithubリポジトリにWebhookが作成され、プッシュイベントが発生するたびに同期がトリガーされます。Webhook URLの例: https://f931b47b-18c8-45a2-9d6d-0211545d8c02.webhook.eus.azure-automation.net/webhooks?token=DRjQyFiOrUtz%2fw7o23XbDpOlTe1%2bUqPQm4pQH2WBfJg%3d

これらのWebhookは、Githubリポジトリに関連付けられたRunbooksのWebhookをリストする際に表示されません。また、作成後にソース管理のリポジトリURLを変更することはできません

構成されたソース管理が機能するためには、Azure Automation Accountに**Contributorロールを持つマネージドアイデンティティ(システムまたはユーザー)が必要です。さらに、Automation Accountにユーザーマネージドアイデンティティを割り当てるには、変数AUTOMATION_SC_USER_ASSIGNED_IDENTITY_ID**にユーザーMIのクライアントIDを指定する必要があります。

ランタイム環境

Runbookを作成する際に、ランタイム環境を選択できます。デフォルトでは、以下のランタイム環境が利用可能です:

  • Powershell 5.1
  • Powershell 7.1
  • PowerShell 7.2
  • Python 3.10
  • Python 3.8
  • Python 2.7

ただし、これらのいずれかをベースにして独自の環境を作成することも可能です。Pythonの場合、使用する環境に.whlパッケージをアップロードできます。PowerShellの場合、ランタイムに必要なモジュールを含む.zipパッケージをアップロードできます。

ハイブリッドワーカーグループ

Azure Automationでは、Runbookのデフォルトの実行環境はAzure Sandboxであり、Azureリソースに関するタスクに適したAzureが管理するクラウドベースのプラットフォームです。ただし、このサンドボックスには、オンプレミスリソースへのアクセス制限や実行時間とリソース使用の制約などの制限があります。これらの制限を克服するために、ハイブリッドワーカーグループが使用されます。ハイブリッドワーカーグループは、自分のマシンにインストールされた1つ以上のハイブリッドRunbookワーカーで構成され、オンプレミス、他のクラウド環境、またはAzure VM上で実行されます。このセットアップにより、Runbookはこれらのマシン上で直接実行され、ローカルリソースへの直接アクセス、より長くリソース集約的なタスクの実行、Azureの即時の範囲を超えた環境との相互作用の柔軟性が提供されます。

ハイブリッドワーカーグループを作成する際には、使用する資格情報を指定する必要があります。2つのオプションがあります:

  • デフォルトの資格情報: 資格情報を提供する必要はなく、RunbookはSystemとしてVM内で実行されます。
  • 特定の資格情報: 自動化アカウント内の資格情報オブジェクトの名前を提供する必要があり、これがVM内でRunbookを実行するために使用されます。したがって、この場合、VMの有効な資格情報を盗むことが可能です。

したがって、ハイブリッドワーカーRunbookを実行することを選択できる場合、Systemとして外部マシン内で任意のコマンドを実行します(良いピボット技術)。

さらに、ハイブリッドワーカーがAzureで他のマネージドアイデンティティと共に実行されている場合、RunbookはRunbookのマネージドアイデンティティとVMのメタデータサービスからのすべてのマネージドアイデンティティにアクセスできます。

tip

メタデータサービスのURLは、Automationアカウントのマネージドアイデンティティトークンを取得するサービスとは異なる(http://169.254.169.254)ことを忘れないでください。

状態構成 (SC)

warning

ドキュメントに示されているように、Azure Automation State Configurationは2027年9月30日に廃止され、Azure Machine Configurationに置き換えられます。

Automation Accountsは、状態構成 (SC)もサポートしており、これはVMの状態構成および維持するのに役立つ機能です。WindowsおよびLinuxマシンにDSC構成を作成および適用することが可能です。

攻撃者の視点から見ると、これはすべての構成されたVMで任意のPSコードを実行することを可能にし、これらのVMのマネージドアイデンティティへの権限昇格を許可し、新しいネットワークへのピボットを可能にするため、興味深いものでした... また、構成には機密情報が含まれる可能性があります。

列挙

bash
# List Automation Accounts
az automation account list --output table

# Get Automation Account details
# Check the network access in `privateEndpointConnections` and `publicNetworkAccess`
# Check the managed identities in `identity`
az automation account show --name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME>

# Get keys of automation account
## These are used for the DSC
az automation account list-keys --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME>

# Get schedules of automation account
az automation schedule list --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME>

# Get connections of automation account
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/connections?api-version=2023-11-01"

# Get connection details
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/connections/<connection-name>?api-version=2023-11-01"

# Get credentials of automation account
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/credentials?api-version=2023-11-01"

# Get credential details
## Note that you will only be able to access the password from inside a Runbook
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/credentials/<credential-name>?api-version=2023-11-01"

# Get certificates of automation account
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/certificates?api-version=2023-11-01"

# Get certificate details
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/certificates/<certificate-name>?api-version=2023-11-01"

# Get variables of automation account
## It's possible to get the value of unencrypted variables but not the encrypted ones
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/variables?api-version=2023-11-01"

# Get variable details
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/variables/<variable-name>?api-version=2023-11-01"

# Get runbooks of an automation account
az automation runbook list --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME>

# Get runbook details
az automation runbook show --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME> --name <RUNBOOK-NAME>

# Get runbook content
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/runbooks/<runbook-name>/content?api-version=2023-11-01"

# Get jobs of an automation account
az automation job list --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME>

# Get job details
az automation job show --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME> --name <JOB-NAME>

# Get job output
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/jobs/<job-name>/output?api-version=2023-11-01"

# Get the Runbook content when the job was executed
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/jobs/<job-name>/runbookContent?api-version=2023-11-01"

# Get webhooks inside an automation account
## It's possible to see to which runbook it belongs in the given data
## For security reasons it's not possible to see the URL of the webhook after creating it, here is a URL example: https://f931b47b-18c8-45a2-9d6d-0211545d8c02.webhook.eus.azure-automation.net/webhooks?token=dOdnxk6z7ugAxiuyUMKgPuDMav2Jw5EJediMdiN4jLo%3d
## Generating a webhook can be useful from a persistence perspective
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/webhooks?api-version=2018-06-30"

# Get the source control setting of an automation account (if any)
## inside the output it's possible to see if the autoSync is enabled, if the publishRunbook is enabled and the repo URL
az automation source-control list --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME>

# Get custom runtime environments
## Check in defaultPackages for custom ones, by default Python envs won't have anything here and PS1 envs will have "az" and "azure cli"
az automation runtime-environment list \
--resource-group <res-group>> \
--automation-account-name <account-name> \
--query "[?!(starts_with(description, 'System-generated'))]"

# Get State Configurations (SC) of an automation account
az automation dsc configuration list --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME>

# Get State Configuration details
az automation dsc configuration show --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME> --name <DSC-CONFIG-NAME>

# Get State Configuration content
az automation dsc configuration show-content --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME> --name <DSC-CONFIG-NAME>

# Get hybrid worker groups for an automation account
az automation hrwg list --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME>

# Get hybrid worker group details
az automation hrwg show --automation-account-name <AUTOMATION-ACCOUNT> --resource-group <RG-NAME> --name <HYBRID-WORKER-GROUP>

# Get more details about a hybrid worker group (like VMs inside it)
az rest --method GET --url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/hybridRunbookWorkerGroups/<hybrid-worker-group-name>/hybridRunbookWorkers?&api-version=2021-06-22"
bash
# Check user right for automation
az extension add --upgrade -n automation
az automation account list # if it doesn't return anything the user is not a part of an Automation group

# Gets Azure Automation accounts in a resource group
Get-AzAutomationAccount

# List & get DSC configs
Get-AzAutomationAccount | Get-AzAutomationDscConfiguration
Get-AzAutomationAccount | Get-AzAutomationDscConfiguration | where {$_.name -match '<name>'} | Export-AzAutomationDscConfiguration -OutputFolder . -Debug
## Automation Accounts named SecurityBaselineConfigurationWS... are there by default (not interesting)

# List & get Run books code
Get-AzAutomationAccount | Get-AzAutomationRunbook
Get-AzAutomationAccount | Get-AzAutomationRunbook | Export-AzAutomationRunbook -OutputFolder /tmp

# List credentials & variables & others
Get-AzAutomationAccount | Get-AzAutomationCredential
Get-AzAutomationAccount | Get-AzAutomationVariable
Get-AzAutomationAccount | Get-AzAutomationConnection
Get-AzAutomationAccount | Get-AzAutomationCertificate
Get-AzAutomationAccount | Get-AzAutomationSchedule
Get-AzAutomationAccount | Get-AzAutomationModule
Get-AzAutomationAccount | Get-AzAutomationPython3Package
## Exfiltrate credentials & variables and the other info loading them in a Runbook and printing them

# List hybrid workers
Get-AzAutomationHybridWorkerGroup -AutomationAccountName <AUTOMATION-ACCOUNT> -ResourceGroupName <RG-NAME>

権限昇格とポストエクスプロイト

Az - Automation Accounts Privesc

参考文献

tip

AWSハッキングを学び、実践する:HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE) Azureハッキングを学び、実践する:HackTricks Training Azure Red Team Expert (AzRTE)

HackTricksをサポートする