# Az Static Web Apps
{{#include ../../../banners/hacktricks-training.md}}
## Static Web Apps 基本情報
Azure Static Web Appsは、**GitHubなどのリポジトリからの自動CI/CDを備えた静的Webアプリをホスティングするためのクラウドサービス**です。グローバルなコンテンツ配信、サーバーレスバックエンド、組み込みのHTTPSを提供し、安全でスケーラブルです。しかし、サービスが「静的」と呼ばれていても、完全に安全であるとは限りません。リスクには、誤って構成されたCORS、不十分な認証、コンテンツの改ざんが含まれ、適切に管理されないとXSSやデータ漏洩などの攻撃にさらされる可能性があります。
### デプロイ認証
> [!TIP]
> Static Appが作成されると、**デプロイ認証ポリシー**として**デプロイメントトークン**と**GitHub Actionsワークフロー**の間で選択できます。
- **デプロイメントトークン**: トークンが生成され、デプロイプロセスを認証するために使用されます。**このトークンを持つ人は、新しいバージョンのアプリをデプロイするのに十分です**。リポジトリにトークンを秘密として持つ**Github Actionが自動的にデプロイされ**、リポジトリが更新されるたびに新しいバージョンのアプリをデプロイします。
- **GitHub Actionsワークフロー**: この場合、非常に似たGithub Actionもリポジトリにデプロイされ、**トークンも秘密に保存されます**。ただし、このGithub Actionには違いがあり、**`actions/github-script@v6`**アクションを使用してリポジトリのIDTokenを取得し、それを使用してアプリをデプロイします。
- 両方のケースでアクション**`Azure/static-web-apps-deploy@v1`**が`azure_static_web_apps_api_token`パラメータのトークンと共に使用されますが、2番目のケースでは、`github_id_token`パラメータのIDTokenで認証が行われるため、`12345cbb198a77a092ff885781a62a15d51ef5e3654ca11234509ab54547270704-4140ccee-e04f-424f-b4ca-3d4dd123459c00f0702071d12345`のような有効な形式のランダムトークンだけでアプリをデプロイするのに十分です。
### Webアプリ基本認証
Webアプリにアクセスするための**パスワードを設定する**ことが可能です。Webコンソールでは、ステージング環境のみ、またはステージングと本番の両方を保護するように設定できます。
執筆時点でのパスワード保護されたWebアプリの外観は次のとおりです:
**どのパスワードが使用されているか**、およびどの環境が保護されているかを確認することができます:
```bash
az rest --method GET \
--url "/subscriptions//resourceGroups/Resource_Group_1/providers/Microsoft.Web/staticSites//config/basicAuth?api-version=2024-04-01"
```
しかし、これは**パスワードを平文で表示しません**。代わりに、次のようなものが表示されます: `"password": "**********************"`。
### ルートとロール
ルートは、静的ウェブアプリ内で**受信HTTPリクエストがどのように処理されるか**を定義します。**`staticwebapp.config.json`**ファイルで構成されており、URLの書き換え、リダイレクト、アクセス制限、ロールベースの認証を制御し、適切なリソースの処理とセキュリティを確保します。
いくつかの例:
```json
{
"routes": [
{
"route": "/",
"rewrite": "/index.html"
},
{
"route": "/about",
"rewrite": "/about.html"
},
{
"route": "/api/*",
"allowedRoles": ["authenticated"]
},
{
"route": "/admin",
"redirect": "/login",
"statusCode": 302
},
{
"route": "/google",
"redirect": "https://google.com",
"statusCode": 307
}
],
"navigationFallback": {
"rewrite": "/index.html",
"exclude": ["/api/*", "/assets/*"]
}
}
```
パスを**ロールで保護する**ことが可能であることに注意してください。これにより、ユーザーはアプリに認証し、そのロールを付与されてパスにアクセスする必要があります。また、特定のユーザーに特定のロールを付与する**招待を作成する**ことも可能で、これはアプリ内で権限を昇格させるのに役立つかもしれません。
> [!TIP]
> `staticwebapp.config.json`ファイルへの**変更が受け入れられない**ようにアプリを構成することが可能であることに注意してください。この場合、Githubからファイルを変更するだけでは不十分で、**アプリ内の設定を変更する**必要があります。
ステージングURLは次の形式です: `https://-..` 例えば: `https://ambitious-plant-0f764e00f-2.eastus2.4.azurestaticapps.net`
### スニペット
静的ウェブアプリ内にHTMLスニペットを保存することが可能で、アプリ内で読み込まれます。これは、アプリに**悪意のあるコードを注入する**ために使用でき、例えば**資格情報を盗むためのJSコード**や**キーロガー**などです。詳細は権限昇格のセクションにあります。
### マネージドアイデンティティ
Azure Static Web Appsは**マネージドアイデンティティ**を使用するように構成できますが、[このFAQ](https://learn.microsoft.com/en-gb/azure/static-web-apps/faq#does-static-web-apps-support-managed-identity-)で述べられているように、**認証目的でAzure Key Vaultからシークレットを抽出するためにのみサポートされています。他のAzureリソースにアクセスするためではありません**。
詳細については、静的アプリでボールトシークレットを使用するAzureガイドをhttps://learn.microsoft.com/en-us/azure/static-web-apps/key-vault-secretsで見つけることができます。
## 列挙
{{#tabs }}
{{#tab name="az cli" }}
```bash
# List Static Webapps
az staticwebapp list --output table
# Get Static Webapp details
az staticwebapp show --name --resource-group --output table
# Get appsettings
az staticwebapp appsettings list --name
# Get env information
az staticwebapp environment list --name
az staticwebapp environment functions --name
# Get API key
az staticwebapp secrets list --name
# Get invited users
az staticwebapp users list --name
# Get current snippets
az rest --method GET \
--url "https://management.azure.com/subscriptions//resourceGroups//providers/Microsoft.Web/staticSites/trainingdemo/snippets?api-version=2022-03-01"
# Get database connections
az rest --method GET \
--url "https://management.azure.com/subscriptions//resourceGroups//providers/Microsoft.Web/staticSites//databaseConnections?api-version=2021-03-01"
## Once you have the database connection name ("default" by default) you can get the connection string with the credentials
az rest --method POST \
--url "https://management.azure.com/subscriptions//resourceGroups//providers/Microsoft.Web/staticSites//databaseConnections/default/show?api-version=2021-03-01"
# Check connected backends
az staticwebapp backends show --name --resource-group
```
{{#endtab }}
{{#tab name="Az Powershell" }}
```bash
Get-Command -Module Az.Websites
# Retrieves details of a specific Static Web App in the specified resource group.
Get-AzStaticWebApp -ResourceGroupName -Name
# Retrieves the build details for a specific Static Web App.
Get-AzStaticWebAppBuild -ResourceGroupName -Name
# Retrieves the application settings for a specific build environment in a Static Web App.
Get-AzStaticWebAppBuildAppSetting -ResourceGroupName -Name -EnvironmentName
# Retrieves functions for a specific build environment in a Static Web App.
Get-AzStaticWebAppBuildFunction -ResourceGroupName -Name -EnvironmentName
# Retrieves function app settings for a specific build environment in a Static Web App.
Get-AzStaticWebAppBuildFunctionAppSetting -ResourceGroupName -Name -EnvironmentName
# Retrieves the configured roles for a Static Web App.
Get-AzStaticWebAppConfiguredRole -ResourceGroupName -Name
# Retrieves the custom domains configured for a Static Web App.
Get-AzStaticWebAppCustomDomain -ResourceGroupName -Name
# Retrieves details of the functions associated with a Static Web App.
Get-AzStaticWebAppFunction -ResourceGroupName -Name
# Retrieves the app settings for the function app associated with a Static Web App.
Get-AzStaticWebAppFunctionAppSetting -ResourceGroupName -Name
# Retrieves the secrets for a Static Web App.
Get-AzStaticWebAppSecret -ResourceGroupName -Name
# Retrieves general app settings for a Static Web App.
Get-AzStaticWebAppSetting -ResourceGroupName -Name
# Retrieves user details for a Static Web App with a specified authentication provider.
Get-AzStaticWebAppUser -ResourceGroupName -Name -AuthProvider
# Retrieves user-provided function apps associated with a Static Web App.
Get-AzStaticWebAppUserProvidedFunctionApp -ResourceGroupName -Name
```
{{#endtab }}
{{#endtabs }}
## Webアプリを生成するための例
以下のリンクでウェブアプリを生成するための良い例を見つけることができます: [https://learn.microsoft.com/en-us/azure/static-web-apps/get-started-portal?tabs=react&pivots=github](https://learn.microsoft.com/en-us/azure/static-web-apps/get-started-portal?tabs=react&pivots=github)
1. リポジトリ https://github.com/staticwebdev/react-basic/generate をあなたのGitHubアカウントにフォークし、`my-first-static-web-app`という名前を付けます
2. AzureポータルでStatic Web Appを作成し、GitHubアクセスを設定し、先にフォークした新しいリポジトリを選択します
3. 作成し、数分待って、新しいページを確認してください!
## 権限昇格とポストエクスプロイト
Azure Static Web Appsにおける権限昇格とポストエクスプロイトに関するすべての情報は、以下のリンクで見つけることができます:
{{#ref}}
../az-privilege-escalation/az-static-web-apps-privesc.md
{{#endref}}
## 参考文献
- [https://learn.microsoft.com/en-in/azure/app-service/overview](https://learn.microsoft.com/en-in/azure/app-service/overview)
- [https://learn.microsoft.com/en-us/azure/app-service/overview-hosting-plans](https://learn.microsoft.com/en-us/azure/app-service/overview-hosting-plans)
{{#include ../../../banners/hacktricks-training.md}}