Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Firewalls and Segmentation

NAT, Proxies and Egress Filtering

Learn what NAT does and does not protect, how forward and reverse proxies work, why egress filtering matters for malware and exfiltration, with Python simulations.

Most firewall thinking focuses on traffic coming in. But the traffic that goes out is just as important: it is how malware calls home, how stolen data leaves, and how a compromised server becomes part of a botnet. This lesson covers three related topics. NAT changes addresses and is often mistaken for a security feature. Proxies stand between clients and servers and can enforce policy on the content. Egress filtering restricts what the inside may send out, and is one of the most underused and most effective controls.

NAT: what it is

Network Address Translation (NAT) rewrites addresses in packet headers as they cross a router. The commonest form is source NAT with port translation (PAT, or "masquerade"): many internal hosts with private addresses (10.x.x.x, 172.16.x.x-172.31.x.x, 192.168.x.x) share one public address. The router keeps a translation table: when 10.0.0.5:51514 talks to a server, the router rewrites the source to 203.0.113.1:40001 and remembers the mapping so the reply can be sent back to the right host.

Destination NAT (port forwarding) does the reverse: connections to 203.0.113.1:443 are rewritten to an internal server 10.0.1.10:8443.

flowchart LR
    A["10.0.0.5:51514"] --> N["NAT router public IP 203.0.113.1"]
    B["10.0.0.6:51514"] --> N
    N -->|"203.0.113.1:40001"| S["Server 198.51.100.7:443"]
    N -->|"203.0.113.1:40002"| S

Two internal clients using the same source port are told apart by the translated public port.

NAT is not a firewall

A frequent myth says that because internal hosts have private addresses, they are protected. The truth is more careful:

  • NAT's side effect is that unsolicited inbound packets have no translation entry and are dropped: that is stateful filtering, and a firewall is doing it, not the address rewrite itself.
  • It does nothing against outbound-initiated threats: a user visiting a malicious page, malware calling out, phishing.
  • Port forwards deliberately open holes.
  • It does not hide you well: servers see your public address, and techniques like WebRTC leaks, application data and fingerprints reveal more.
  • With IPv6 there is usually no NAT at all and every host can have a public address. Security then relies completely on the firewall, which is fine as long as it is configured.

Conclusion: keep NAT for address conservation, and rely on a real firewall for security.

A NAT table in Python

The program below implements port address translation: it assigns public ports to outgoing connections, translates replies back, and drops inbound packets that match no mapping.

Python Source NAT with a translation table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Replies find their way back; strangers get nothing. The table is a mapping, exactly like the firewall's state table from the previous lesson, and it is the state, not the translation, that blocks the strangers.

Proxies

A proxy is an intermediary that makes requests on behalf of someone else.

  • A forward proxy serves internal clients going out. Clients are configured (or transparently redirected) to send web requests to it. The proxy can authenticate users, enforce URL and category filtering, scan downloads for malware, cache content, log every request and, with TLS inspection, look inside HTTPS. Because only the proxy talks to the internet, you can block all direct outbound web traffic and force everything through it.
  • A reverse proxy sits in front of servers. Clients connect to it, believing it is the server. It terminates TLS, balances load, caches, hides the internal topology, and (with a WAF) filters attacks. Nginx, HAProxy and cloud load balancers are reverse proxies.

TLS inspection (a "break and inspect" proxy) decrypts HTTPS using a company-installed certificate authority, inspects the content and re-encrypts. It gives visibility but is a privacy and risk trade-off: it concentrates sensitive data in one place, can break certificate pinning, and should exclude banking and health sites. Decide it with legal, privacy and security people together.

Egress filtering

Most networks allow almost any outbound connection. That is convenient and dangerous. Egress filtering means defaulting outbound traffic to deny, too, and allowing only what is needed:

  • Servers rarely need to browse the internet. A database server should talk to the application tier, the update mirror and the monitoring system, and nothing else. If it tries to contact an unknown address, that is a strong alert.
  • Workstations should use the proxy and approved DNS resolvers.
  • IoT and guest zones get internet access only, perhaps with category filtering.
  • Block outbound SMTP from everything except the mail server, to stop spam relays.
  • Block direct DNS to the internet except from the internal resolver, and monitor it for tunnelling (see the packet analysis lesson).
  • Ingress and egress anti-spoofing: drop outbound packets whose source address does not belong to your network (BCP 38), so you cannot be used as a launching pad for spoofed floods.

Egress control also limits the damage of exploits that need to fetch a second stage from the internet, and it makes data exfiltration noisier. It takes work to find the legitimate flows first, so introduce it gradually: log, review, then enforce.

Python Egress allow-list with alerts for violations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two of the blocked entries (a server reaching an unknown host on 443, and a staff machine using an outside DNS resolver) are exactly the patterns that point to malware or tunnelling. Even if you cannot block at first, logging denied-but-would-have-been flows gives you an early-warning signal.

Putting it together

Typical outbound design:

  1. Clients and servers use internal DNS, which forwards to the outside and is logged.
  2. Web traffic from staff goes through the forward proxy; direct 80/443 from staff VLANs to the internet is blocked.
  3. Servers have allow-lists for update mirrors and partner APIs.
  4. Everything else outbound is dropped and logged.
  5. Alerts fire on the first denied outbound connection from a server zone.

Start with servers. They have few legitimate destinations, so an allow-list is quick to build, and a server that suddenly calls out to a new address is highly suspicious.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is NAT not a substitute for a firewall?

    Dropping unsolicited inbound packets is stateful filtering. NAT translates addresses and offers no protection against traffic that the inside initiates.
  2. 2. What does a reverse proxy do?

    A reverse proxy represents the servers to the outside: it can terminate TLS, balance load, cache and filter.
  3. 3. What is the main security value of egress filtering on a server zone?

    Servers have few legitimate destinations, so denying everything else blocks command-and-control and data theft and makes odd attempts stand out.
  4. 4. Why might you block outbound SMTP from everything except the mail server?

    Only the official mail server should relay email; other machines sending SMTP directly are usually spam bots.
  5. 5. What is a key trade-off of TLS inspection?

    Decrypting traffic lets you scan it but exposes private content to the inspection device and requires careful policy.

Summary

  • NAT rewrites addresses (PAT shares one public address, DNAT forwards ports); it is not a security control by itself.
  • Forward proxies control outbound web use; reverse proxies protect and present servers.
  • TLS inspection gives visibility at a privacy and risk cost.
  • Egress filtering defaults outbound to deny, starting with servers, and adds anti-spoofing and DNS/SMTP controls.
  • Log denied outbound flows first, then enforce; unexpected outbound connections are strong detection signals.

Next: VPN Fundamentals: Tunnels, Types and Threat Models