Blog | G5 Cyber Security

AWS Credentials: Security Risks & Fixes

TL;DR

Your AWS credentials are likely insecure if they’re hardcoded in your code, stored in plain text files, or shared unnecessarily. This guide shows you how to improve security using IAM roles, environment variables, and the AWS CLI configuration.

Why Are My AWS Credentials Insecure?

AWS credentials (Access Key ID and Secret Access Key) grant powerful access to your cloud resources. If compromised, attackers can:

Common insecure practices include:

How to Secure Your AWS Credentials

  1. Use IAM Roles (Recommended)
    • IAM roles allow your EC2 instances, Lambda functions, and other AWS services to access resources without needing long-term credentials.
    • When you launch an instance or configure a service, it automatically assumes the role’s permissions.
    • Steps:
      1. Create an IAM Role with the necessary permissions (e.g., S3 read access).
      2. Attach a trust policy to the role specifying which AWS services can assume it.
      3. When launching an EC2 instance, select the created role during configuration.
  2. Use Environment Variables
    • Store credentials as environment variables on your servers or in container configurations.
    • This keeps them separate from your code and makes it easier to manage.
    • Example (Linux/macOS):
      export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
      export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
    • Important: Ensure your environment is secure and access to these variables is restricted. Don’t commit them to version control!
  3. Configure the AWS CLI
    • The AWS CLI can store credentials in a configuration file (~/.aws/credentials). This is better than hardcoding, but still requires careful management.
    • Steps:
      1. Install and configure the AWS CLI: AWS CLI Configuration
      2. Use aws configure to set up your credentials.
        aws configure
        AWS Access Key ID [None]: YOUR_ACCESS_KEY
        AWS Secret Access Key [None]: YOUR_SECRET_KEY
        Default region name [None]: eu-west-1
        Default output format [None]: json
  4. Use AWS Secrets Manager
    • AWS Secrets Manager allows you to securely store, rotate, and manage secrets like database passwords and API keys.
    • It integrates with other AWS services for easy access.
  5. Regularly Rotate Credentials
    • Periodically change your credentials (especially if you suspect a compromise).
    • IAM roles simplify rotation as you don’t need to update instances directly.

Checking for Exposed Credentials

Tools like git grep can help find accidentally committed credentials:

git grep -n 'AWS_ACCESS_KEY' .

Be extremely careful if you find any matches and immediately revoke the compromised keys.

Exit mobile version