Translated ['src/pentesting-cloud/azure-security/az-services/az-app-serv

This commit is contained in:
Translator
2025-01-04 00:40:51 +00:00
parent a5b1b1539c
commit eba921c636
2 changed files with 287 additions and 1 deletions

View File

@@ -0,0 +1,286 @@
# Az - App Services
{{#include ../../../banners/hacktricks-training.md}}
## App Service Basic Information
Azure App Servicesは、開発者が**ウェブアプリケーション、モバイルアプリのバックエンド、およびAPIをシームレスに構築、展開、スケール**できるようにします。複数のプログラミング言語をサポートし、機能と管理を強化するためにさまざまなAzureツールやサービスと統合されています。
各アプリはサンドボックス内で実行されますが、隔離はApp Serviceプランに依存します
- FreeおよびSharedティアのアプリは**共有VM**上で実行されます。
- StandardおよびPremiumティアのアプリは**同じApp Serviceプラン内のアプリのみが共有する専用VM**上で実行されます。
- Isolatedティアは**専用の仮想ネットワーク上の専用VM**で実行され、アプリの隔離が向上します。
> [!WARNING]
> これらの隔離は、他の一般的な**ウェブ脆弱性**(ファイルアップロードやインジェクションなど)を**防ぐものではありません**。また、**管理アイデンティティ**が使用される場合、それに**権限を昇格させる**ことができる可能性があります。
アプリにはいくつかの興味深い設定があります:
- **Always On**: アプリが常に実行されることを保証します。これが有効でない場合、アプリは20分間の非アクティブ後に停止し、リクエストが受信されると再起動します。
- ウェブジョブが継続的に実行される必要がある場合、アプリが停止するとウェブジョブも停止するため、これは重要です。
- **SSH**: 有効にすると、十分な権限を持つユーザーがSSHを使用してアプリに接続できます。
- **デバッグ**: 有効にすると、十分な権限を持つユーザーがアプリをデバッグできます。ただし、これは48時間ごとに自動的に無効になります。
- **Web App + Database**: ウェブコンソールを使用してデータベースを持つアプリを作成できます。この場合、使用するデータベースSQLAzure、PostgreSQL、MySQL、MongoDBを選択でき、Azure Cache for Redisを作成することもできます。
- データベースとRedisの資格情報を含むURLは、**appsettings**に保存されます。
- **コンテナ**: コンテナのURLとアクセスするための資格情報を指定することで、App Serviceにコンテナをデプロイできます。
- **マウント**: Azure Blob読み取り専用またはAzure Filesから5つのマウントを作成できます。設定はストレージアカウントのアクセスキーを保存します。
## Basic Authentication
ウェブアプリ通常はAzure関数を作成する際に、**Basic Authenticationを有効にするかどうかを指定できます**(デフォルトでは無効)。これは基本的に、アプリケーションのために**SCMソースコントロールマネージャーおよびFTPファイル転送プロトコル**を有効にすることを意味し、これらの技術を使用してアプリケーションをデプロイできるようになります。
SCMおよびFTPサーバーにアクセスするには、**ユーザー名とパスワード**が必要です。したがって、AzureはこれらのプラットフォームへのURLと資格情報を取得するための**APIを提供します**。
**FTPサーバーには特別な魔法はありません**。有効なURL、ユーザー名、パスワードがあれば、接続してアプリ環境に対する読み取りおよび書き込み権限を取得できます。
SCM
`https://<SMC-URL>/BasicAuth`を使用してSCMに接続し、そこにあるすべてのファイルとデプロイメントを確認できます。
### Kudu
Kuduは、**SCMとアプリサービスを管理するためのウェブおよびAPIインターフェースの両方を管理するプラットフォーム**であり、Gitベースのデプロイメント、リモートデバッグ、およびファイル管理機能を提供します。ウェブアプリで定義されたSCM URLを通じてアクセスできます。
App ServicesとFunction Appsで使用されるKuduのバージョンは異なり、Function Appsのバージョンははるかに制限されています。
Kuduで見つけることができるいくつかの興味深いエンドポイントは次のとおりです
- `/BasicAuth`: Kuduに**ログインするためにこのパスにアクセスする必要があります**。
- `/DebugConsole`: Kuduが実行されている環境でコマンドを実行できるコンソールです。
- この環境は**トークンを取得するためのメタデータサービスにアクセスできません**。
- `/webssh/host`: アプリが実行されているコンテナ内に接続できるウェブベースのSSHクライアントです。
- この環境は、割り当てられた管理アイデンティティからトークンを取得するための**メタデータサービスにアクセスできます**。
- `/Env`: システム、アプリ設定、環境変数、接続文字列、およびHTTPヘッダーに関する情報を取得します。
- `/wwwroot/`: ウェブアプリのルートディレクトリです。ここからすべてのファイルをダウンロードできます。
さらに、Kuduは以前は[https://github.com/projectkudu/kudu](https://github.com/projectkudu/kudu)でオープンソースでしたが、プロジェクトは非推奨となり、Azureの現在のKuduの動作と古いKuduを比較すると、**すでにいくつかのことが変更されている**ことがわかります。
## Sources
App Servicesは、デフォルトでコードをzipファイルとしてアップロードすることを許可しますが、サードパーティサービスに接続してそこからコードを取得することも可能です。
- 現在サポートされているサードパーティソースは**Github**と**Bitbucket**です。
- 認証トークンを取得するには、`az rest --url "https://management.azure.com/providers/Microsoft.Web/sourcecontrols?api-version=2024-04-01"`を実行します。
- Azureはデフォルトで、コードが更新されるたびにApp Serviceにコードをデプロイするための**Github Action**を設定します。
- そこからコードを取得するために、**リモートgitリポジトリ**(ユーザー名とパスワードを使用)を指定することも可能です。
- リモートリポジトリの資格情報を取得するには、`az webapp deployment source show --name <app-name> --resource-group <res-group>`または`az rest --method POST --url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/config/metadata/list?api-version=2022-03-01" --resource "https://management.azure.com"`を実行します。
- **Azure Repository**を使用することも可能です。
- **ローカルgitリポジトリ**を構成することも可能です。
- `az webapp deployment source show --name <app-name> --resource-group <res-group>`を使用してgitリポジトリのURLを取得でき、これはアプリのSCM URLになります。
- クローンするには、`az webapp deployment list-publishing-profiles --resource-group <res-group> -n <name>`で取得できるSCM資格情報が必要です。
## Webjobs
Azure WebJobsは、**Azure App Service環境で実行されるバックグラウンドタスク**です。これにより、開発者はウェブアプリケーションと並行してスクリプトやプログラムを実行でき、ファイル処理、データ処理、またはスケジュールされたタスクなどの非同期または時間集約型の操作を簡単に処理できます。
ウェブジョブには2種類あります
- **継続的**: 無限にループで実行され、作成されるとすぐにトリガーされます。常に処理が必要なタスクに最適です。ただし、Always Onが無効になっていて、過去20分間リクエストを受信していない場合、アプリが停止するとウェブジョブも停止します。
- **トリガー**: オンデマンドまたはスケジュールに基づいて実行されます。バッチデータの更新やメンテナンスルーチンなどの定期的なタスクに最適です。
ウェブジョブは、環境内で**コードを実行**し、接続された管理アイデンティティに**権限を昇格させる**ために使用できる可能性があるため、攻撃者の視点から非常に興味深いです。
さらに、ウェブジョブによって生成された**ログ**を確認することは常に興味深いです。これらには**機密情報**が含まれている可能性があります。
## Slots
Azure App Service Slotsは、**同じApp Serviceに異なるバージョンのアプリケーションをデプロイするために使用されます**。これにより、開発者は新しい機能や変更を本番環境にデプロイする前に別の環境でテストできます。
さらに、特定のスロットに**トラフィックの割合をルーティング**することが可能で、これはA/Bテストや**バックドア目的**に便利です。
## Azure Function Apps
基本的に**Azure Function AppsはAzure App Serviceのサブセット**であり、ウェブコンソールに表示されます。ウェブコンソールにアクセスしてすべてのアプリサービスをリストするか、az cliで`az webapp list`を実行すると、**Function Appsもそこにリストされているのが見えます**。
したがって、両方のサービスは実際にはほとんど**同じ設定、機能、およびaz cliのオプションを持っています**が、アプリ設定のデフォルト値やFunction Appsでのストレージアカウントの使用など、少し異なる方法で構成される場合があります。
## Enumeration
{{#tabs }}
{{#tab name="az" }}
```bash
# List webapps
az webapp list
## Less information
az webapp list --query "[].{hostName: defaultHostName, state: state, name: name, resourcegroup: resourceGroup}" -o table
## Get SCM URL of each webapp
az webapp list | grep '"name"' | grep "\.scm\." | awk '{print $2}' | sed 's/"//g'
# Get info about 1 app
az webapp show --name <name> --resource-group <res-group>
# Get instances of a webapp
az webapp list-instances --name <name> --resource-group <res-group>
## If you have enough perm you can go to the "consoleUrl" and access a shell inside the instance form the web
# Get access restrictions of an app
az webapp config access-restriction show --name <name> --resource-group <res-group>
# Remove access restrictions
az webapp config access-restriction remove --resource-group <res-group> -n <name> --rule-name <rule-name>
# Get connection strings of a webapp
az webapp config connection-string list --name <name> --resource-group <res-group>
# Get appsettings of an app
az webapp config appsettings list --name <name> --resource-group <res-group>
# Get SCM and FTP credentials
az webapp deployment list-publishing-profiles --name <name> --resource-group <res-group>
# Get configured Auth information
az webapp auth show --name <app-name> --resource-group <res-group>
# Get backups of a webapp
az webapp config backup list --webapp-name <name> --resource-group <res-group>
# Get backups scheduled for a webapp
az webapp config backup show --webapp-name <name> --resource-group <res-group>
# Get snapshots
az webapp config snapshot list --resource-group <res-group> -n <name>
# Restore snapshot
az webapp config snapshot restore -g <res-group> -n <name> --time 2018-12-11T23:34:16.8388367
# Get slots
az webapp deployment slot list --name <AppName> --resource-group <ResourceGroupName> --output table
az webapp show --slot <SlotName> --name <AppName> --resource-group <ResourceGroupName>
# Get traffic-routing
az webapp traffic-routing show --name <AppName> --resource-group <ResourceGroupName>
# Get used container by the app
az webapp config container show --name <name> --resource-group <res-group>
# Get storage account configurations of a webapp (contains access key)
az webapp config storage-account list --name <name> --resource-group <res-group>
# Get configured container (if any) in the webapp, it could contain credentials
az webapp config container show --name <name> --resource-group <res-group>
# Get Webjobs
az webapp webjob continuous list --resource-group <res-group> --name <app-name>
az webapp webjob triggered list --resource-group <res-group> --name <app-name>
# Read webjobs logs with Azure permissions
az rest --method GET --url "<SCM-URL>/vfs/data/jobs/<continuous | triggered>/rev5/job_log.txt" --resource "https://management.azure.com/"
az rest --method GET --url "https://lol-b5fyaeceh4e9dce0.scm.canadacentral-01.azurewebsites.net/vfs/data/jobs/continuous/rev5/job_log.txt" --resource "https://management.azure.com/"
# Read webjobs logs with SCM credentials
curl "https://windowsapptesting-ckbrg3f0hyc8fkgp.scm.canadacentral-01.azurewebsites.net/vfs/data/jobs/continuous/lala/job_log.txt" \
--user '<username>:<password>' -v
# Get connections of a webapp
az webapp conection list --name <name> --resource-group <res-group>
# Get hybrid-connections of a webapp
az webapp hybrid-connections list --name <name> --resource-group <res-group>
```
{{#endtab }}
{{#tab name="Az Powershell" }}
```powershell
# Get App Services and Function Apps
Get-AzWebApp
# Get only App Services
Get-AzWebApp | ?{$_.Kind -notmatch "functionapp"}
```
{{#endtab }}
{{#tab name="az get all" }}
```bash
#!/bin/bash
# Get all App Service and Function Apps
# Define Azure subscription ID
azure_subscription="your_subscription_id"
# Log in to Azure
az login
# Select Azure subscription
az account set --subscription $azure_subscription
# Get all App Services in the specified subscription
list_app_services=$(az appservice list --query "[].{appServiceName: name, group: resourceGroup}" -o tsv)
# Iterate over each App Service
echo "$list_app_services" | while IFS=$'\t' read -r appServiceName group; do
# Get the type of the App Service
service_type=$(az appservice show --name $appServiceName --resource-group $group --query "kind" -o tsv)
# Check if it is a Function App and print its name
if [ "$service_type" == "functionapp" ]; then
echo "Function App Name: $appServiceName"
fi
done
```
{{#endtab }}
{{#endtabs }}
#### 資格情報を取得し、webappコードにアクセスする
```bash
# Get connection strings that could contain credentials (with DBs for example)
az webapp config connection-string list --name <name> --resource-group <res-group>
## Check how to use the DBs connection strings in the SQL page
# Get credentials to access the code and DB credentials if configured.
az webapp deployment list-publishing-profiles --resource-group <res-group> -n <name>
# Get git URL to access the code
az webapp deployment source config-local-git --resource-group <res-group> -n <name>
# Access/Modify the code via git
git clone 'https://<username>:<password>@name.scm.azurewebsites.net/repo-name.git'
## In my case the username was: $nameofthewebapp and the password some random chars
## If you change the code and do a push, the app is automatically redeployed
```
{{#ref}}
../az-privilege-escalation/az-app-services-privesc.md
{{#endref}}
## Webアプリを生成する例
### ローカルからのPython
このチュートリアルは、[https://learn.microsoft.com/en-us/azure/app-service/quickstart-python](https://learn.microsoft.com/en-us/azure/app-service/quickstart-python?tabs=flask%2Cwindows%2Cazure-cli%2Cazure-cli-deploy%2Cdeploy-instructions-azportal%2Cterminal-bash%2Cdeploy-instructions-zip-azcli)のものに基づいています。
```bash
# Clone repository
git clone https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart
cd msdocs-python-flask-webapp-quickstart
# Create webapp from this code
az webapp up --runtime PYTHON:3.9 --sku B1 --logs
```
SCMポータルにログインするか、FTP経由でログインすると、`/wwwroot`にウェブアプリのコードを含む圧縮ファイル`output.tar.gz`を見ることができます。
> [!TIP]
> FTP経由で接続して`output.tar.gz`ファイルを変更するだけでは、ウェブアプリで実行されるコードを変更するには不十分です。
**攻撃者はこのファイルをダウンロードし、変更して再アップロードすることで、ウェブアプリで任意のコードを実行することができます。**
### GithubからのPython
このチュートリアルは前のものに基づいていますが、Githubリポジトリを使用しています。
1. Githubアカウントでリポジトリmsdocs-python-flask-webapp-quickstartをフォークします。
2. Azureで新しいPython Web Appを作成します。
3. `Deployment Center`でソースを変更し、Githubでログインし、フォークしたリポジトリを選択して`Save`をクリックします。
前のケースと同様に、SCMポータルにログインするか、FTP経由でログインすると、`/wwwroot`にウェブアプリのコードを含む圧縮ファイル`output.tar.gz`を見ることができます。
> [!TIP]
> FTP経由で接続して`output.tar.gz`ファイルを変更し、デプロイメントを再トリガーするだけでは、ウェブアプリで実行されるコードを変更するには不十分です。
## 特権昇格
{{#ref}}
../az-privilege-escalation/az-app-services-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}}