AWS - ECS Enum

Tip

学んで実践する AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学んで実践する GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学んで実践する Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks をサポートする

ECS

Basic Information

Amazon Elastic Container Services or ECS は、クラウド上でコンテナ化されたアプリケーションをホストするためのプラットフォームを提供します。ECS には 2 つのデプロイ方式があり、EC2 インスタンスタイプと、serverless オプションの Fargate があります。このサービスにより、クラウド上でコンテナを非常に簡単かつストレスなく実行できます。

ECS は、次の 3 つの構成要素で動作します: ClustersServices、および Task Definitions

  • Clusters は、クラウド上で実行されているコンテナのグループです。前述のとおり、コンテナの起動タイプには EC2 と Fargate の 2 種類があります。AWS は EC2 起動タイプを、顧客が「管理する Amazon EC2 インスタンスの cluster 上でコンテナ化されたアプリケーションを実行する」ことを可能にするものと定義しています。Fargate も同様で、「バックエンドインフラをプロビジョニングおよび管理する必要なく、コンテナ化されたアプリケーションを実行できる」ものと定義されています。
  • Services は cluster 内で作成され、task の実行を担当します。Service 定義の中では、実行する task 数、自動スケーリング、capacity provider (Fargate/EC2/External)、 VPC、subnets、security groups などのnetworking 情報を定義します。
  • 2 種類のアプリケーションがあります:
  • Service: 停止と再起動が可能な長時間実行の計算処理を扱う task のグループ。たとえば web application。
  • Task: 実行して終了する standalone な task。たとえば batch job。
  • Service アプリケーションには、2 種類の service schedulers があります:
  • REPLICA: replica scheduling strategy は、cluster 全体に task を配置し、希望する数を維持します。何らかの理由で task が停止した場合、同じ node または別の node で新しい task が起動します。
  • DAEMON: 必要な要件を満たすすべての active container instance に、ちょうど 1 つの task を配置します。desired number of tasks、task placement strategy、Service Auto Scaling policies を指定する必要はありません。
  • Task Definitions は、どの container を実行するかと、container に対して設定される各種パラメータ、たとえば host との port mappingsenv variables、Docker entrypoint などを定義します…
  • env variables に sensitive info がないか確認してください!

Sensitive Data In Task Definitions

Task definitions は、ECS で実際に実行される containers を設定する役割を持ちます。Task definitions が container の動作を定義するため、そこには非常に多くの情報が含まれている可能性があります。

Pacu は ECS を列挙できます (list-clusters, list-container-instances, list-services, list-task-definitions)。また、task definitions をダンプすることもできます。

Enumeration

# Clusters info
aws ecs list-clusters
aws ecs describe-clusters --clusters <cluster>

# Container instances
## An Amazon ECS container instance is an Amazon EC2 instance that is running the Amazon ECS container agent and has been registered into an Amazon ECS cluster.
aws ecs list-container-instances --cluster <cluster>
aws ecs describe-container-instances --cluster <cluster> --container-instances <container_instance_arn>

# Services info
aws ecs list-services --cluster <cluster>
aws ecs describe-services --cluster <cluster> --services <services>
aws ecs describe-task-sets --cluster <cluster> --service <service>

# Task definitions
aws ecs list-task-definition-families
aws ecs list-task-definitions
aws ecs list-tasks --cluster <cluster>
aws ecs describe-tasks --cluster <cluster> --tasks <tasks>
## Look for env vars and secrets used from the task definition
aws ecs describe-task-definition --task-definition <TASK_NAME>:<VERSION>

ECS Agent State DB (agent.db) を使った On-Host Enumeration

shell access を ECS container instance 上で持っている場合、または /var/lib/ecs の host bind-mount を持つ container から escaped した場合(tasks が privileged で実行されている、または volumesFrom により host data dir が露出している場合によくある misconfiguration)、ECS agent は disk 上に agent.db を残します。これは AWS API call なしIAM permission なしCloudTrail を trigger せずに 読み取れます。

/var/lib/ecs/data/agent.db

(または、ホストが /host にマウントされている container から読む場合は、/host/var/lib/ecs/data/agent.db)。

# Most useful one-liner — dumps everything readable
strings /var/lib/ecs/data/agent.db

# From inside a container with the host mounted at /host
strings /host/var/lib/ecs/data/agent.db

# Filter for the highest-value artefacts
strings /var/lib/ecs/data/agent.db | grep -aE 'arn:aws:|AKIA|ASIA|"secret|password|TOKEN|credentials|taskRoleArn|executionRoleArn'

# Save the outcome from strings for offline analysis
strings /host/var/lib/ecs/data/agent.db >> /tmp/agent.txt
tr -s '{}[],:"\\' '\n' < /tmp/agent.txt | sed 's/^[[:space:]]*//; s/[[:space:]]*$//' | awk 'NF && length($0)>2 && !/^[0-9.]+$/' | sort -u

何を回収できるか

クラスターの経過時間と workload churn によっては、agent.db に対する strings で通常以下が得られます。

  • Task と execution の IAM role ARNtaskRoleArn, executionRoleArn): agent が実行した全 task 分が含まれます。[credential retrieval via the task metadata endpoint](https://cloud.hacktricks.wiki/en/pentesting-cloud/aws-security/aws-services/aws-ecs-enum.html)169.254.170.2)を使った credential 取得の対象として有用です。
  • 完全な task definition — image URI(多くは private ECR repo)、command、entrypoint、port mappings、mount points、log configuration、そして plaintext environment variables。これらには database URL、API token、third-party secret が頻繁に含まれます。
  • Secrets referencesecretOptionssecrets ブロックで、SSM Parameter Store の path や Secrets Manager の ARN を指しています(pivot list として非常に有用です)。
  • Container instance ARN、cluster ARN、registration token — API call なしで cluster 名と account/region の文脈を確認できます。
  • ENI metadataawsvpc mode で割り当てられた private IP、MAC address、subnet ID、security group ID(lateral movement の計画に有用です)。
  • Image pull credential — task definition が repositoryCredentials を使う場合、参照されている Secrets Manager ARN がここにあります。古い agent では private-registry auth blob(ECS_ENGINE_AUTH_DATA)もキャッシュされていることがあります。
  • 最近停止した task container — name、ID、exit code、label を含み、対応する aws ecs describe-tasks call で API response から消えた後でも残っていることがあります。

Unauthenticated Access

AWS - ECS Unauthenticated Enum

Privesc

次のページでは、ECS permissions を悪用して privilege を escalate する方法を確認できます:

AWS - ECS Privesc

Post Exploitation

AWS - ECS Post Exploitation

Persistence

AWS - ECS Persistence

Tip

学んで実践する AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学んで実践する GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学んで実践する Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks をサポートする