Serverless Security
Serverless (AWS Lambda, Azure Functions, GCP Cloud Functions/Run) runs code without managing servers. For the attacker, each function is a small surface with its own weaknesses: secrets in environment variables, a role/identity with abusable permissions, vulnerable dependencies, and injection if it processes untrusted input. And since functions usually have cloud permissions, compromising one is often the jump into the rest of the account.
Threat model
Section titled “Threat model”A function isn’t “just code”: it runs with an identity (execution role) that has permissions over other cloud resources. The attacker has two paths: read the function’s configuration (secrets in environment variables, code) with enumeration permissions, or execute code inside the function (injection, vulnerable dependency) to steal the role’s credentials from the execution environment. Either one inherits the function’s permissions → escalation and lateral movement (see IAM Abuse and Privilege Escalation).
flowchart TD
A[Serverless function] --> B{Attack path}
B -->|enumeration| C[Read env vars / code]
B -->|injection / vuln dep| D[RCE in the function]
C --> E[Secrets: API keys, DB creds]
D --> F[Execution-role credentials]
F --> G[Role permissions -> cloud-iam]
E --> G
B -->|PassRole + CreateFunction| H[Function as admin role]
H --> G
Red Team
Section titled “Red Team”Secrets in environment variables
Section titled “Secrets in environment variables”# functions store config/secrets in env vars (common, insecure practice)aws lambda get-function-configuration --function-name X # shows Environment.Variables# inside the function (if there's RCE): printenv / os.environ -> API keys, DB creds, tokensAbusing the execution role
Section titled “Abusing the execution role”The function runs with a role/identity whose credentials are in the environment. In AWS Lambda, the runtime exposes AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN as environment variables. If you get RCE in the function (injection), you steal those credentials and use them outside:
# after stealing the role's temporary credentialsexport AWS_ACCESS_KEY_ID=ASIA... AWS_SECRET_ACCESS_KEY=... AWS_SESSION_TOKEN=...aws sts get-caller-identity # confirm which role it is -> escalate (cloud-iam)Injection and untrusted input
Section titled “Injection and untrusted input”The function processes events (HTTP, queues, S3…). If it doesn’t validate input: command injection, SSRF (→ metadata and other services, see Metadata Service (IMDS)), path traversal, deserialization. A function behind API Gateway is a webapp: the Web section attacks apply.
Vulnerable dependencies and deployment
Section titled “Vulnerable dependencies and deployment”# vulnerable npm/pip packages in the function bundle# deployment permissions (create/update functions) -> backdoor or PassRolePassRole / creating functions (escalation)
Section titled “PassRole / creating functions (escalation)”With lambda:CreateFunction + iam:PassRole over an admin role, you create a function that runs as admin and executes your code (see IAM Abuse and Privilege Escalation).
Enumeration
Section titled “Enumeration”# AWSaws lambda list-functionsaws lambda get-function --function-name X # code + configuration# Azureaz functionapp listaz functionapp config appsettings list --name X --resource-group RG # secrets# GCPgcloud functions listgcloud functions describe X ; gcloud run services listBlue Team
Section titled “Blue Team”- Secrets outside code/env: use Secrets Manager / Key Vault / Secret Manager, not cleartext environment variables.
- Least privilege in the execution role: the function should only be able to do what it needs (limits the damage of a compromise).
- Validate all event input (injection, SSRF); scan dependencies (SCA) and the bundle.
- Don’t grant
CreateFunction+PassRoletogether to non-admin identities; review deployment permissions. - Logging (CloudWatch / Application Insights / Cloud Logging) and time/resource limits; WAF in front of HTTP functions.
Checklist
Section titled “Checklist”- Enumerate functions and their configuration (code + env vars)
- Secrets in environment variables
- Execution role/identity and its permissions (IAM Abuse and Privilege Escalation)
- Injection/SSRF if it processes untrusted input (Metadata Service (IMDS))
- Role credentials stealable via RCE in the function
- Vulnerable dependencies in the bundle
-
PassRole+CreateFunction→ escalation - HTTP functions treated as a webapp (Web section)