Exploiting Lambda Functions for Fun and Profit
The platform built untrusted pull-request code on an AWS Lambda function, and the Lambda runtime API on port 9001 exposed the event payload containing the victim's GitHub token. An attacker could execute code in the Lambda, read the token, and use it to access all of the victim's repositories.
Published on praetorian.com
Praetorian recently performed an assessment of a platform responsible for downloading and building untrusted, user-supplied code. The client was concerned about the possibility of attackers leveraging this process to compromise the client’s AWS environment or gain access to sensitive data belonging to other users.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Praetorian assessed a platform that downloads and builds untrusted, user-supplied code. The client was worried that an attacker could use the build process to compromise the AWS environment or reach other users' sensitive data. Their sandboxing approach was to run builds on a serverless AWS Lambda function with minimal access to the rest of the AWS environment. The product integrates with the user's GitHub account to scan their repositories, and it may also resolve dependencies and build the project. Because some project manifest files can contain scripts that run during the build, the developers chose Lambda instances for isolation: even if an attacker scanned a repository with a malicious manifest, they would only get code execution on an ephemeral Lambda instance. The only valuable secret passed to the Lambda is a GitHub token belonging to the user who started the scan, so the reasoning was that an attacker would only reach a token for their own GitHub user.
Praetorian found a way to execute arbitrary code through this process and then explored how far that foothold could be pushed inside the client's AWS environment.
Setup
The engagement tested a product that integrates with the user's GitHub account to scan their repositories and may resolve dependencies and build the project. The Lambda functions were provisioned with an IAM execution role that was correctly limited to the minimal privileges needed for their intended behaviour, and they ran as a low-privileged user inside the Linux environment. The report does not list specific versions, tooling or account details beyond this.
The key background is the Lambda runtime API, described in research by Yuval Avharami and Nick Frichette that the team drew on. It is a listening service on port 9001 responsible for returning incoming event data and terminating events with successful responses or errors. From a security perspective, code running on the Lambda can terminate the event that triggered it and then listen for future incoming events. For functions that receive sensitive information such as tokens or PII in the event payload, this API is a way for an attacker with code execution to expose those secrets.
Finding it
After adding a GitHub repository to the application, the platform also attaches a GitHub workflow to the repository. The team discovered that pull requests from any GitHub user account could trigger this workflow, which led to the Lambda being invoked with the code provided in the pull request. The untrusted code from the PR was then checked out and built on the Lambda instance.
That broke the assumption behind the isolation. The Lambda was not only handling the scanning user's own code: it was building code supplied by anyone who could open a pull request. An attacker could therefore access the victim's GitHub token by querying the Lambda API endpoint and viewing the event JSON. In this case the GitHub tokens were used in the event payload and were configured with the repo scope, so anyone holding one could read and write all of the user's public and private repositories.

Exploitation
Step 1: Get code execution on the Lambda via a pull request
Open a pull request from any GitHub user account against a repository that the platform has attached its workflow to. The workflow triggers, the Lambda is invoked with the code from the pull request, and that code is checked out and built on the Lambda instance. The build executes attacker-controlled code.
Step 2: Read the event JSON from the runtime API
With code execution on the Lambda, query the Lambda runtime API on port 9001 and view the event JSON. In this case the GitHub tokens were used in the event payload and were configured with the repo scope.
Step 3: Terminate the current invocation and wait for another user
The attacker can also capture other users' GitHub tokens by querying the Lambda API to terminate the current invocation and then waiting for the Lambda to be triggered by another user.

Step 4: Persist and collect future events automatically
Praetorian wrote a script to automate retrieval of the event JSON containing sensitive data, demonstrating how an attacker might keep persistent access to incoming sensitive information. Once run on a compromised Lambda function, the script calls back to an attacker-controlled server with the contents of any future events, which in this case contain captured GitHub tokens from other users.
Lambda execution is capped at 15 minutes, so the Lambda must be invoked regularly while the collection script runs, to stop it going cold and starting again with a fresh environment.


Step 5: Command injection via the default branch name
The team also found a command injection vulnerability that could be triggered by changing the name of the GitHub repository's default branch. This was another entry point into the target Lambda function and gave access to build environments that had not previously been compromised.
Step 6: Pivot to other languages
After gaining a foothold on a Lambda instance supporting one language, the attacker can use the stolen GitHub token to modify the GitHub branch of another repository and get code execution on a Lambda instance for another language, depending on how the repositories are configured.
Root cause
The design assumed that the only secret reachable from the Lambda belongs to the user who started the scan, and that the Lambda's isolation therefore limits the blast radius to that user. Two things undermined this. First, the attached GitHub workflow could be triggered by pull requests from any GitHub account, so untrusted code from third parties was built on the Lambda alongside the victim's token. Second, the Lambda runtime API on port 9001 exposes the event payload to any code running in the function, and lets that code terminate the current event and listen for later ones. The IAM execution role was minimal and the process ran as a low-privileged user, but neither control addresses secrets inside the event payload or the runtime API itself. The command injection via the default branch name provided a second, independent route into the build environment.
Impact
An attacker who can open a pull request against a repository with the platform's workflow gets code execution on the Lambda and can read the GitHub token of the user who initiated the scan. With the repo scope, that token allows reading and writing all of the user's public and private repositories. By terminating the current invocation and waiting, the attacker can capture tokens belonging to other users as their scans arrive, and a callback script keeps collecting future events for as long as the Lambda stays warm. The stolen token can then be used to modify branches of other repositories and reach Lambda instances for other languages. The report notes that it is not possible to entirely prevent attackers from running code on these Lambdas, given that the projects being scanned inherently include code that runs at build time.
Fix
Praetorian's recommendations focus on reducing the impact of running arbitrary code:
- Stop building code included in pull requests. Pull requests can come from actors who are unauthenticated with respect to the target platform, and executing that code lets adversaries view victims' GitHub secrets and subsequent project updates.
- For authenticated users' code, install a sandbox environment on the Lambda function that restricts access to the runtime API and to any accessible memory containing the event payload.
- Use AWS NACLs that prohibit all non-essential outbound traffic, for example a whitelist of acceptable addresses with traffic to everything else blocked.
The report does not say which of these the client implemented.
Variants to test
- PR-triggered workflows on build platforms. Open a pull request from a separate account against a repository with the platform's workflow attached and see whether the build runs your code. A positive result is your code executing in the build environment.
- Runtime API exposure. From code running in a Lambda, query the runtime API on port 9001 and inspect the event JSON. A positive result is secrets such as tokens or PII in the payload.
- Event termination and re-listen. Terminate the current invocation through the runtime API and wait for the next event. A positive result is receiving another user's event data.
- Warm-instance collection. Run a callback script and keep the Lambda invoked regularly within the 15-minute execution cap. A positive result is a steady stream of events sent to your server.
- Branch-name command injection. Change a repository's default branch name and trigger a build. A positive result is command execution in the build environment.
- Cross-language pivot. Use a token captured from one Lambda to modify a branch of a repository built on a Lambda for another language. A positive result is code execution on that second environment.
What happened, in plain words
A security company called Praetorian was hired to test a web platform. The platform's job is to take code that users upload, download it, and build it. "Building" means turning source code into something runnable, a bit like taking a recipe and actually cooking the meal. The client was worried about one thing: could someone use that build process to break into their Amazon Web Services (AWS) cloud account, or to steal data belonging to other users?
To keep things safe, the platform ran each build inside an AWS Lambda function. A Lambda function is a small program that runs in the cloud only when it is needed, then disappears. Think of it as a disposable kitchen that is set up, used for one dish, and then thrown away. The idea was that even if someone uploaded nasty code, the damage would be limited to that one disposable kitchen.
Praetorian found a way to run their own code inside that kitchen. Then they looked at how much they could do from there. The short answer: quite a lot. They could read a secret key that belonged to the person who started the build, and they could even wait around to collect the secret keys of other users.
This article walks through what they found, step by step, in the same order as the technical report, and explains every term along the way.
Background: what a Lambda function is, and why it was chosen
Before the details, a few ideas need to be clear.
Cloud and AWS. "The cloud" just means someone else's computers, rented over the internet. AWS (Amazon Web Services) is one company that rents out such computers. You do not own the machine; you rent a slice of it for as long as you need.
Lambda function. AWS Lambda is a service where you hand over a small piece of code and AWS runs it on demand. You do not manage a server. Each run happens in a fresh, temporary container (a lightweight, isolated environment), and when the run finishes, the container is normally discarded. This is why people call it "serverless": there is still a server, you just do not see or manage it.
IAM execution role. Every Lambda function runs with a set of permissions, called an IAM role. IAM stands for Identity and Access Management. The role says what the function is allowed to do in the AWS account: which files it can read, which services it can call. In this case, the report says the role was correctly limited to only what the function needed. That is good practice, and it is why the client felt safe.
Low-privileged user. Inside the Linux environment where the Lambda runs, the code does not run as an administrator. It runs as an ordinary, restricted user. Again, good practice.
Ephemeral. Meaning short-lived. The Lambda container exists only for the duration of a run (or a short while afterwards, if it is kept "warm", which we will come back to).
So the client's reasoning was: the Lambda has few permissions, runs as a nobody, and disappears quickly. Even if an attacker gets code to run there, what could they possibly reach? The answer, as we will see, is the secret that gets handed to the function when it starts.
What is the Lambda runtime API?
An API (Application Programming Interface) is a way for two pieces of software to talk to each other. A simple comparison: a restaurant menu. You do not walk into the kitchen; you pick something from the menu, and the kitchen sends it back. An API is that menu plus the waiter.
The Lambda runtime API is a small service that listens on port 9001 inside the Lambda environment. A port is like a numbered door on a computer; services listen at specific doors so other programs know where to knock. This particular service has two jobs:
- It hands the incoming event data to the function. An "event" is the message that triggered the run, for example "please build this repository".
- It lets the function report back whether the run succeeded or failed, which ends (terminates) the event.
The security implication is subtle but important. If your code is running inside the Lambda, it can talk to this API directly. It can tell the API "I am done with this event", and then it can sit and ask "is there another event for me?". In other words, code that was only supposed to build a project can take control of the event queue.
For a Lambda that receives sensitive information in the event, such as tokens or personal data (PII, personally identifiable information), this API becomes a way to read those secrets. The team drew on public research by Yuval Avharami and Nick Frichette, which described this behaviour.
Why the isolation was not enough
The product Praetorian tested connects to a user's GitHub account and scans their repositories. GitHub is a website where people store and share code; a repository (repo) is a project folder hosted there. The platform can also download dependencies (other libraries the project needs) and build the project.
Here is the catch. Some project files, called manifest files, can contain scripts that run automatically during a build. A manifest is a list of instructions for the build tool, a bit like a shopping list that also says "while you are at it, run this command". If an attacker puts a malicious command in that list, the build will run it.
Because the developers knew this, they chose Lambda for isolation. Their reasoning: even if an attacker scans a repository with a malicious manifest, the attacker only gets code execution on a temporary Lambda. The only valuable secret given to that Lambda is a GitHub token belonging to the user who started the scan. A token is like a temporary key or badge: instead of typing your password everywhere, software uses a token to prove who it is. So the reasoning was: an attacker would only ever reach a token for their own GitHub account. Nothing to steal.

The image illustrates that assumption: the only valuable secret in the Lambda belongs to the user who started the scan.
That assumption was wrong, and the next section explains why.
Finding the flaw
When you add a GitHub repository to the platform, the platform also attaches a GitHub workflow to that repository. A workflow is an automated recipe that GitHub runs when something happens, for example when someone opens a pull request. A pull request (PR) is a proposal to change code: someone copies the project, makes edits, and asks the owner to merge them in. Anyone with a GitHub account can usually open a pull request against a public repository.
The team discovered that pull requests from any GitHub user account could trigger this workflow. When the workflow ran, the Lambda was invoked with the code from the pull request, and that code was checked out and built on the Lambda instance.
This broke the whole safety argument. The Lambda was no longer only building the scanning user's own code. It was building code supplied by anyone who could open a pull request. So an attacker could get their own code running inside the Lambda while the Lambda was holding the victim's token.
From there, the attacker could query the Lambda API endpoint and view the event JSON. JSON is a text format for structured data, like a very strict shopping list with labels. The event payload (the contents of the event) contained the GitHub token of the user whose scan triggered the run. Those tokens were configured with the repo scope. A scope is a permission level: repo means the token can read and write all of the user's public and private repositories. That is a lot of power.

The attack chain, step by step
Step 1: Get code execution on the Lambda via a pull request
Open a pull request from any GitHub account against a repository that has the platform's workflow attached. The workflow triggers, the Lambda is invoked with the code from the pull request, and that code is checked out and built on the Lambda. The build runs the attacker's code.
Why this step: without code running inside the Lambda, the attacker cannot talk to the runtime API. This is the foothold.
Step 2: Read the event JSON from the runtime API
With code execution on the Lambda, query the Lambda runtime API on port 9001 and view the event JSON. In this case the GitHub tokens were used in the event payload and were configured with the repo scope.
Why this step: the token is the prize. It is sitting in the event data that the runtime API hands out.
Step 3: Terminate the current invocation and wait for another user
The attacker can also capture other users' tokens by asking the Lambda API to terminate the current invocation, then waiting for the Lambda to be triggered by another user.
Why this step: one token is useful, but many tokens are more useful. By ending the current run and listening for the next, the attacker turns a single compromise into a collection point.
Step 4: Persist and collect future events automatically
Praetorian wrote a script to automate retrieval of the event JSON containing sensitive data. This demonstrated how an attacker might keep persistent access to incoming sensitive information. Once the script runs on a compromised Lambda, it calls back to an attacker-controlled server with the contents of any future events. In this case, those events contain captured GitHub tokens from other users.
A callback simply means the script reaches out to a server the attacker controls and sends data to it. It is like leaving a forwarding address at the post office.
There is a time limit. Lambda execution is capped at 15 minutes. So the Lambda must be invoked regularly while the collection script runs, to stop it going cold and starting again with a fresh environment. "Cold" means the container has been shut down; the next run starts from scratch, and anything the script left in memory is gone.


Step 5: Command injection via the default branch name
The team also found a command injection vulnerability that could be triggered by changing the name of the GitHub repository's default branch. A branch is a parallel version of the code; the default branch is the main one. Command injection means tricking a program into running extra commands by hiding them inside data it was not expecting to be dangerous. It is like writing "buy milk; also unlock the back door" on a shopping list and having the shop assistant follow both instructions.
This was another entry point into the target Lambda function and gave access to build environments that had not previously been compromised.
Why this matters: it is a second, independent way in. If one route is closed, the other may still work.
Step 6: Pivot to other languages
After gaining a foothold on a Lambda instance supporting one language, the attacker can use the stolen GitHub token to modify the GitHub branch of another repository and get code execution on a Lambda instance for another language, depending on how the repositories are configured.
A pivot means using one foothold to reach another. The stolen token is the key that opens the next door.
Root cause
The design assumed that the only secret reachable from the Lambda belongs to the user who started the scan, and that the Lambda's isolation therefore limits the blast radius to that user. Two things undermined this.
First, the attached GitHub workflow could be triggered by pull requests from any GitHub account, so untrusted code from third parties was built on the Lambda alongside the victim's token.
Second, the Lambda runtime API on port 9001 exposes the event payload to any code running in the function, and lets that code terminate the current event and listen for later ones.
The IAM execution role was minimal and the process ran as a low-privileged user, but neither control addresses secrets inside the event payload or the runtime API itself. The command injection via the default branch name provided a second, independent route into the build environment.
Impact
An attacker who can open a pull request against a repository with the platform's workflow gets code execution on the Lambda and can read the GitHub token of the user who started the scan. With the repo scope, that token allows reading and writing all of the user's public and private repositories. By terminating the current invocation and waiting, the attacker can capture tokens belonging to other users as their scans arrive, and a callback script keeps collecting future events for as long as the Lambda stays warm. The stolen token can then be used to modify branches of other repositories and reach Lambda instances for other languages.
The report notes that it is not possible to entirely prevent attackers from running code on these Lambdas, given that the projects being scanned inherently include code that runs at build time.
Fix
Praetorian's recommendations focus on reducing the impact of running arbitrary code:
- Stop building code included in pull requests. Pull requests can come from actors who are unauthenticated with respect to the target platform, and executing that code lets adversaries view victims' GitHub secrets and subsequent project updates.
- For authenticated users' code, install a sandbox environment on the Lambda function that restricts access to the runtime API and to any accessible memory containing the event payload.
- Use AWS NACLs that prohibit all non-essential outbound traffic, for example a whitelist of acceptable addresses with traffic to everything else blocked. A NACL (Network Access Control List) is a firewall rule list: it decides which network traffic is allowed in or out.
The report does not say which of these the client implemented.
Variants to test
- PR-triggered workflows on build platforms. Open a pull request from a separate account against a repository with the platform's workflow attached and see whether the build runs your code. A positive result is your code executing in the build environment.
- Runtime API exposure. From code running in a Lambda, query the runtime API on port 9001 and inspect the event JSON. A positive result is secrets such as tokens or PII in the payload.
- Event termination and re-listen. Terminate the current invocation through the runtime API and wait for the next event. A positive result is receiving another user's event data.
- Warm-instance collection. Run a callback script and keep the Lambda invoked regularly within the 15-minute execution cap. A positive result is a steady stream of events sent to your server.
- Branch-name command injection. Change a repository's default branch name and trigger a build. A positive result is command execution in the build environment.
- Cross-language pivot. Use a token captured from one Lambda to modify a branch of a repository built on a Lambda for another language. A positive result is code execution on that second environment.
Glossary
- API: a way for two pieces of software to talk to each other, like a menu and a waiter in a restaurant.
- AWS: Amazon Web Services, a company that rents out cloud computers.
- Blast radius: how much damage a single compromise can cause.
- Branch: a parallel version of a code project; the default branch is the main one.
- Callback: a script reaching out to a server the attacker controls to send data.
- Cloud: computers owned by someone else, rented over the internet.
- Cold start: when a Lambda container has been shut down and the next run starts from scratch.
- Command injection: tricking a program into running extra commands hidden inside data it did not expect to be dangerous.
- Container: a lightweight, isolated environment in which code runs.
- Dependency: another library or package that a project needs in order to build or run.
- Ephemeral: short-lived; existing only for a short time.
- Event: the message that triggers a Lambda run, for example "build this repository".
- Event payload: the contents of that message.
- GitHub: a website where people store and share code.
- GitHub token: a key that software uses to prove who it is to GitHub, instead of a password.
- IAM: Identity and Access Management, the AWS system that controls who and what can do which actions.
- IAM execution role: the set of permissions a Lambda function runs with.
- JSON: a text format for structured data, with labels and values.
- Lambda function: a small program that AWS runs on demand in a temporary container.
- Low-privileged user: an ordinary, restricted user account, not an administrator.
- Manifest file: a file that tells a build tool what to do, and which can contain scripts that run during the build.
- NACL: Network Access Control List, a firewall rule list that decides which network traffic is allowed.
- PII: personally identifiable information, data that identifies a person.
- Port: a numbered door on a computer where a service listens.
- Pull request (PR): a proposal to change code, made by copying a project, editing it, and asking the owner to merge it.
- Repository (repo): a project folder hosted on GitHub.
- Runtime API: the service on port 9001 inside a Lambda that hands out events and lets the function end them.
- Sandbox: an isolated environment that restricts what code inside it can reach.
- Scope: a permission level attached to a token;
repomeans read and write access to repositories. - Serverless: a way of running code where you do not manage the server yourself.
- Token: a key or badge that software uses to prove who it is.
- Warm: a Lambda container that is still running and has not been shut down.
- Workflow: an automated recipe that GitHub runs when something happens, such as a pull request.