From ac812ff970cd7e44f243c7392238fe3a326e0508 Mon Sep 17 00:00:00 2001 From: carnal0wnage Date: Thu, 17 May 2018 19:11:21 -0400 Subject: [PATCH] lightsail module starter --- libs/lightsail.py | 73 ++++++++++++++++++++++++++++++++++++++++++++ modules/lightsail.py | 13 ++++++++ 2 files changed, 86 insertions(+) create mode 100644 libs/lightsail.py create mode 100644 modules/lightsail.py diff --git a/libs/lightsail.py b/libs/lightsail.py new file mode 100644 index 0000000..3237fc0 --- /dev/null +++ b/libs/lightsail.py @@ -0,0 +1,73 @@ +''' +Lightsail functions for WeirdAAL +''' + +import boto3 +import botocore +import os +import pprint +import sys + +pp = pprint.PrettyPrinter(indent=5, width=80) + +''' +Code to get the AWS_ACCESS_KEY_ID from boto3 +''' +session = boto3.Session() +credentials = session.get_credentials() +AWS_ACCESS_KEY_ID = credentials.access_key +AWS_SECRET_ACCESS_KEY = credentials.secret_key + +# from http://docs.aws.amazon.com/general/latest/gr/rande.html +regions = ['us-east-1', 'us-east-2', 'us-west-2', 'ca-central-1', 'ap-south-1', 'ap-northeast-1', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'eu-west-3'] + + +def lightsail_get_instances(): + ''' + Lightsail Get Instances + ''' + try: + for region in regions: + try: + client = boto3.client('lightsail', region_name=region) + response = client.get_instances() + # print(response) + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == 'UnauthorizedOperation': + print('{} : (UnauthorizedOperation) when calling the DescribeInstances -- sure you have ec2 permissions?' .format(AWS_ACCESS_KEY_ID)) + sys.exit() + elif e.response['Error']['Code'] == 'AuthFailure': + print('{} : (AuthFailure) when calling the Get Instances -- key is invalid or no permissions.' .format(AWS_ACCESS_KEY_ID)) + sys.exit() + elif e.response['Error']['Code'] == 'AccessDeniedException': + print('{} : (AccessDeniedException) no permissions.' .format(AWS_ACCESS_KEY_ID)) + sys.exit() + + else: + print(e) + if len(response['instances']) <= 0: + print("[-] get_instances allowed for {} but no results [-]" .format(region)) + else: + print("[+] Listing instances for region: {} [+]" .format(region)) + #db_logger = [] + for r in response['instances']: + # db_logger.append(['ec2', 'DescribeInstances', str(r), AWS_ACCESS_KEY_ID, target, datetime.datetime.now()]) + #for i in r['Instances']: + pp.pprint(r) + # logging to db here + # try: + # print(db_logger) + # insert_sub_service_data(db_name, db_logger) + # except sqlite3.OperationalError as e: + # print(e) + # print("You need to set up the database...exiting") + # sys.exit() + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == 'UnauthorizedOperation': + print('{} : (UnauthorizedOperation) when calling the Get Instances -- sure you have lightsail permissions?' .format(AWS_ACCESS_KEY_ID)) + elif e.response['Error']['Code'] == 'SubscriptionRequiredException': + print('{} : Has permissions but isnt signed up for service - usually means you have a root account' .format(AWS_ACCESS_KEY_ID)) + else: + print(e) + except KeyboardInterrupt: + print("CTRL-C received, exiting...") \ No newline at end of file diff --git a/modules/lightsail.py b/modules/lightsail.py new file mode 100644 index 0000000..32c1485 --- /dev/null +++ b/modules/lightsail.py @@ -0,0 +1,13 @@ +''' +Module for interacting with the lightsail +''' + +from libs.lightsail import * + + +def module_lightsail_get_instances(): + ''' + Lightsail get_instances + python3 weirdAAL.py -m lightsail_get_instances -t demo + ''' + lightsail_get_instances() \ No newline at end of file