Analysing policies attached to our user

We are going to start out by gathering the inline policy documents attached to our user. Luckily for us, the entire document for any inline policies is included with our user. We will add the following code to our script:

# Create an empty list that will hold all the policies related to our user
my_policies = []

# Check if any inline policies are attached to my user
if current_user.get('UserPolicyList'):
# Iterate through the inline policies to pull their documents
for policy in current_user['UserPolicyList']:
# Add the policy to our list
my_policies.append(policy['PolicyDocument'])

Now my_policies should include all the inline policies that are directly attached to our user. Next, we will gather the managed policy documents that are attached to our user. The policy documents are not directly attached to our user, so we must use the identifying information to find the policy document in our policy_details variable:

# Check if any managed policies are attached to my user
if current_user.get('AttachedManagedPolicies'):
# Iterate through the list of managed policies
for managed_policy in user['AttachedManagedPolicies']:
# Note the policy ARN so we can find it in our other variable
policy_arn = managed_policy['PolicyArn']

# Iterate through the policies stored in policy_details to find this policy
for policy_detail in policy_details:
# Check if we found the policy yet
if policy_detail['Arn'] == policy_arn:
# Determine the default policy version, so we know which version to grab
default_version = policy_detail['DefaultVersionId']

# Iterate the available policy versions to find the one we want
for version in policy_detail['PolicyVersionList']:
# Check if we found the default version yet
if version['VersionId'] == default_version:
# Add this policy document to our original variable
my_policies.append(version['Document'])

# We found the document, so exit this loop
break
# We found the policy, so exit this loop
break

Now my_policies should include all the inline policies and managed policies that are directly attached to our user. Next, we will figure out what groups we are a part of, then enumerate the inline policies and managed policies that are attached to each of those groups. When that is complete, we will have a complete list of the permissions that are assigned to our user:

# Check if we are in any groups
if current_user.get('GroupList'):
# Iterate through the list of groups
for user_group in current_user['GroupList']:
# Iterate through all groups to find this one
for group in group_details:
# Check if we found this group yet
if group['GroupName'] == user_group:
# Check for any inline policies on this group
if group.get('GroupPolicyList'):
# Iterate through each inline policy
for inline_policy in group['GroupPolicyList']:
# Add the policy document to our original variable
my_policies.append(inline_policy['PolicyDocument'])

# Check for any managed policies on this group
if group.get('AttachedManagedPolicies'):
# Iterate through each managed policy detail
for managed_policy in group['AttachedManagedPolicies']:
# Grab the policy ARN
policy_arn = managed_policy['PolicyArn']

# Find the policy in our list of policies
for policy in policy_details:
# Check and see if we found it yet
if policy['Arn'] == policy_arn:
# Get the default version
default_version = policy['DefaultVersionId']

# Find the document for the default version
for version in policy['PolicyVersionList']:
# Check and see if we found it yet
if version['VersionId'] == default_version:
# Add the document to our original variable
my_policies.append(version['Document'])

# Found the version, so break out of this loop
break
# Found the policy, so break out of this loop
break

Now the script should be complete and our my_policies variable should have the policy documents for all inline and managed policies that are directly attached to our user, as well as all inline and managed policies attached to each group that our user is a member of. We can check these results out by adding one final snippet that outputs the data to a local file:

with open('./my-user-permissions.json', 'w+') as f:
json.dump(my_policies, f, indent=4, default=str)

We can run the file with the same command:

   python3 get_account_details.py

Then we can check the generated my-user-permissions.json, which should contain the list of all policies and permissions that apply to your user. It should look something like the following screenshot:

Now we have a nice list of what permissions we have, what resources we can use those permissions on, and under what conditions we can apply those permissions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.181.52