From 236bb190631585c98cba0e492a7215fea3e86fcd Mon Sep 17 00:00:00 2001 From: cktricky Date: Thu, 17 May 2018 22:15:49 -0400 Subject: [PATCH 1/3] added the ability to list topics in each region as well as list all subscribers for a topic --- libs/sns.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ modules/sns.py | 23 ++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 libs/sns.py create mode 100644 modules/sns.py diff --git a/libs/sns.py b/libs/sns.py new file mode 100644 index 0000000..400ca00 --- /dev/null +++ b/libs/sns.py @@ -0,0 +1,59 @@ +''' +utilities for working with SNS +''' + +import boto3 +import botocore + +regions = ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2', 'ca-central-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'ap-northeast-1', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2'] + +session = boto3.Session() +credentials = session.get_credentials() +AWS_ACCESS_KEY_ID = credentials.access_key + +def list_sns_topics(): + title = "SNS Topics" + print(title) + print("-" * len(title)) + try: + for region in regions: + client = boto3.client('sns', region_name=region) + topics = client.list_topics() + print(region) + print("=" * len(region)) + if topics['Topics']: + for topic in topics['Topics']: + print(topic) + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == 'InvalidClientTokenId': + sys.exit("The AWS KEY IS INVALID. Exiting") + if e.response['Error']['Code'] == 'AccessDenied': + print('{} : Is NOT a root key' .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("Unexpected error: {}" .format(e)) + except KeyboardInterrupt: + print("CTRL-C received, exiting...") + +def list_sns_subscribers(topic,region): + try: + client = boto3.client('sns', region_name=region) + result = client.list_subscriptions_by_topic(TopicArn=topic) + subscriptions = result['Subscriptions'] + for sub in subscriptions: + print("Protocol: {}".format(sub['Protocol'])) + print("Endpoint: {}".format(sub['Endpoint'])) + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == 'InvalidClientTokenId': + sys.exit("The AWS KEY IS INVALID. Exiting") + if e.response['Error']['Code'] == 'AccessDenied': + print('{} : Is NOT a root key' .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)) + elif e.response['Error']['Code'] == 'InvalidParameter': + print('The region you provided ({}) is invalid for the Topic ARN. Are you sure this topic exists in this region?'.format(region)) + else: + print("Unexpected error: {}" .format(e)) + except KeyboardInterrupt: + print("CTRL-C received, exiting...") diff --git a/modules/sns.py b/modules/sns.py new file mode 100644 index 0000000..8a8e42a --- /dev/null +++ b/modules/sns.py @@ -0,0 +1,23 @@ +''' +SNS module +''' + +from libs.sns import * + +def module_sns_list_topics(): + ''' + SNS list all topics + python3 weirdAAL.py -m sns_list_topics -t demo + ''' + list_sns_topics() + +def module_sns_list_subscribers(*args): + ''' + SNS list subscribers for a topic. Takes two arguments - the topic arn and then the region. + python3 weirdAAL.py -m sns_list_subscribers -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1 + ''' + try: + if args[0][0] and args[0][1]: + list_sns_subscribers(args[0][0], args[0][1]) + except IndexError: + print("Please provide a topic arn *AND* region, ex: -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1") From 77f99e34ff58175a8d738e87991aaf4411bea040 Mon Sep 17 00:00:00 2001 From: cktricky Date: Sat, 19 May 2018 22:00:31 -0300 Subject: [PATCH 2/3] cool, now we can delete sns topics --- libs/sns.py | 19 +++++++++++++++++++ modules/sns.py | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/libs/sns.py b/libs/sns.py index 400ca00..3faabec 100644 --- a/libs/sns.py +++ b/libs/sns.py @@ -57,3 +57,22 @@ def list_sns_subscribers(topic,region): print("Unexpected error: {}" .format(e)) except KeyboardInterrupt: print("CTRL-C received, exiting...") + +def delete_sns_topic(topic, region): + try: + client = boto3.client('sns', region_name=region) + action = client.delete_topic(TopicArn=topic) + print("Deleted Topic: {}".format(topic)) + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == 'InvalidClientTokenId': + sys.exit("The AWS KEY IS INVALID. Exiting") + if e.response['Error']['Code'] == 'AccessDenied': + print('{} : Is NOT a root key' .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)) + elif e.response['Error']['Code'] == 'InvalidParameter': + print('The region you provided ({}) is invalid for the Topic ARN. Are you sure this topic exists in this region?'.format(region)) + else: + print("Unexpected error: {}" .format(e)) + except KeyboardInterrupt: + print("CTRL-C received, exiting...") diff --git a/modules/sns.py b/modules/sns.py index 8a8e42a..3f37609 100644 --- a/modules/sns.py +++ b/modules/sns.py @@ -21,3 +21,14 @@ def module_sns_list_subscribers(*args): list_sns_subscribers(args[0][0], args[0][1]) except IndexError: print("Please provide a topic arn *AND* region, ex: -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1") + +def module_sns_delete_topic(*args): + ''' + SNS delete a topic. Takes two arguments - the topic arn and the region. + python3 weirdAAL.py -m sns_delete_topic -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1 + ''' + try: + if args[0][0] and args[0][1]: + delete_sns_topic(args[0][0], args[0][1]) + except IndexError: + print("Please provide a topic arn *AND* region, ex: -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1") From 1ed839cfda710815a715823a149a184eab9e2b58 Mon Sep 17 00:00:00 2001 From: cktricky Date: Sat, 19 May 2018 23:05:17 -0300 Subject: [PATCH 3/3] so you can delete SNS topics --- libs/sns.py | 19 +++++++++++++++++++ modules/sns.py | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/libs/sns.py b/libs/sns.py index 3faabec..e4033a0 100644 --- a/libs/sns.py +++ b/libs/sns.py @@ -42,6 +42,7 @@ def list_sns_subscribers(topic,region): result = client.list_subscriptions_by_topic(TopicArn=topic) subscriptions = result['Subscriptions'] for sub in subscriptions: + print("Subscription Arn: {}".format(sub['SubscriptionArn'])) print("Protocol: {}".format(sub['Protocol'])) print("Endpoint: {}".format(sub['Endpoint'])) except botocore.exceptions.ClientError as e: @@ -76,3 +77,21 @@ def delete_sns_topic(topic, region): print("Unexpected error: {}" .format(e)) except KeyboardInterrupt: print("CTRL-C received, exiting...") + +def delete_sns_subscriber(endpoint, region): + try: + client = boto3.client('sns', region_name=region) + action = client.delete_endpoint(EndpointArn=endpoint) + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == 'InvalidClientTokenId': + sys.exit("The AWS KEY IS INVALID. Exiting") + if e.response['Error']['Code'] == 'AccessDenied': + print('{} : Is NOT a root key' .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)) + #elif e.response['Error']['Code'] == 'InvalidParameter': + # print('The region you provided ({}) is invalid for the Subscriber endpoint. Are you sure this subscriber exists in this region?'.format(region)) + else: + print("Unexpected error: {}" .format(e)) + except KeyboardInterrupt: + print("CTRL-C received, exiting...") diff --git a/modules/sns.py b/modules/sns.py index 3f37609..cf9460d 100644 --- a/modules/sns.py +++ b/modules/sns.py @@ -32,3 +32,15 @@ def module_sns_delete_topic(*args): delete_sns_topic(args[0][0], args[0][1]) except IndexError: print("Please provide a topic arn *AND* region, ex: -a arn:aws:sns:us-east-1:123456789123:sometopic,us-east-1") + +# Shit is broke atm +#def module_sns_delete_subscriber(*args): +# ''' +# SNS delete a subscriber. Takes two arguments - the subscriber arn and the region. +# python3 weirdAAL.py -m sns_delete_subscriber -a arn:aws:sns:us-east-1:123456789123:pwned-topic:05ac3eaa-703a-4bda-83ad-6861893f7542,us-east-1 +# ''' +# try: +# if args[0][0] and args[0][1]: +# delete_sns_subscriber(args[0][0], args[0][1]) +# except IndexError: +# print("Please provide a subscriber arn *AND* region, ex: -a arn:aws:sns:us-east-1:123456789123:pwned-topic:05ac3eaa-703a-4bda-83ad-6861893f7542,us-east-1")