TExcelTAD
Home › Networking for Security: Packets, Protocols & Traffic Analysis › Routing, Gateways and Spoofed Sources

Routing, Gateways and Spoofed Sources

A router forwards a packet by looking at one field: the destination address. It doesn't ask where the packet came from, and it doesn't check that the source address is real. That asymmetry is why source spoofing works across the Internet, why reflection attacks are possible, and why the fix has to be done at the edges, by networks that know which addresses they own. Routing tables, longest-prefix match and routing protocols are explained in Routing & Routing Tables and Routing Protocols. This lesson implements the lookup, then adds the check that stops spoofed sources: reverse-path filtering.

Longest-prefix match

A routing table maps prefixes to next hops. When several prefixes contain the destination, the most specific (longest) one wins; 0.0.0.0/0, the default route, matches everything and is used last.

python · try it yourself
import ipaddress as ipa

table = [  # prefix, next hop, interface: a lab edge router
    ("0.0.0.0/0",       "203.0.113.1", "wan0"),
    ("10.10.0.0/16",    "10.10.255.1", "core0"),
    ("10.10.0.0/24",    "direct",      "lan0"),
    ("10.10.0.128/25",  "10.10.0.254", "lan0"),
    ("192.0.2.0/24",    "direct",      "dmz0"),
]
routes = [(ipa.ip_network(p), nh, ifc) for p, nh, ifc in table]

def lookup(dst):
    a = ipa.ip_address(dst)
    matches = [r for r in routes if a in r[0]]
    return max(matches, key=lambda r: r[0].prefixlen)

for dst in ["10.10.0.10", "10.10.0.200", "10.10.7.7", "192.0.2.25", "198.51.100.9"]:
    net, nh, ifc = lookup(dst)
    print(f"{dst:<13} -> {str(net):<15} via {nh:<12} on {ifc}")
10.10.0.10    -> 10.10.0.0/24    via direct       on lan0
10.10.0.200   -> 10.10.0.128/25  via 10.10.0.254  on lan0
10.10.7.7     -> 10.10.0.0/16    via 10.10.255.1  on core0
192.0.2.25    -> 192.0.2.0/24    via direct       on dmz0
198.51.100.9  -> 0.0.0.0/0       via 203.0.113.1  on wan0

10.10.0.200 matches four entries (/0, /16, /24 and /25) and the /25 wins. A more specific route is powerful: whoever can inject one into a routing table attracts that traffic. That is the mechanism behind route hijacks on the Internet, where a network announces someone else's prefix (or a more specific part of it) in BGP.

Spoofed sources

Because forwarding ignores the source, an attacker can send packets with any source address. They won't get replies (those go to the real owner of the address), but they don't need them for:

  • floods whose sources can't be traced or blocked by address,
  • reflection and amplification, where the forged source is the victim and servers send their (larger) answers to it (UDP and Amplification),
  • evading address-based filters, for example a packet claiming to come from an internal address.

Reverse-path filtering

The fix is to check the source on arrival: "if I wanted to send a packet to this source, would I use the interface it just arrived on?" That is unicast reverse-path forwarding (uRPF, RFC 3704):

  • strict mode: the best route back to the source must use the arrival interface. Right for edge links with symmetric routing.
  • loose mode: any route back to the source must exist (not via a null route). Catches bogons and unallocated space, safe with asymmetric routing.

The program adds both checks to the router above and feeds it packets arriving on different interfaces.

python · try it yourself
import ipaddress as ipa

routes = [(ipa.ip_network(p), ifc) for p, ifc in [
    ("0.0.0.0/0", "wan0"), ("10.10.0.0/16", "core0"), ("10.10.0.0/24", "lan0"),
    ("10.10.0.128/25", "lan0"), ("192.0.2.0/24", "dmz0")]]
BOGONS = [ipa.ip_network(n) for n in ("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.0/8", "0.0.0.0/8")]

def best_iface(src):
    a = ipa.ip_address(src)
    return max((r for r in routes if a in r[0]), key=lambda r: r[0].prefixlen)[1]

def urpf(src, arrived_on, mode):
    a = ipa.ip_address(src)
    if arrived_on == "wan0" and any(a in b for b in BOGONS):
        return "drop (bogon from the Internet)"
    if mode == "strict" and best_iface(src) != arrived_on:
        return f"drop (route back is {best_iface(src)})"
    return "accept"

packets = [("198.51.100.9", "wan0"), ("10.10.0.5", "lan0"), ("10.10.0.5", "wan0"),
           ("198.51.100.9", "lan0"), ("192.0.2.25", "dmz0"), ("192.0.2.25", "lan0")]
print(f"{'source':<14}{'arrived':<9}{'loose':<34}strict")
for src, ifc in packets:
    print(f"{src:<14}{ifc:<9}{urpf(src, ifc, 'loose'):<34}{urpf(src, ifc, 'strict')}")
source        arrived  loose                             strict
198.51.100.9  wan0     accept                            accept
10.10.0.5     lan0     accept                            accept
10.10.0.5     wan0     drop (bogon from the Internet)    drop (bogon from the Internet)
198.51.100.9  lan0     accept                            drop (route back is wan0)
192.0.2.25    dmz0     accept                            accept
192.0.2.25    lan0     accept                            drop (route back is dmz0)

Read the rows: an internal address arriving from the Internet is dropped in both modes (bogon filtering is the minimum every edge should do). A public source arriving on the LAN port means an inside host is spoofing, possibly as part of a reflection attack; only strict mode stops it. BCP 38 (RFC 2827) asks every network to do exactly this on its customer-facing edges, so spoofed packets never leave it.

Routing on a host

On a Linux host the routing table is shown by ip route. The sandbox has only loopback (no network), so its main table is empty and only the kernel's local table exists; ip route get performs the same longest-prefix lookup as the program above.

bash · try it yourself
echo "main table:"; ip route show
echo "local table:"; ip route show table local
echo "lookup:"; ip route get 127.0.0.53
cat /proc/sys/net/ipv4/conf/all/rp_filter
main table:
local table:
local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1
local 127.0.0.1 dev lo proto kernel scope host src 127.0.0.1
broadcast 127.255.255.255 dev lo proto kernel scope link src 127.0.0.1
lookup:
local 127.0.0.53 dev lo src 127.0.0.1 uid 1000
    cache <local>
2

The last number is the kernel's reverse-path filter setting, here 2: 0 = off, 1 = strict, 2 = loose (RFC 3704 modes). For each interface the kernel uses the higher of conf/all/rp_filter and that interface's own value.

How problems are detected

  • Bogon and martian sources on edge interfaces (Linux can log them with log_martians).
  • Flow records (NetFlow, IPFIX) showing your own networks as sources on the inbound Internet link, or foreign sources leaving your network.
  • Unexpected routes: new more-specific prefixes in internal routing, default routes learned from user segments, ICMP redirects (type 5) from hosts that aren't routers. For BGP, route-monitoring services alert when someone else announces your prefixes.

How it is fixed

  • Ingress and egress filtering (BCP 38 / BCP 84) and uRPF on edges; bogon filters on Internet links.
  • Authenticate routing protocols (OSPF and BGP authentication, TCP-AO), filter what peers may announce, and on the Internet sign route origins with RPKI (Route Origin Authorizations) and reject invalid ones.
  • Ignore ICMP redirects on hosts and servers (net.ipv4.conf.all.accept_redirects = 0).

Pitfalls

  • Strict uRPF on links with asymmetric routing (multi-homed sites) drops legitimate traffic. Use loose mode there.
  • Filtering only inbound. Egress filtering is what keeps your compromised hosts from spoofing others.
  • Assuming a source address in a log identifies the sender of a UDP packet. For TCP, a completed handshake shows the source could receive replies; for UDP it shows nothing.

Practice

1. Add the route 10.10.0.0/26 via lan1 to the first program. Where does 10.10.0.10 go now?

To lan1: a /26 is more specific than the /24, so it wins for 10.10.0.0 to 10.10.0.63.

2. Why can't spoofing be used to log in to a TCP service from a forged address?

The server's SYN-ACK goes to the real owner of the address, so the attacker never learns the server's initial sequence number and can't complete the handshake (unless it can see that traffic or guess the number, which modern random ISNs prevent).

3. A packet with source 192.0.2.25 arrives on lan0. Strict uRPF drops it, loose accepts it. Which is right?

192.0.2.0/24 is the DMZ, reachable only via dmz0, so a packet from it arriving on the LAN is spoofed or misrouted: strict mode is right here. Loose mode only checks that some route to the source exists.

Test yourself

Which route does a router use for 10.10.0.200 if it has 10.10.0.0/16, 10.10.0.0/24 and 10.10.0.128/25?

  1. 10.10.0.0/16
  2. 10.10.0.0/24
  3. 10.10.0.128/25
  4. The default route

What does strict unicast RPF check?

  1. That the destination exists
  2. That the best route back to the source uses the interface the packet arrived on
  3. That the packet is encrypted
  4. That the TTL is above 64

What does BCP 38 ask networks to do?

  1. Use IPv6 only
  2. Filter traffic with source addresses that don't belong to the network it comes from
  3. Block all ICMP
  4. Encrypt BGP sessions

Which technology lets networks reject BGP announcements of prefixes by unauthorised origins?

  1. DNSSEC
  2. RPKI route origin validation
  3. uRPF
  4. SYN cookies