Az - CosmosDB

Reading time: 16 minutes

tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें

Azure CosmosDB

Azure Cosmos DB एक पूरी तरह से प्रबंधित NoSQL, रिलेशनल, और वेक्टर डेटाबेस है जो एकल अंकों में मिलीसेकंड प्रतिक्रिया समय, स्वचालित स्केलेबिलिटी, और उद्यम-ग्रेड सुरक्षा के साथ SLA-समर्थित उपलब्धता प्रदान करता है। यह टर्नकी मल्टी-क्षेत्र डेटा वितरण, ओपन-सोर्स APIs, लोकप्रिय भाषाओं के लिए SDKs, और AI डेटाबेस सुविधाओं जैसे एकीकृत वेक्टर समर्थन और निर्बाध Azure AI एकीकरण के माध्यम से तेज ऐप विकास को सक्षम बनाता है।

Azure Cosmos DB कई डेटाबेस APIs प्रदान करता है जो दस्तावेज़ों, रिलेशनल, की-वैल्यू, ग्राफ, और कॉलम-परिवार डेटा मॉडल का उपयोग करके वास्तविक दुनिया के डेटा को मॉडल करने के लिए हैं, ये APIs NoSQL, MongoDB, PostgreSQL, Cassandra, Gremlin और Table हैं।

CosmosDB का एक प्रमुख पहलू Azure Cosmos Account है। Azure Cosmos Account डेटाबेस के लिए प्रवेश बिंदु के रूप में कार्य करता है। खाता वैश्विक वितरण, स्थिरता स्तर, और उपयोग किए जाने वाले विशिष्ट 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 को अपने डेटा प्रारूप के रूप में उपयोग करता है। यह JSON वस्तुओं को क्वेरी करने के लिए SQL-जैसी क्वेरी सिंटैक्स प्रदान करता है, जिससे यह संरचित और अर्ध-संरचित डेटा के साथ काम करने के लिए उपयुक्त बनाता है। सेवा का एंडपॉइंट है:

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

Databases

एक खाते के भीतर, आप एक या अधिक डेटाबेस बना सकते हैं, जो कंटेनरों के तार्किक समूह के रूप में कार्य करते हैं। एक डेटाबेस संसाधन प्रबंधन और उपयोगकर्ता अनुमतियों के लिए एक सीमा के रूप में कार्य करता है। डेटाबेस कई कंटेनरों को साझा प्रदर्शन क्षमता का उपयोग करने की अनुमति दे सकते हैं या प्रत्येक कंटेनर को अपनी खुद की समर्पित शक्ति दे सकते हैं।

Containers

डेटा भंडारण की मुख्य इकाई कंटेनर है, जो JSON दस्तावेज़ों को रखता है और कुशल क्वेरी के लिए स्वचालित रूप से अनुक्रमित होता है। कंटेनर लचीले ढंग से स्केलेबल होते हैं और विभाजनों में वितरित होते हैं, जो उपयोगकर्ता द्वारा परिभाषित विभाजन कुंजी द्वारा निर्धारित होते हैं। विभाजन कुंजी अनुकूल प्रदर्शन और समान डेटा वितरण सुनिश्चित करने के लिए महत्वपूर्ण है। उदाहरण के लिए, एक कंटेनर ग्राहक डेटा को स्टोर कर सकता है, जिसमें "customerId" विभाजन कुंजी के रूप में हो सकता है।

Key Features

  • Global Distribution: क्रॉस-क्षेत्र प्रतिकृति के लिए Geo-Redundancy को सक्षम या अक्षम करें और बेहतर उपलब्धता के लिए Multi-region Writes।
  • Networking & Security: कनेक्टिविटी के लिए सार्वजनिक (सभी/चुनिंदा नेटवर्क) या निजी एंडपॉइंट के बीच। TLS 1.2 एन्क्रिप्शन के साथ सुरक्षित कनेक्शन। संसाधनों तक नियंत्रित पहुंच के लिए CORS (Cross-Origin Resource Sharing) का समर्थन करता है। Microsoft Defender for Cloud को सक्षम किया जा सकता है। कनेक्शन बनाने के लिए आप कुंजी का उपयोग कर सकते हैं।
  • Backup & Recovery: समय-समय पर, निरंतर (7 दिन), या निरंतर (30 दिन) बैकअप नीतियों से कॉन्फ़िगर करने योग्य अंतराल और संरक्षण के साथ।
  • Data Encryption: एन्क्रिप्शन के लिए डिफ़ॉल्ट सेवा-प्रबंधित कुंजी या ग्राहक-प्रबंधित कुंजी (CMK) (CMK चयन अपरिवर्तनीय है)।

Enumeration

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

कनेक्शन

इसके 2 प्रमुख प्रकार हैं, पढ़ने-लिखने (पूर्ण) और केवल पढ़ने। ये सभी डेटाबेस, संग्रह और Cosmos DB खाते के अंदर डेटा तक निर्दिष्ट पहुंच प्रदान करते हैं। कनेक्ट करने के लिए azure-cosmosDB (pip install azure-cosmos) पुस्तकालय की आवश्यकता है। इसके अतिरिक्त, एंडपॉइंट और कुंजी कनेक्शन बनाने के लिए महत्वपूर्ण घटक हैं।

python
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) जिसके पास अनुमतियाँ हैं और इसे निष्पादित करना है। इस मामले में एक भूमिका असाइनमेंट किया जाना चाहिए, आवश्यक अनुमतियाँ देने के लिए (अधिक जानकारी के लिए देखें)

python
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) का उपयोग करता है। यह एक क्वेरी भाषा प्रदान करता है जिसमें समेकन क्षमताएँ होती हैं, जिससे यह संरचित, अर्ध-संरचित, और असंरचित डेटा के साथ काम करने के लिए उपयुक्त होता है। सेवा का एंडपॉइंट आमतौर पर इस प्रारूप का पालन करता है:

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

Databases

MongoDB में, आप एक या एक से अधिक डेटाबेस एक उदाहरण के भीतर बना सकते हैं। प्रत्येक डेटाबेस संग्रहों का एक तार्किक समूह के रूप में कार्य करता है और संसाधन संगठन और प्रबंधन के लिए एक सीमा प्रदान करता है। डेटाबेस डेटा को तार्किक रूप से अलग करने और प्रबंधित करने में मदद करते हैं, जैसे कि विभिन्न अनुप्रयोगों या परियोजनाओं के लिए।

Collections

MongoDB में डेटा संग्रहण की मुख्य इकाई संग्रह है, जो दस्तावेज़ों को रखती है और कुशल क्वेरीिंग और लचीले स्कीमा डिज़ाइन के लिए डिज़ाइन की गई है। संग्रह लचीले ढंग से स्केलेबल होते हैं और वितरित सेटअप में कई नोड्स के बीच उच्च-थ्रूपुट संचालन का समर्थन कर सकते हैं।

Key Features of Request unit (RU) type

Global Distribution: क्रॉस-क्षेत्र प्रतिकृति के लिए Geo-Redundancy को सक्षम या अक्षम करें और बेहतर उपलब्धता के लिए Multi-region Writes।
Networking & Security: कनेक्टिविटी के लिए सार्वजनिक (सभी/चुनिंदा नेटवर्क) या निजी एंडपॉइंट्स के बीच। TLS 1.2 एन्क्रिप्शन के साथ सुरक्षित कनेक्शन। संसाधनों तक नियंत्रित पहुंच के लिए CORS (Cross-Origin Resource Sharing) का समर्थन करता है। कनेक्शन बनाने के लिए आप कुंजी का उपयोग कर सकते हैं।
Backup & Recovery: समय-समय पर, निरंतर (7 दिन, मुफ्त), या निरंतर (30 दिन, भुगतान) बैकअप नीतियों से कॉन्फ़िगर करने योग्य अंतराल और संरक्षण के साथ।
Data Encryption: एन्क्रिप्शन के लिए डिफ़ॉल्ट सेवा-प्रबंधित कुंजी या ग्राहक-प्रबंधित कुंजी (CMK) (CMK चयन अपरिवर्तनीय है)।

Key Features of vCore cluster type

Global Distribution: उच्च उपलब्धता और फेलओवर समर्थन के लिए दूसरे Azure क्षेत्र में एक पढ़ने की प्रति सक्षम करें। प्रति शार्ड प्रति नाम, क्षेत्र और भंडारण कॉन्फ़िगर करें।
Networking & Security: असाइन किए गए सार्वजनिक आईपी के साथ सार्वजनिक पहुंच का समर्थन करता है और निजी पहुंच। फ़ायरवॉल नियमों का उपयोग करके कनेक्शनों को प्रतिबंधित करें—डिफ़ॉल्ट रूप से, कोई सार्वजनिक आईपी की अनुमति नहीं है।
Encrypted Connections: सुरक्षित डेटा ट्रांसमिशन के लिए TLS एन्क्रिप्शन को लागू करता है।

Enumeration

bash
# 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 खाते के सभी डेटाबेस, संग्रह और डेटा तक निर्दिष्ट पहुंच प्रदान करते हैं। पासवर्ड के लिए आप कुंजी का उपयोग कर सकते हैं या प्रिवेस्क अनुभाग में वर्णित विधि का उपयोग कर सकते हैं।

python
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 के भीतर एक उपयोगकर्ता का उपयोग करके:

bash
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

करने के लिए

  • यहाँ बाकी DB, टेबल, कैसेंड्रा, ग्रेमलिन...
  • पोस्ट एक्सप्लोइटेशन "Microsoft.DocumentDB/databaseAccounts/mongodbUserDefinitions/write" && "Microsoft.DocumentDB/databaseAccounts/mongodbUserDefinitions/read" और भूमिका परिभाषाओं पर एक नज़र डालें क्योंकि यहाँ विशेषाधिकार वृद्धि हो सकती है
  • पुनर्स्थापनों पर एक नज़र डालें

tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें