TExcelTAD
Home › Networking for Security: Packets, Protocols & Traffic Analysis › NAT: SNAT, DNAT, PAT and NAT Traversal

NAT: SNAT, DNAT, PAT and NAT Traversal

An abuse report arrives: "203.0.113.7 attacked our server at 14:02:11 UTC from port 40112." 203.0.113.7 is your firewall's public address, shared by 3,000 employees. Which of them was it? Network address translation (NAT) rewrites addresses and ports as packets cross a boundary, and that changes how security work is done: it decides what the outside can reach, what your logs need to record, and why peer-to-peer apps go to great lengths to get through it. NAT itself is explained in NAT; here we look at it as a defender.

The three kinds

Kind Rewrites Typical use
SNAT (source NAT) the source address of outgoing packets many inside hosts share one public address
PAT / masquerade (SNAT with ports) source address and source port home and office routers: one public IP, thousands of connections
DNAT (destination NAT, "port forwarding") the destination address of incoming packets publish an inside server on a public address and port

A NAT device keeps a translation table (connection tracking). Replies match an entry and are translated back; packets that match no entry and no forward have nowhere to go.

A PAT router in Python

The model below is a lab edge router with public address 203.0.113.7. Inside hosts in 10.10.0.0/24 open connections; one port forward publishes the web server 10.10.0.10:80 on public port 8080. It translates, logs, and then handles three inbound packets.

python · try it yourself
import itertools

PUBLIC = "203.0.113.7"
forwards = {8080: ("10.10.0.10", 80)}                    # DNAT rules
table, log = {}, []                                      # public port -> (inside ip, inside port, remote)
ports = itertools.count(40110)

def outbound(t, src, sport, dst, dport):
    pport = next(ports)
    table[pport] = (src, sport, (dst, dport))
    log.append(f"{t} SNAT {src}:{sport} -> {PUBLIC}:{pport} to {dst}:{dport}")
    return f"{PUBLIC}:{pport} -> {dst}:{dport}"

def inbound(src, sport, dport):
    if dport in table and table[dport][2] == (src, sport):
        ip, p, _ = table[dport]
        return f"reply, translated to {ip}:{p}"
    if dport in forwards:
        ip, p = forwards[dport]
        return f"DNAT to {ip}:{p} (port forward)"
    return "no translation: dropped"

print(outbound("14:02:10", "10.10.0.21", 51000, "198.51.100.80", 443))
print(outbound("14:02:11", "10.10.0.37", 52344, "198.51.100.80", 443))
print(outbound("14:02:11", "10.10.0.21", 51002, "192.0.2.53", 53))
print()
for src, sport, dport, note in [("198.51.100.80", 443, 40111, "reply to the 2nd connection"),
                                ("198.51.100.66", 443, 40111, "same port, other remote"),
                                ("192.0.2.99", 50000, 8080, "to the forwarded port"),
                                ("192.0.2.99", 50000, 22, "unsolicited, port 22")]:
    print(f"{src}:{sport} -> {PUBLIC}:{dport:<6} {inbound(src, sport, dport):<36} ({note})")
print("\nNAT log:"); print("\n".join(log))
203.0.113.7:40110 -> 198.51.100.80:443
203.0.113.7:40111 -> 198.51.100.80:443
203.0.113.7:40112 -> 192.0.2.53:53

198.51.100.80:443 -> 203.0.113.7:40111  reply, translated to 10.10.0.37:52344 (reply to the 2nd connection)
198.51.100.66:443 -> 203.0.113.7:40111  no translation: dropped              (same port, other remote)
192.0.2.99:50000 -> 203.0.113.7:8080   DNAT to 10.10.0.10:80 (port forward) (to the forwarded port)
192.0.2.99:50000 -> 203.0.113.7:22     no translation: dropped              (unsolicited, port 22)

NAT log:
14:02:10 SNAT 10.10.0.21:51000 -> 203.0.113.7:40110 to 198.51.100.80:443
14:02:11 SNAT 10.10.0.37:52344 -> 203.0.113.7:40111 to 198.51.100.80:443
14:02:11 SNAT 10.10.0.21:51002 -> 203.0.113.7:40112 to 192.0.2.53:53

Three lessons from the output:

  1. Attribution needs the port and the time. The abuse report's port 40112 (14:02:11) maps to the third line, inside host 10.10.0.21. Without NAT logs that include the public port, you can't answer the question at all. This is why logging requirements for CGNAT and enterprise NAT always include source ports and precise timestamps.
  2. Unsolicited inbound traffic is dropped, but only because there is no table entry. That is a side effect, not a security policy: an explicit stateful firewall is what you rely on (and what IPv6, which usually has no NAT, needs anyway).
  3. Port forwards are holes by design. Anything arriving at :8080 reaches the inside server; it must be patched and monitored like an Internet-facing host, because it is one.

The same idea on a Linux router

On Linux, NAT is configured in nftables. These rules need root and a real network, so they are shown as a lab session for a router with wan0 and lan0:

console
lab-router# nft add table ip nat
lab-router# nft add chain ip nat prerouting '{ type nat hook prerouting priority dstnat; }'
lab-router# nft add chain ip nat postrouting '{ type nat hook postrouting priority srcnat; }'
lab-router# nft add rule ip nat postrouting oifname "wan0" ip saddr 10.10.0.0/24 masquerade
lab-router# nft add rule ip nat prerouting iifname "wan0" tcp dport 8080 dnat to 10.10.0.10:80
lab-router# conntrack -L -p tcp --dport 443
tcp      6 431999 ESTABLISHED src=10.10.0.21 dst=198.51.100.80 sport=51000 dport=443 src=198.51.100.80 dst=203.0.113.7 sport=443 dport=40110 [ASSURED] mark=0 use=1

The conntrack line shows both directions of one translation: the original tuple and the reply tuple the router expects.

NAT traversal

Two hosts behind different NATs can't simply connect to each other, since neither has a table entry for the other. Video calls, games and WebRTC use NAT traversal:

  • STUN (RFC 8489): the client asks a server on the Internet "what address and port do you see me as?" and learns its public mapping.
  • Hole punching: both peers send packets to each other's public mapping at about the same time; each NAT creates an entry that lets the other's packets in.
  • TURN (RFC 8656): when punching fails (symmetric NATs that pick a new port per destination), traffic is relayed through a server.
  • ICE (RFC 8445) tries all candidate addresses (local, STUN-learned, TURN relay) and picks the best pair that works.
  • UPnP IGD / NAT-PMP / PCP: protocols that let an inside device ask the router to create a port forward.

Security consequences:

  • UPnP lets any inside device open inbound ports, including malware and badly designed IoT devices; it has also been exposed on WAN interfaces by buggy routers. Turn it off on business networks and review the forwards it created at home.
  • Address disclosure: ICE candidates reveal local and public addresses to the other peer (the "WebRTC IP leak" seen with VPNs). Browsers now hide local addresses behind random mDNS names (….local) by default, unless the page has camera or microphone permission.
  • TURN servers are open relays if misconfigured: require authentication and restrict which destinations they may relay to (never internal ranges).

How it is detected

  • NAT and conntrack logs with public port, inside address and timestamps (also needed for law-enforcement and abuse requests).
  • New port forwards in router configurations or UPnP mapping tables; external scans of your own public addresses to confirm only intended ports answer.
  • Unusual traversal traffic: STUN/TURN (UDP 3478, TCP 3478, 5349 for TLS) to unexpected servers from hosts that shouldn't run real-time apps.

How it is fixed or prevented

  • Treat NAT as addressing, not as a firewall: write explicit stateful rules for both IPv4 and IPv6.
  • Keep port forwards to a reviewed list, each with an owner, and prefer a reverse proxy or VPN to exposing services directly.
  • Disable UPnP IGD on edge devices in organisations; on home routers, check what it has opened.
  • Keep synchronised clocks (NTP) on NAT devices so the logs can be matched with others.

Pitfalls

  • Logging only the public IP of a CGNAT customer and then being unable to identify anyone.
  • Believing a service behind NAT "isn't on the Internet" while a forward or UPnP mapping points at it.
  • Forgetting hairpin NAT: inside hosts reaching the public address of an inside server go through the router, so internal traffic can appear in edge logs with the router's address.

Practice

1. In the simulation, why is the reply from 198.51.100.66 to port 40111 dropped?

The table entry for 40111 expects replies from 198.51.100.80:443 only. This is address- and port-dependent filtering (RFC 4787's term); a NAT with endpoint-independent ("full cone") filtering would accept it, which is easier for traversal and weaker as a filter.

2. An abuse report gives only "203.0.113.7 at 14:02". What else do you need?

The source port (and a precise, time-zoned timestamp, ideally to the second), plus the destination. With thousands of inside hosts behind one address, the public IP and minute alone match many translations.

3. Why do symmetric NATs make hole punching fail?

They allocate a new public port for each destination. The port that STUN reported (for the STUN server) is not the one the NAT uses towards the peer, so the peer sends to the wrong port; TURN relaying is the fallback.

Test yourself

Which NAT type publishes an inside web server on a public port?

  1. SNAT
  2. DNAT (port forwarding)
  3. PAT
  4. Hairpin NAT

In the simulation, which inside host used public port 40112?

  1. 10.10.0.21
  2. 10.10.0.37
  3. 10.10.0.10
  4. None

Why is NAT not a substitute for a firewall?

  1. It is slower
  2. It only drops unsolicited traffic as a side effect; forwards, UPnP and IPv6 bypass it
  3. It encrypts traffic
  4. It blocks all outbound traffic

What does a STUN server tell a client?

  1. Its public address and port as seen from outside
  2. The peer's password
  3. Its DNS server
  4. Which ports are open on the peer