Serverless function abuse
| Class | Availability and abuse, Secrets |
|---|---|
| Also known as | Serverless execution |
| MITRE ATT&CK | T1648 Serverless Execution |
| Typical severity | High |
| Affects | AWS Lambda, Google Cloud Run functions and Azure Functions; automation in Microsoft 365 and Google Workspace |
A serverless function is code that a cloud provider runs on demand, and every function runs as an identity with permissions of its own. Serverless function abuse is the use of that arrangement by someone holding stolen cloud credentials. They create a function, or replace the code of one that exists, and the provider runs their code with the rights of the function's identity.1 The code may mine cryptocurrency (Cryptojacking) or act on the cloud account itself, and a function tied to a cloud event can run again each time that event happens.1
Automation services in office suites work the same way: a Power Automate flow in Microsoft 365 that forwards every email a user receives (compare Email forwarding rules), or an Apps Script in Google Workspace that sends a user's data out when a file is opened.1
The identity inside a function
On AWS Lambda each function has an execution role, which Lambda takes on whenever the function is invoked.2 The runtime puts the role's temporary keys into the environment variables AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN, where any code in the function can read them, including code an attacker uploaded.3 Google Cloud Run gets the same result another way: the code asks the metadata server for an OAuth 2.0 access token belonging to the service account the function runs as (Cloud instance metadata service).4
When a deployment names no service account, Cloud Run uses the Compute Engine default service account. Depending on the organization's policy, that account may have been given the Editor role on the project automatically.4
Passing a role
A new function needs a role to run as, and the caller names it. AWS then checks that the caller holds the iam:PassRole permission for that role. The permission exists so that only approved users can give a service a role, and AWS warns against letting a user pass a role with more rights than the user should have: someone barred from storage could otherwise pass a role that can read it, and the service reads it for them.5 On Google Cloud the matching permission is iam.serviceAccounts.actAs.1
Three combinations of Lambda permissions lead to more access. With iam:PassRole, lambda:CreateFunction and lambda:InvokeFunction, a user creates a function under an existing, more powerful role and calls it. Without the invoke permission, lambda:CreateEventSourceMapping ties the new function to a DynamoDB table, and a new item in the table starts it. With lambda:UpdateFunctionCode alone, the attacker swaps the code of a function that already has a role and waits for its usual trigger. The attacker gains whatever the passed or reused role holds, which can be full administrator access to the account.6
CloudTrail writes no entry for PassRole, which is a permission check with no API call of its own. The passed role appears only in the event that created or changed the function.5
Functions that wait for an event
A function can start on a cloud event instead of a request. In one AWS example, the attacker deploys a function that adds a new access key to a user, together with a CloudWatch Events rule that runs it whenever a user is created, so each new user arrives with an extra credential for the attacker.1
The 2024 .env scanning campaign
In August 2024 Unit 42 reported an extortion campaign that started from AWS credentials found in exposed environment files. Each operation opened with GetCallerIdentity, then ListUsers and ListBuckets (Cloud discovery). The stolen credential was not an administrator, but it could create roles and attach policies, so the attackers created a role named lambda-ex and attached the AWS-managed AdministratorAccess policy to it.7
An attempt to start EC2 servers for mining failed for lack of permissions. The attackers then created a Lambda function named ex in us-east-1 under the new role and copied it to every other enabled region within a second. The function was a bash script. It read a list of domains from a public S3 bucket, requested /.env from each one, and stored the credentials it found in another public bucket the attackers controlled, looking in particular for Mailgun keys, which can send phishing mail from real domains. From that bucket Unit 42 assessed that the attackers could copy the environment files of at least 110,000 domains. The leaked variables included 7,000 access keys for cloud services.7
The attackers created the functions over the Tor network. Lambda still logged every run, in a CloudWatch log group it creates for each function (here /aws/lambda/ex) and that the attacker cannot turn off. That log let Unit 42 follow what the function did.7
Detection
A new Lambda function shows in CloudTrail as a CreateFunction20150331 event, followed by a log group named after the function.7 In the 2024 case the trail held a new role with AdministratorAccess and the same function in every enabled region.7
Once switched on, Amazon GuardDuty Lambda Protection reads the network activity logs of every function in the account, including functions that do not use VPC networking (a private network inside AWS).8 It raises CryptoCurrency:Lambda/BitcoinTool.B when a function contacts an address tied to cryptocurrency activity, and Backdoor:Lambda/C&CActivity.B, default severity High, when it contacts a known command and control server.9
Limiting it
Only the identities that deploy functions need permission to create, change or run them.1 For those that keep it, PassRole is narrowed in two places: the Resource lists the roles a user may pass, by ARN (the full ID AWS gives each resource) or by name prefix, and the iam:PassedToService condition key fixes the service the role may go to.5
{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "arn:aws:iam::111122223333:role/app-fn-*",
"Condition": {
"StringEquals": { "iam:PassedToService": "lambda.amazonaws.com" }
}
}A function whose role holds only what its code uses gives a replaced function little to work with (Principle of least privilege). On Google Cloud that means a user-managed service account per function in place of the default one, with the automatic Editor grant switched off by enforcing the iam.automaticIamGrantsForDefaultServiceAccounts organization policy, which is on by default for organizations created after 3 May 2024.4
Prompt for a coding agent
Find every IAM policy in this project's infrastructure code (Terraform, CloudFormation, CDK, SAM, Serverless Framework) that grants iam:PassRole, lambda:CreateFunction, lambda:UpdateFunctionCode or lambda:CreateEventSourceMapping. Where iam:PassRole has Resource "*", limit it to the ARNs of the roles this project's functions use and add the condition iam:PassedToService = lambda.amazonaws.com. Check that each Lambda function has its own role and that no function role has AdministratorAccess. On Google Cloud, find functions and Cloud Run services deployed without an explicit service account. List each change and any policy you were unsure about.
In SecFlash
The function in the 2024 campaign collected environment files that websites served to anyone. SecFlash reports such a file as SF-EXP-307 on the Exposure page and as SF-SEC-617 on the Security page.
See also
- Stolen cloud credentials
- Cloud discovery
- Additional cloud credentials
- Cloud instance metadata service
- Short-lived credentials and OIDC federation
- Principle of least privilege
- Cryptojacking
- Exposed sensitive files
- Category: Availability and abuse
References
- a b c d e f MITRE (version 1.2, modified 15 April 2025). "Serverless Execution, Technique T1648". MITRE ATT&CK.
- back Amazon Web Services. "Defining Lambda function permissions with an execution role". AWS Lambda Developer Guide.
- back Amazon Web Services. "Working with Lambda environment variables". AWS Lambda Developer Guide.
- a b c Google Cloud. "Introduction to service identity". Cloud Run documentation.
- a b c Amazon Web Services. "Grant a user permissions to pass a role to an AWS service". AWS Identity and Access Management User Guide.
- back Rhino Security Labs. "AWS IAM Privilege Escalation: Methods and Mitigation". Rhino Security Labs.
- a b c d e Unit 42 (15 August 2024). "Leaked Environment Variables Allow Large-Scale Extortion Operation in Cloud Environments". Palo Alto Networks.
- back Amazon Web Services. "GuardDuty Lambda Protection". Amazon GuardDuty User Guide.
- back Amazon Web Services. "Lambda Protection finding types". Amazon GuardDuty User Guide.
External links
- Serverless Execution, MITRE ATT&CK