-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrap-w-emails.py
323 lines (298 loc) · 16.8 KB
/
scrap-w-emails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import logging
import os
import json
import boto3
import time
import datetime
import argparse
import csv
import string
import random
from botocore.exceptions import ClientError
from datetime import timezone
current_date = datetime.datetime.now(tz=timezone.utc)
current_date_string = str(current_date)
timestamp_date = datetime.datetime.now(tz=timezone.utc).strftime("%Y-%m-%d-%H%M%S")
timestamp_date_string = str(timestamp_date)
sts = boto3.client('sts')
cloudtrail = boto3.client('cloudtrail')
organizations = boto3.client('organizations')
region = os.environ['AWS_REGION']
region_list = ['af-south-1', 'ap-east-1', 'ap-south-1', 'ap-northeast-1', 'ap-northeast-2', 'ap-northeast-3', 'ap-southeast-1', 'ap-southeast-2', 'ca-central-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'eu-north-1', 'eu-south-1', 'me-south-1', 'sa-east-1', 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']
# -----BEGIN RSA PRIVATE KEY-----
# 1. Obtain the AWS Accounts inside of AWS Organizations.
def org_account_grab():
"""Function to list accounts inside of AWS Organizations"""
try:
OrgAccountIdList: list = []
org_account_list = organizations.list_accounts()
for accounts in org_account_list['Accounts']:
OrgAccountIdList.append(accounts['Id'])
get_organization_id = organizations.describe_organization()
organization_id = get_organization_id['Organization']['Id']
except Exception as exception_handle:
logging.error(exception_handle)
logging.error("Multi account mode is only for accounts using AWS Organizations.")
logging.error("Please run the Assisted Log Enabler in single account mode to turn on AWS Logs.")
exit()
return OrgAccountIdList, organization_id
# 2. Obtain the current AWS Account Number.
def get_account_number():
"""Function to grab AWS Account number that Assisted Log Enabler runs from."""
sts = boto3.client('sts')
account_number = sts.get_caller_identity()["Account"]
return account_number
# 3. Find VPCs and check if VPC Flow Logs are on.
def dryrun_flow_log_activator(account_number, OrgAccountIdList, region_list):
"""Function to define the list of VPCs without logging turned on"""
logging.info("Creating a list of VPCs without Flow Logs on.")
for org_account in OrgAccountIdList:
for aws_region in region_list:
sts = boto3.client('sts')
RoleArn = 'arn:aws:iam::%s:role/Assisted_Log_Enabler_IAM_Role' % org_account
logging.info('Assuming Target Role %s for Assisted Log Enabler...' % RoleArn)
assisted_log_enabler_sts = sts.assume_role(
RoleArn=RoleArn,
RoleSessionName='assisted-log-enabler-activation',
DurationSeconds=3600,
)
ec2_ma = boto3.client(
'ec2',
aws_access_key_id=assisted_log_enabler_sts['Credentials']['AccessKeyId'],
aws_secret_access_key=assisted_log_enabler_sts['Credentials']['SecretAccessKey'],
aws_session_token=assisted_log_enabler_sts['Credentials']['SessionToken'],
region_name=aws_region
)
logging.info("Creating a list of VPCs without Flow Logs on in region " + aws_region + ".")
try:
VPCList: list = []
FlowLogList: list = []
logging.info("DescribeVpcs API Call")
vpcs = ec2_ma.describe_vpcs()
for vpc_id in vpcs["Vpcs"]:
VPCList.append(vpc_id["VpcId"])
logging.info("List of VPCs found within account " + org_account + ", region " + aws_region + ":")
print(VPCList)
logging.info("DescribeFlowLogs API Call")
vpcflowloglist = ec2_ma.describe_flow_logs()
for resource_id in vpcflowloglist["FlowLogs"]:
FlowLogList.append(resource_id["ResourceId"])
working_list = (list(set(VPCList) - set(FlowLogList)))
logging.info("List of VPCs found within account " + org_account + ", region " + aws_region + " WITHOUT VPC Flow Logs:")
print(working_list)
for no_logs in working_list:
logging.info(no_logs + " does not have VPC Flow logging on. This will not be turned on within the Dry Run option.")
except Exception as exception_handle:
logging.error(exception_handle)
# 4. List EKS Clusters for visibility.
def dryrun_eks_logging(region_list, OrgAccountIdList):
"""Function to turn on logging for EKS Clusters"""
for org_account in OrgAccountIdList:
for aws_region in region_list:
logging.info("Showing Amazon EKS clusters in AWS account " + org_account + ", in region " + aws_region + ".")
sts = boto3.client('sts')
RoleArn = 'arn:aws:iam::%s:role/Assisted_Log_Enabler_IAM_Role' % org_account
logging.info('Assuming Target Role %s for Assisted Log Enabler...' % RoleArn)
assisted_log_enabler_sts = sts.assume_role(
RoleArn=RoleArn,
RoleSessionName='assisted-log-enabler-activation',
DurationSeconds=3600,
)
eks_ma = boto3.client(
'eks',
aws_access_key_id=assisted_log_enabler_sts['Credentials']['AccessKeyId'],
aws_secret_access_key=assisted_log_enabler_sts['Credentials']['SecretAccessKey'],
aws_session_token=assisted_log_enabler_sts['Credentials']['SessionToken'],
region_name=aws_region
)
try:
logging.info("ListClusters API Call")
eks_clusters = eks_ma.list_clusters()
eks_cluster_list = eks_clusters ['clusters']
logging.info("EKS Clusters found in " + aws_region + ":")
print(eks_cluster_list)
for cluster in eks_cluster_list:
logging.info("Please check if Audit and Authenticator logs are on for EKS Cluster " + cluster)
except Exception as exception_handle:
logging.error(exception_handle)
# 6. Turn on Route 53 Query Logging.
def dryrun_route_53_query_logs(region_list, account_number, OrgAccountIdList):
"""Function to turn on Route 53 Query Logs for VPCs"""
for org_account in OrgAccountIdList:
for aws_region in region_list:
logging.info("Checking Route 53 Query Logging on in AWS Account " + org_account + " VPCs, in region " + aws_region + ".")
sts = boto3.client('sts')
RoleArn = 'arn:aws:iam::%s:role/Assisted_Log_Enabler_IAM_Role' % org_account
logging.info('Assuming Target Role %s for Assisted Log Enabler...' % RoleArn)
assisted_log_enabler_sts = sts.assume_role(
RoleArn=RoleArn,
RoleSessionName='assisted-log-enabler-activation',
DurationSeconds=3600,
)
ec2_ma = boto3.client(
'ec2',
aws_access_key_id=assisted_log_enabler_sts['Credentials']['AccessKeyId'],
aws_secret_access_key=assisted_log_enabler_sts['Credentials']['SecretAccessKey'],
aws_session_token=assisted_log_enabler_sts['Credentials']['SessionToken'],
region_name=aws_region
)
route53resolver_ma = boto3.client(
'route53resolver',
aws_access_key_id=assisted_log_enabler_sts['Credentials']['AccessKeyId'],
aws_secret_access_key=assisted_log_enabler_sts['Credentials']['SecretAccessKey'],
aws_session_token=assisted_log_enabler_sts['Credentials']['SessionToken'],
region_name=aws_region
)
try:
VPCList: list = []
QueryLogList: list = []
logging.info("DescribeVpcs API Call")
vpcs = ec2_ma.describe_vpcs()
for vpc_id in vpcs["Vpcs"]:
VPCList.append(vpc_id["VpcId"])
logging.info("List of VPCs found within account " + org_account + ", region " + aws_region + ":")
print(VPCList)
logging.info("ListResolverQueryLogConfigAssociations API Call")
query_log_details = route53resolver_ma.list_resolver_query_log_config_associations()
for query_log_vpc_id in query_log_details['ResolverQueryLogConfigAssociations']:
QueryLogList.append(query_log_vpc_id['ResourceId'])
r53_working_list = (list(set(VPCList) - set(QueryLogList)))
logging.info("List of VPCs found within account " + org_account + ", region " + aws_region + " WITHOUT Route 53 Query Logs:")
print(r53_working_list)
for no_query_logs in r53_working_list:
logging.info(no_query_logs + " does not have Route 53 Query logging on. Running Assisted Log Enabler for AWS will turn this on.")
except Exception as exception_handle:
logging.error(exception_handle)
# 7. Turn on S3 Logging.
def dryrun_s3_logs(region_list, account_number, OrgAccountIdList):
"""Function to turn on Bucket Logs for Buckets"""
for org_account in OrgAccountIdList:
for aws_region in region_list:
logging.info("Turning on Bucket Logging on in AWS Account " + org_account + " Buckets, in region " + aws_region + ".")
sts = boto3.client('sts')
RoleArn = 'arn:aws:iam::%s:role/Assisted_Log_Enabler_IAM_Role' % org_account
logging.info('Assuming Target Role %s for Assisted Log Enabler...' % RoleArn)
assisted_log_enabler_sts = sts.assume_role(
RoleArn=RoleArn,
RoleSessionName='assisted-log-enabler-activation',
DurationSeconds=3600,
)
s3_ma = boto3.client(
's3',
aws_access_key_id=assisted_log_enabler_sts['Credentials']['AccessKeyId'],
aws_secret_access_key=assisted_log_enabler_sts['Credentials']['SecretAccessKey'],
aws_session_token=assisted_log_enabler_sts['Credentials']['SessionToken'],
region_name=aws_region
)
try:
S3List: list = []
S3LogList: list = []
logging.info("ListBuckets API Call")
buckets = s3_ma.list_buckets()
for bucket in buckets['Buckets']:
s3region=s3_ma.get_bucket_location(Bucket=bucket["Name"])['LocationConstraint']
if s3region == aws_region:
S3List.append(bucket["Name"])
elif s3region is None and aws_region == 'us-east-1':
S3List.append(bucket["Name"])
if S3List != []:
logging.info("List of Buckets found within account " + org_account + ", region " + aws_region + ":")
print(S3List)
logging.info("Parsed out buckets created by Assisted Log Enabler for AWS in " + aws_region)
logging.info("Checking remaining buckets to see if logs were enabled by Assisted Log Enabler for AWS in " + aws_region)
logging.info("GetBucketLogging API Call")
for bucket in S3List:
if 'aws-log-collection-' + org_account + '-' + aws_region not in str(bucket):
s3temp=s3_ma.get_bucket_logging(Bucket=bucket)
if 'TargetBucket' not in str(s3temp):
S3LogList.append(bucket)
if S3LogList != []:
logging.info("List of Buckets found within account " + org_account + ", region " + aws_region + " WITHOUT S3 Bucket Logs:")
print(S3LogList)
for bucket in S3LogList:
logging.info(bucket + " does not have S3 BUCKET logging on. It will be turned on within this function.")
else:
logging.info("No S3 Bucket WITHOUT Logging enabled on account " + org_account + " region " + aws_region)
else:
logging.info("No S3 Buckets found within account " + org_account + ", region " + aws_region + ":")
except Exception as exception_handle:
logging.error(exception_handle)
# 8. Turn on LB Logging.
def dryrun_lb_logs(region_list, account_number, OrgAccountIdList):
"""Function to turn on Load Balancer Logs"""
for org_account in OrgAccountIdList:
for aws_region in region_list:
logging.info("Checking for Load Balancer Logging in the account " + org_account + " in region " + aws_region + ".")
sts = boto3.client('sts')
RoleArn = 'arn:aws:iam::%s:role/Assisted_Log_Enabler_IAM_Role' % org_account
logging.info('Assuming Target Role %s for Assisted Log Enabler...' % RoleArn)
assisted_log_enabler_sts = sts.assume_role(
RoleArn=RoleArn,
RoleSessionName='assisted-log-enabler-activation',
DurationSeconds=3600,
)
elbv1_ma = boto3.client(
'elb',
aws_access_key_id=assisted_log_enabler_sts['Credentials']['AccessKeyId'],
aws_secret_access_key=assisted_log_enabler_sts['Credentials']['SecretAccessKey'],
aws_session_token=assisted_log_enabler_sts['Credentials']['SessionToken'],
region_name=aws_region
)
elbv2_ma = boto3.client(
'elbv2',
aws_access_key_id=assisted_log_enabler_sts['Credentials']['AccessKeyId'],
aws_secret_access_key=assisted_log_enabler_sts['Credentials']['SecretAccessKey'],
aws_session_token=assisted_log_enabler_sts['Credentials']['SessionToken'],
region_name=aws_region
)
try:
ELBList1: list = []
ELBList2: list = []
ELBLogList: list = []
ELBv1LogList: list = []
ELBv2LogList: list = []
logging.info("DescribeLoadBalancers API Call")
ELBList1 = elbv1_ma.describe_load_balancers()
for lb in ELBList1['LoadBalancerDescriptions']:
logging.info("DescribeLoadBalancerAttibute API Call")
lblog=elbv1_ma.describe_load_balancer_attributes(LoadBalancerName=lb['LoadBalancerName'])
logging.info("Parsing out for ELB Access Logging")
if lblog['LoadBalancerAttributes']['AccessLog']['Enabled'] == False:
ELBv1LogList.append([lb['LoadBalancerName'],'classic'])
logging.info("DescribeLoadBalancers v2 API Call")
ELBList2 = elbv2_ma.describe_load_balancers()
for lb in ELBList2['LoadBalancers']:
logging.info("DescribeLoadBalancerAttibute v2 API Call")
lblog=elbv2_ma.describe_load_balancer_attributes(LoadBalancerArn=lb['LoadBalancerArn'])
logging.info("Parsing out for ELBv2 Access Logging")
for lbtemp in lblog['Attributes']:
if lbtemp['Key'] == 'access_logs.s3.enabled':
if lbtemp['Value'] == 'false':
ELBv2LogList.append([lb['LoadBalancerName'],lb['LoadBalancerArn']])
ELBLogList=ELBv1LogList+ELBv2LogList
if ELBLogList != []:
logging.info("List of Load Balancers found within account " + account_number + ", region " + aws_region + " without logging enabled:")
print(ELBLogList)
for elb in ELBLogList:
logging.info(elb[0] + " does not have Load Balancer logging on. It will be turned on within this function.")
logging.info("Creating S3 Logging Bucket for Load Balancers")
else:
logging.info("No Load Balancers WITHOUT logging found within account " + account_number + ", region " + aws_region + ":")
except Exception as exception_handle:
logging.error(exception_handle)
def lambda_handler(event, context):
"""Function that runs all of the previously defined functions"""
account_number = get_account_number()
OrgAccountIdList, organization_id = org_account_grab()
dryrun_flow_log_activator(account_number, OrgAccountIdList, region_list)
dryrun_eks_logging(region_list, OrgAccountIdList)
dryrun_route_53_query_logs(region_list, account_number, OrgAccountIdList)
dryrun_s3_logs(region_list, account_number, OrgAccountIdList)
dryrun_lb_logs(region_list, account_number, OrgAccountIdList)
logging.info("This is the end of the script. Please check the logs for the resources that would be turned on outside of the Dry Run option.")
if __name__ == '__main__':
event = "event"
context = "context"
lambda_handler(event, context)