-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws-s3-user-access.py
499 lines (457 loc) · 29.8 KB
/
aws-s3-user-access.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
'''
Enumerates the Users and their S3 bucket privileges through the following way:
UserName: user1
User Inline Policy: inline_policy_1
S3 Bucket: arn:aws:s3:::s3bucket-1
Action: s3:*
User Inline Policy: managed_policy_1
S3 Bucket: arn:aws:s3:::s3bucket_2
Action: s3:*
Group Name: group_1
Inline Policy: group_inline_policy_1
S3 Bucket: arn:aws:s3:::s3bucket_3
Action: s3:*
Group Name: group_2
Managed Policy: group_managed_policy_1
S3 Bucket: arn:aws:s3:::s3bucket_4
Action: s3:*
Author: aarvee
Notice: Please note that this script is in Beta Stage and any bug fixes or issues can be notofied to me via github!
'''
import boto3
import json
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
iam = boto3.client('iam')
iamresource = boto3.resource('iam')
roles = iam.list_roles()
users = iam.list_users()
s3 = boto3.resource('s3')
s3client = boto3.client('s3')
list_of_buckets = s3client.list_buckets()
print(color.CYAN + color.BOLD + '----------------------------------------------------')
print('No. of S3 Buckets: {}'.format(len(list_of_buckets["Buckets"])))
print('List given below:')
print('----------------------------------------------------' + color.END)
for bucket in list_of_buckets["Buckets"]:
# NOTE: LISTING OF FILES IN THE BUCKETS HAS BEEN COMMENTED OUT AS IT TAKES A LONG TIME IF YOU HAVE LARGE NUMBER OF FILES IN BUCKETS
# Create a paginator to pull 1000 objects at a time
#paginator = s3client.get_paginator('list_objects')
#pageresponse = paginator.paginate(Bucket=bucket["Name"])
#s3objects_list = []
#try:
# PageResponse Holds 1000 objects at a time and will continue to repeat in chunks of 1000.
# for pageobject in pageresponse:
# for file in pageobject["Contents"]:
# s3objects_list.append(file["Key"])
#except KeyError:
# print 'EMPTY BUCKET'
# pass
# Final Result
print(color.CYAN + 'Bucket Name: {}'.format(bucket['Name']) + color.END)
# Enumerate all the roles
Role_list = roles['Roles']
role_arn_list = []
for role in Role_list:
role_arn_list.append(role['Arn'])
# Enumerate all the users and their policies and groups
for key in users['Users']:
user_name = key['UserName']
print(color.CYAN + '=' * 112 + color.END)
print(color.BOLD + color.RED + 'UserName: {}'.format(user_name) + color.END)
Inline_user_policies = iam.list_user_policies(UserName=user_name)
Managed_user_policies = iam.list_attached_user_policies(UserName=user_name)
List_of_Groups = iam.list_groups_for_user(UserName=key['UserName'])
managed_policy_list = [] # All Managed policies
final_managed_policy_list = [] # All Managed policies with S3 Bucket action
managed_policy_action_list = [] # All Managed Policy S3 Action list
managed_policy_resource_list = [] # All Managed Policy S3 Buckets
inline_policy_list = [] # All Inline policies
final_inline_policy_list = [] # All Inline policies with S3 Bucket action
inline_policy_action_list = [] # All Inline Policy S3 Action list
inline_policy_resource_list = [] # All Inline Policy S3 Buckets
group_inline_policy_list = [] # All Groups Inline policies with S3 Bucket action
group_inline_policy_action_list = [] # All Groups Inline Policy S3 Action list
group_inline_policy_resource_list = [] # All Groups Inline Policy S3 Buckets
group_managed_policy_list = [] # All Groups Managed policies with S3 Bucket action
group_managed_policy_action_list = [] # All Groups Managed Policy S3 Action list
group_managed_policy_resource_list = [] # All Groups Managed Policy S3 Buckets
# Read all the Inline policies attached to the user
for inline_policy_name in Inline_user_policies['PolicyNames']:
inline_policy_list.append(inline_policy_name)
for policy_name in inline_policy_list:
description = iam.get_user_policy(
UserName=user_name,
PolicyName=policy_name
)
# Convert the output into JSON
desc_json = json.dumps(description)
final_desc = json.loads(desc_json)
# Load Statement in the Policy Document into a separate JSON
desc_stmt = json.dumps(final_desc['PolicyDocument']['Statement'])
final_stmt = json.loads(desc_stmt)
# Iterate through all the Actions in the Statement JSON as there could be multiple Actions
for statement in final_stmt:
action_raw = json.dumps(statement['Action'])
action_json = json.loads(action_raw)
resource_raw = json.dumps(statement['Resource'])
resource_json = json.loads(resource_raw)
flag = '0'
if type(resource_json) is list:
for s3bucket in resource_json:
if 's3' in s3bucket:
if flag == '0':
print(color.YELLOW + '\tUser Inline Policy: {}'.format(inline_policy_name) + color.END)
flag = '1'
print(color.BLUE + '\t\tS3 Bucket: {}'.format(s3bucket) + color.END)
if type(action_json) is list:
for action_print in action_json:
print(color.PURPLE + '\t\t\tAction: {}'.format(action_print) + color.END)
else:
print(color.PURPLE + '\t\t\tAction: {}'.format(action_json) + color.END)
else:
if 's3' in resource_json:
if flag == '0':
print(color.YELLOW + '\tUser Inline Policy: {}'.format(inline_policy_name) + color.END)
flag = '1'
print(color.BLUE + '\t\tS3 Bucket: {}'.format(resource_json) + color.END)
if type(action_json) is list:
for action_print in action_json:
print(color.PURPLE + '\t\t\tAction: {}'.format(action_print) + color.END)
else:
print(color.PURPLE + '\t\t\tAction: {}'.format(action_json) + color.END)
# Read all the Managed Policies of every user
for managed_policy in Managed_user_policies['AttachedPolicies']:
policy_arn = managed_policy['PolicyArn']
managed_policy_list.append(managed_policy['PolicyName'])
for policy_name in managed_policy_list:
description = iam.get_policy(
PolicyArn=policy_arn
)
policy_policy = description['Policy']
policy_version = policy_policy['DefaultVersionId']
document = iam.get_policy_version(
PolicyArn=policy_arn,
VersionId=policy_version
)
document_policy_version = document['PolicyVersion']
document_policy_doc = document_policy_version['Document']
document_policy_statement = document_policy_doc['Statement']
policy_doc_dump = json.dumps(document_policy_statement)
policy_doc_json = json.loads(policy_doc_dump)
flag = '0'
if type(policy_doc_json) is list:
for policy in policy_doc_json:
policy_dump = json.dumps(policy)
policy_json = json.loads(policy_dump)
flag = '0'
if type(policy_json['Resource']) is list:
for resource in policy_json['Resource']:
flag2 = '0'
if ('s3' in resource) or ('*' == resource):
try:
if type(policy_json['Action']) is list:
for action_print in policy_json['Action']:
if ('s3' in action_print) or ('*' == action_print):
if flag == '0':
print(color.YELLOW + '\tUser Managed Policy: {}'.format(managed_policy['PolicyName']) + color.END)
flag = '1'
if flag2 == '0':
print(color.BLUE + '\t\tS3 Bucket: {}'.format(resource) + color.END)
flag2 = '1'
print(color.PURPLE + '\t\t\tAction: {}'.format(action_print) + color.END)
else:
if ('s3' in policy_json['Action']) or ('*' == policy_json['Action']):
if flag == '0':
print(color.YELLOW + '\tUser Managed Policy: {}'.format(managed_policy['PolicyName']) + color.END)
flag = '1'
if flag2 == '0':
print(color.BLUE + '\t\tS3 Bucket: {}'.format(resource) + color.END)
flag2 = '1'
print(color.PURPLE + '\t\t\tAction: {}'.format(policy_json['Action']) + color.END)
except KeyError:
pass
else:
resource = policy_json['Resource']
flag2 = '0'
if ('s3' in resource) or ('*' == resource):
try:
if type(policy_json['Action']) is list:
for action_print in policy_json['Action']:
if ('s3' in action_print) or ('*' == action_print):
if flag == '0':
print(color.YELLOW + '\tUser Managed Policy: {}'.format(managed_policy['PolicyName']) + color.END)
flag = '1'
if flag2 == '0':
print(color.BLUE + '\t\tS3 Bucket: {}'.format(resource) + color.END)
flag2 = '1'
print(color.PURPLE + '\t\t\tAction: {}'.format(action_print) + color.END)
else:
action_print = policy_json['Action']
if ('s3' in action_print) or ('*' == action_print):
if flag == '0':
print(color.YELLOW + '\tUser Managed Policy: {}'.format(managed_policy['PolicyName']) + color.END)
flag = '1'
if flag2 == '0':
print(color.BLUE + '\t\tS3 Bucket: {}'.format(resource) + color.END)
flag2 = '1'
print(color.PURPLE + '\t\t\tAction: {}'.format(action_print) + color.END)
except KeyError:
pass
# Find the Users in the Groups that have S3 Access
for group_dump in List_of_Groups['Groups']:
group = iamresource.Group(group_dump['GroupName'])
# A Group can have inline policies and managed policies attached to it
inline_group_policy = list(group.policies.all())
managed_group_policy = list(group.attached_policies.all())
flag_group = '0'
# Start the function if a User is a part of a group that has Inline Policies
if inline_group_policy:
flag3 = '0'
flag_inline_policy = '0'
for policy in inline_group_policy:
group_policy_doc_raw = policy.policy_document
group_policy_doc_raw_json = json.dumps(group_policy_doc_raw)
group_policy_doc_json = json.loads(group_policy_doc_raw_json)
statement_raw = group_policy_doc_json['Statement']
statement_dump = json.dumps(statement_raw)
statement_json = json.loads(statement_dump)
flag4 = '0'
if type(statement_json) is list:
for statement in statement_json:
if type(statement['Resource']) is list:
for resource in statement['Resource']:
if ('s3' in resource) or ('*' == resource):
if type(statement['Action']) is list:
for action_print in statement['Action']:
if ('s3' in action_print) or ('*' == action_print):
if flag3 == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag3 = '1'
if flag_inline_policy == '0':
print(color.YELLOW + '\t\tInline Policy: {}'.format(arn) + color.END)
flag_inline_policy = '1'
if flag4 == '0':
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(resource) + color.END)
flag4 = '1'
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
action_print = statement['Action']
if ('s3' in action_print) or ('*' == action_print):
if flag3 == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag3 = '1'
if flag_inline_policy == '0':
print(color.YELLOW + '\t\tInline Policy: {}'.format(arn) + color.END)
flag_inline_policy = '1'
if flag4 == '0':
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(resource) + color.END)
flag4 = '1'
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
resource = statement['Resource']
if ('s3' in resource) or ('*' == resource):
if type(statement['Action']) is list:
for action_print in statement['Action']:
if ('s3' in action_print) or ('*' == action_print):
if flag3 == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag3 = '1'
if flag_inline_policy == '0':
print(color.YELLOW + '\t\tInline Policy: {}'.format(arn) + color.END)
flag_inline_policy = '1'
if flag4 == '0':
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(resource) + color.END)
flag4 = '1'
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
action_print = statement['Action']
if ('s3' in action_print) or ('*' == action_print):
if flag3 == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag3 = '1'
if flag_inline_policy == '0':
print(color.YELLOW + '\t\tInline Policy: {}'.format(arn) + color.END)
flag_inline_policy = '1'
if flag4 == '0':
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(resource) + color.END)
flag4 = '1'
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
statement = statement_json
if type(statement['Resource']) is list:
for resource in statement['Resource']:
if ('s3' in resource) or ('*' == resource):
if type(statement['Action']) is list:
for action_print in statement['Action']:
if ('s3' in action_print) or ('*' == action_print):
if flag3 == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag3 = '1'
if flag_inline_policy == '0':
print(color.YELLOW + '\t\tInline Policy: {}'.format(arn) + color.END)
flag_inline_policy = '1'
if flag4 == '0':
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(resource) + color.END)
flag4 = '1'
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
action_print = statement['Action']
if ('s3' in action_print) or ('*' == action_print):
if flag3 == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag3 = '1'
if flag_inline_policy == '0':
print(color.YELLOW + '\t\tInline Policy: {}'.format(arn) + color.END)
flag_inline_policy = '1'
if flag4 == '0':
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(resource) + color.END)
flag4 = '1'
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
resource = statement['Resource']
if ('s3' in resource) or ('*' == resource):
if type(statement['Action']) is list:
for action_print in statement['Action']:
if ('s3' in action_print) or ('*' == action_print):
if flag3 == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag3 = '1'
if flag_inline_policy == '0':
print(color.YELLOW + '\t\tInline Policy: {}'.format(arn) + color.END)
flag_inline_policy = '1'
if flag4 == '0':
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(resource) + color.END)
flag4 = '1'
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
action_print = statement['Action']
if ('s3' in action_print) or ('*' == action_print):
if flag3 == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag3 = '1'
if flag_inline_policy == '0':
print(color.YELLOW + '\t\tInline Policy: {}'.format(arn) + color.END)
flag_inline_policy = '1'
if flag4 == '0':
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(resource) + color.END)
flag4 = '1'
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
# Start the function if a User is a part of a group that has Managed Policies
if managed_group_policy:
for policy in managed_group_policy:
policy_string = str(policy)
remove, result1 = policy_string.split("iam.Policy(arn='")
arn, remove = result1.split("')")
description = iam.get_policy(
PolicyArn=arn
)
policy_policy = description['Policy']
policy_version = policy_policy['DefaultVersionId']
document = iam.get_policy_version(
PolicyArn=arn,
VersionId=policy_version
)
document_policy_version = document['PolicyVersion']
document_policy_doc = document_policy_version['Document']
document_policy_statement = document_policy_doc['Statement']
policy_doc = json.dumps(document_policy_statement)
policy_doc_json = json.loads(policy_doc)
flag_managed_policy = '0'
for policy in policy_doc_json:
policy_dump = json.dumps(policy)
policy_json = json.loads(policy_dump)
try:
flag5 = '0'
if policy_json['Resource'] is dict:
for resource in policy_json['Resource']:
if type(policy_json['Action']) is list:
for action_print in policy_json['Action']:
if ('s3' in action_print) or ('*' == action_print):
if flag_group == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag_group = '1'
if flag_managed_policy == '0':
print(color.YELLOW + '\t\tManaged Policy: {}'.format(arn) + color.END)
flag_managed_policy = '1'
if flag5 == '0':
if type(resource) is list:
for bucket_name in resource:
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(bucket_name) + color.END)
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
bucket_name = resource
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(bucket_name) + color.END)
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
flag5 = '1'
else:
action_print = policy_json['Action']
if ('s3' in action_print) or ('*' == action_print):
if flag_group == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag_group = '1'
if flag_managed_policy == '0':
print(color.YELLOW + '\t\tManaged Policy: {}'.format(arn) + color.END)
flag_managed_policy = '1'
if flag5 == '0':
if type(resource) is list:
for bucket_name in resource:
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(bucket_name) + color.END)
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
bucket_name = resource
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(bucket_name) + color.END)
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
flag5 = '1'
else:
resource = policy_json['Resource']
if type(policy_json['Action']) is list:
for action_print in policy_json['Action']:
if ('s3' in action_print) or ('*' == action_print):
if flag_group == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag_group = '1'
if flag_managed_policy == '0':
print(color.YELLOW + '\t\tManaged Policy: {}'.format(arn) + color.END)
flag_managed_policy = '1'
if flag5 == '0':
if type(resource) is list:
for bucket_name in resource:
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(bucket_name) + color.END)
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
bucket_name = resource
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(bucket_name) + color.END)
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
flag5 = '1'
else:
action_print = policy_json['Action']
if ('s3' in action_print) or ('*' == action_print):
if flag_group == '0':
print(color.DARKCYAN + '\tGroup Name: {}'.format(group_dump['GroupName']) + color.END)
flag_group = '1'
if flag_managed_policy == '0':
print(color.YELLOW + '\t\tManaged Policy: {}'.format(arn) + color.END)
flag_managed_policy = '1'
if flag5 == '0':
if type(resource) is list:
for bucket_name in resource:
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(bucket_name) + color.END)
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
else:
bucket_name = resource
print(color.BLUE + '\t\t\tS3 Bucket: {}'.format(bucket_name) + color.END)
print(color.PURPLE + '\t\t\t\tAction: {}'.format(action_print) + color.END)
flag5 = '1'
except KeyError:
print(KeyError)
pass