stab at ec2 log sub_service to db
This commit is contained in:
@@ -2,12 +2,13 @@ from __future__ import print_function
|
||||
|
||||
import boto3
|
||||
import botocore
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import pprint
|
||||
import sys
|
||||
import datetime
|
||||
|
||||
|
||||
from libs.sql import *
|
||||
|
||||
|
||||
32
libs/ec2.py
32
libs/ec2.py
@@ -1,7 +1,13 @@
|
||||
import boto3
|
||||
import botocore
|
||||
import datetime
|
||||
import pprint
|
||||
|
||||
from libs.sql import *
|
||||
|
||||
# we chould probably load this from one place in the future #TODO
|
||||
db_name = "weirdAAL.db"
|
||||
|
||||
'''
|
||||
EC2 functions for WeirdAAL
|
||||
'''
|
||||
@@ -68,10 +74,19 @@ def describe_instances():
|
||||
print("[-] List instances allowed for {} but no results [-]" .format(region))
|
||||
else:
|
||||
print("[+] Listing instances for region: {} [+]" .format(region))
|
||||
db_logger = []
|
||||
for r in response['Reservations']:
|
||||
db_logger.append(['ec2', 'DescribeInstances', str(r), AWS_ACCESS_KEY_ID, datetime.datetime.now()])
|
||||
for i in r['Instances']:
|
||||
pp.pprint(i)
|
||||
|
||||
# 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 DescribeInstances -- sure you have ec2 permissions?' .format(AWS_ACCESS_KEY_ID))
|
||||
@@ -93,17 +108,28 @@ def describe_instances_basic():
|
||||
else:
|
||||
# print (response)
|
||||
print("[+] Listing instances for region: {} [+]" .format(region))
|
||||
db_logger = []
|
||||
for r in response['Reservations']:
|
||||
# logging the full blob
|
||||
db_logger.append(['ec2', 'DescribeInstances', str(r), AWS_ACCESS_KEY_ID, datetime.datetime.now()])
|
||||
for i in r['Instances']:
|
||||
launchtime = i['LaunchTime']
|
||||
instanceid = i['InstanceId']
|
||||
instancetype = i['InstanceType']
|
||||
state = i['State']
|
||||
print("InstanceID: {}, InstanceType: {}, State: {}, Launchtime: {}".format(instanceid, instancetype, state, launchtime))
|
||||
|
||||
# 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()
|
||||
print("\n")
|
||||
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))
|
||||
print('{} : (UnauthorizedOperation) when calling the DescribeInstances -- sure you have ec2 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:
|
||||
|
||||
12
libs/sql.py
12
libs/sql.py
@@ -27,6 +27,7 @@ def create_table(db_name,table_name,sql):
|
||||
cursor.execute(sql)
|
||||
db.commit()
|
||||
|
||||
|
||||
def create_recon_table(db_name, table_name):
|
||||
sql = """CREATE TABLE recon
|
||||
(ID integer,
|
||||
@@ -39,6 +40,7 @@ def create_recon_table(db_name, table_name):
|
||||
create_table(db_name,table_name,sql)
|
||||
print ("created table: {}".format(table_name))
|
||||
|
||||
|
||||
def create_awskey_table(db_name, table_name):
|
||||
sql = """CREATE TABLE AWSKey
|
||||
(ID integer,
|
||||
@@ -48,6 +50,7 @@ def create_awskey_table(db_name, table_name):
|
||||
create_table(db_name,table_name,sql)
|
||||
print ("created table: {}".format(table_name))
|
||||
|
||||
|
||||
def create_services_table(db_name, table_name):
|
||||
sql = """CREATE TABLE services
|
||||
(ID integer,
|
||||
@@ -66,11 +69,19 @@ def insert_awskey_data(db_name, records):
|
||||
for record in records:
|
||||
query(db_name, sql,record)
|
||||
|
||||
|
||||
def insert_reconservice_data(db_name, records):
|
||||
sql = """INSERT INTO recon(service, sub_service, AWSKeyID, checked_at) VALUES (?,?,?,?)"""
|
||||
for record in records:
|
||||
query(db_name,sql,record)
|
||||
|
||||
|
||||
def insert_sub_service_data(db_name, records):
|
||||
sql = """INSERT INTO services(service, sub_service, sub_service_data, AWSKeyID, checked_at) VALUES (?,?,?,?,?)"""
|
||||
for record in records:
|
||||
query(db_name,sql,record)
|
||||
|
||||
|
||||
def search_recon_by_key(db_name,AWSKeyID):
|
||||
with sqlite3.connect(db_name) as db:
|
||||
cursor = db.cursor()
|
||||
@@ -78,6 +89,7 @@ def search_recon_by_key(db_name,AWSKeyID):
|
||||
results = cursor.fetchall()
|
||||
return results
|
||||
|
||||
|
||||
def query(db_name,sql,data):
|
||||
with sqlite3.connect(db_name) as db:
|
||||
cursor = db.cursor()
|
||||
|
||||
Reference in New Issue
Block a user