Forked from kananlanginhooper/ExtractAWSRoleBasedKeys.sh
Created
November 5, 2021 15:25
-
-
Save dalmosantos/a3a99b81e25cfa729fd298af34acaf62 to your computer and use it in GitHub Desktop.
Shell script to extract Temporary AWS keys (Access Key Id and Secret Access Key) from Role Based EC2 instance using 169.254.169.254 and dynamic role names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
ROLENAME=$(curl http://169.254.169.254/latest/meta-data/iam/security-credentials/ -s) | |
KeyURL="http://169.254.169.254/latest/meta-data/iam/security-credentials/"$ROLENAME"/" | |
wget $KeyURL -q -O Iam.json | |
KEYID=$(grep -Po '.*"AccessKeyId".*' Iam.json | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/AccessKeyId://g') | |
SECRETKEY=$(grep -Po '.*"SecretAccessKey".*' Iam.json | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/SecretAccessKey://g') | |
SECURITYTOKEN=$(grep -Po '.*"Token".*' Iam.json | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/Token://g') | |
rm Iam.json -f | |
echo $KEYID | |
echo $SECRETKEY | |
echo $SECURITYTOKEN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment