Certopolis

AWS Certified CloudOps Engineer – Associate

Security groups vs network ACLs: stateful vs stateless

The Amazon VPC User Guide says a security group is stateful and attached to a resource: replies to allowed traffic pass automatically, and it has allow rules only. A network ACL is stateless and attached to a subnet: it has numbered allow and deny rules, evaluated from the lowest number, and replies need their own rule.

How does a security group behave?

A security group controls traffic to and from the resources it is associated with, such as an EC2 instance. You can write allow rules but not deny rules. A new security group has no inbound rules, so nothing gets in until you add one, and it has one outbound rule that allows all outbound traffic.

Security groups are stateful. Replies to allowed inbound traffic may leave regardless of the outbound rules, and replies to a request the instance sent may come back regardless of the inbound rules.

A rule's source or destination can be a single address, a CIDR range, a prefix list or another security group. When several security groups are attached to a resource, their rules are combined into one set.

How does a network ACL behave?

A network ACL allows or denies traffic at the subnet level. Every subnet is associated with exactly one network ACL, and one ACL can serve several subnets. Rules are evaluated when traffic enters or leaves the subnet, not when it moves inside the subnet.

Each rule has a number from 1 to 32766. Evaluation starts at the lowest number and stops at the first match, whatever a higher-numbered rule says. To carve an exception out of a deny, give the allow rule a lower number than the deny. AWS suggests numbering in steps of 10 or 100 to leave room.

Network ACLs are stateless. Allowing inbound traffic does not allow the reply. For every rule you add, there must be a rule in the other direction that allows the response traffic.

What are the defaults, and which ephemeral ports matter?

The default network ACL that comes with a VPC allows all traffic in and out. A custom network ACL you create has only the rule numbered with an asterisk in each direction, which denies any packet that matched nothing else, so it blocks everything until you add rules. The asterisk rule cannot be deleted.

Replies go to the ephemeral port the client chose, and the range depends on the client. Many Linux kernels use 32768-61000. Windows Server 2008 and later use 49152-65535. Elastic Load Balancing, NAT gateways and Lambda functions use 1024-65535.

If an instance in the subnet is the client, the ACL needs an inbound rule for that instance's ephemeral range. If the instance is the server, the ACL needs an outbound rule for the clients' range. In VPC flow logs, an ACCEPT record for a request followed by a REJECT record for its reply points at a network ACL, because a security group would have allowed the reply.

Where the exam catches people

  • Associating a new custom network ACL and expecting traffic to pass: it denies everything until you add allow rules in both directions.
  • Adding a more specific allow with a higher rule number than a deny: the lowest-numbered matching rule wins.
  • Opening only the service port on a network ACL: the reply uses an ephemeral port and needs its own rule in the other direction.
  • Looking for a deny rule in a security group: security groups support allow rules only.
  • Believing a security group with no outbound rules blocks replies to allowed inbound requests: it is stateful, so replies leave anyway.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A SysOps administrator creates a new network ACL in a VPC and associates it with a subnet that hosts web servers, in place of the default network ACL. The administrator has not added any rules to the new network ACL. Right away, the web servers stop answering requests from the internet and cannot reach the internet either. What is a reason for this issue?

    Right. In the game that’s Renown for your Networking and Content Delivery building.

    Not that one: A is right. In the game your Networking and Content Delivery building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Adding allow rules for the web traffic, and for the return traffic in each direction, restores service.

    Written from docs.aws.amazon.com/vpc/latest/userguide/create-network-acl.html

  2. A database subnet's network ACL has these inbound rules: 100 DENY TCP 5432 from 10.1.0.0/16 200 ALLOW TCP 5432 from 10.0.0.0/8 * DENY all traffic Its outbound rules allow all traffic, and the databases' security group allows TCP 5432 from 10.0.0.0/8. One reporting server at 10.1.4.25 must now reach the databases. The rest of 10.1.0.0/16 must stay blocked. Which action should the SysOps administrator take to meet these requirements?

    Right. In the game that’s Renown for your Networking and Content Delivery building.

    Not that one: D is right. In the game your Networking and Content Delivery building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Giving the /32 allow the number 90 puts it ahead of rule 100: the reporting server matches it and is let in, while every other address in 10.1.0.0/16 still falls through to rule 100 and is denied. The outbound rules already let the replies leave.

    Written from docs.aws.amazon.com/vpc/latest/userguide/nacl-rules.html

  3. A SysOps administrator is hardening a subnet that hosts an internal API on Amazon EC2 instances. Clients connect on TCP port 443. The administrator wants each filtering layer to hold as few outbound rules as possible, and must know which layer will pass replies to admitted requests even when that layer has no outbound rule that matches them. Which of the following is correct?

    Right. In the game that’s Renown for your Networking and Content Delivery building.

    Not that one: B is right. In the game your Networking and Content Delivery building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The subnet's network ACL keeps no such memory: each reply packet is checked against its outbound rules, so the administrator still needs an outbound allow for the clients' ephemeral ports there.

    Written from docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html

Ask for something