Skip to main contentSkip to main content
Room Banner
Room Icon

IAM Permissions

Learn how authorization is handled in AWS IAM.

medium

30 min

416

User profile photo.
User profile photo.

To access material, start machines and answer questions login.

An Principal can have multiple policies attached to it. These policies determine what the principal is authorized to do in your account.

Each policy consists of one or more statements that consist of:

  1. An optional statement Identifier (Sid (opens in new tab))
  2. Action - a list of things the policy allows or denies
  3. Resources - the ARNs of resources the statement applies to
  4. Effect - either Allow or Deny
  5. Condition - optional conditions that must be satisfied for the policy to grant permission

provides many Managed Policies (opens in new tab) that contain a list of actions that usually map to a specific job function. However, customers can also create custom policies that are more fine-grained. For example, you can craft an policy that only allows someone to read from one specific Bucket or to start or stop, but not terminate, a specific instance.

Customer policies can be either Customer Managed Policies (opens in new tab) or Inline Policies (opens in new tab) directly attached to a specific principal and cannot be shared. Inline policies are best to ensure a direct relationship between a principal and the policies granted. In contrast, managed policies can reuse the policy across multiple principals.

The most powerful managed policy is the AdministratorAccess policy shown below.

           Patton:~ chris$ aws iam get-policy --policy-arn arn:aws:iam::aws:policy/AdministratorAccess
{
    "Policy": {
        "PolicyName": "AdministratorAccess",
        "PolicyId": "ANPAIWMBCKSKIEE64ZLYK",
        "Arn": "arn:aws:iam::aws:policy/AdministratorAccess",
        "Path": "/",
        "DefaultVersionId": "v1",
        "AttachmentCount": 3,
        "PermissionsBoundaryUsageCount": 0,
        "IsAttachable": true,
        "Description": "Provides full access to AWS services and resources.",
        "CreateDate": "2015-02-06T18:39:46+00:00",
        "UpdateDate": "2015-02-06T18:39:46+00:00",
        "Tags": []
    }
}
Patton:~ chris$ aws iam get-policy-version --policy-arn arn:aws:iam::aws:policy/AdministratorAccess --version-id v1
{
    "PolicyVersion": {
        "Document": {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Action": "*",
                    "Resource": "*"
                }
            ]
        },
        "VersionId": "v1",
        "IsDefaultVersion": true,
        "CreateDate": "2015-02-06T18:39:46+00:00"
    }
}

        

Looking at the policy statement, we can see it allows all actions on all resources.


"Statement": [
    {
        "Effect": "Allow",
        "Action": "*",
        "Resource": "*"
    }
]

In the remaining tasks in this room, we will go through all the different elements of an Statement in more depth.

There is another managed policy called ReadOnlyAccess. Using the commands above, go look at the permissions granted to the ReadOnlyAccess user. At the time of this writing, the VersionId was v83. Use the (as shown above) to answer the following question:

Answer the questions below
What is the PolicyId of the ReadOnlyAccess managed policy?

Select the Cloud details button at the top of the room:

Where needed generate the environment required for the room. The "Generate Environment" button will appear if the room contains an environment that needs to be generated. 

For any issues with the environment, select the "Reset Environment" button. Review this article for more information.

To view the credentials required for the environment, select the credentials tab. You can use these credentials to access the environment in various ways. More information can be found here:

Answer the questions below
Generate environment or set up your credentials

Actions consist of a service and an call. Some examples are:
  • :StopInstance
  • :GetObject
  • sts:AssumeRole
  • :ListUsers
In the above examples, “”, “”, “sts”, and “” are the services, while StopInstance, GetObject, AssumeRole, etc., are the calls. Actions are case insensitive however service names are almost always written in the lower case, and calls match the camel case in documentation.
Actions support wildcards, so to permit all the actions for the service, you can use an action of “:*”, or to scope to read-only actions, you can use “:Get*”
does not enforce consistency across services for calls. Some services use Describe* while others use Get* or List*.
The complete list of actions can be found here: https://docs..amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html (opens in new tab). If you write policies on a regular basis, you will probably want to bookmark this link.
Answer the questions below

What action "Grants permission to create access key and secret access key for the specified IAM user"? You can find a hint in the Service Authorization docs for IAM (opens in new tab)

A Resource (opens in new tab) is always in an ARN format (opens in new tab) or a wildcard ("*") to represent any resource. 

Resources in policies are essential to achieving the principle of least privilege (opens in new tab). Suppose there are multiple applications or teams in a single account. In that case, least privilege will dictate that application A cannot access the data belonging to application B unless there is a business need. By crafting a policy that only permits application A to access buckets belonging to application A, least privilege is possible.

Recall that ARNs include the Region, Account ID, and resource name as part of the ARN. ARNs that are global (like ) have an empty value for the region.

Because cannot identify what resources a customer may have, all Managed Policies the wildcard ("*") as the resource value.

In the case of the ARN for an Managed Policy, since these are managed by and not owned by anyone's account, the Account ID field is an empty value. 
Answer the questions below
In your account, there is a bucket that begins with "tryhackme-bucket-" and ends with your unique account ID. What is the ARN of that bucket excluding the last 12 digit account ID?

Effect can be either Allow or Deny. With , there is a default implicit deny. All policy statements that match the request's Action, Resource, and Condition are evaluated. If there is any explicit deny, then the action is denied. Otherwise, if there is an explicit allow, the action is allowed. Otherwise, the final decision is to deny the request.

For the following question, look at this policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "ec2:Describe*",
                "secretsmanager:ListSecrets",
                "secretsmanager:GetSecretValue"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": ["*"],
            "Resource": "arn:aws:secretsmanager:us-east-1:123456789012:secret:HR-Passwords*",
            "Effect": "Deny"
        }
    ]
}
Answer the questions below

Given the policy above, can this user get the HR Password (Y/N)?

Only required for resource policies, not identity-based policies, the Principal Element (opens in new tab) specifies the Principal that is allowed or denied access to a resource. If the principal is "*", the policy applies to anyone or all customers (depending on the Service). We’ll cover resource policies in the next room.

The following resource policy grants public access to read a bucket, and only allows user snape the ability to write to the bucket:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AlowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": [ "*" ]
            },
            "Action": [
                "s3:GetObject",
            ],
            "Resource": "arn:aws:s3:::dark-arts-class-notes/*"
        },
        {
            "Sid": "AlowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::111122223333:user/snape"
                ]
            },
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::dark-arts-class-notes/*"
        }     
    ]
}

You can specify any of the following principals in a policy:

  • account and root user
  • roles
  • Role sessions
  • users
  • Federated user sessions
  • services
  • All principals
Answer the questions below
Look in your account. What is the Principal that is allowed to assume the OrganizationAccountAccessRole (opens in new tab) role?

Conditions are optional elements that users can add to a policy. They evaluate against specific keys and values in the context of the request.

The context of the request is specific to the action and resource type. For example, the condition keys available for can be found in the Actions Table (opens in new tab) (scroll to the right on the inner table if you don’t see the condition keys column).

All conditions must be met for the condition block to evaluate true. However, if multiple values for a condition key are provided, the presence of any of those values allows the condition to return true.

Condition Block AND OR Logic

Condition statements have special operators (opens in new tab) that are used to evaluate the conditions. Examples are StringEquals, StringNotEquals, StringNotLike, StringLikeIfExists, etc.

There are a number of global condition context keys (opens in new tab) that you can add to your statements based on the attributes of the call. One of the most common ones is :SourceIp - allowing or denying an action based on the IP address it's coming from.

Another popular context key is :ResourceTag/tag-key. This condition key can be used to grant or deny access based on the specific tags of the resource. If the statement were:


{
    "Sid": "ManageHouseServer",
    "Effect": "Allow",
    "Action": "ec2:TerminateInstance",
    "Resource": "arn:aws:ec2::123456789012:instance/*",
    "Condition": {
        "StringEquals": {
            "aws:ResourceTag/hogwarts-house": "Hufflepuff"
        }
}
The principal with this role would only be granted access to the Hufflepuff Machines. If the machine were tagged "hogwarts-house": "Gryffindor", the allow effect would not apply.

This use of :ResourceTag is a fundamental building block of Attribute-Based Access Control (or ABAC).

In the next room, we'll see a global context key, :PrincipalOrgID, used to ensure only members of the TryHackMe community are able to invoke a Lambda function.

Given this Statement:


{
  "Effect": "Deny",
  "Principal": "*",
  "Action": "s3:PutObject",
  "Resource": "arn:aws:s3:::my-logs-bucket/AWSLogs/AccountNumber/*",
  "Condition": {
    "StringNotEquals": {
      "aws:SourceVpc": "vpc-abcdef2",
      "aws:PrincipalServiceName": "glue.amazonaws.com"
    }
  }
}
Answer the questions below
The Glue Service, running in vpc-12345, can write an object to the my-logs-bucket? (T/F)

has the concept of NotAction (opens in new tab), NotResource (opens in new tab), and NotPrincipal (opens in new tab). These can be used in more complex cases, but be careful. Doing an allow on all principals except one would allow all customers access to the resource.

You can use NotAction to grant broad access to an account while reserving sensitive actions for special administrators. This is one way to allow someone to administer an account but not modify the network.

NotPrincipal is useful for explicitly denying access to a resource except for a small number of users. For example, this policy will explicitly deny access to the dynamo table ForbiddenForest to all users of account 012345678901 except Hagrid:

{
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Deny",
        "NotPrincipal": {"AWS": [
            "arn:aws:iam::012345678901:user/Hagrid"
        ]},
        "Action": "dynamodb:PutItem",
        "Resource": [
            "arn:aws:dynamodb:eu-west-2:012345678901:table/ForbiddenForest"
        ]
    }]
}

Another way of protecting access to something would be via NotResource. The same policy prevents most users in 012345678901 from accessing the ForbiddenForest table.
{
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Principal": {"AWS": [
            "arn:aws:iam::012345678901:user/*"
        ]},
        "Action": "dynamodb:PutItem",
        "NotResource": [
            "arn:aws:dynamodb:eu-west-2:012345678901:table/ForbiddenForest"
        ]
    }]
}
Now, all the students at Hogwarts have access to put to any DynamoDB table, except the ForbiddenForest table.
Answer the questions below
As headmaster, Dumbledore's IAM policy is:
{"Effect": "Allow", "Action": "*", "Resource": "*"}
Would he have access to the ForbiddenForest if the second policy were in place? (Y/N)