Objective:
Understand how improper permissions on cloud functions, such as AWS Lambda, can lead to unauthorized access or privilege escalation. Demonstrate how attackers can exploit weak IAM policies and recommend best practices for securing cloud functions.
Scenario:
An AWS Lambda function is deployed with overly permissive IAM roles, granting access to sensitive resources, such as S3 buckets, DynamoDB tables, or other Lambda functions. Using a compromised user account or misconfigured policies, attackers can exploit these permissions to invoke or modify the Lambda function and escalate privileges.
Lab Setup:
Prerequisites:
- AWS account (free-tier works for this lab).
- Installed tools:
- aws-cli (Installation Guide).
- Burp Suite (Download Burp Suite).
Steps to Set Up the Lab:
- Create an AWS Lambda Function:
- Log in to the AWS Management Console.
- Navigate to Lambda > Create function > Author from scratch.
- Function name:
over-permissive-lambda
. - Runtime: Python 3.x.
- Add the following function code to simulate resource access:pythonCopyEdit
import boto3 import json def lambda_handler(event, context): s3 = boto3.client('s3') buckets = s3.list_buckets() return { 'statusCode': 200, 'body': json.dumps({ 'message': 'Lambda executed successfully', 'buckets': [bucket['Name'] for bucket in buckets['Buckets']] }) }
- Deploy the function.
- Assign an Overly Permissive IAM Role:
- Navigate to IAM > Roles > Create role.
- Select AWS Service > Lambda and attach the following policies:
- AdministratorAccess (grants full permissions).
- Assign this role to the Lambda function.
- Set Up a Public Trigger:
- Create an API Gateway trigger for the Lambda function:
- Navigate to API Gateway > Create API > HTTP API.
- Integrate the Lambda function with the API Gateway.
- Deploy the API to generate a public Invoke URL.
- Create an API Gateway trigger for the Lambda function:
- Simulate a Compromised User Account:
- Create an IAM user with programmatic access:
- Attach overly permissive policies, such as LambdaFullAccess or IAMFullAccess.
- Save the user’s Access Key and Secret Key.
- Create an IAM user with programmatic access:
Exercise: Exploiting Weak Permissions
Objective:
Simulate an attacker exploiting overly permissive IAM roles to invoke, modify, or access the Lambda function.
- Invoke the Lambda Function:
- Use the compromised IAM credentials to invoke the Lambda function via the aws-cli:bashCopyEdit
aws lambda invoke --function-name over-permissive-lambda output.json --profile compromised-user
- Check the output for the list of S3 buckets.
- Use the compromised IAM credentials to invoke the Lambda function via the aws-cli:bashCopyEdit
- Modify the Lambda Function:
- Use the compromised credentials to update the Lambda function code:bashCopyEdit
aws lambda update-function-code --function-name over-permissive-lambda --zip-file fileb://new-function-code.zip --profile compromised-user
- Confirm that the new function is deployed.
- Use the compromised credentials to update the Lambda function code:bashCopyEdit
- Enumerate Sensitive Resources:
- Use the compromised permissions to list or access resources, such as:
- S3 buckets:bashCopyEdit
aws s3 ls --profile compromised-user
- IAM roles:bashCopyEdit
aws iam list-roles --profile compromised-user
- S3 buckets:bashCopyEdit
- Use the compromised permissions to list or access resources, such as:
- Test API Gateway Trigger:
- Send a request to the API Gateway endpoint using Postman or Burp Suite:phpCopyEdit
GET https://<api-id>.execute-api.<region>.amazonaws.com/
- Observe if sensitive data (e.g., bucket names) is exposed in the response.
- Send a request to the API Gateway endpoint using Postman or Burp Suite:phpCopyEdit
Tools Required:
- AWS IAM and Lambda: For setting up the permissions and function.
- aws-cli: For invoking and modifying the Lambda function.
- Burp Suite: For intercepting and testing API requests.
Deliverables:
- Exploit Report:
- Evidence of invoking the Lambda function using compromised credentials.
- Logs showing modifications to the Lambda function.
- Screenshots of enumerated resources or sensitive data.
- Recommendations for Mitigating Risks:
- Steps to implement least privilege, monitor permissions, and secure IAM roles.
Solution:
- Identified Vulnerabilities:
- Overly Permissive IAM Role: The Lambda function had permissions beyond its operational requirements.
- Weak IAM User Policies: The compromised user account had unnecessary access to Lambda and IAM resources.
- Public API Trigger: The API Gateway trigger allowed unauthorized access to invoke the function.
- Consequences:
- Unauthorized Resource Access: Attackers could list or manipulate sensitive resources, such as S3 buckets.
- Privilege Escalation: Attackers could modify IAM roles or policies to escalate privileges.
- Data Breach: Exposed sensitive information through public API responses.
- Prevention Techniques:
- Implement Least Privilege:
- Use the principle of least privilege to grant only the necessary permissions to IAM roles.
- Example IAM policy for Lambda:jsonCopyEdit
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListBucket" ], "Resource": "arn:aws:s3:::specific-bucket-name" } ] }
- Monitor IAM Role Usage:
- Enable AWS CloudTrail to log and monitor access to sensitive resources.
- Set alerts for unusual activity.
- Restrict API Gateway Access:
- Use AWS API Gateway Authorizers (e.g., Cognito User Pools, Lambda Authorizers) to enforce authentication.
- Rotate Compromised Credentials:
- Immediately revoke and rotate credentials if an IAM user account is compromised.
- Implement Least Privilege:
Conclusion:
This exercise demonstrates how overly permissive IAM roles and misconfigured API triggers expose cloud functions to exploitation. By adhering to the principle of least privilege, monitoring permissions, and securing API triggers, organizations can significantly reduce the risk of privilege escalation and unauthorized access.
0 Comments