# Firewalls: allow only the traffic you need

Understand stateful and stateless filtering, inspect nftables, and separate host firewalls from cloud security groups and network ACLs.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 8. About 23 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/08-firewalls

## What you will learn

- Explain why stateful and stateless firewalls treat reply traffic differently
- Read a simple nftables input chain and its default policy
- Distinguish a host firewall, a security group, and a network ACL

> **Lab status.** **Verified lab:** an `inet` nftables table with a drop policy and loopback allow rule ran inside an Alpine container. The listed rules are real. No host firewall was changed. <br />**Cloud lab:** none. Security groups and network ACLs are explained; no AWS resources were created.

Before host firewalls, a server often relied on each application to reject unwanted traffic. One missed setting
could expose a service. A firewall makes the network policy explicit and checks packets before they reach a process.
It can remember connections or treat each packet independently. In this lesson, you will read the policy and locate
which firewall layer made a decision.

## Stateful and stateless

> **Figure.** A client sends a TCP SYN; a stateful firewall records the permitted flow and allows the matching SYN-ACK reply.

A **stateful firewall** tracks a connection. After an allowed TCP request, it recognizes packets belonging to that
flow and permits the reply. A **stateless filter** evaluates each packet using rules, without remembering the
earlier request. It needs rules for both directions, including return ports when relevant.

This difference matters when debugging. If a stateful rule allows outbound HTTPS but the response never arrives,
check the route, the server, and other stateless filters. Do not assume every firewall on the path shares the same
connection state.

## iptables and nftables

> **Figure.** Incoming traffic passes a network interface, prerouting, route lookup and the input chain before reaching a local socket; forwarded traffic follows a different hook.

Linux firewall rules are evaluated at kernel networking hooks. `nftables` is the current framework; `iptables`
commands may use a compatibility layer. A **chain** attaches rules to a point such as incoming local traffic
(`input`) or forwarded traffic (`forward`). A base chain also has a policy for packets that reach its end.

The isolated lab created an input chain with a drop policy and allowed loopback:

```text
$ nft list chain inet lesson input
table inet lesson {
    chain input {
        type filter hook input priority filter; policy drop;
        iif "lo" accept
    }
}
```

The same ruleset reports separate default-accept policies in the `iptables` compatibility view. The lesson uses
only the nftables chain it created. Do not copy a policy change to a remote host without an approved recovery path.

## Security groups and NACLs

> **Figure.** A packet passes a subnet network ACL, then a workload security group, then the service listener; the subnet ACL is stateless and the security group is stateful.

In AWS, a **security group** attaches to network interfaces and is stateful. A **network ACL** applies at the
subnet boundary, is stateless, and checks numbered rules in order. A packet can pass one control and fail the
other. The instance's host firewall and listening process still matter too.

Use security groups for the normal workload allow-list. Use a network ACL when you need a subnet-wide rule or an
explicit deny. A common mistake is allowing inbound traffic in a NACL but forgetting the outbound reply rule. For
console examples, see the [AWS VPC lesson](/courses/aws).

## Default deny

With **default deny**, the firewall rejects traffic unless a rule allows it. Start by listing existing rules and
finding the access path you use to manage the host. Then add the smallest required allow rule, test it, and only
then tighten the default policy. This order prevents locking yourself out.

```sh
nft list ruleset
```

Prefer explicit service ports and trusted source ranges over broad rules. A firewall is one layer, not identity
management: a permitted connection still needs application authentication and authorization.

> **Keep firewall experiments isolated.** The mini mission changes a policy only inside a temporary container. Do not set a default-drop policy on a remote host until you have tested the rules and have console or out-of-band recovery.

## Mission: Inspect a default-deny chain

Create a tiny nftables input chain in an Alpine container, allow loopback, inspect the resulting policy, and let the container remove it.

- You need: Docker with NET_ADMIN capability available
- You'll have: An nftables chain with a drop policy and a loopback rule

```sh
docker run --rm --cap-add NET_ADMIN alpine:3.22 sh -ec '
  apk add --no-cache nftables iptables
  nft add table inet lesson
  nft add chain inet lesson input "{ type filter hook input priority 0; policy drop; }"
  nft add rule inet lesson input iif lo accept
  nft list chain inet lesson input
  iptables -S
'
```

The container exits after the inspection, so this policy cannot lock you out of the host.

## Try it

- Run `nft list ruleset` on a Linux host where you have permission.
- Identify the default policy before changing a firewall.
- Trace one request across the host firewall and any cloud firewall in front of it.
- Run the mini mission only inside its disposable Alpine container.

## Quiz

1. A stateful firewall allows an outbound TCP connection. What usually happens to matching reply packets?
   - They need an unrelated new allow rule
   - **They are allowed as part of the tracked connection** (answer)
   - They are always dropped
   - They bypass routing
   Why: Stateful filtering tracks the flow and permits its return traffic when the initial connection was allowed.

2. Which AWS control is stateless and evaluates numbered rules in order?
   - Security group
   - **Network ACL** (answer)
   - IAM policy
   - Route table
   Why: A network ACL evaluates inbound and outbound rules separately; return traffic must also match a rule.

3. Why is a default-deny policy safer than default-allow?
   - It automatically opens common ports
   - **It allows only explicitly permitted traffic** (answer)
   - It disables connection tracking
   - It replaces application authentication
   Why: Default deny makes unknown traffic fail closed, while required traffic must be allowed deliberately.

## Resources

- [nftables manual](https://netfilter.org/projects/nftables/manpage.html) (Netfilter)
- [Control traffic using security groups](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-security-groups.html) (AWS Docs)
- [Control subnet traffic with network ACLs](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html) (AWS Docs)
- [AWS VPC lesson](https://devops.rajeev.pro/courses/aws) (DevOps Academy)
