All guides

Security groups and network ACLs: what each one filters

A security group is stateful and holds allow rules only. A network ACL is stateless, works at the subnet level and can also deny. AWS evaluates the rules of the two in different ways.

Hands typing on a laptop on a dark table

The differences in one list

AWS compares the two on five points:

  • Level of operation. A security group works at the instance level. A network ACL works at the subnet level.
  • Scope. A security group applies to all instances associated with it. A network ACL applies to all instances in its associated subnets.
  • Rule type. A security group has allow rules only. A network ACL has allow rules and deny rules.
  • Rule evaluation. A security group evaluates all rules before it decides whether to allow traffic. A network ACL evaluates rules in ascending order until it finds a match.
  • Return traffic. A security group allows it automatically, because it is stateful. A network ACL is stateless, so return traffic must be explicitly allowed.

Security group rules

A new security group has no inbound rules, so no inbound traffic is allowed until you add one. It has one outbound rule that allows all outbound traffic. A security group with zero outbound rules blocks all outbound traffic.

When you associate several security groups with a resource, AWS aggregates their rules into a single set and uses that set to decide whether to allow access.

Stateful means two things. Responses to allowed inbound traffic may leave the instance, regardless of the outbound rules. When an instance sends a request, the response may reach the instance, regardless of the inbound rules.

Network ACL rules

Each subnet must be associated with a network ACL, and with only one at a time. A subnet that you do not associate explicitly uses the default network ACL, which allows all traffic to flow in and out.

Each rule has a number from 1 to 32766. AWS starts with the lowest numbered rule. As soon as a rule matches the traffic, it is applied and no further rules are evaluated. Every network ACL also has an inbound and an outbound rule numbered *, which deny any packet that matches no other rule. You cannot delete them. AWS recommends numbering rules in increments such as 10 or 100, which leaves room to insert rules later.

Because a network ACL is stateless, every rule you add needs an inbound or outbound rule that allows the response traffic. The ephemeral port range is chosen by the client that starts the request, and it differs by 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

To cover the different types of clients, AWS says you can open ephemeral ports 1024-65535.

A security group as a source

A security group rule can name another security group as its source. The rule then allows traffic from the private IP addresses of the network interfaces associated with that group.

Shell

aws ec2 authorize-security-group-ingress \
  --group-id <security-group-id> \
  --protocol tcp \
  --port <port> \
  --source-group <source-security-group-id>

Set --port to the port your service listens on. The rule affects all instances associated with the source group. No rules from the referenced group are added to the group that references it. For an inbound rule, the two groups must be associated with the same VPC, or with VPCs joined by a peering connection or a transit gateway.

Where network ACLs still help

AWS states that you can secure your instances using only security groups, and that network ACLs add another layer of defense. A network ACL gives you three things that a security group does not:

  • A deny rule. In the AWS example, you open a wide range of ports and deny certain ports inside it.
  • A rule for the whole subnet. It applies to all instances in the associated subnets.
  • An immediate interruption. Changing a security group rule does not cut its tracked connections right away. Packets keep passing until the existing connections time out. A network ACL that blocks the traffic breaks existing connections.

Shell

aws ec2 create-network-acl-entry \
  --network-acl-id <network-acl-id> \
  --ingress \
  --rule-number <rule-number> \
  --protocol tcp \
  --port-range From=<port>,To=<port> \
  --cidr-block <cidr-block> \
  --rule-action deny

Give a deny rule a lower number than the allow rule that opens the wider range.

Mistakes to avoid

  1. SSH or RDP open to 0.0.0.0/0. For ports 22 and 3389, authorize specific IP address ranges only.
  2. A network ACL rule with no rule for the response. Responses are not allowed automatically. Add the rule for the ephemeral ports.
  3. A deny rule with a higher number than the allow rule. A matching rule is applied regardless of any higher-numbered rule that contradicts it.
  4. A network ACL expected to filter inside a subnet. AWS applies network ACL rules to traffic as it enters and as it leaves the subnet. Traffic routed inside the subnet is never evaluated.
  5. The default security group left in use. An instance launched with no security group gets it. Its default inbound rule allows traffic from all resources assigned to the same group. AWS recommends creating security groups for specific resources.
  6. DNS or metadata filtering. Neither filter can block DNS requests to the Route 53 Resolver, and neither filters traffic to instance metadata. For DNS, AWS points to Route 53 Resolver DNS Firewall.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides