Advertisement
JamesBops

Untitled

Mar 3rd, 2023
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. import boto3
  2. import botocore
  3. import sys
  4.  
  5.  
  6. # save file
  7. def save_file(file_name, file_content):
  8. with open(file_name, "a") as file:
  9. file.write(file_content + "\n")
  10. file.close()
  11.  
  12.  
  13. def build_aws_key(key, secret):
  14. aws_key = key + "|" + secret + "|" + "us-east-1"
  15. return aws_key
  16.  
  17.  
  18. # configure aws
  19. def check_aws(key, secret):
  20. aws_key = build_aws_key(key, secret)
  21. try:
  22. session = boto3.Session(
  23. aws_access_key_id=key,
  24. aws_secret_access_key=secret,
  25. region_name="us-east-1"
  26. )
  27. check_identity = session.client("sts").get_caller_identity()
  28. check = Check(aws_key, session)
  29. if "root" in check_identity["Arn"]:
  30. print("Root Aws Key > " + key)
  31. save_file("root_aws_key.txt", aws_key)
  32. check.check_ses()
  33. check.check_ec2()
  34. else:
  35. print("Valid Aws Key > " + key)
  36. save_file("valid_aws_key.txt", aws_key)
  37. check.check_ses()
  38. check.check_ec2()
  39. except botocore.exceptions.ClientError as error:
  40. er = error.response["Error"]["Code"]
  41. if er == "InvalidClientTokenId":
  42. print("Invalid Aws Key > " + key)
  43. elif er == "SignatureDoesNotMatch":
  44. print("Signature Does Not Match > " + key)
  45. else:
  46. print(er + " > " + key)
  47. except Exception as e:
  48. print(str(e))
  49.  
  50. class Check(object):
  51.  
  52. def __init__(self, aws_key, session):
  53. self.aws_key = aws_key
  54. self.session = session
  55. self.region_list = [
  56. "us-east-1",
  57. "us-east-2",
  58. "us-west-1",
  59. "us-west-2",
  60. "ap-south-1",
  61. "ap-southeast-1",
  62. "ap-southeast-2",
  63. "ap-northeast-1",
  64. "ap-northeast-2",
  65. "ap-northeast-3",
  66. "ca-central-1",
  67. "eu-south-1",
  68. "eu-central-1",
  69. "eu-north-1",
  70. "eu-west-1",
  71. "eu-west-2",
  72. "eu-west-3",
  73. "sa-east-1",
  74. "me-south-1",
  75. ]
  76.  
  77. def check_ec2(self):
  78. print("\nChecking EC2 List Quotas in all regions\n")
  79. all_save = self.aws_key + "\n"
  80. for reg in self.region_list:
  81. try:
  82. service_quotas = self.session.client(
  83. "service-quotas", region_name=reg).list_service_quotas(ServiceCode="ec2")
  84. quotas_list = service_quotas["Quotas"]
  85. Result = "Region: " + reg + "\n"
  86. for quotas in quotas_list:
  87. if "All" in str(quotas):
  88. quotaname = quotas["QuotaName"]
  89. value = quotas["Value"]
  90. Result += quotaname + ": " + str(value) + "\n"
  91. all_save += Result + "\n"
  92. print(Result)
  93. except botocore.exceptions.ClientError as error:
  94. er = error.response["Error"]["Code"]
  95. if er == "AccessDeniedException":
  96. print("Access Denied for EC2\n")
  97. break
  98. elif er == "UnrecognizedClientException":
  99. print("Region " + reg + " locked for EC2\n")
  100. else:
  101. print(str(error))
  102. except botocore.exceptions.ReadTimeoutError:
  103. print("Cant connect to EC2 " + reg + " endpoint\n")
  104. except KeyboardInterrupt:
  105. continue
  106. except Exception as e:
  107. print(str(e))
  108. if "Region" in all_save:
  109. save_file("ec2_quotas.txt", all_save)
  110.  
  111. def check_ses(self):
  112. print("\nChecking SES in all regions\n")
  113. all_save = self.aws_key + "\n"
  114. for reg in self.region_list:
  115. try:
  116. sesv2 = self.session.client("sesv2", region_name=reg)
  117. check_account = sesv2.get_account()
  118. status = check_account["EnforcementStatus"]
  119. quota = check_account["SendQuota"]
  120. max24 = quota["Max24HourSend"]
  121. maxsend = quota["MaxSendRate"]
  122. sentlast = quota["SentLast24Hours"]
  123. # checking identity
  124. identity = ""
  125. identities = self.session.client(
  126. "ses", region_name=reg).list_identities()["Identities"]
  127. if len(identities) > 0:
  128. identity = "SES Identity : " + ", ".join(identities)
  129. else:
  130. identity = "No SES Identities found"
  131. Result = "Region: " + reg + "\nStatus: " + status + "\nMax24HourSend: " + \
  132. str(max24) + "\nMaxSendRate: " + str(maxsend) + \
  133. "\nSentLast24Hours: " + \
  134. str(sentlast) + "\n" + identity + "\n"
  135. all_save += Result + "\n"
  136. print(Result)
  137. except botocore.exceptions.ClientError as error:
  138. er = error.response["Error"]["Code"]
  139. if er == "AccessDeniedException":
  140. print("Access Denied for SESV2\n")
  141. break
  142. elif er == "UnrecognizedClientException":
  143. print("Region " + reg + " locked for SESV2\n")
  144. else:
  145. print(str(error))
  146. except botocore.exceptions.ReadTimeoutError:
  147. print("Cant connect to SESV2 " + reg + " endpoint\n")
  148. except KeyboardInterrupt:
  149. continue
  150. except Exception as e:
  151. print(str(e))
  152. if "Region" in all_save:
  153. save_file("sesv2_aws_key.txt", all_save)
  154.  
  155.  
  156. if __name__ == "__main__":
  157. key = input("Enter your aws key: ")
  158. secret = input("Enter your aws secret: ")
  159. check_aws(key, secret)
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement