Created
January 7, 2022 07:08
-
-
Save Anon-Exploiter/056bd01809dbbf227abba64f5329c163 to your computer and use it in GitHub Desktop.
Get STS token returned with export AWS_={key}
This file contains 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
#!/usr/bin/python3 | |
from sys import argv | |
from os import popen | |
import json | |
import argparse | |
def getMfaSerial(profile): | |
if profile == None: | |
command = "aws iam list-mfa-devices --query MFADevices[*].SerialNumber" | |
else: | |
command = f"aws iam list-mfa-devices --query MFADevices[*].SerialNumber --profile {profile}" | |
try: | |
mfaSerial = json.loads(popen(command).read()) | |
return(mfaSerial[0]) | |
except IndexError: | |
print("[!] Wrong MFA token for the wrong profile? Maybe?") | |
exit(1) | |
def getSessionTokens(profile, mfaSerial, mfaToken): | |
try: | |
if profile == None: | |
command = f"aws sts get-session-token --serial-number {mfaSerial} --token {mfaToken}" | |
else: | |
command = f"aws sts get-session-token --serial-number {mfaSerial} --token {mfaToken} --profile {profile}" | |
token = json.loads(popen(command).read())["Credentials"] | |
return(token) | |
except json.decoder.JSONDecodeError: | |
exit('[!] Please enter correct MFA!') | |
def addArguments(): | |
parser = argparse.ArgumentParser(description='', usage=f'\r[#] Usage: getSTSToken --mfa 123456 --profile default') | |
parser._optionals.title = "Basic Help" | |
opts = parser.add_argument_group(f'Arguments') | |
opts.add_argument('-m', '--mfa', action="store", dest="mfa", default=False, help='MFA of the user account') | |
opts.add_argument('-p', '--profile', action="store", dest="profile", default=False, help='Access keys profile ( -> default if none given)') | |
args = parser.parse_args() | |
return(args, parser) | |
def main(): | |
args, parser = addArguments() | |
if args.mfa: | |
mfa = int(args.mfa) | |
if args.profile: | |
profile = args.profile | |
else: | |
profile = None | |
mfaSerial = getMfaSerial(profile) | |
sessionTokens = getSessionTokens(profile, mfaSerial, mfa) | |
print(f"export AWS_ACCESS_KEY_ID={sessionTokens['AccessKeyId']}") | |
print(f"export AWS_SECRET_ACCESS_KEY={sessionTokens['SecretAccessKey']}") | |
print(f"export AWS_SESSION_TOKEN={sessionTokens['SessionToken']}") | |
else: | |
parser.print_help() | |
exit() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
For some custom profile
For default profile
Additionally, you can make the file executable and move it to any directory in path, saves some time.