Translated ['src/pentesting-cloud/azure-security/az-enumeration-tools.md

This commit is contained in:
Translator
2025-01-03 19:25:44 +00:00
parent 2d8e69a62e
commit a5b1b1539c
3 changed files with 410 additions and 99 deletions

View File

@@ -2,47 +2,97 @@
{{#include ../../../banners/hacktricks-training.md}}
## App Service 基本情報
## App Service Basic Information
Azure App Services は、開発者が **ウェブアプリケーション、モバイルアプリのバックエンド、および API をシームレスに構築、展開、スケール** できるようにします。複数のプログラミング言語をサポートし、機能と管理を強化するためにさまざまな Azure ツールやサービスと統合されています。
Azure App Servicesは、開発者が**ウェブアプリケーション、モバイルアプリのバックエンド、およびAPIをシームレスに構築、デプロイ、スケール**できるようにします。複数のプログラミング言語をサポートし、機能と管理を強化するためにさまざまなAzureツールやサービスと統合されています。
各アプリはサンドボックス内で実行されますが、隔離は App Service プランに依存します。
各アプリはサンドボックス内で実行されますが、隔離はApp Serviceプランに依存します。
- 無料および共有プランのアプリは共有 VM で実行されます。
- スタンダードおよびプレミアムプランのアプリは専用 VM で実行されます。
- 無料および共有プランのアプリは共有VMで実行されます。
- スタンダードおよびプレミアムプランのアプリは専用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にコンテナをデプロイできます。
## Basic Authentication
ウェブアプリ通常はAzure関数を作成する際に、Basic Authenticationを有効にするかどうかを指定できます。これは基本的に**SCMとFTP**をアプリケーションに対して有効にするため、これらの技術を使用してアプリケーションをデプロイできるようになります。\
さらに、それらに接続するために、Azureは**ユーザー名、パスワード、URLを取得するためのAPI**を提供します。
`https://<SMC-URL>/BasicAuth`を使用してウェブブラウザからSCMに接続し、そこにあるすべてのファイルとデプロイメントを確認できます。
### Kudu
Kuduは、**Azure App ServiceおよびFunction Appsのためのデプロイメントエンジンおよび管理プラットフォーム**であり、Gitベースのデプロイメント、リモートデバッグ、およびウェブアプリケーションのファイル管理機能を提供します。ウェブアプリのSCM URLを通じてアクセスできます。
App ServicesとFunction Appsで使用されるKuduのバージョンは異なり、Function Appsのバージョンははるかに制限されています。
Kuduで見つけることができるいくつかの興味深いエンドポイントは次のとおりです
- `/DebugConsole`: Kuduが実行されている環境でコマンドを実行できるコンソール。
- この環境は**メタデータサービスにアクセスできない**ことに注意してください。
- `/webssh/host`: アプリが実行されているコンテナ内に接続できるウェブベースのSSHクライアント。
- この環境は**メタデータサービスにアクセスできる**ため、割り当てられた管理アイデンティティからトークンを取得できます。
- `/Env`: システム、アプリ設定、環境変数、接続文字列、およびHTTPヘッダーに関する情報を取得します。
- `/wwwroot/`: ウェブアプリのルートディレクトリ。ここからすべてのファイルをダウンロードできます。
## 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環境で実行されるバックグラウンドタスク**です。これにより、開発者はウェブアプリケーションと並行してスクリプトやプログラムを実行でき、ファイル処理、データ処理、またはスケジュールされたタスクなどの非同期または時間集約型の操作を簡単に処理できます。
Webジョブには2種類あります
- **Continuous**: 無限にループで実行され、作成されるとすぐにトリガーされます。常に処理が必要なタスクに最適です。ただし、Always Onが無効になっていて、過去20分間リクエストを受信していない場合、アプリが停止するとウェブジョブも停止します。
- **Triggered**: オンデマンドまたはスケジュールに基づいて実行されます。バッチデータの更新やメンテナンスルーチンなどの定期的なタスクに最適です。
Webジョブは、環境内で**コードを実行**し、接続された管理アイデンティティに**権限を昇格**させるために使用できるため、攻撃者の視点から非常に興味深いです。
さらに、Webジョブによって生成された**ログ**を確認することは常に興味深いです。これには**機密情報**が含まれている可能性があります。
### Slots
Azure App Service Slotsは、同じApp Serviceに**アプリケーションの異なるバージョンをデプロイ**するために使用されます。これにより、開発者は新しい機能や変更を本番環境にデプロイする前に別の環境でテストできます。
さらに、特定のスロットに**トラフィックの割合をルーティング**することが可能で、これは**A/Bテスト**やバックドア目的に便利です。
### Azure Function Apps
基本的に **Azure Function アプリは Azure App Service のサブセット** であり、ウェブコンソールにアクセスしてすべてのアプリサービスをリスト表示するか、az cli`az webapp list` を実行すると、**Function アプリもここにリストされているのが見える**でしょう
基本的に**Azure Function AppsはAzure App Serviceのサブセット**であり、ウェブコンソールにアクセスしてすべてのアプリサービスをリスト表示するか、az cli`az webapp list`を実行すると、**Function Appsもここにリストされているのを見ることができます**
実際、App Services が使用する **セキュリティ関連の機能**az cli`webapp`)は、**Function アプリでも使用されています**。
実際、App Servicesが使用する**セキュリティ関連の機能**az cli`webapp`)は、**Function Appsでも使用されています**。
## 基本認証
ウェブアプリ(通常は Azure Functionを作成する際に、基本認証を有効にするかどうかを指定することができます。これは基本的に **アプリケーションの SCM と FTP を有効にする** ため、これらの技術を使用してアプリケーションを展開できるようになります。\
さらに、それらに接続するために、Azure は **ユーザー名、パスワード、および SCM と FTP サーバーに接続するための URL を取得する API** を提供しています。
- 認証: az webapp auth show --name lol --resource-group lol_group
SSH
常時オン
デバッグ
### 列挙
### 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}"
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>
@@ -51,18 +101,24 @@ az webapp show --name <name> --resource-group <res-group>
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 configured Auth information
az webapp auth show --name <app-name> --resource-group <res-group>
# 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>
@@ -75,61 +131,39 @@ 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 connection strings of a webapp
az webapp config connection-string list --name <name> --resource-group <res-group>
# 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
az webapp config storage-account list --name <name> --resource-gl_group
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/"
# List all the functions
az functionapp list
# 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 info of 1 funciton (although in the list you already get this info)
az functionapp show --name <app-name> --resource-group <res-group>
## If "linuxFxVersion" has something like: "DOCKER|mcr.microsoft.com/..."
## This is using a container
# Get connections of a webapp
az webapp conection list --name <name> --resource-group <res-group>
# Get details about the source of the function code
az functionapp deployment source show \
--name <app-name> \
--resource-group <res-group>
## If error like "This is currently not supported."
## Then, this is probalby using a container
# Get more info if a container is being used
az functionapp config container show \
--name <name> \
--resource-group <res-group>
# Get settings (and privesc to the sorage account)
az functionapp config appsettings list --name <app-name> --resource-group <res-group>
# Check if a domain was assigned to a function app
az functionapp config hostname list --webapp-name <app-name> --resource-group <res-group>
# Get SSL certificates
az functionapp config ssl list --resource-group <res-group>
# Get network restrictions
az functionapp config access-restriction show --name <app-name> --resource-group <res-group>
# Get more info about a function (invoke_url_template is the URL to invoke and script_href allows to see the code)
az rest --method GET \
--url "https://management.azure.com/subscriptions/<subscription>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/functions?api-version=2024-04-01"
# Get source code with Master Key of the function
curl "<script_href>?code=<master-key>"
## Python example
curl "https://newfuncttest123.azurewebsites.net/admin/vfs/home/site/wwwroot/function_app.py?code=<master-key>" -v
# Get source code
az rest --url "https://management.azure.com/<subscription>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/hostruntime/admin/vfs/function_app.py?relativePath=1&api-version=2022-03-01"
# Get hybrid-connections of a webapp
az webapp hybrid-connections list --name <name> --resource-group <res-group>
```
{{#endtab }}
@@ -196,6 +230,40 @@ git clone 'https://<username>:<password>@name.scm.azurewebsites.net/repo-name.gi
../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`ファイルを変更し、デプロイを再トリガーするだけでは、ウェブアプリで実行されるコードを変更するには不十分です。
## 参考文献
- [https://learn.microsoft.com/en-in/azure/app-service/overview](https://learn.microsoft.com/en-in/azure/app-service/overview)