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

This commit is contained in:
Translator
2025-01-09 08:11:38 +00:00
parent 32c5c30cf7
commit dc05dfd19b
373 changed files with 3 additions and 553 deletions

View File

@@ -1,271 +0,0 @@
# Az - App Services
{{#include ../../../banners/hacktricks-training.md}}
## App Service Basic Information
Azure App Servicesは、開発者が**ウェブアプリケーション、モバイルアプリのバックエンド、およびAPIをシームレスに構築、デプロイ、スケール**できるようにします。複数のプログラミング言語をサポートし、機能と管理を強化するためにさまざまなAzureツールやサービスと統合されています。
各アプリはサンドボックス内で実行されますが、隔離はApp Serviceプランに依存します。
- 無料および共有プランのアプリは共有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 AppsはAzure App Serviceのサブセット**であり、ウェブコンソールにアクセスしてすべてのアプリサービスをリスト表示するか、az cliで`az webapp list`を実行すると、**Function Appsもここにリストされているのを見ることができます**。
実際、App Servicesが使用する**セキュリティ関連の機能**az cliの`webapp`)は、**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
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`ファイルを変更し、デプロイを再トリガーするだけでは、ウェブアプリで実行されるコードを変更するには不十分です。
## 参考文献
- [https://learn.microsoft.com/en-in/azure/app-service/overview](https://learn.microsoft.com/en-in/azure/app-service/overview)
{{#include ../../../banners/hacktricks-training.md}}