跳至內容

Need IAM policies to block specific ports and IP ranges at user and resource level

0

Need IAM policies to block specific ports and IP ranges at user and resource level with code.

已提問 3 年前檢視次數 996 次

2 個答案
0

Hi, there exist specific conditions in IAM policies to bock ranges of IP addresses. See https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_aws_deny-ip.html

Example from the page:

{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Deny",
        "Action": "*",
        "Resource": "*",
        "Condition": {
            "NotIpAddress": {
                "aws:SourceIp": [
                    "192.0.2.0/24",
                    "203.0.113.0/24"
                ]
            },
            "Bool": {"aws:ViaAWSService": "false"}
        }
    }
}

In the example, the blocker is broads with Deny for Action:* and Resource:*. You can tune that to your own use case with more specific actions and resources, depending on what you want to block exactly.

AFAIK, ports can be blocked via security groups but not by IAM policies

專家

已回答 3 年前

專家

已審閱 3 年前

專家

已審閱 3 年前

  • You can’t block with security groups. You can only allow.

  • Agree with Gary . To block port, network acl is needed.

  • @Didier As Gary mentioned, in AWS, you don't explicitly "block" ports using security groups. Instead, you only "allow" specific traffic. By default, a security group denies all inbound traffic and allows all outbound traffic unless rules are added to allow specific types of inbound traffic. I would suggest you update your answer so we don't confuse other users.

  • what does this line mean? "Bool": {"aws:ViaAWSService": "false"}

  • and above policy is for blocking specific IP addresses or allow?

0

You can block HTTP (port 80) requests to services. Example:

{
  "Id": "ExamplePolicy",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowSSLRequestsOnly",
      "Action": "s3:*",
      "Effect": "Deny",
      "Resource": [
        "arn:aws:s3:::DOC-EXAMPLE-BUCKET",
        "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*"
      ],
      "Condition": {
        "Bool": {
          "aws:SecureTransport": "false"
        }
      },
      "Principal": "*"
    }
  ]
}
AWS
專家

已回答 3 年前

  • Thanks for the answer but if I want to block port for EC2 instance will it be possible using IAM policy?

  • No. You must use Security Groups (allow only), Network ACLS, or something like a Network Firewall. If you are blocking at the HTTP/HTTPS level, you could use AWS WAF if your front the EC2 Instances with an ALB. AWS IAM is about protecting AWS APIs (authentication and authorization). All of the policy examples above are protecting these APIs, not instances. There is some gray area where you can use IAM policies to control access to Session Manager and the ability to login to an instance, but IAM policies are not network firewall rules.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。