# Az - CosmosDB {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * Check the [**subscription plans**](https://github.com/sponsors/carlospolop)! * **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks_live)**.** * **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %} ## Azure CosmosDB **Azure Cosmos DB** は、単一桁のミリ秒応答時間、自動スケーラビリティ、エンタープライズグレードのセキュリティを備えた SLA バックの可用性を提供する完全に **管理された NoSQL、リレーショナル、ベクトルデータベース** です。これは、ターンキーのマルチリージョンデータ配信、オープンソースAPI、人気のある言語用のSDK、統合ベクトルサポートやシームレスなAzure AI統合などのAIデータベース機能を通じて、アプリ開発を加速します。 Azure Cosmos DB は、ドキュメント、リレーショナル、キー-バリュー、グラフ、カラムファミリーデータモデルを使用して現実のデータをモデル化するための複数のデータベースAPIを提供し、これらのAPIはNoSQL、MongoDB、PostgreSQL、Cassandra、Gremlin、Tableです。 CosmosDBの重要な側面の1つはAzure Cosmos Accountです。**Azure Cosmos Account** は、データベースへのエントリーポイントとして機能します。このアカウントは、グローバル配信、一貫性レベル、使用する特定のAPI(NoSQLなど)などの主要な設定を決定します。アカウントを通じて、データが複数のリージョンで低遅延アクセスのために利用可能であることを保証するために、グローバルレプリケーションを構成できます。さらに、パフォーマンスとデータの正確性のバランスを取る一貫性レベルを選択でき、強い一貫性から最終的な一貫性までのオプションがあります。 ### NoSQL (sql) Azure Cosmos DB NoSQL API は、JSON をデータ形式として使用するドキュメントベースの API です。これは、JSON オブジェクトをクエリするための SQL に似たクエリ構文を提供し、構造化データおよび半構造化データの操作に適しています。サービスのエンドポイントは次のとおりです: {% code overflow="wrap" %} ```bash https://.documents.azure.com:443/ ``` {% endcode %} #### データベース アカウント内では、1つ以上のデータベースを作成でき、これらはコンテナの論理的なグループとして機能します。データベースは、リソース管理とユーザー権限の境界として機能します。データベースは、コンテナ間でプロビジョニングされたスループットを共有するか、個々のコンテナに専用のスループットを割り当てることができます。 #### コンテナ データストレージのコアユニットはコンテナであり、JSONドキュメントを保持し、効率的なクエリのために自動的にインデックスが付けられます。コンテナは弾力的にスケーラブルで、ユーザー定義のパーティションキーによって決定されるパーティション全体に分散されます。パーティションキーは、最適なパフォーマンスと均等なデータ分配を確保するために重要です。たとえば、コンテナは顧客データを保存し、「customerId」をパーティションキーとして使用することがあります。 #### 列挙 {% tabs %} {% tab title="az cli" %} {% code overflow="wrap" %} ```bash # CosmoDB Account ## List Azure Cosmos DB database accounts. az cosmosdb list --resource-group az cosmosdb show --resource-group --name ## Lists the virtual network accounts associated with a Cosmos DB account az cosmosdb network-rule list --resource-group --name ## List the access keys or connection strings for a Azure Cosmos DB az cosmosdb keys list --name --resource-group ## List all the database accounts that can be restored. az cosmosdb restorable-database-account list --account-name ## Show the identities for a Azure Cosmos DB database account. az cosmosdb identity show --resource-group --name # CosmoDB (NoSQL) ## List the SQL databases under an Azure Cosmos DB account. az cosmosdb sql database list --resource-group --account-name ## List the SQL containers under an Azure Cosmos DB SQL database. az cosmosdb sql container list --account-name --database-name --resource-group ## List all SQL role assignments under an Azure Cosmos DB az cosmosdb sql role assignment list --resource-group --account-name ## List all SQL role definitions under an Azure Cosmos DB az cosmosdb sql role definition list --resource-group --account-name ## List the SQL stored procedures under an Azure Cosmos DB az cosmosdb sql stored-procedure list --account-name --container-name --database-name --resource-group ## List the SQL triggers under an Azure Cosmos DB SQL container. az cosmosdb sql trigger list --account-name --container-name --database-name --resource-group ## List the SQL user defined functions under an Azure Cosmos DB SQL container az cosmosdb sql user-defined-function list --account-name --container-name --database-name --resource-group ``` {% endcode %} {% endtab %} {% tab title="Az PowerShell" %} {% code overflow="wrap" %} ```bash Get-Command -Module Az.CosmosD # List all Cosmos DB accounts in a specified resource group. Get-AzCosmosDBAccount -ResourceGroupName "" # Get the access keys for a specific Cosmos DB account. Get-AzCosmosDBAccountKey -ResourceGroupName "" -Name "" # Retrieve the client encryption keys for a specific Cosmos DB account. Get-AzCosmosDbClientEncryptionKey -ResourceGroupName "" -AccountName "" -DatabaseName "" # List all SQL containers in a specific Cosmos DB SQL database. Get-AzCosmosDBSqlContainer -ResourceGroupName "" -AccountName "" -DatabaseName "" # Get backup information for a specific Cosmos DB SQL container. Get-AzCosmosDBSqlContainerBackupInformation -ResourceGroupName "" -AccountName "" -DatabaseName "" -Name "" -Location "" # Get the throughput (RU/s) settings for a specific Cosmos DB SQL container. Get-AzCosmosDBSqlContainerThroughput -ResourceGroupName "" -AccountName "" -DatabaseName "" -Name "" # List all SQL databases under a specific Cosmos DB account. Get-AzCosmosDBSqlDatabase -ResourceGroupName "" -AccountName "" # Get the throughput (RU/s) settings for a specific Cosmos DB SQL database. Get-AzCosmosDBSqlDatabaseThroughput -ResourceGroupName "" -AccountName "" -Name "" # List all SQL role assignments for a specific Cosmos DB account. Get-AzCosmosDBSqlRoleAssignment -ResourceGroupName "" -AccountName "" # List all SQL role definitions for a specific Cosmos DB account. Get-AzCosmosDBSqlRoleDefinition -ResourceGroupName "" -AccountName "" # List all stored procedures in a specific Cosmos DB SQL container. Get-AzCosmosDBSqlStoredProcedure -ResourceGroupName "" -AccountName "" -DatabaseName "" -ContainerName "" # List all triggers in a specific Cosmos DB SQL container. Get-AzCosmosDBSqlTrigger -ResourceGroupName "" -AccountName "" -DatabaseName "" -ContainerName "" # List all user-defined functions (UDFs) in a specific Cosmos DB SQL container. Get-AzCosmosDBSqlUserDefinedFunction -ResourceGroupName "" -AccountName "" -DatabaseName "" -ContainerName "" ``` {% endcode %} {% endtab %} {% endtabs %} #### 接続 azure-cosmosDB(pip install azure-cosmos)ライブラリが必要です。さらに、エンドポイントとキーは接続を確立するための重要な要素です。 {% code overflow="wrap" %} ```python from azure.cosmos import CosmosClient, PartitionKey # Connection details endpoint = "" key = "" # Initialize Cosmos Client client = CosmosClient(endpoint, key) # Access existing database and container database_name = '' container_name = '' database = client.get_database_client(database_name) container = database.get_container_client(container_name) # Insert multiple documents items_to_insert = [ {"id": "1", "name": "Sample Item", "description": "This is a sample document."}, {"id": "2", "name": "Another Sample Item", "description": "This is another sample document."}, {"id": "3", "name": "Sample Item", "description": "This is a duplicate name sample document."}, ] for item in items_to_insert: container.upsert_item(item) # Query all documents query = "SELECT * FROM c" all_items = list(container.query_items( query=query, enable_cross_partition_query=True )) # Print all queried items print("All items in the container:") for item in all_items: print(item) ``` {% endcode %} 接続を確立する別の方法は、**DefaultAzureCredential()**を使用することです。権限を持つアカウントでログイン(az login)するだけで実行できます。この場合、必要な権限を与える役割の割り当てを行う必要があります(詳細については参照してください)。 {% code overflow="wrap" %} ```python from azure.identity import DefaultAzureCredential from azure.cosmos import CosmosClient # Use Azure AD for authentication credential = DefaultAzureCredential() endpoint = "" client = CosmosClient(endpoint, credential) # Access database and container database_name = "" container_name = "" database = client.get_database_client(database_name) container = database.get_container_client(container_name) # Insert a document item = { "id": "1", "name": "Sample Item", "description": "This is a test item." } container.create_item(item) print("Document inserted.") ``` {% endcode %} ### MongoDB MongoDB NoSQL APIは、JSONに似たBSON(Binary JSON)をデータ形式として使用するドキュメントベースのAPIです。これは、集約機能を持つクエリ言語を提供し、構造化データ、半構造化データ、および非構造化データの処理に適しています。サービスのエンドポイントは通常、次の形式に従います: {% code overflow="wrap" %} ```bash mongodb://:/ ``` {% endcode %} #### データベース MongoDBでは、インスタンス内に1つ以上のデータベースを作成できます。各データベースはコレクションの論理的なグループとして機能し、リソースの組織と管理の境界を提供します。データベースは、異なるアプリケーションやプロジェクトのためにデータを論理的に分離し、管理するのに役立ちます。 #### コレクション MongoDBにおけるデータストレージのコアユニットはコレクションであり、ドキュメントを保持し、効率的なクエリと柔軟なスキーマ設計のために設計されています。コレクションは弾力的にスケーラブルで、分散セットアップ内の複数のノードにわたって高スループットの操作をサポートできます。 #### 列挙 {% tabs %} {% tab title="az cli" %} {% code overflow="wrap" %} ```bash # CosmoDB Account ## List Azure Cosmos DB database accounts. az cosmosdb list --resource-group az cosmosdb show --resource-group --name ## Lists the virtual network accounts associated with a Cosmos DB account az cosmosdb network-rule list --resource-group --name ## List the access keys or connection strings for a Azure Cosmos DB az cosmosdb keys list --name --resource-group ## List all the database accounts that can be restored. az cosmosdb restorable-database-account list --account-name ## Show the identities for a Azure Cosmos DB database account. az cosmosdb identity show --resource-group --name ## MongoDB # List all MongoDB databases in a specified Azure Cosmos DB account az cosmosdb mongodb database list --account-name --resource-group # List all collections in a specific MongoDB database within an Azure Cosmos DB account az cosmosdb mongodb collection list --account-name --database-name --resource-group # List all role definitions for MongoDB within an Azure Cosmos DB account az cosmosdb mongodb role definition list --account-name --resource-group # List all user definitions for MongoDB within an Azure Cosmos DB account az cosmosdb mongodb user definition list --account-name --resource-group ``` {% endcode %} {% endtab %} {% tab title="Az PowerShell" %} {% code overflow="wrap" %} ```bash Get-Command -Module Az.CosmosDB # List all Cosmos DB accounts in a specified resource group. Get-AzCosmosDBAccount -ResourceGroupName "" # Get the access keys for a specific Cosmos DB account. Get-AzCosmosDBAccountKey -ResourceGroupName "" -Name "" # Retrieve the client encryption keys for a specific Cosmos DB account. Get-AzCosmosDbClientEncryptionKey -ResourceGroupName "" -AccountName "" -DatabaseName "" # List all MongoDB collections in a specific database. Get-AzCosmosDBMongoDBCollection -AccountName -ResourceGroupName -DatabaseName # Retrieve backup information for a specific MongoDB collection in a database. Get-AzCosmosDBMongoDBCollectionBackupInformation -AccountName -ResourceGroupName -DatabaseName -Name -Location # Get the throughput (RU/s) of a specific MongoDB collection in a database. Get-AzCosmosDBMongoDBCollectionThroughput -AccountName -ResourceGroupName -DatabaseName -Name # List all MongoDB databases in a specified Cosmos DB account. Get-AzCosmosDBMongoDBDatabase -AccountName -ResourceGroupName # Get the throughput (RU/s) of a specific MongoDB database. Get-AzCosmosDBMongoDBDatabaseThroughput -AccountName -ResourceGroupName -DatabaseName # Retrieve the role definitions for MongoDB users in a specified Cosmos DB account. Get-AzCosmosDBMongoDBRoleDefinition -AccountName -ResourceGroupName ``` {% endcode %} {% endtab %} {% endtabs %} #### 接続 ここでは、パスワードをキーまたは特権昇格セクションで説明されている方法で見つけることができます。 {% code overflow="wrap" %} ```python from pymongo import MongoClient # Updated connection string with retryWrites=false connection_string = "mongodb://.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retryWrites=false" # Create the client client = MongoClient(connection_string, username="", password="") # Access the database db = client[''] # Access a collection collection = db[''] # Insert a single document document = { "name": "John Doe", "email": "johndoe@example.com", "age": 30, "address": { "street": "123 Main St", "city": "Somewhere", "state": "CA", "zip": "90210" } } # Insert document result = collection.insert_one(document) print(f"Inserted document with ID: {result.inserted_id}") ``` {% endcode %} ## 参考文献 * [https://learn.microsoft.com/en-us/azure/cosmos-db/choose-api](https://learn.microsoft.com/en-us/azure/cosmos-db/choose-api) * [https://learn.microsoft.com/en-us/azure/cosmos-db/](https://learn.microsoft.com/en-us/azure/cosmos-db/) * [https://learn.microsoft.com/en-us/azure/cosmos-db/introduction](https://learn.microsoft.com/en-us/azure/cosmos-db/introduction) * [https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/security/how-to-grant-data-plane-role-based-access?tabs=built-in-definition%2Ccsharp&pivots=azure-interface-cli](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/security/how-to-grant-data-plane-role-based-access?tabs=built-in-definition%2Ccsharp&pivots=azure-interface-cli) ## 権限昇格 {% content-ref url="../az-privilege-escalation/az-cosmosDB-privesc.md" %} [az-cosmosDB-privesc.md](../az-privilege-escalation/az-cosmosDB-privesc.md) {% endcontent-ref %} ## ポストエクスプロイト {% content-ref url="../az-post-exploitation/az-cosmosDB-post-exploitation.md" %} [az-cosmosDB-post-exploitation.md](../az-post-exploitation/az-sql-post-exploitation.md) {% endcontent-ref %} ## ToDo * ここに残りのDB、テーブル、Cassandra、Gremlin... * ポストエクスプロイトの "Microsoft.DocumentDB/databaseAccounts/mongodbUserDefinitions/write" と "Microsoft.DocumentDB/databaseAccounts/mongodbUserDefinitions/read" およびロール定義を確認してください。ここには権限昇格の可能性があります。 * 復元を確認してください。 {% hint style="success" %} AWSハッキングを学び、実践する:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ GCPハッキングを学び、実践する:[**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
HackTricksをサポートする * [**サブスクリプションプラン**](https://github.com/sponsors/carlospolop)を確認してください! * **💬 [**Discordグループ**](https://discord.gg/hRep4RUj7f)または[**Telegramグループ**](https://t.me/peass)に参加するか、**Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks_live)**をフォローしてください。** * **[**HackTricks**](https://github.com/carlospolop/hacktricks)および[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud)のGitHubリポジトリにPRを提出してハッキングトリックを共有してください。**
{% endhint %}