Return traffic & session tracking

Stateless or stateful: who lets the reply in?

Lesson 4 of 20 · 12 min · Module 1: How a firewall thinks

Every conversation has two directions. Allowing the request is half the work, because the answer has to get back through the same firewall.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Describe the reply to a request in terms of addresses and ports
  • Write a return rule for a stateless filter
  • Explain what session tracking adds

The idea

A stateless filter judges every packet alone, so replies need their own rule. A stateful firewall remembers each conversation it allowed, keeps it in a session table, and lets the matching replies through automatically. This lesson uses a stateless filter so that you can see the problem a stateful firewall solves.

# request: client port 51000 -> server port 22
allow tcp from 10.0.5.0/24 to 10.0.6.20 port 22
# reply:   server port 22 -> client port 51000
allow tcp from 10.0.6.20 port 22 to 10.0.5.0/24 port 1024-65535

01Turn the packet around

A staff PC sends tcp from 10.0.1.25 port 51000 to 10.0.2.10 port 443. The reply swaps both pairs: from 10.0.2.10 port 443 to 10.0.1.25 port 51000. A stateless filter needs a rule for that reply. The source port is known (443); the destination port is whatever the client picked, so you allow the range clients use, here 1024-65535. In this format a port written after the source address is a source port.

02See what the return rule also allows

The return rule cannot tell a real reply from any other packet the web server sends from port 443 to a high port in lan. If the server were compromised, that opening would be usable. A stateless filter has no memory, so it cannot do better.

03What a stateful firewall does instead

A stateful firewall records the allowed request in its session table and accepts only packets that belong to a recorded session. You write the request rule and nothing else. FortiGate and Huawei USG work this way by default; on nftables you add the rule yourself.

# nftables: accept replies to connections that were allowed
nft add rule inet filter forward ct state established,related accept

# Look at the session table
# FortiGate:   diagnose sys session list
# Huawei USG:  display firewall session table
# Linux:       conntrack -L

Your turn

This filter is stateless. The request rule is already there, but the web server's answer is dropped. Add a return rule: tcp from web, source port 443, to lan, destination ports 1024-65535. Do not allow the web server to open connections of its own.

What the checks look for

  • The request still passes
  • The reply from port 443 gets back to the client
  • The web server cannot open its own connections into the office
  • The return rule is limited to tcp, source port 443, and client ports
Try it in the editor →

Take this with you

A stateless filter needs a rule for each direction and cannot recognise a genuine reply. A stateful firewall tracks sessions, so you write only the rule for who may start the conversation.

Go a little further

Remove the source port from your return rule and run the lab. The web-opens-rdp packet now passes, which shows how much that one detail was doing.

Learn Firewall Foundations for free

20 short lessons with real code and instant checks.

See the whole course →