Browse the Repo

file-type-icon.circleci
file-type-icon_ci
file-type-icon_docs
file-type-iconexamples
file-type-iconauto-update
file-type-iconaws-config
file-type-iconaws-organizations-config-rules
file-type-iconaws-organizations
file-type-iconcloudtrail
file-type-iconcross-account-iam-roles
file-type-iconcustom-iam-entity
file-type-iconfail2ban
file-type-iconiam-groups
file-type-iconiam-user-password-policy
file-type-iconiam-users
file-type-iconip-lockdown
file-type-iconaws-example
file-type-iconuser-data
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-iconlocal-test
file-type-iconREADME.md
file-type-iconip-lockdown-sample.json
file-type-iconkms-master-key
file-type-iconntp
file-type-iconos-hardening
file-type-iconsaml-iam-roles
file-type-iconssh-grunt
file-type-iconssm-healthchecks-iam-permissions
file-type-iconmodules
file-type-icontest
file-type-icon.editorconfig
file-type-icon.gitignore
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc

Browse the Repo

file-type-icon.circleci
file-type-icon_ci
file-type-icon_docs
file-type-iconexamples
file-type-iconauto-update
file-type-iconaws-config
file-type-iconaws-organizations-config-rules
file-type-iconaws-organizations
file-type-iconcloudtrail
file-type-iconcross-account-iam-roles
file-type-iconcustom-iam-entity
file-type-iconfail2ban
file-type-iconiam-groups
file-type-iconiam-user-password-policy
file-type-iconiam-users
file-type-iconip-lockdown
file-type-iconaws-example
file-type-iconuser-data
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-iconlocal-test
file-type-iconREADME.md
file-type-iconip-lockdown-sample.json
file-type-iconkms-master-key
file-type-iconntp
file-type-iconos-hardening
file-type-iconsaml-iam-roles
file-type-iconssh-grunt
file-type-iconssm-healthchecks-iam-permissions
file-type-iconmodules
file-type-icontest
file-type-icon.editorconfig
file-type-icon.gitignore
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc
ssh-grunt

ssh-grunt

Manage SSH access to EC2 Instances using groups in AWS IAM or your Identity Provider (e.g., ADFS, Google, Okta, etc).

Code Preview

Preview the Code

mobile file icon

README.md

down

ip-lockdown AWS Example

This is an example of a real world scenario where we demonstrate how ip-lockdown can be invoked as part of User Data to automatically lock down the EC2 metadata IP upon startup of an EC2 instance.

The main motivation for locking down EC2 metadata is as follows:

  1. EC2 metadata gives you the credentials you need to assume any IAM role associated with the EC2 instance, and thereby, get all the permissions available in that IAM role.
  2. Locking down the metadata to, for example, only the root user, makes sure that if a hacker breaks into your server with a privileged user, they cannot get the full power of the IAM role.

For example, we often give EC2 Instances access to a KMS key via an IAM role so that instance can decrypt secrets before booting. Using ip-lockdown ensures that only the root user can access that KMS key, and therefore, secrets, which is a big improvement to your security posture.

Quick start

To build the AMIs:

  1. Install Packer
  2. Set your GitHub access token as the environment variable GITHUB_OAUTH_TOKEN.
  3. Run packer build ../ip-lockdown-sample.json

Running the Terraform Code

  1. Edit vars.tf and add your own values for: aws_region and key_name
    1. aws_region - This is the AWS region where your EC2 instance will be created. Ex: us-east-1
    2. key_name - This is the name of the AWS key pair you will use to ssh to this EC2 instance. To create a new key pair see this guide
  2. Run terraform init
  3. Run terraform plan
    • Verify that you will be creating one new EC2 instance and one new security group to associate with that instance.
  4. Run terraform apply

Verifying that ip-lockdown worked.

  1. Check your EC2 Dashboard for your new EC2 instance.

  2. SSH to your new EC2 instance

  3. Run sudo iptables -L and observe that there are now rules that block access for all users except root. Example below.

    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     all  --  anywhere             instance-data.ec2.internal  owner UID match root
    REJECT     all  --  anywhere             instance-data.ec2.internal  reject-with icmp-port-unreachable
    
  4. Try to curl metadata by doing curl http://169.254.169.254/latest/meta-data/ (You should receive an error: curl: (7) Failed to connect to 169.254.169.254 port 80: Connection refused)

  5. Try to curl the metadata again: sudo curl http://169.254.169.254/latest/meta-data/ (this should work)

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?