From 12b3b85fdb72ba5c6f2eb9e50682fdd6a6ba5e6b Mon Sep 17 00:00:00 2001 From: carnal0wnage Date: Wed, 14 Jun 2017 00:40:21 -0400 Subject: [PATCH] ec2 modules --- ec2/ec2.py | 63 +++++++++++++++++++++++++++++++- ec2/ec2.pyc | Bin 2087 -> 3843 bytes ec2_get_all_instances.py | 23 ++++++++++++ ec2_get_instance_volumes.py | 25 +++++++++++++ ec2_review_encrypted_volumes.py | 2 +- 5 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 ec2_get_all_instances.py create mode 100644 ec2_get_instance_volumes.py diff --git a/ec2/ec2.py b/ec2/ec2.py index 8643308..39573d3 100644 --- a/ec2/ec2.py +++ b/ec2/ec2.py @@ -6,7 +6,8 @@ import pprint pp = pprint.PrettyPrinter(indent=5, width=80) -regions = ['us-east-1', 'us-west-2', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'eu-central-1', 'eu-west-1'] +#from http://docs.aws.amazon.com/general/latest/gr/rande.html +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', ] # right now this will print a file with nothing if bad key, should fix at some point --otherwise can assume its a valid key # we are past the enumeration stage at this point @@ -52,4 +53,64 @@ def review_encrypted_volumes(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY): print e +def get_instance_details(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY): + try: + for region in regions: + client = boto3.client( + 'ec2', + aws_access_key_id = AWS_ACCESS_KEY_ID, + aws_secret_access_key = AWS_SECRET_ACCESS_KEY, + region_name=region + ) + + instances = client.describe_instances() + for r in instances['Reservations']: + for i in r['Instances']: + pp.pprint(i) + + except botocore.exceptions.ClientError as e: + print e + +#show volumes sorted by instanceId ex: instanceID-->multiple volumes less detail than get_instance_volume_details2 +def get_instance_volume_details(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY): + try: + for region in regions: + client = boto3.client( + 'ec2', + aws_access_key_id = AWS_ACCESS_KEY_ID, + aws_secret_access_key = AWS_SECRET_ACCESS_KEY, + region_name=region + ) + + instances = client.describe_instances() + for r in instances['Reservations']: + for i in r['Instances']: + volumes = client.describe_instance_attribute(InstanceId=i['InstanceId'], Attribute='blockDeviceMapping') + print ("Instance ID: {} \n" .format(i['InstanceId'])) + pp.pprint(volumes) + + except botocore.exceptions.ClientError as e: + print e + +#show volumes by instanceId but instanceID->volume1 of ID, instanceID->volume2 of ID but more details. +def get_instance_volume_details2(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY): + try: + for region in regions: + client = boto3.client( + 'ec2', + aws_access_key_id = AWS_ACCESS_KEY_ID, + aws_secret_access_key = AWS_SECRET_ACCESS_KEY, + region_name=region + ) + response = client.describe_volumes(Filters=[{ + 'Name' : 'status', + 'Values' : ['in-use'] + }])['Volumes'] + for volume in response: + print("InstandID:{} \n" .format(volume['Attachments'][0]['InstanceId'])) + pp.pprint(volume) + print("\n") + + except botocore.exceptions.ClientError as e: + print e diff --git a/ec2/ec2.pyc b/ec2/ec2.pyc index c60ab9c1e1bb7c65ecf459c6cdc907d92e2fe62f..a871bf3b8a657e1582ce204a228007ccfd5f3f8a 100644 GIT binary patch delta 1727 zcmai!Pj4GV7{=e(wRgQ+C(S>Xq$weVR&t}dC8k7GKve^lnu?}}MUb{qlsY@BXt%*h zJmcU9TYE4Px1wMP!2!grAAoZYaG;ld13m^{fae{@joX^CtM}QNo!Ob4`91SK8SBi= z+W(CBH=f?SAJXxa$MYsaJwBRU|5%QaJWXs}83^lJ&D?>6%GnI`ZwX@WGhgw*v!!IVO?U2q;lBJeIom7z6zfbG? zs|cUg*K2c>(GDdpB{_QJQ~hTWZI?vLrCX~tLH8tKGhF=R-N1$h8I~-I1JS(vAW~8N zpsM5Dn$oD>-OBzZ$ zxPq2|w=y^lF}uH_H8?hOWUy}S?1qmj(Lo$WYt_BIxVEjv(X(%e`P-G{`ShFiykF!= z2A#px_j%A+$ZPJ{toEk~3fyy%WtdZp;f6RRDDxK|uuNQoYPR&j?oQ*YNClU>f4QHe zJn>7m=r#A<@fn&GPdVHj=fuQ`LwN8P4*3(6!(XWRW6vRv|HC0Tny;U5oaz{fz+hhj z_HA_XXbF!F380_h^S{jgsce+lJMt7;g^zz8J{dub_HB`+1Xju#TOcdmTHW6oOaiG% z0k8&Vfv50=17A3Uz8J$ohWmoo8<<1Nt5%#D;##SpqQK`CX01jcJs(K8fISfKs~BPI z6q4eT>le)@1381+XIS1$-u%Nb^6&At3oM57+Bp>8z`6Vo?TXXRE1<2saK#v6M4Uy8 uBOMg|?)UD6*`6KEFPHFH@FB}G%SYWm+VPJ@2V@P3S2-e`A zxXzBPJhiw)*JyG)qorUmA5eK>fo@)YQAtK>VsXi2Sw_XpGZ?wp7 diff --git a/ec2_get_all_instances.py b/ec2_get_all_instances.py new file mode 100644 index 0000000..056621d --- /dev/null +++ b/ec2_get_all_instances.py @@ -0,0 +1,23 @@ +''' +This file is used to list ec2 instances +''' + +import boto3 +import botocore + +import json +import urllib +import logging +import sys,os +import pprint + +pp = pprint.PrettyPrinter(indent=5, width=80) + +from ec2.ec2 import * + +#insert AWS key, will figure out how to pull this in from a single file for all scripts + +AWS_ACCESS_KEY_ID = '' +AWS_SECRET_ACCESS_KEY = '' + +get_instance_details(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) \ No newline at end of file diff --git a/ec2_get_instance_volumes.py b/ec2_get_instance_volumes.py new file mode 100644 index 0000000..cfd8a41 --- /dev/null +++ b/ec2_get_instance_volumes.py @@ -0,0 +1,25 @@ +''' +This file is used to list volumes of ec2 instances +''' + +import boto3 +import botocore + +import json +import urllib +import logging +import sys,os +import pprint + +pp = pprint.PrettyPrinter(indent=5, width=80) + +from ec2.ec2 import * + +#insert AWS key, will figure out how to pull this in from a single file for all scripts + +AWS_ACCESS_KEY_ID = '' +AWS_SECRET_ACCESS_KEY = '' + + +get_instance_volume_details(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) +get_instance_volume_details2(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) \ No newline at end of file diff --git a/ec2_review_encrypted_volumes.py b/ec2_review_encrypted_volumes.py index 4642eef..bb66f12 100644 --- a/ec2_review_encrypted_volumes.py +++ b/ec2_review_encrypted_volumes.py @@ -19,6 +19,6 @@ from ec2.ec2 import * #insert AWS key, will figure out how to pull this in from a single file for all scripts AWS_ACCESS_KEY_ID = '' -AWS_SECRET_ACCESS_KEY ='' +AWS_SECRET_ACCESS_KEY = '' review_encrypted_volumes(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) \ No newline at end of file