Az - CosmosDB

Tip

学习并练习 AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

Azure CosmosDB

Azure Cosmos DB 是一个完全 托管的 NoSQL、关系型和向量数据库,提供单毫秒级的响应时间、自动扩展和具有企业级安全性的 SLA 支持的可用性。它通过即插即用的多区域数据分发、流行语言的开源 API、SDK 和集成向量支持及无缝 Azure AI 集成等 AI 数据库功能,加速应用程序开发。

Azure Cosmos DB 提供多种数据库 API,以使用文档、关系型、键值、图形和列族数据模型来建模现实世界的数据,这些 API 包括 NoSQL、MongoDB、PostgreSQL、Cassandra、Gremlin 和 Table。

CosmosDB 的一个关键方面是 Azure Cosmos 账户。Azure Cosmos 账户 作为数据库的入口点。该账户决定关键设置,如全球分布、一致性级别和要使用的特定 API,例如 NoSQL。通过该账户,您可以配置全球复制,以确保数据在多个区域可用,以实现低延迟访问。此外,您可以选择在性能和数据准确性之间取得平衡的一致性级别,选项范围从强一致性到最终一致性。

Azure Cosmos DB 支持 用户分配的身份系统分配的托管身份,这些身份会自动创建并与资源的生命周期绑定。然而,Cosmos DB 没有内置机制直接查询外部数据源,如 Azure Blob Storage。与 SQL Server 的外部表功能不同,Cosmos DB 需要使用 Azure Data Factory、数据迁移工具或自定义脚本等外部工具将数据导入其容器,然后才能使用其本地查询功能进行查询。

NoSQL

Azure Cosmos DB NoSQL API 是一个基于文档的 API,使用 JSON 作为其数据格式。它提供类似 SQL 的查询语法来查询 JSON 对象,使其适合处理结构化和半结构化数据。该服务的端点是:

https://<Account-Name>.documents.azure.com:443/

数据库

在一个帐户内,您可以创建一个或多个数据库,这些数据库作为容器的逻辑分组。数据库充当资源管理和用户权限的边界。数据库可以让多个容器使用共享的性能容量池,或者为每个容器提供其自己的专用能力。

容器

数据存储的核心单元是容器,它保存 JSON 文档并自动建立索引以便高效查询。容器具有弹性可扩展性,并分布在由用户定义的分区键确定的分区中。分区键对于确保最佳性能和均匀数据分布至关重要。例如,一个容器可能存储客户数据,“customerId” 作为分区键。

关键特性

  • 全球分布:启用或禁用地理冗余以进行跨区域复制和多区域写入以提高可用性。
  • 网络与安全:在公共(所有/选择网络)或私有端点之间进行连接。使用 TLS 1.2 加密进行安全连接。支持 CORS(跨源资源共享)以控制对资源的访问。可以启用 Microsoft Defender for Cloud。要建立连接,您可以使用密钥。
  • 备份与恢复:从定期、连续(7 天)或连续(30 天)备份策略中选择,具有可配置的间隔和保留期。
  • 数据加密:默认服务管理密钥或客户管理密钥(CMK)用于加密(CMK 选择是不可逆的)。

枚举

# CosmoDB Account
## List Azure Cosmos DB database accounts.
az cosmosdb list --resource-group <ResourceGroupName>
az cosmosdb show --resource-group <ResourceGroupName> --name <AccountName>

## Lists the virtual network accounts associated with a Cosmos DB account
az cosmosdb network-rule list --resource-group <ResourceGroupName> --name <AccountName>
## List the access keys or connection strings for a Azure Cosmos DB
az cosmosdb keys list --name <AccountName> --resource-group <ResourceGroupName>
## List all the database accounts that can be restored.
az cosmosdb restorable-database-account list --account-name <AccountName>
## Show the identities for a Azure Cosmos DB database account.
az cosmosdb identity show --resource-group <ResourceGroupName> --name <AccountName>


# CosmoDB (NoSQL)
## List the NoSQL databases under an Azure Cosmos DB account.
az cosmosdb sql database list --resource-group <ResourceGroupName> --account-name <AccountName>
## List the NoSQL containers under an Azure Cosmos DB NoSQL database.
az cosmosdb sql container list --account-name <AccountName> --database-name <DatabaseName> --resource-group <ResourceGroupName>

## List all NoSQL role assignments under an Azure Cosmos DB
az cosmosdb sql role assignment list --resource-group <ResourceGroupName> --account-name <AccountName>
## List all NoSQL role definitions under an Azure Cosmos DB
az cosmosdb sql role definition list --resource-group <ResourceGroupName> --account-name <AccountName>

## List the NoSQL stored procedures under an Azure Cosmos DB
az cosmosdb sql stored-procedure list --account-name <AccountName> --container-name <ContainerName> --database-name <DatabaseName> --resource-group <ResourceGroupName>
## List the NoSQL triggers under an Azure Cosmos DB NoSQL container.
az cosmosdb sql trigger list --account-name <AccountName> --container-name <ContainerName> --database-name <DatabaseName> --resource-group <ResourceGroupName>
## List the NoSQL user defined functions under an Azure Cosmos DB NoSQL container
az cosmosdb sql user-defined-function list --account-name <AccountName> --container-name <ContainerName> --database-name <DatabaseName> --resource-group <ResourceGroupName>


## MongoDB (vCore)
# Install az cli extension
az extension add --name cosmosdb-preview
# List all MongoDB databases in a specified Azure Cosmos DB account
az cosmosdb mongocluster list
az cosmosdb mongocluster show --cluster-name <name> --resource-group <ResourceGroupName>
# Get firewall rules
az cosmosdb mongocluster firewall rule list --cluster-name <name> --resource-group <ResourceGroupName>
# Connect to in
brew install mongosh
mongosh "mongodb://<username>:<password>@<account-name>.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retryWrites=false" --username <username> --password <password>

连接

它有两种关键类型,读写(完全)和只读。它们提供对Cosmos DB帐户内所有数据库、集合和数据的指示访问。 要连接azure-cosmosDB(pip install azure-cosmos)库是必需的。此外,端点和密钥是建立连接的关键组件。

from azure.cosmos import CosmosClient, PartitionKey

# Connection details
endpoint = "<your-account-endpoint>"
key = "<your-account-key>"

# Initialize Cosmos Client
client = CosmosClient(endpoint, key)

# Access existing database and container
database_name = '<SampleDB>'
container_name = '<SampleContainer>'
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)

另一种建立连接的方法是使用 DefaultAzureCredential()。只需使用具有权限的帐户登录 (az login) 并执行它。对于这种情况,必须进行角色分配,以授予必要的权限 (see for mor)

from azure.identity import DefaultAzureCredential
from azure.cosmos import CosmosClient

# Use Azure AD for authentication
credential = DefaultAzureCredential()
endpoint = "<your-account-endpoint>"
client = CosmosClient(endpoint, credential)

# Access database and container
database_name = "<mydatabase>"
container_name = "<mycontainer>"
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.")

MongoDB

MongoDB NoSQL API 是一个基于文档的 API,使用类似 JSON 的 BSON(Binary JSON)作为其数据格式。它提供了具有聚合能力的查询语言,适合处理结构化、半结构化和非结构化数据。服务的端点通常遵循以下格式:

mongodb://<hostname>:<port>/<database>

数据库

在 MongoDB 中,您可以在一个实例中创建一个或多个数据库。每个数据库作为集合的逻辑分组,并提供资源组织和管理的边界。数据库有助于逻辑上分离和管理数据,例如用于不同的应用程序或项目。

集合

MongoDB 中的数据存储核心单元是集合,它保存文档,并设计用于高效查询和灵活的模式设计。集合具有弹性可扩展性,并可以支持在分布式设置中跨多个节点的高吞吐量操作。

请求单位 (RU) 类型的关键特性

全球分布:启用或禁用跨区域复制的地理冗余和多区域写入以提高可用性。
网络与安全:在公共(所有/选择网络)或私有端点之间进行连接。使用 TLS 1.2 加密的安全连接。支持 CORS(跨源资源共享)以控制对资源的访问。要建立连接,您可以使用密钥。
备份与恢复:从定期、连续(7 天,免费)或连续(30 天,付费)备份策略中进行选择,具有可配置的间隔和保留。
数据加密:默认服务管理密钥或客户管理密钥(CMK)用于加密(CMK 选择是不可逆的)。

vCore 集群类型的关键特性

全球分布:在另一个 Azure 区域启用只读副本以实现高可用性和故障转移支持。配置副本名称、区域和每个分片的存储。
网络与安全:支持公共访问,分配公共 IP 和私有访问。使用防火墙规则限制连接——默认情况下,不允许公共 IP。
加密连接:强制使用 TLS 加密以确保数据传输的安全性。

枚举

# CosmoDB Account
## List Azure Cosmos DB database accounts.
az cosmosdb list --resource-group <ResourceGroupName>
az cosmosdb show --resource-group <ResourceGroupName> --name <AccountName>

## Lists the virtual network accounts associated with a Cosmos DB account
az cosmosdb network-rule list --resource-group <ResourceGroupName> --name <AccountName>
## List the access keys or connection strings for a Azure Cosmos DB
az cosmosdb keys list --name <AccountName> --resource-group <ResourceGroupName>
## List all the database accounts that can be restored.
az cosmosdb restorable-database-account list --account-name <AccountName>
## Show the identities for a Azure Cosmos DB database account.
az cosmosdb identity show --resource-group <ResourceGroupName> --name <AccountName>

## MongoDB
# List all MongoDB databases in a specified Azure Cosmos DB account
az cosmosdb mongodb database list --account-name <AccountName> --resource-group <ResourceGroupName>
# List all collections in a specific MongoDB database within an Azure Cosmos DB account
az cosmosdb mongodb collection list --account-name <AccountName> --database-name <DatabaseName> --resource-group <ResourceGroupName>

#RBAC FUNCTIONALITIES MUST BE ENABLED TO USE THIS
# List all role definitions for MongoDB within an Azure Cosmos DB account
az cosmosdb mongodb role definition list --account-name <AccountName> --resource-group <ResourceGroupName>
# List all user definitions for MongoDB within an Azure Cosmos DB account
az cosmosdb mongodb user definition list --account-name <AccountName> --resource-group <ResourceGroupName>

## MongoDB (vCore)
# Install az cli extension
az extension add --name cosmosdb-preview
# List all MongoDB databases in a specified Azure Cosmos DB account
az cosmosdb mongocluster list
az cosmosdb mongocluster show --cluster-name <name> --resource-group <ResourceGroupName>
# Get firewall rules
az cosmosdb mongocluster firewall rule list --cluster-name <name> --resource-group <ResourceGroupName>
# Connect to in
brew install mongosh
mongosh "mongodb://<username>:<password>@<account-name>.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retryWrites=false" --username <username> --password <password>

连接

RU MongoDB 类型在 CosmoDB 中有 2 种关键类型,读写(完整)和只读。它们提供对 Cosmos DB 账户内所有数据库、集合和数据的指示访问。 对于密码,您可以使用密钥或使用特权提升部分中描述的方法。

from pymongo import MongoClient

# Updated connection string with retryWrites=false
connection_string = "mongodb://<account-name>.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retryWrites=false"

# Create the client. The password and username is a custom one if the type is "vCore cluster".
# In case that is a Request unit (RU) the username is the account name and the password is the key of the cosomosDB account.
client = MongoClient(connection_string, username="<username>", password="<password>")

# Access the database
db = client['<database>']

# Access a collection
collection = db['<collection>']

# 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}")

或使用 mongo 中的用户:

mongosh "mongodb://<myUser>:<mySecurePassword>@<account_name>.mongo.cosmos.azure.com:10255/<mymongodatabase>?ssl=true&replicaSet=globaldb&retrywrites=false"

参考文献

权限提升

Az - CosmosDB Privesc

后期利用

Az - SQL Post Exploitation

待办事项

  • 这里的其余数据库,表,cassandra,gremlin…
  • 查看后期利用 “Microsoft.DocumentDB/databaseAccounts/mongodbUserDefinitions/write” && “Microsoft.DocumentDB/databaseAccounts/mongodbUserDefinitions/read” 和角色定义,因为这里可能存在权限提升
  • 查看恢复

Tip

学习并练习 AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks