AWS - Lambda Persistance
{{#include ../../../../banners/hacktricks-training.md}}
Lambda
Pour plus d'informations, consultez :
{{#ref}} ../../aws-services/aws-lambda-enum.md {{#endref}}
Lambda Layer Persistance
It's possible to introduce/backdoor a layer to execute arbitrary code when the lambda is executed in a stealthy way:
{{#ref}} aws-lambda-layers-persistence.md {{#endref}}
Lambda Extension Persistance
En abusant des Lambda Layers, il est également possible d'abuser des extensions et de persister dans la lambda tout en volant et modifiant les requêtes.
{{#ref}} aws-abusing-lambda-extensions.md {{#endref}}
Via resource policies
Il est possible d'accorder l'accès à différentes actions lambda (such as invoke or update code) à des comptes externes :

Versions, Aliases & Weights
A Lambda can have different versions (with different code each version).
Ensuite, vous pouvez créer différents aliases avec différentes versions de la lambda et attribuer des weights différents à chacun.
De cette façon un attaquant pourrait créer une backdoored version 1 et une version 2 with only the legit code et n'exécuter la version 1 que dans 1% des requêtes pour rester furtif.

Version Backdoor + API Gateway
- Copy the original code of the Lambda
- Create a new version backdooring the original code (or just with malicious code). Publish and deploy that version to $LATEST
- Call the API gateway related to the lambda to execute the code
- Create a new version with the original code, Publish and deploy that version to $LATEST.
- This will hide the backdoored code in a previous version
- Go to the API Gateway and create a new POST method (or choose any other method) that will execute the backdoored version of the lambda:
arn:aws:lambda:us-east-1:<acc_id>:function:<func_name>:1 - Note the final :1 of the arn indicating the version of the function (version 1 will be the backdoored one in this scenario).
- Select the POST method created and in Actions select
Deploy API - Now, when you call the function via POST your Backdoor will be invoked
Cron/Event actuator
Le fait que vous puissiez faire exécuter des fonctions lambda lorsqu'un événement survient ou quand un certain temps s'écoule fait des lambda un moyen fréquent et efficace d'obtenir de la persistance et d'éviter la détection.
Voici quelques idées pour rendre votre présence dans AWS plus furtive en créant des lambdas.
- Every time a new user is created lambda generates a new user key and send it to the attacker.
- Every time a new role is created lambda gives assume role permissions to compromised users.
- Every time new cloudtrail logs are generated, delete/alter them
RCE abusing AWS_LAMBDA_EXEC_WRAPPER + Lambda Layers
Abusez 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. Fournissez 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 role d'exécution de la fonction, et finit par exec le runtime réel afin que le handler original s'exécute normalement.
{{#ref}} aws-lambda-exec-wrapper-persistence.md {{#endref}}
AWS - Lambda Function URL Public Exposure
Abuse Lambda asynchronous destinations together with the Recursion configuration to make a function continually re-invoke itself with no external scheduler (no EventBridge, cron, etc.). By default, Lambda terminates recursive loops, but setting the recursion config to Allow re-enables them. Destinations deliver on the service side for async invokes, so a single seed invoke creates a stealthy, code-free heartbeat/backdoor channel. Optionally throttle with reserved concurrency to keep noise low.
{{#ref}} aws-lambda-async-self-loop-persistence.md {{#endref}}
AWS - Lambda Alias-Scoped Resource Policy Backdoor
Créez une version cachée de la Lambda contenant la logique de l'attaquant et scopez une resource-based policy à cette version spécifique (ou alias) en utilisant le paramètre --qualifier dans lambda add-permission. Accordez seulement lambda:InvokeFunction sur arn:aws:lambda:REGION:ACCT:function:FN:VERSION à un principal attaquant. Les invocations normales via le nom de la fonction ou l'alias principal restent inchangées, tandis que l'attaquant peut invoquer directement l'ARN de la version backdoored.
Ceci est plus furtif que d'exposer une Function URL et ne change pas l'alias de trafic principal.
{{#ref}} aws-lambda-alias-version-policy-backdoor.md {{#endref}}
{{#include ../../../../banners/hacktricks-training.md}}