Az - CosmosDB

Tip

Leer & oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer & oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Leer & oefen Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Ondersteun HackTricks

Azure CosmosDB

Azure Cosmos DB is ’n volledig bestuurde NoSQL, relationele, en vektordatabasis wat enkel-digit millisekonde reaksietye, outomatiese skaalbaarheid, en SLA-ondersteunde beskikbaarheid met ondernemingsgraad sekuriteit bied. Dit stel vinniger app-ontwikkeling in staat deur kant-en-klaar multi-streek data verspreiding, oopbron API’s, SDK’s vir gewilde tale, en KI-databasis funksies soos geĆÆntegreerde vektorondersteuning en naatlose Azure KI-integrasie.

Azure Cosmos DB bied verskeie databasis API’s om werklike data te modelleer met behulp van dokumente, relationeel, sleutel-waarde, grafiek, en kolom-familie datamodelles, wat hierdie API’s NoSQL, MongoDB, PostgreSQL, Cassandra, Gremlin en Tabel is.

Een sleutel aspek van CosmosDB is Azure Cosmos Account. Azure Cosmos Account dien as die toegangspunt tot die databasisse. Die rekening bepaal sleutelinstellings soos globale verspreiding, konsekwentheidsvlakke, en die spesifieke API wat gebruik moet word, soos NoSQL. Deur die rekening kan jy globale replikaasies konfigureer om te verseker dat data beskikbaar is oor verskeie streke vir lae-latensie toegang. Boonop kan jy ’n konsekwentheidsvlak kies wat ’n balans tussen prestasie en datanauwkeurigheid handhaaf, met opsies wat wissel van Sterk tot Geleidelike konsekwentheid.

Azure Cosmos DB ondersteun gebruikers-toegewyde identiteite en sisteem-toegewyde bestuurde identiteite wat outomaties geskep en aan die hulpbron se lewensiklus gekoppel is. egter, Cosmos DB het nie ’n ingeboude meganisme om direk eksterne databasisse soos Azure Blob Storage te ondervra nie. Anders as SQL Server se eksterne tabel funksies, vereis Cosmos DB dat data in sy houers ingeneem word met behulp van eksterne gereedskap soos Azure Data Factory, die Data Migrasie Gereedskap, of pasgemaakte skripte voordat dit met sy inheemse ondervragingsvermoĆ«ns ondervra kan word.

NoSQL

Die Azure Cosmos DB NoSQL API is ’n dokument-gebaseerde API wat JSON as sy dataformaat gebruik. Dit bied ’n SQL-agtige ondervragingsintaksis vir die ondervraging van JSON-objekte, wat dit geskik maak vir die werk met gestruktureerde en semi-gestruktureerde data. Die eindpunt van die diens is:

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

Databasas

Binne ’n rekening kan jy een of meer databasas skep, wat dien as logiese groepe van houers. ’n Databasis dien as ’n grense vir hulpbronbestuur en gebruikersregte. Databasas kan of verskeie houers toelaat om ’n gedeelde poel van prestasiekapasiteit te gebruik, of elke houer sy eie toegewyde krag gee.

Houers

Die kern eenheid van data-opberging is die houer, wat JSON-dokumente bevat en outomaties geindexeer word vir doeltreffende navraag. Houers is elasties skaalbaar en versprei oor partisie, wat bepaal word deur ’n gebruiker-gedefinieerde partisie sleutel. Die partisie sleutel is krities vir die verseker van optimale prestasie en eweredige data verspreiding. Byvoorbeeld, ’n houer kan klantdata stoor, met ā€œcustomerIdā€ as die partisie sleutel.

Sleutelkenmerke

  • Globale Verspreiding: Aktiveer of deaktiveer Geo-Retensie vir kruis-streek replika en Multi-streek Skrywe vir verbeterde beskikbaarheid.
  • Netwerk & Sekuriteit: tussen openbare (alle/selekteer netwerke) of private eindpunte vir konnektiwiteit. Veilige verbindings met TLS 1.2 versleuteling. Ondersteun CORS (Cross-Origin Resource Sharing) vir beheerde toegang tot hulpbronne. Microsoft Defender vir Cloud kan geaktiveer word. Om die verbinding te maak, kan jy sleutels gebruik.
  • Rugsteun & Herstel: van Periodieke, KontinuĆÆteit (7 dae), of KontinuĆÆteit (30 dae) rugsteunbeleide met konfigureerbare tydsintervalle en behoud.
  • Data Versleuteling: Standaard diens-beheerde sleutels of klant-beheerde sleutels (CMK) vir versleuteling (CMK keuse is onomkeerbaar).

Enumerasie

# 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>

Verbinding

Dit het 2 sleutel tipes, Lees-skryf (vol) en Lees-alleen. Hulle gee die aangeduide toegang tot alle databasisse, versamelings, en data binne die Cosmos DB rekening. Om te verbind is die azure-cosmosDB (pip install azure-cosmos) biblioteek nodig. Boonop is die eindpunt en die sleutel kritieke komponente om die verbinding te maak.

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)

’n Ander manier om ’n verbinding te vestig, is om die DefaultAzureCredential() te gebruik. Jy moet net aanmeld (az login) met die rekening wat die toestemmings het en dit uitvoer. Vir hierdie geval moet ’n roltoewysing gedoen word, wat die nodige toestemmings gee (sien vir meer)

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

Die MongoDB NoSQL API is ’n dokument-gebaseerde API wat JSON-agtige BSON (Binary JSON) as sy dataformaat gebruik. Dit bied ’n navraagtaal met aggregasie vermoĆ«ns, wat dit geskik maak om met gestruktureerde, semi-gestruktureerde en ongestruktureerde data te werk. Die eindpunt van die diens volg tipies hierdie formaat:

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

Databases

In MongoDB kan jy een of meer databasisse binne ’n instansie skep. Elke databasis dien as ’n logiese groep van versamelings en bied ’n grense vir hulpbronorganisasie en -bestuur. Databasisse help om data logies te skei en te bestuur, soos vir verskillende toepassings of projekte.

Collections

Die kern eenheid van data-opberging in MongoDB is die versameling, wat dokumente hou en ontwerp is vir doeltreffende navraag en buigsame skema-ontwerp. Versamelings is elasties skaalbaar en kan hoĆ«-deurset operasies oor verskeie nodes in ’n verspreide opstelling ondersteun.

Key Features of Request unit (RU) type

Global Distribution: Aktiveer of deaktiveer Geo-Redundancy vir kruisgebied replikasie en Multi-region Writes vir verbeterde beskikbaarheid.
Networking & Security: tussen openbare (alle/selekteer netwerke) of private eindpunte vir konnektiwiteit. Veilige verbindings met TLS 1.2-enkripsie. Ondersteun CORS (Cross-Origin Resource Sharing) vir beheerde toegang tot hulpbronne. Om die verbinding te maak, kan jy sleutels gebruik.
Backup & Recovery: van Periodieke, KontinuĆÆteit (7 dae, gratis), of KontinuĆÆteit (30 dae, betaalde) rugsteunbeleide met konfigureerbare tussenposes en behoud.
Data Encryption: Standaard diens-beheerde sleutels of kliƫnt-beheerde sleutels (CMK) vir enkripsie (CMK-keuse is onomkeerbaar).

Key Features of vCore cluster type

Global Distribution: Aktiveer ’n lees replika in ’n ander Azure streek vir hoĆ« beskikbaarheid en failover ondersteuning. Konfigureer die replika naam, streek, en stoor per shard.
Networking & Security: Ondersteun openbare toegang met toegewyde openbare IP’s en private toegang. Beperk verbindings met firewall-reĆ«ls—per standaard, geen openbare IP’s is toegelaat.
Encrypted Connections: Handhaaf TLS-enkripsie vir veilige datatransmissie.

Enumeration

# 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>

Verbinding

RU MongoDB tipe in CosmoDB het 2 sleutel tipes, Lees-skrif (vol) en Lees-alleen. Hulle gee die aangeduide toegang tot alle databasisse, versamelings, en data binne die Cosmos DB rekening. Vir die wagwoord kan jy die sleutels gebruik of met die metode beskryf in die privesc afdeling.

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

Of deur ’n gebruiker binne die mongo:

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

Verwysings

Privilege Escalation

Az - CosmosDB Privesc

Post Exploitation

Az - SQL Post Exploitation

ToDo

  • Die res van die DB hier, tabelle, cassandra, gremlin…
  • Kyk na die post exploitation ā€œMicrosoft.DocumentDB/databaseAccounts/mongodbUserDefinitions/writeā€ && ā€œMicrosoft.DocumentDB/databaseAccounts/mongodbUserDefinitions/readā€ en rol definisies want hier mag daar ’n privesc wees
  • Kyk na herstelwerk

Tip

Leer & oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer & oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Leer & oefen Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Ondersteun HackTricks